Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.neko233.toolchain.counter.CounterApi Maven / Gradle / Ivy
package com.neko233.toolchain.counter;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
/**
* counter is composite 3 part = id, type, count
* refresh == del
* if refresh_ms == -1, it means forever
*
* @author SolarisNeko
*/
public interface CounterApi {
/**
* 永久不过期
*/
int NEXT_REFRESH_MS_FOREVER_FLAG = -1;
static boolean isForever(long nextRefreshMs) {
return Objects.equals(NEXT_REFRESH_MS_FOREVER_FLAG, nextRefreshMs);
}
static boolean isNeedRefresh(Long nextRefreshMs) {
if (nextRefreshMs == null) {
return false;
}
if (nextRefreshMs == -1) {
return false;
}
return System.currentTimeMillis() > nextRefreshMs;
}
/**
* refresh
*/
default boolean refresh(Object id, String type) {
del(id, type);
return true;
}
/**
* get
*/
default CounterData get(Object id, String type) {
return get(String.valueOf(id), type);
}
CounterData get(String id, String type);
default Number getValue(Object id, String type) {
Map value = getValue(id, Collections.singleton(type));
if (value == null || value.size() == 0) {
return null;
}
return value.get(type);
}
/**
* get value
*/
default Map getValue(Object id, Collection types) {
return getValue(String.valueOf(id), types);
}
Map getValue(String id, Collection types);
/**
* set
*/
default boolean set(Object id, String type) {
return set(id, type, 0);
}
default boolean set(Object id, String type, Number count) {
return set(id, type, count, -1);
}
default boolean set(Object id, String type, Number count, int nextTime, TimeUnit nextTimeUnit) {
return set(id, type, count, System.currentTimeMillis() + nextTimeUnit.toMillis(nextTime));
}
default boolean set(Object id, String type, Number count, long nextRefreshMs) {
return set(id, Collections.singletonMap(type, count), nextRefreshMs);
}
default boolean set(Object id, Map typeCountMap, long nextRefreshMs) {
return set(String.valueOf(id), typeCountMap, nextRefreshMs);
}
boolean set(String id, Map typeCountMap, long nextRefreshMs);
/**
* del
*/
default boolean del(Object id) {
return del(String.valueOf(id));
}
boolean del(String id);
default boolean del(Object id, String type) {
return del(id, Collections.singleton(type));
}
default boolean del(Object id, Collection types) {
return del(String.valueOf(id), types);
}
boolean del(String id, Collection types);
/**
* minus 1
*/
default boolean minus(Object id, String type) {
return minus(id, type, 1);
}
default boolean minus(Object id, String type, Number minusCount) {
if (minusCount == null) {
return false;
}
return plus(id, type, -minusCount.longValue());
}
/**
* plus 1
*/
default boolean plus(Object id, String type) {
return plus(id, type, 1);
}
default boolean plus(Object id, String type, Number addCount) {
if (addCount == null) {
return false;
}
CounterData data = get(id, type);
if (data == null) {
return false;
}
if (isNeedRefresh(data.getNextRefreshMs())) {
refresh(id, type);
return false;
}
if (data.getCount() == null) {
return false;
}
set(id, type, data.getCount().longValue() + addCount.longValue(), data.getNextRefreshMs());
return true;
}
/**
* 过期时间点
*
* @param nextRefreshMs 下一次刷新的毫秒数
*/
default boolean expireAtMs(Object id, String type, long nextRefreshMs) {
return expireAtMs(String.valueOf(id), type, nextRefreshMs);
}
boolean expireAtMs(String id, String type, long nextRefreshMs);
default boolean expireNext(Object id, String type, int nextTime, TimeUnit nextTimeUnit) {
return expireAtMs(id, type, System.currentTimeMillis() + nextTimeUnit.toMillis(nextTime));
}
}