/** * 存入k-v对,如果在local/remote中有相同的key,会覆盖 * * @param key * @param value */ public void addConfig(String key, String value) { synchronized (this) { head.getMap().put(key, value); if (!exist(key)) { merageMap.put(key, value); } } } private boolean exist(String key) {
/** * 删除k-v对,如果在local/remote中有相同的key,执行此操作后,在下次local/remote刷新前,key将不会存在 * * @param key */ public void removeConfig(String key) { synchronized (this) { head.getMap().remove(key); if (!exist(key)) { merageMap.remove(key); } } }
private void replace(MapItem targetItem,String name, Map<String, String> dest) { Map<String, String> orMap = targetItem.getMap(); Map<String, String> cloneOrMap = orMap; if(orMap instanceof ConfigMap){ cloneOrMap = ((ConfigMap)orMap).clone();//使用clone,不触发事件put }else{ cloneOrMap = new HashMap<String, String>(orMap); } Map<String, String> temp = new HashMap<String, String>(dest); compareAndUpdate(cloneOrMap,temp);//将map合并到cloneOrMap targetItem.setMap(cloneOrMap); putMapItem(name,targetItem); }
private void addBefore(MapItem targetItem, String name, Map<String, String> map) { MapItem mapitem = new MapItem(map,isConfigMap); MapItem oldpre = targetItem.pre; oldpre.next = mapitem; mapitem.pre = oldpre; mapitem.next = targetItem; targetItem.pre = mapitem; putMapItem(name, mapitem); }
private void addAfter(MapItem targetItem, String name, Map<String, String> map) { MapItem mapitem = new MapItem(map,isConfigMap); MapItem oldAfter = targetItem.next; oldAfter.pre = mapitem; mapitem.next = oldAfter; mapitem.pre = targetItem; targetItem.next = mapitem; putMapItem(name, mapitem); }
private boolean exist(String key) { MapItem item = head.next; while (item != null) { if (item.containsKey(key)) { return true; } item = item.next; } return false; } /**
private void notconfigMapMod() { Map<String, String> tempStore = new HashMap<String, String>(); MapItem item = head; while( item!=null ){ //除tail将所有map合并至tempMap2 merage(tempStore,new HashMap<String, String>(item.getMap())); item = item.next; } tempStore = ConfigurationUtil.replace(tempStore); Map<String,String> tempMap3 = ((ConfigMap)merageMap).clone();//不会触发事件 compareAndUpdate(tempMap3, tempStore); //触发事件 merageMap = tempMap3; }
private void configMapMod() { Map<String, String> tempMap = new HashMap<String, String>(); MapItem item = head; while (item != null) { tempMap.putAll(item.getMap()); item = item.next; } merageMap = tempMap; }