Skip to content
This repository has been archived by the owner on Jan 6, 2025. It is now read-only.

Commit

Permalink
v3.2.0
Browse files Browse the repository at this point in the history
Honestly don't even ask me why I wasted a week on working on a library 😭
  • Loading branch information
Manered committed Dec 19, 2023
1 parent 7f6fabf commit d0ec585
Show file tree
Hide file tree
Showing 137 changed files with 4,753 additions and 1,378 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ replay_pid*
/target/
target/
target
/target
/target
dependency-reduced-pom.xml
29 changes: 28 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>dev.manere</groupId>
<artifactId>utils</artifactId>
<version>3.1.0</version>
<version>3.2.0</version>
<packaging>jar</packaging>

<name>Utils</name>
Expand All @@ -27,6 +27,27 @@
<target>17</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.5.0</version>
<configuration>
<relocations>
<relocation>
<pattern>com.jeff_media.customblockdata</pattern>
<shadedPattern>dev.manere.utils.customblockdata</shadedPattern>
</relocation>
</relocations>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<resources>
<resource>
Expand Down Expand Up @@ -86,6 +107,12 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.jeff-media</groupId>
<artifactId>custom-block-data</artifactId>
<version>2.2.2</version>
<scope>compile</scope>
</dependency>
</dependencies>

<dependencyManagement>
Expand Down
171 changes: 171 additions & 0 deletions src/main/java/dev/manere/utils/cachable/Cachable.java
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 src/main/java/dev/manere/utils/cachable/CachableSnapshot.java
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();
}
Loading

0 comments on commit d0ec585

Please sign in to comment.