v1.5
This commit is contained in:
parent
991a9c54fd
commit
c307d065d1
|
@ -0,0 +1,17 @@
|
|||
package com.io.yutian.aulib.expiringmap;
|
||||
|
||||
/**
|
||||
* Loads entries on demand.
|
||||
*
|
||||
* @param <K> Key type
|
||||
* @param <V> Value type
|
||||
*/
|
||||
public interface EntryLoader<K, V> {
|
||||
/**
|
||||
* Called to load a new value for the {@code key} into an expiring map.
|
||||
*
|
||||
* @param key to load a value for
|
||||
* @return new value to load
|
||||
*/
|
||||
V load(K key);
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
package com.io.yutian.aulib.expiringmap;
|
||||
|
||||
/**
|
||||
* A listener for expired object events.
|
||||
*
|
||||
* @param <K> Key type
|
||||
* @param <V> Value type
|
||||
*/
|
||||
public interface ExpirationListener<K, V> {
|
||||
/**
|
||||
* Called when a map entry expires.
|
||||
*
|
||||
* @param key Expired key
|
||||
* @param value Expired value
|
||||
*/
|
||||
void expired(K key, V value);
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
package com.io.yutian.aulib.expiringmap;
|
||||
|
||||
/**
|
||||
* Determines how ExpiringMap entries should be expired.
|
||||
*/
|
||||
public enum ExpirationPolicy {
|
||||
/** Expires entries based on when they were last accessed */
|
||||
ACCESSED,
|
||||
/** Expires entries based on when they were created */
|
||||
CREATED;
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
package com.io.yutian.aulib.expiringmap;
|
||||
|
||||
/**
|
||||
* Loads entries on demand, with control over each value's expiry duration (i.e. variable expiration).
|
||||
*
|
||||
* @param <K> Key type
|
||||
* @param <V> Value type
|
||||
*/
|
||||
public interface ExpiringEntryLoader<K, V> {
|
||||
/**
|
||||
* Called to load a new value for the {@code key} into an expiring map.
|
||||
*
|
||||
* @param key to load a value for
|
||||
* @return contains new value to load along with its expiry duration
|
||||
*/
|
||||
ExpiringValue<V> load(K key);
|
||||
}
|
1422
src/main/java/com/io/yutian/aulib/expiringmap/ExpiringMap.java
Normal file
1422
src/main/java/com/io/yutian/aulib/expiringmap/ExpiringMap.java
Normal file
File diff suppressed because it is too large
Load Diff
122
src/main/java/com/io/yutian/aulib/expiringmap/ExpiringValue.java
Normal file
122
src/main/java/com/io/yutian/aulib/expiringmap/ExpiringValue.java
Normal file
|
@ -0,0 +1,122 @@
|
|||
package com.io.yutian.aulib.expiringmap;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* A value which should be stored in an {@link ExpiringMap} with optional control over its expiration.
|
||||
*
|
||||
* @param <V> the type of value being stored
|
||||
*/
|
||||
public final class ExpiringValue<V> {
|
||||
private static final long UNSET_DURATION = -1L;
|
||||
private final V value;
|
||||
private final ExpirationPolicy expirationPolicy;
|
||||
private final long duration;
|
||||
private final TimeUnit timeUnit;
|
||||
|
||||
/**
|
||||
* Creates an ExpiringValue to be stored in an {@link ExpiringMap}. The map's default values for
|
||||
* {@link ExpirationPolicy expiration policy} and {@link ExpiringMap#getExpiration()} expiration} will be used.
|
||||
*
|
||||
* @param value the value to store
|
||||
* @see ExpiringMap#put(Object, Object)
|
||||
*/
|
||||
public ExpiringValue(V value) {
|
||||
this(value, UNSET_DURATION, null, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an ExpiringValue to be stored in an {@link ExpiringMap}. The map's default
|
||||
* {@link ExpiringMap#getExpiration()} expiration} will be used.
|
||||
*
|
||||
* @param value the value to store
|
||||
* @param expirationPolicy the expiration policy for the value
|
||||
* @see ExpiringMap#put(Object, Object, ExpirationPolicy)
|
||||
*/
|
||||
public ExpiringValue(V value, ExpirationPolicy expirationPolicy) {
|
||||
this(value, UNSET_DURATION, null, expirationPolicy);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an ExpiringValue to be stored in an {@link ExpiringMap}. The map's default {@link ExpirationPolicy
|
||||
* expiration policy} will be used.
|
||||
*
|
||||
* @param value the value to store
|
||||
* @param duration the length of time after an entry is created that it should be removed
|
||||
* @param timeUnit the unit that {@code duration} is expressed in
|
||||
* @see ExpiringMap#put(Object, Object, long, TimeUnit)
|
||||
* @throws NullPointerException on null timeUnit
|
||||
*/
|
||||
public ExpiringValue(V value, long duration, TimeUnit timeUnit) {
|
||||
this(value, duration, timeUnit, null);
|
||||
if (timeUnit == null) {
|
||||
throw new NullPointerException();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an ExpiringValue to be stored in an {@link ExpiringMap}.
|
||||
*
|
||||
* @param value the value to store
|
||||
* @param duration the length of time after an entry is created that it should be removed
|
||||
* @param timeUnit the unit that {@code duration} is expressed in
|
||||
* @param expirationPolicy the expiration policy for the value
|
||||
* @see ExpiringMap#put(Object, Object, ExpirationPolicy, long, TimeUnit)
|
||||
* @throws NullPointerException on null timeUnit
|
||||
*/
|
||||
public ExpiringValue(V value, ExpirationPolicy expirationPolicy, long duration, TimeUnit timeUnit) {
|
||||
this(value, duration, timeUnit, expirationPolicy);
|
||||
if (timeUnit == null) {
|
||||
throw new NullPointerException();
|
||||
}
|
||||
}
|
||||
|
||||
private ExpiringValue(V value, long duration, TimeUnit timeUnit, ExpirationPolicy expirationPolicy) {
|
||||
this.value = value;
|
||||
this.expirationPolicy = expirationPolicy;
|
||||
this.duration = duration;
|
||||
this.timeUnit = timeUnit;
|
||||
}
|
||||
|
||||
public V getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public ExpirationPolicy getExpirationPolicy() {
|
||||
return expirationPolicy;
|
||||
}
|
||||
|
||||
public long getDuration() {
|
||||
return duration;
|
||||
}
|
||||
|
||||
public TimeUnit getTimeUnit() {
|
||||
return timeUnit;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return value != null ? value.hashCode() : 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if (o == null || getClass() != o.getClass()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
ExpiringValue<?> that = (ExpiringValue<?>) o;
|
||||
return !(value != null ? !value.equals(that.value) : that.value != null)
|
||||
&& expirationPolicy == that.expirationPolicy && duration == that.duration && timeUnit == that.timeUnit;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ExpiringValue{" + "value=" + value + ", expirationPolicy=" + expirationPolicy + ", duration=" + duration
|
||||
+ ", timeUnit=" + timeUnit + '}';
|
||||
}
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
package com.io.yutian.aulib.expiringmap.internal;
|
||||
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
/**
|
||||
* @author Jonathan Halterman
|
||||
*/
|
||||
public final class Assert {
|
||||
private Assert() {
|
||||
}
|
||||
|
||||
public static <T> T notNull(T reference, String parameterName) {
|
||||
if (reference == null)
|
||||
throw new NullPointerException(parameterName + " cannot be null");
|
||||
return reference;
|
||||
}
|
||||
|
||||
public static void operation(boolean condition, String message) {
|
||||
if (!condition)
|
||||
throw new UnsupportedOperationException(message);
|
||||
}
|
||||
|
||||
public static void state(boolean expression, String errorMessageFormat, Object... args) {
|
||||
if (!expression)
|
||||
throw new IllegalStateException(String.format(errorMessageFormat, args));
|
||||
}
|
||||
|
||||
public static void element(Object element, Object key) {
|
||||
if (element == null)
|
||||
throw new NoSuchElementException(key.toString());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
package com.io.yutian.aulib.expiringmap.internal;
|
||||
|
||||
import java.util.concurrent.ThreadFactory;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
/**
|
||||
* Named thread factory.
|
||||
*/
|
||||
public class NamedThreadFactory implements ThreadFactory {
|
||||
private final AtomicInteger threadNumber = new AtomicInteger(1);
|
||||
private final String nameFormat;
|
||||
|
||||
/**
|
||||
* Creates a thread factory that names threads according to the {@code nameFormat} by supplying a
|
||||
* single argument to the format representing the thread number.
|
||||
*/
|
||||
public NamedThreadFactory(String nameFormat) {
|
||||
this.nameFormat = nameFormat;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Thread newThread(Runnable r) {
|
||||
Thread thread = new Thread(r, String.format(nameFormat, threadNumber.getAndIncrement()));
|
||||
thread.setDaemon(true);
|
||||
return thread;
|
||||
}
|
||||
}
|
|
@ -1,5 +1,6 @@
|
|||
package com.io.yutian.aulib.util;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import sun.misc.Unsafe;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
@ -9,6 +10,16 @@ import java.security.PrivilegedAction;
|
|||
|
||||
public class ReflectionUtil {
|
||||
|
||||
private static String nmsVersion;
|
||||
|
||||
public static String getNMSVersion() {
|
||||
if (nmsVersion == null) {
|
||||
String name = Bukkit.getServer().getClass().getPackage().getName();
|
||||
nmsVersion = name.substring(name.lastIndexOf('.') + 1);
|
||||
}
|
||||
return nmsVersion;
|
||||
}
|
||||
|
||||
public static Field getDeclaredField(Class clazz, String fieldName) {
|
||||
try {
|
||||
return clazz.getDeclaredField(fieldName);
|
||||
|
|
Loading…
Reference in New Issue
Block a user