com.github.unclecatmyself.bootstrap.channel.cache.CacheMap Maven / Gradle / Ivy
package com.github.unclecatmyself.bootstrap.channel.cache;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
/**
* Create by UncleCatMySelf in 2018/12/06
**/
public class CacheMap {
private ConcurrentHashMap> datas = new ConcurrentHashMap<>();
public boolean putData(K[] topic, V v){
if(topic.length==1){
Node kvNode = buildOne(topic[0], v);
if(kvNode!=null && kvNode.topic.equals(topic[0])){
return true;
}
}
else{
Node kvNode = buildOne(topic[0], null);
for(int i=1;i kvNode = datas.get(ks[0]);
for(int i=1;i getData(K[] ks){
if(ks.length==1){
return datas.get(ks[0]).get();
}
else{
Node node = datas.get(ks[0]);
if(node!=null){
List all = new ArrayList<>();
all.addAll(node.get());
for(int i=1;i buildOne(K k,V v){
Node node = this.datas.computeIfAbsent(k, key -> {
Node kObjectNode = new Node<>(k);
return kObjectNode;
});
if(v!=null){
node.put(v);
}
return node;
}
class Node{
private final K topic;
private volatile ConcurrentHashMap> map =new ConcurrentHashMap<>() ;
List vs = new CopyOnWriteArrayList<>();
public K getTopic() {return topic;}
Node(K topic) {
this.topic = topic;
}
public boolean delValue(V v){
return vs.remove(v);
}
public Node putNextValue(K k,V v){
Node kvNode = map.computeIfAbsent(k, key -> {
Node node = new Node<>(k);
return node;
});
if(v!=null){
kvNode.put(v);
}
return kvNode;
}
public Node getNext(K k){
return map.get(k);
}
public boolean put(V v){
return vs.add(v);
}
public List get(){
return vs;
}
}
}