-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* HERE WE GO * Bug fix: Filter out offers with 100% filled units from product * i am da failed future * Include 1.21.x branches in workflow * A step closer to Java Future API * Move more stuffs to Java Future API * [ci skip] Update README.md, banner.png and CONTRIBUTON.md * Replace construction data with metadata + Update license headers * Modify the list directly * A new economy system interface * Java Edition 1.21
- Loading branch information
Showing
82 changed files
with
1,402 additions
and
318 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 132 additions & 0 deletions
132
core/src/main/java/stonks/core/caching/FutureCache.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
/* | ||
* Copyright (c) 2023-2024 nahkd | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
package stonks.core.caching; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.CompletionException; | ||
import java.util.function.Supplier; | ||
|
||
public class FutureCache<T> { | ||
private Supplier<CompletableFuture<T>> invoker; | ||
private long lastFetch = -1; | ||
private CompletableFuture<T> fetchingTask = null; | ||
private T result = null; | ||
|
||
// Options | ||
private long maxCacheTime = 5000L; | ||
|
||
public FutureCache(Supplier<CompletableFuture<T>> invoker, T defaultResult) { | ||
this.invoker = invoker; | ||
this.result = defaultResult; | ||
} | ||
|
||
public FutureCache(Supplier<CompletableFuture<T>> invoker) { | ||
this(invoker, null); | ||
} | ||
|
||
/** | ||
* <p> | ||
* Configure the cache time, which is the minimum amount of time the object is | ||
* allowed to stay in this cache. Default cache time is 5000 milliseconds. | ||
* </p> | ||
* | ||
* @param maxCacheTime The maximum cache time. | ||
* @return The cache. | ||
*/ | ||
public FutureCache<T> withMaxCacheTime(long maxCacheTime) { | ||
this.maxCacheTime = maxCacheTime; | ||
return this; | ||
} | ||
|
||
/** | ||
* <p> | ||
* Return the state that determine if {@link #forceFetch()} should be called | ||
* again. | ||
* </p> | ||
* | ||
* @return The state. | ||
*/ | ||
public boolean shouldFetch() { | ||
return fetchingTask != null || lastFetch == -1L || (System.currentTimeMillis() - lastFetch > maxCacheTime); | ||
} | ||
|
||
/** | ||
* <p> | ||
* Force this cache to fetch the value. If there is already a task running, it | ||
* will return that task. | ||
* </p> | ||
* | ||
* @return The fetch task. | ||
*/ | ||
public CompletableFuture<T> forceFetch() { | ||
if (fetchingTask != null) return fetchingTask; | ||
return fetchingTask = invoker.get().handle((r, t) -> { | ||
lastFetch = System.currentTimeMillis(); | ||
result = r; | ||
fetchingTask = null; | ||
|
||
if (t != null) throw new CompletionException(t); | ||
return r; | ||
}); | ||
} | ||
|
||
/** | ||
* <p> | ||
* Get the object that is still under cache retention wrapped as immediately | ||
* completed future, or future of the fetching task that is still running. | ||
* </p> | ||
* | ||
* @return The future. | ||
*/ | ||
public CompletableFuture<T> get() { | ||
if (fetchingTask != null && fetchingTask.isDone()) { | ||
if (!fetchingTask.isCompletedExceptionally()) try { | ||
result = fetchingTask.get(); | ||
} catch (Throwable e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
fetchingTask = null; | ||
} | ||
|
||
if (!shouldFetch()) { | ||
if (fetchingTask != null) return fetchingTask; | ||
return CompletableFuture.completedFuture(result); | ||
} | ||
|
||
return forceFetch(); | ||
} | ||
|
||
/** | ||
* <p> | ||
* Get the value immediately, regardless the cache state. If the value need to | ||
* be fetched again, it will call the {@link #forceFetch()} method and return | ||
* previous value. | ||
* </p> | ||
* | ||
* @return The cached value. | ||
*/ | ||
public T getNow() { | ||
if (shouldFetch()) forceFetch(); | ||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* Copyright (c) 2023-2024 nahkd | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
package stonks.core.caching; | ||
|
||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.UUID; | ||
import java.util.WeakHashMap; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
import stonks.core.market.Offer; | ||
import stonks.core.market.ProductMarketOverview; | ||
import stonks.core.product.Category; | ||
import stonks.core.product.Product; | ||
import stonks.core.service.StonksService; | ||
|
||
public class StonksCache { | ||
private StonksService service; | ||
private CompletableFuture<List<Category>> categories; | ||
private Map<String, Category> categoryIdMap = null; | ||
|
||
// Expiring cache | ||
private Map<UUID, FutureCache<List<Offer>>> offers = new WeakHashMap<>(); | ||
private Map<Product, FutureCache<ProductMarketOverview>> overviews = new WeakHashMap<>(); | ||
|
||
public StonksCache(StonksService service) { | ||
this.service = service; | ||
} | ||
|
||
public StonksService getService() { return service; } | ||
|
||
public CompletableFuture<List<Category>> getAllCategories() { | ||
if (categories == null) return categories = service.queryAllCategoriesAsync() | ||
.thenApply(categories -> { | ||
categoryIdMap = new HashMap<>(); | ||
for (var category : categories) { categoryIdMap.put(category.getCategoryId(), category); } | ||
return categories; | ||
}); | ||
return categories; | ||
} | ||
|
||
public CompletableFuture<Category> getCategoryById(String id) { | ||
if (categoryIdMap != null) return CompletableFuture.completedFuture(categoryIdMap.get(id)); | ||
return getAllCategories().thenApply($ -> { return categoryIdMap.get(id); }); | ||
} | ||
|
||
public FutureCache<List<Offer>> getOffers(UUID offerer) { | ||
var cachedList = offers.get(offerer); | ||
if (cachedList == null) | ||
offers.put(offerer, cachedList = new FutureCache<>(() -> getService().getOffersFromUserAsync(offerer))); | ||
return cachedList; | ||
} | ||
|
||
public FutureCache<ProductMarketOverview> getOverview(Product product) { | ||
var cachedOverview = overviews.get(product); | ||
if (cachedOverview == null) overviews.put(product, | ||
cachedOverview = new FutureCache<>(() -> getService().queryMarketOverviewAsync(product))); | ||
return cachedOverview; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright (c) 2023-2024 nahkd | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
package stonks.core.dynamic; | ||
|
||
/** | ||
* <p> | ||
* A {@link Dynamic} is an interface that proxies the underlying dynamic object | ||
* structure, such as JSON or NBT. | ||
* </p> | ||
*/ | ||
public interface Dynamic { | ||
public DynamicFactory getFactory(); | ||
} |
Oops, something went wrong.