
com.ociweb.pronghorn.util.SelectedKeyHashMapHolder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of pronghorn Show documentation
Show all versions of pronghorn Show documentation
Primary dependency for any project using the Pronghorn framework
The newest version!
package com.ociweb.pronghorn.util;
import java.lang.reflect.Field;
import java.nio.channels.SelectionKey;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class SelectedKeyHashMapHolder {
private boolean checkedForKeyMap;
private HashMap keyMap;
private final static Logger logger = LoggerFactory.getLogger(SelectedKeyHashMapHolder.class);
private final static int initSizeMaxSelections = 1<<15;
public HashMap selectedKeyMap(Set selectedKeys) {
if (!checkedForKeyMap && null==keyMap) {
checkedForKeyMap = true;
Field[] fields = selectedKeys.getClass().getDeclaredFields();
int f = fields.length;
while (--f>=0) {
fields[f].setAccessible(true);
Field[] fields2;
try {
Object objectF = fields[f].get(selectedKeys);
if (objectF instanceof HashSet) {
fields2 = objectF.getClass().getDeclaredFields();
int f2 = fields2.length;
while (--f2>=0) {
fields2[f2].setAccessible(true);
Object map = fields2[f2].get(objectF);
if (map instanceof HashMap) {
HashMap localMap = (HashMap)map;
SelectionKeyHashMap lhm = new SelectionKeyHashMap(initSizeMaxSelections);
lhm.putAll(localMap);
fields2[f2].set(objectF, lhm);
keyMap = lhm;
}
fields2[f2].setAccessible(false);
}
}
} catch (Throwable e) {
//ignore we will fall back
logger.info("unable to find map",e);
}
fields[f].setAccessible(false);
}
}
return keyMap;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy