This repository has been archived by the owner on Jan 6, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Honestly don't even ask me why I wasted a week on working on a library 😭
- Loading branch information
Showing
137 changed files
with
4,753 additions
and
1,378 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,4 +34,5 @@ replay_pid* | |
/target/ | ||
target/ | ||
target | ||
/target | ||
/target | ||
dependency-reduced-pom.xml |
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,171 @@ | ||
package dev.manere.utils.cachable; | ||
|
||
import dev.manere.utils.cachable.impl.CachableImpl; | ||
import dev.manere.utils.consumers.PairConsumer; | ||
import dev.manere.utils.model.Tuple; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.Map; | ||
|
||
/** | ||
* The {@code Cachable} interface defines methods for a caching mechanism that associates keys with values. | ||
* | ||
* @param <K> The type of keys in the cache. | ||
* @param <V> The type of values in the cache. | ||
*/ | ||
public interface Cachable<K, V> { | ||
/** | ||
* Creates and returns a Cachable instance. | ||
* | ||
* @param <K> The key type | ||
* @param <V> The value type | ||
* @return A new Cachable instance | ||
*/ | ||
static <K, V> CachableImpl<K, V> of() { | ||
return new CachableImpl<>(); | ||
} | ||
|
||
/** | ||
* Creates and returns a Cachable instance initialized with the given entries. | ||
* | ||
* @param <K> The key type | ||
* @param <V> The value type | ||
* @param entries The initial entries to cache | ||
* @return A Cachable instance with the given initial entries | ||
*/ | ||
static <K, V> CachableImpl<K, V> of(Collection<Tuple<K, V>> entries) { | ||
CachableImpl<K, V> cachable = new CachableImpl<>(); | ||
cachable.cacheAll(entries); | ||
|
||
return cachable; | ||
} | ||
|
||
/** | ||
* Creates and returns a Cachable instance initialized with the given entries. | ||
* | ||
* @param <K> The key type | ||
* @param <V> The value type | ||
* @param entries The initial entries to cache | ||
* @return A Cachable instance with the given initial entries | ||
*/ | ||
static <K, V> CachableImpl<K, V> of(Map<K, V> entries) { | ||
CachableImpl<K, V> cachable = new CachableImpl<>(); | ||
cachable.cacheAll(entries); | ||
|
||
return cachable; | ||
} | ||
|
||
/** | ||
* Creates and returns a Cachable instance initialized with the given entries. | ||
* | ||
* @param <K> The key type | ||
* @param <V> The value type | ||
* @param entries The initial entries to cache | ||
* @return A Cachable instance with the given initial entries | ||
*/ | ||
@SafeVarargs | ||
static <K, V> CachableImpl<K, V> of(Tuple<K, V>... entries) { | ||
return Cachable.of(Arrays.asList(entries)); | ||
} | ||
|
||
/** | ||
* Retrieves the value associated with the given key. | ||
* | ||
* @param key The key whose associated value is to be returned. | ||
* @return The value associated with the given key, or null if the key is not present in the cache. | ||
*/ | ||
V val(K key); | ||
|
||
/** | ||
* Retrieves the key associated with the given value. | ||
* | ||
* @param val The value whose associated key is to be returned. | ||
* @return The key associated with the given value, or null if the value is not present in the cache. | ||
*/ | ||
K key(V val); | ||
|
||
/** | ||
* Returns true if the cachable contains a key | ||
* @param key The key to check for | ||
* @return true if exists, false otherwise | ||
*/ | ||
boolean hasKey(K key); | ||
|
||
/** | ||
* Returns true if the cachable contains a value | ||
* @param val The val to check for | ||
* @return true if exists, false otherwise | ||
*/ | ||
boolean hasVal(V val); | ||
|
||
/** | ||
* Caches the specified key-value pair. | ||
* | ||
* @param key The key to be cached. | ||
* @param val The value to be cached. | ||
*/ | ||
void cache(K key, V val); | ||
|
||
/** | ||
* Removes the specified key-value pair from the cache. | ||
* | ||
* @param key The key to be removed. | ||
* @param val The value to be removed. | ||
*/ | ||
void del(K key, V val); | ||
|
||
/** | ||
* Removes the entry associated with the specified key from the cache. | ||
* | ||
* @param key The key to be removed. | ||
*/ | ||
void del(K key); | ||
|
||
/** | ||
* Caches multiple key-value pairs. | ||
* | ||
* @param entries A collection of key-value pairs to be cached. | ||
*/ | ||
void cacheAll(Collection<Tuple<K, V>> entries); | ||
|
||
/** | ||
* Caches a key-value map. | ||
* | ||
* @param entries A key-value map to be cached. | ||
*/ | ||
void cacheAll(Map<K, V> entries); | ||
|
||
/** | ||
* Removes multiple entries from the cache based on the specified keys. | ||
* | ||
* @param keys A collection of keys to be removed. | ||
*/ | ||
void delAll(Collection<K> keys); | ||
|
||
/** | ||
* Performs the given action for each entry in the cache. | ||
* | ||
* @param forEach The action to be performed for each entry. | ||
*/ | ||
void forEach(PairConsumer<K, V> forEach); | ||
|
||
/** | ||
* Returns the number of entries currently cached. | ||
* | ||
* @return The number of entries in the cache. | ||
*/ | ||
int cached(); | ||
|
||
/** | ||
* Clears all entries from the cache. | ||
*/ | ||
void clear(); | ||
|
||
/** | ||
* Creates and returns a snapshot of the current state of the cache. | ||
* | ||
* @return A snapshot of the cache. | ||
*/ | ||
CachableSnapshot<K, V> snapshot(); | ||
} |
45 changes: 45 additions & 0 deletions
45
src/main/java/dev/manere/utils/cachable/CachableSnapshot.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,45 @@ | ||
package dev.manere.utils.cachable; | ||
|
||
import dev.manere.utils.model.Tuple; | ||
|
||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* The {@code CachableSnapshot} interface represents a snapshot or view of a cache's current state. | ||
* It provides methods to access the cached entries as a map, list, or collection of key-value pairs. | ||
* <P></P> | ||
* <strong>WARNING: THIS DOES NOT UPDATE! ONCE CREATED YOUR VALUES WILL BE OLD VALUES IF UPDATED AFTER YOU MADE A SNAPSHOT</strong> | ||
* @param <K> The type of keys in the cache. | ||
* @param <V> The type of values in the cache. | ||
*/ | ||
public interface CachableSnapshot<K, V> { | ||
/** | ||
* Returns the cached entries as an unmodifiable map. | ||
* | ||
* @return An unmodifiable map view of the cached entries | ||
*/ | ||
Map<K, V> asMap(); | ||
|
||
/** | ||
* Returns the cached entries as an unmodifiable list. | ||
* | ||
* @return An unmodifiable list view of the cached entries | ||
*/ | ||
List<Tuple<K, V>> asList(); | ||
|
||
/** | ||
* Returns the cached entries as an unmodifiable collection. | ||
* | ||
* @return An unmodifiable collection view of the cached entries | ||
*/ | ||
Collection<Tuple<K, V>> asCollection(); | ||
|
||
/** | ||
* Returns the cached entries as an array of tuples. | ||
* | ||
* @return An array view of the cached entries | ||
*/ | ||
Tuple<K, V>[] asTupleArray(); | ||
} |
Oops, something went wrong.