Java Code Examples for java.util.WeakHashMap#get()

The following examples show how to use java.util.WeakHashMap#get() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LatinKeyboardBaseView.java    From hackerskeyboard with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 2
Source File: ObjectStreamClass.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * 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;

}
 
Example 3
Source File: XulUtils.java    From starcor.xul with GNU Lesser General Public License v3.0 5 votes vote down vote up
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);
	}
}
 
Example 4
Source File: SunToolkit.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
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;
}
 
Example 5
Source File: SunToolkit.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 6
Source File: CameraObjectEventLogicDefaultMockTest.java    From canon-sdk-java with MIT License 5 votes vote down vote up
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");
    }
}
 
Example 7
Source File: CameraPropertyEventLogicDefaultMockTest.java    From canon-sdk-java with MIT License 5 votes vote down vote up
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");
    }
}
 
Example 8
Source File: CameraStateEventLogicDefaultMockTest.java    From canon-sdk-java with MIT License 5 votes vote down vote up
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");
    }
}
 
Example 9
Source File: DefaultChannelPipeline.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 10
Source File: FillPatternStyleHelper.java    From chart-fx with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 11
Source File: ArrayCache.java    From chart-fx with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
    }
}
 
Example 12
Source File: ArrayCache.java    From chart-fx with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
    }
}
 
Example 13
Source File: ArrayCache.java    From chart-fx with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
    }
}
 
Example 14
Source File: ArrayCache.java    From chart-fx with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
    }
}
 
Example 15
Source File: ArrayCache.java    From chart-fx with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
    }
}
 
Example 16
Source File: ArrayCache.java    From chart-fx with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
    }
}
 
Example 17
Source File: ArrayCache.java    From chart-fx with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
    }
}
 
Example 18
Source File: ArrayCache.java    From chart-fx with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
    }
}
 
Example 19
Source File: DefaultFormatter.java    From chart-fx with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 20
Source File: DocValuesCache.java    From SearchServices with GNU Lesser General Public License v3.0 4 votes vote down vote up
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;
    }
}