下面列出了java.util.WeakHashMap#get ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private Keyboard getLongPressKeyboard(Key popupKey) {
final WeakHashMap<Key, Keyboard> cache;
if (popupKey.isDistinctCaps()) {
cache = mMiniKeyboardCacheCaps;
} else if (popupKey.isShifted()) {
cache = mMiniKeyboardCacheShift;
} else {
cache = mMiniKeyboardCacheMain;
}
Keyboard kbd = cache.get(popupKey);
if (kbd == null) {
kbd = popupKey.getPopupKeyboard(getContext(), getPaddingLeft() + getPaddingRight());
if (kbd != null) cache.put(popupKey, kbd);
}
//Log.i(TAG, "getLongPressKeyboard returns " + kbd + " for " + popupKey);
return kbd;
}
private static Image createDefaultHatch(final Paint color, final double strokeWidth) {
WeakHashMap<Double, Image> checkCache = FillPatternStyleHelper.defaultHatchCacheWithStrokeWidth.get(color);
if (checkCache != null) {
final Image val = checkCache.get(Double.valueOf(strokeWidth));
if (val != null) {
// found existing Image with given parameter
return val;
}
}
// need to recompute hatch pattern image
final Pane pane = new Pane();
pane.setPrefSize(10, 10);
final Line fw = new Line(-5, -5, 25, 25);
final Line bw = new Line(-5, 25, 25, -5);
fw.setSmooth(false);
bw.setSmooth(false);
fw.setStroke(color);
bw.setStroke(color);
fw.setStrokeWidth(strokeWidth);
bw.setStrokeWidth(strokeWidth);
pane.getChildren().addAll(fw, bw);
pane.setStyle("-fx-background-color: rgba(0, 0, 0, 0.0)");
final Scene scene = new Scene(pane);
scene.setFill(Color.TRANSPARENT);
final Image retVal = pane.snapshot(null, null);
// add retVal to cache
if (checkCache == null) {
final WeakHashMap<Double, Image> temp = new WeakHashMap<>();
temp.put(Double.valueOf(strokeWidth), retVal);
FillPatternStyleHelper.defaultHatchCacheWithStrokeWidth.put(color, temp);
// checkCache = new WeakHashMap<>();
} else {
checkCache.put(Double.valueOf(strokeWidth), retVal);
}
return retVal;
}
/**
* Converts the object provided into its string form. Format of the returned string is defined by this converter.
*
* @return a string representation of the object passed in.
* @see StringConverter#toString
*/
@Override
public String toString(final Number object) {
// TODO: just for testing need to clean-up w.r.t. use of cache etc.
// return labelCache.get(formatter, object.doubleValue());
// return labelCache.get(formatter, object.doubleValue());
if (isExponentialForm) {
return labelCache.get(myFormatter, object.doubleValue());
}
final WeakHashMap<Number, String> hash = numberFormatCache.get(formatterPattern.hashCode());
if (hash != null) {
final String label = hash.get(object);
if (label != null) {
return label;
}
}
// couldn't find label in cache
final String retVal = String.format(formatterPattern, object.doubleValue());
// add retVal to cache
if (hash == null) {
final WeakHashMap<Number, String> temp = new WeakHashMap<>();
temp.put(object, retVal);
numberFormatCache.put(Integer.valueOf(formatterPattern.hashCode()), temp);
// checkCache = new WeakHashMap<>();
} else {
hash.put(object, retVal);
}
return retVal;
}
/**
* Returns cached large recurring primitive arrays, e.g. to be used in functions where often large temporary arrays
* are needed but that are otherwise outside the function scope not needed.
*
* <p>
* N.B. do not forget to release/return ownership of the array via {@link #release}
*
* @param arrayName unique array name
* @param size requested array size
* @return cached copy (N.B. removed from internal HashMap)
*/
public static boolean[] getCachedBooleanArray(final String arrayName, final int size) {
synchronized (booleanArrayCache) {
final WeakHashMap<Integer, boolean[]> nameHashMap = booleanArrayCache.computeIfAbsent(arrayName,
key -> new WeakHashMap<>());
boolean[] cachedArray = nameHashMap.get(size);
if (cachedArray == null) {
cachedArray = new boolean[size];
} else {
booleanArrayCache.get(arrayName).remove(size);
}
return cachedArray;
}
}
/**
* Returns cached large recurring primitive arrays, e.g. to be used in functions where often large temporary arrays
* are needed but that are otherwise outside the function scope not needed.
*
* <p>
* N.B. do not forget to release/return ownership of the array via {@link #release}
*
* @param arrayName unique array name
* @param size requested array size
* @return cached copy (N.B. removed from internal HashMap)
*/
public static byte[] getCachedByteArray(final String arrayName, final int size) {
synchronized (byteArrayCache) {
final WeakHashMap<Integer, byte[]> nameHashMap = byteArrayCache.computeIfAbsent(arrayName,
key -> new WeakHashMap<>());
byte[] cachedArray = nameHashMap.get(size);
if (cachedArray == null) {
cachedArray = new byte[size];
} else {
byteArrayCache.get(arrayName).remove(size);
}
return cachedArray;
}
}
/**
* Returns cached large recurring primitive arrays, e.g. to be used in functions where often large temporary arrays
* are needed but that are otherwise outside the function scope not needed.
*
* <p>
* N.B. do not forget to release/return ownership of the array via {@link #release}
*
* @param arrayName unique array name
* @param size requested array size
* @return cached copy (N.B. removed from internal HashMap)
*/
public static double[] getCachedDoubleArray(final String arrayName, final int size) {
synchronized (doubleArrayCache) {
final WeakHashMap<Integer, double[]> nameHashMap = doubleArrayCache.computeIfAbsent(arrayName,
key -> new WeakHashMap<>());
double[] cachedArray = nameHashMap.get(size);
if (cachedArray == null) {
cachedArray = new double[size];
} else {
doubleArrayCache.get(arrayName).remove(size);
}
return cachedArray;
}
}
/**
* Returns cached large recurring primitive arrays, e.g. to be used in functions where often large temporary arrays
* are needed but that are otherwise outside the function scope not needed.
*
* <p>
* N.B. do not forget to release/return ownership of the array via {@link #release}
*
* @param arrayName unique array name
* @param size requested array size
* @return cached copy (N.B. removed from internal HashMap)
*/
public static float[] getCachedFloatArray(final String arrayName, final int size) {
synchronized (floatArrayCache) {
final WeakHashMap<Integer, float[]> nameHashMap = floatArrayCache.computeIfAbsent(arrayName,
key -> new WeakHashMap<>());
float[] cachedArray = nameHashMap.get(size);
if (cachedArray == null) {
cachedArray = new float[size];
} else {
floatArrayCache.get(arrayName).remove(size);
}
return cachedArray;
}
}
/**
* Returns cached large recurring primitive arrays, e.g. to be used in functions where often large temporary arrays
* are needed but that are otherwise outside the function scope not needed.
*
* <p>
* N.B. do not forget to release/return ownership of the array via {@link #release}
*
* @param arrayName unique array name
* @param size requested array size
* @return cached copy (N.B. removed from internal HashMap)
*/
public static int[] getCachedIntArray(final String arrayName, final int size) {
synchronized (intArrayCache) {
final WeakHashMap<Integer, int[]> nameHashMap = intArrayCache.computeIfAbsent(arrayName,
key -> new WeakHashMap<>());
int[] cachedArray = nameHashMap.get(size);
if (cachedArray == null) {
cachedArray = new int[size];
} else {
intArrayCache.get(arrayName).remove(size);
}
return cachedArray;
}
}
/**
* Returns cached large recurring primitive arrays, e.g. to be used in functions where often large temporary arrays
* are needed but that are otherwise outside the function scope not needed.
*
* <p>
* N.B. do not forget to release/return ownership of the array via {@link #release}
*
* @param arrayName unique array name
* @param size requested array size
* @return cached copy (N.B. removed from internal HashMap)
*/
public static long[] getCachedLongArray(final String arrayName, final int size) {
synchronized (longArrayCache) {
final WeakHashMap<Integer, long[]> nameHashMap = longArrayCache.computeIfAbsent(arrayName,
key -> new WeakHashMap<>());
long[] cachedArray = nameHashMap.get(size);
if (cachedArray == null) {
cachedArray = new long[size];
} else {
longArrayCache.get(arrayName).remove(size);
}
return cachedArray;
}
}
/**
* Returns cached large recurring primitive arrays, e.g. to be used in functions where often large temporary arrays
* are needed but that are otherwise outside the function scope not needed.
*
* <p>
* N.B. do not forget to release/return ownership of the array via {@link #release}
*
* @param arrayName unique array name
* @param size requested array size
* @return cached copy (N.B. removed from internal HashMap)
*/
public static short[] getCachedShortArray(final String arrayName, final int size) {
synchronized (shortArrayCache) {
final WeakHashMap<Integer, short[]> nameHashMap = shortArrayCache.computeIfAbsent(arrayName,
key -> new WeakHashMap<>());
short[] cachedArray = nameHashMap.get(size);
if (cachedArray == null) {
cachedArray = new short[size];
} else {
shortArrayCache.get(arrayName).remove(size);
}
return cachedArray;
}
}
/**
* Returns cached large recurring primitive arrays, e.g. to be used in functions where often large temporary arrays
* are needed but that are otherwise outside the function scope not needed.
*
* <p>
* N.B. do not forget to release/return ownership of the array via {@link #release}
*
* @param arrayName unique array name
* @param size requested array size
* @return cached copy (N.B. removed from internal HashMap)
*/
public static String[] getCachedStringArray(final String arrayName, final int size) {
synchronized (stringArrayCache) {
final WeakHashMap<Integer, String[]> nameHashMap = stringArrayCache.computeIfAbsent(arrayName,
key -> new WeakHashMap<>());
String[] cachedArray = nameHashMap.get(size);
if (cachedArray == null) {
cachedArray = new String[size];
} else {
stringArrayCache.get(arrayName).remove(size);
}
return cachedArray;
}
}
/**
* Return the descriptor (ObjectStreamClass) corresponding to the class
* {@code cl}. Returns an ObjectStreamClass even if instances of the
* class cannot be serialized
*
* @param cl
* a java.langClass for which to obtain the corresponding
* descriptor
* @return the corresponding descriptor
*/
static ObjectStreamClass lookupStreamClass(Class<?> cl) {
WeakHashMap<Class<?>, ObjectStreamClass> tlc = getCache();
ObjectStreamClass cachedValue = tlc.get(cl);
if (cachedValue == null) {
cachedValue = createClassDesc(cl);
tlc.put(cl, cachedValue);
}
return cachedValue;
}
private String generateName(ChannelHandler handler) {
WeakHashMap<Class<?>, String> cache = nameCaches[(int) (Thread.currentThread().getId() % nameCaches.length)];
Class<?> handlerType = handler.getClass();
String name;
synchronized (cache) {
name = cache.get(handlerType);
if (name == null) {
name = generateName0(handlerType);
cache.put(handlerType, name);
}
}
synchronized (this) {
// It's not very likely for a user to put more than one handler of the same type, but make sure to avoid
// any name conflicts. Note that we don't cache the names generated here.
if (name2ctx.containsKey(name)) {
String baseName = name.substring(0, name.length() - 1); // Strip the trailing '0'.
for (int i = 1;; i ++) {
String newName = baseName + i;
if (!name2ctx.containsKey(newName)) {
name = newName;
break;
}
}
}
}
return name;
}
private List<WeakReference<CameraStateListener>> getCameraListeners(final EdsdkLibrary.EdsCameraRef cameraRef) {
try {
final Field listenersMap = MockFactory.initialCanonFactory.getCameraStateEventLogic().getClass().getDeclaredField("listenersMap");
listenersMap.setAccessible(true);
final WeakHashMap<EdsdkLibrary.EdsCameraRef, List<WeakReference<CameraStateListener>>> map = (WeakHashMap<EdsdkLibrary.EdsCameraRef, List<WeakReference<CameraStateListener>>>) listenersMap.get(MockFactory.initialCanonFactory.getCameraStateEventLogic());
final List<WeakReference<CameraStateListener>> weakReferences = map.get(cameraRef);
if (weakReferences != null)
return weakReferences;
return Collections.emptyList();
} catch (IllegalAccessException | NoSuchFieldException e) {
Assertions.fail("Failed reflection", e);
throw new IllegalStateException("can not reach");
}
}
private List<WeakReference<CameraPropertyListener>> getCameraListeners(final EdsdkLibrary.EdsCameraRef cameraRef) {
try {
final Field listenersMap = MockFactory.initialCanonFactory.getCameraPropertyEventLogic().getClass().getDeclaredField("listenersMap");
listenersMap.setAccessible(true);
final WeakHashMap<EdsdkLibrary.EdsCameraRef, List<WeakReference<CameraPropertyListener>>> map = (WeakHashMap<EdsdkLibrary.EdsCameraRef, List<WeakReference<CameraPropertyListener>>>) listenersMap.get(MockFactory.initialCanonFactory.getCameraPropertyEventLogic());
final List<WeakReference<CameraPropertyListener>> weakReferences = map.get(cameraRef);
if (weakReferences != null)
return weakReferences;
return Collections.emptyList();
} catch (IllegalAccessException | NoSuchFieldException e) {
Assertions.fail("Failed reflection", e);
throw new IllegalStateException("can not reach");
}
}
private List<WeakReference<CameraObjectListener>> getCameraListeners(final EdsdkLibrary.EdsCameraRef cameraRef) {
try {
final Field listenersMap = MockFactory.initialCanonFactory.getCameraObjectEventLogic().getClass().getDeclaredField("listenersMap");
listenersMap.setAccessible(true);
final WeakHashMap<EdsdkLibrary.EdsCameraRef, List<WeakReference<CameraObjectListener>>> map = (WeakHashMap<EdsdkLibrary.EdsCameraRef, List<WeakReference<CameraObjectListener>>>) listenersMap.get(MockFactory.initialCanonFactory.getCameraObjectEventLogic());
final List<WeakReference<CameraObjectListener>> weakReferences = map.get(cameraRef);
if (weakReferences != null)
return weakReferences;
return Collections.emptyList();
} catch (IllegalAccessException | NoSuchFieldException e) {
Assertions.fail("Failed reflection", e);
throw new IllegalStateException("can not reach");
}
}
public synchronized long getWindowDeactivationTime(Window w) {
AppContext ctx = getAppContext(w);
if (ctx == null) {
return -1;
}
@SuppressWarnings("unchecked")
WeakHashMap<Window, Long> map = (WeakHashMap<Window, Long>)ctx.get(DEACTIVATION_TIMES_MAP_KEY);
if (map == null) {
return -1;
}
Long time = map.get(w);
return time == null ? -1 : time;
}
public synchronized long getWindowDeactivationTime(Window w) {
AppContext ctx = getAppContext(w);
if (ctx == null) {
return -1;
}
@SuppressWarnings("unchecked")
WeakHashMap<Window, Long> map = (WeakHashMap<Window, Long>)ctx.get(DEACTIVATION_TIMES_MAP_KEY);
if (map == null) {
return -1;
}
Long time = map.get(w);
return time == null ? -1 : time;
}
private static void _doSaveInfo(WeakHashMap<Canvas, ArrayList<CanvasSaveInfo>> stack, CanvasSaveInfo info) {
if (stack != null) {
ArrayList<CanvasSaveInfo> canvasSaveInfos = stack.get(info.canvas);
if (canvasSaveInfos == null) {
canvasSaveInfos = new ArrayList<CanvasSaveInfo>();
stack.put(info.canvas, canvasSaveInfos);
}
canvasSaveInfos.add(info);
}
}
public static synchronized NumericDocValues getNumericDocValues(String field, LeafReader reader) throws IOException
{
WeakHashMap<Object, NumericDocValues> fieldCache = cache.get(field);
if(fieldCache == null)
{
fieldCache = new WeakHashMap<Object, NumericDocValues>();
cache.put(field, fieldCache);
}
Object cacheKey = reader.getCoreCacheKey();
NumericDocValues cachedValues = fieldCache.get(cacheKey);
if(cachedValues == null)
{
NumericDocValues fieldValues = reader.getNumericDocValues(field);
if(fieldValues == null)
{
return null;
}
else
{
int maxDoc = reader.maxDoc();
boolean longs = false;
int[] intValues = new int[maxDoc]; //Always start off with an int array.
SettableDocValues settableValues = new IntValues(intValues);
for(int i=0; i<maxDoc; i++)
{
long value = fieldValues.get(i);
if(value > Integer.MAX_VALUE && !longs)
{
longs = true;
settableValues = new LongValues(intValues);
}
settableValues.set(i, value);
}
fieldCache.put(cacheKey, settableValues);
return settableValues;
}
}
else
{
return cachedValues;
}
}