问题一
Collectors.toMap(PositionContent::getName, Function.identity(), (key1, key2) -> key2)
这个怎么理解?
Map map = list.stream().collect(Collectors.toMap(PositionContent::getName, Function.identity(), (key1, key2) -> key2));
问题二
.newBuilder()
.maximumSize(1000)
.expireAfterAccess(10, TimeUnit.MINUTES)
这种点点是什么意思?这段是什么意思?
static LoadingCache screenSource = CacheBuilder.newBuilder()
.maximumSize(1000)
.expireAfterAccess(10, TimeUnit.MINUTES)
.build(
new CacheLoader() {
public Object load(String key) {
return getValue(key);
}
});
源码
public class PurposeFileUtil {
public static final String Positions =”positions”;
static LoadingCache screenSource = CacheBuilder.newBuilder()
.maximumSize(1000)
.expireAfterAccess(10, TimeUnit.MINUTES)
.build(
new CacheLoader() {
public Object load(String key) {
return getValue(key);
}
});
private static Object getValue(String key){
Config conf = ConfigFactory.parseFile(new File(“perception-purpose.conf”));
List list = JSON.parseArray(JSON.toJSONString(conf.getAnyRefList(“screen.positions”)),PositionContent.class);
Map map = list.stream().collect(Collectors.toMap(PositionContent::getName, Function.identity(), (key1, key2) -> key2));
switch(key){
case Positions:
return map;
default :
return conf.getAnyRefList(“screen.responce.time.line”);
}
}
public static Object getSourceByKey(String key)throws Exception{
return screenSource.get(key);
}
public static void initSource(){
try {
JNotify.addWatch(“conf”, JNotify.FILE_MODIFIED, false, new JNotifyListener(){
@Override
public void fileRenamed(int wd, String rootPath, String oldName, String newName) {
}
@Override
public void fileModified(int wd, String rootPath, String name) {
if(name.equals(“source.conf”)){
loadSource(rootPath,name);
}
}
public void fileDeleted(int wd, String rootPath, String name) {
}
public void fileCreated(int wd, String rootPath, String name) {
}
});
} catch (JNotifyException e) {
log.error(“error watch file”);
}
while (true) {
try {
Thread.sleep(60000);
} catch (InterruptedException e) {
}
}
}
private static void loadSource(String rootPath , String name){
Config conf = ConfigFactory.parseFile(new File(rootPath+”/”+name));
List list = JSON.parseArray(JSON.toJSONString(conf.getAnyRefList(“screen.monitor”)),PositionContent.class);
Map map = list.stream().collect(Collectors.toMap(PositionContent::getName, Function.identity(), (key1, key2) -> key2));
screenSource.put(Positions, map);
}
}