Ability to modify api

This commit is contained in:
Sotr
2019-04-10 02:22:38 +08:00
parent 17e0e3edca
commit b2b93d9f87
1055 changed files with 107079 additions and 12 deletions

View File

@@ -0,0 +1,38 @@
package co.aikar.util;
import com.google.common.collect.ForwardingMap;
import java.util.HashMap;
import java.util.Map;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class Counter <T> extends ForwardingMap<T, Long> {
private final Map<T, Long> counts = new HashMap<>();
public long decrement(@Nullable T key) {
return increment(key, -1);
}
public long increment(@Nullable T key) {
return increment(key, 1);
}
public long decrement(@Nullable T key, long amount) {
return decrement(key, -amount);
}
public long increment(@Nullable T key, long amount) {
Long count = this.getCount(key);
count += amount;
this.counts.put(key, count);
return count;
}
public long getCount(@Nullable T key) {
return this.counts.getOrDefault(key, 0L);
}
@NotNull
@Override
protected Map<T, Long> delegate() {
return this.counts;
}
}