Java Code Examples for java.lang.ref.SoftReference#clear()

The following examples show how to use java.lang.ref.SoftReference#clear() . 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: ImmutableJsonObjectTest.java    From ditto with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void validateSoftReferenceStrategy() throws IllegalAccessException, NoSuchFieldException {
    final ImmutableJsonObject jsonObject = ImmutableJsonObject.of(KNOWN_FIELDS);
    assertInternalCachesAreAsExpected(jsonObject, true, false);

    final Field valueListField = jsonObject.getClass().getDeclaredField("fieldMap");
    valueListField.setAccessible(true);
    final SoftReferencedFieldMap valueList = (SoftReferencedFieldMap) valueListField.get(jsonObject);

    final Field softReferenceField = valueList.getClass().getDeclaredField("fieldsReference");
    softReferenceField.setAccessible(true);
    SoftReference softReference = (SoftReference) softReferenceField.get(valueList);

    softReference.clear();

    assertThat(jsonObject.getValue(KNOWN_KEY_FOO).isPresent()).isTrue();
}
 
Example 2
Source File: JLaTeXMathCache.java    From AndroidMathKeyboard with Apache License 2.0 5 votes vote down vote up
private static SoftReference<CachedImage> makeImage(CachedTeXFormula cached)
		throws ParseException {
	TeXFormula formula = new TeXFormula(cached.f);
	TeXIcon icon = formula.createTeXIcon(cached.style, cached.size,
			cached.type, cached.fgcolor);
	icon.setInsets(new Insets(cached.inset, cached.inset, cached.inset,
			cached.inset));
	Bitmap image = Bitmap.createBitmap(icon.getIconWidth(),
			icon.getIconHeight(), Config.ARGB_8888);
	Canvas g2 = new Canvas(image);
	icon.paintIcon(g2, 0, 0);
	cached.setDimensions(icon.getIconWidth(), icon.getIconHeight(),
			icon.getIconDepth());
	SoftReference<CachedImage> img = new SoftReference<CachedImage>(
			new CachedImage(image, cached), queue);

	if (cache.size() >= max) {
		Reference soft;
		while ((soft = queue.poll()) != null) {
			CachedImage ci = (CachedImage) soft.get();
			if (ci != null) {
				cache.remove(ci.cachedTf);
			}
		}
		Iterator<CachedTeXFormula> iter = cache.keySet().iterator();
		if (iter.hasNext()) {
			CachedTeXFormula c = iter.next();
			SoftReference<CachedImage> cachedImage = cache.get(c);
			if (cachedImage != null) {
				cachedImage.clear();
			}
			cache.remove(c);
		}
	}
	cache.put(cached, img);

	return img;
}
 
Example 3
Source File: DefaultVersionedPlayRunAdapter.java    From playframework with Apache License 2.0 5 votes vote down vote up
private void closeOldLoaders() throws IOException {
    SoftReference<Closeable> ref = loadersToClose.poll();
    while (ref != null) {
        Closeable closeable = ref.get();
        if (closeable != null) {
            closeable.close();
        }
        ref.clear();
        ref = loadersToClose.poll();
    }
}
 
Example 4
Source File: SoftCache.java    From JavaRushTasks with MIT License 5 votes vote down vote up
public AnyObject put(Long key, AnyObject value) {
    SoftReference<AnyObject> softReference = cacheMap.put(key, new SoftReference<>(value));

    if (softReference != null) {
        AnyObject o = softReference.get();
        softReference.clear();
        return o;
    } else {
        return null;
    }
}
 
Example 5
Source File: SoftCache.java    From JavaRushTasks with MIT License 5 votes vote down vote up
public AnyObject remove(Long key) {
    SoftReference<AnyObject> softReference = cacheMap.remove(key);

    //напишите тут ваш код
    if (softReference != null) {
        AnyObject o = softReference.get();
        softReference.clear();
        return o;
    } else {
        return null;
    }
}
 
Example 6
Source File: JLaTeXMathCache.java    From FlexibleRichTextView with Apache License 2.0 5 votes vote down vote up
private static SoftReference<CachedImage> makeImage(CachedTeXFormula cached)
		throws ParseException {
	TeXFormula formula = new TeXFormula(cached.f);
	TeXIcon icon = formula.createTeXIcon(cached.style, cached.size,
			cached.type, cached.fgcolor);
	icon.setInsets(new Insets(cached.inset, cached.inset, cached.inset,
			cached.inset));
	Bitmap image = Bitmap.createBitmap(icon.getIconWidth(),
			icon.getIconHeight(), Config.ARGB_8888);
	Canvas g2 = new Canvas(image);
	icon.paintIcon(g2, 0, 0);
	cached.setDimensions(icon.getIconWidth(), icon.getIconHeight(),
			icon.getIconDepth());
	SoftReference<CachedImage> img = new SoftReference<CachedImage>(
			new CachedImage(image, cached), queue);

	if (cache.size() >= max) {
		Reference soft;
		while ((soft = queue.poll()) != null) {
			CachedImage ci = (CachedImage) soft.get();
			if (ci != null) {
				cache.remove(ci.cachedTf);
			}
		}
		Iterator<CachedTeXFormula> iter = cache.keySet().iterator();
		if (iter.hasNext()) {
			CachedTeXFormula c = iter.next();
			SoftReference<CachedImage> cachedImage = cache.get(c);
			if (cachedImage != null) {
				cachedImage.clear();
			}
			cache.remove(c);
		}
	}
	cache.put(cached, img);

	return img;
}
 
Example 7
Source File: JLaTeXMathCache.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private static SoftReference<CachedImage> makeImage(CachedTeXFormula cached) throws ParseException {
    TeXFormula formula = new TeXFormula(cached.f);
    TeXIcon icon = formula.createTeXIcon(cached.style, cached.size, cached.type, cached.fgcolor);
    icon.setInsets(new Insets(cached.inset, cached.inset, cached.inset, cached.inset));
    BufferedImage image = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = image.createGraphics();
    icon.paintIcon(null, g2, 0, 0);
    g2.dispose();
    cached.setDimensions(icon.getIconWidth(), icon.getIconHeight(), icon.getIconDepth());
    SoftReference<CachedImage> img = new SoftReference<CachedImage>(new CachedImage(image, cached), queue);

    if (cache.size() >= max) {
        Reference<? extends CachedImage> soft;
        while ((soft = queue.poll()) != null) {
            CachedImage ci = (CachedImage) soft.get();
            if (ci != null) {
                cache.remove(ci.cachedTf);
            }
        }
        Iterator<CachedTeXFormula> iter = cache.keySet().iterator();
        if (iter.hasNext()) {
            CachedTeXFormula c = iter.next();
            SoftReference<CachedImage> cachedImage = cache.get(c);
            if (cachedImage != null) {
                cachedImage.clear();
            }
            cache.remove(c);
        }
    }
    cache.put(cached, img);

    return img;
}
 
Example 8
Source File: Ref.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Flushes the cached object.  Forces the next invocation of get() to
 * invoke reconstitute().
 */
public synchronized void flush() {
    SoftReference s = soft;
    if (s != null) s.clear();
    soft = null;
}
 
Example 9
Source File: Ref.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Flushes the cached object.  Forces the next invocation of get() to
 * invoke reconstitute().
 */
public synchronized void flush() {
    SoftReference s = soft;
    if (s != null) s.clear();
    soft = null;
}
 
Example 10
Source File: Ref.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Flushes the cached object.  Forces the next invocation of get() to
 * invoke reconstitute().
 */
public synchronized void flush() {
    SoftReference s = soft;
    if (s != null) s.clear();
    soft = null;
}
 
Example 11
Source File: Ref.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Flushes the cached object.  Forces the next invocation of get() to
 * invoke reconstitute().
 */
public synchronized void flush() {
    SoftReference s = soft;
    if (s != null) s.clear();
    soft = null;
}
 
Example 12
Source File: Ref.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Flushes the cached object.  Forces the next invocation of get() to
 * invoke reconstitute().
 */
public synchronized void flush() {
    SoftReference s = soft;
    if (s != null) s.clear();
    soft = null;
}
 
Example 13
Source File: Ref.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Flushes the cached object.  Forces the next invocation of get() to
 * invoke reconstitute().
 */
public synchronized void flush() {
    SoftReference s = soft;
    if (s != null) s.clear();
    soft = null;
}
 
Example 14
Source File: AppletImageRef.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Flushes the cached object.  Forces the next invocation of get() to
 * invoke reconstitute().
 */
public synchronized void flush() {
    SoftReference<Image> s = soft;
    if (s != null) s.clear();
    soft = null;
}
 
Example 15
Source File: Ref.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Flushes the cached object.  Forces the next invocation of get() to
 * invoke reconstitute().
 */
public synchronized void flush() {
    SoftReference s = soft;
    if (s != null) s.clear();
    soft = null;
}
 
Example 16
Source File: Ref.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Flushes the cached object.  Forces the next invocation of get() to
 * invoke reconstitute().
 */
public synchronized void flush() {
    SoftReference s = soft;
    if (s != null) s.clear();
    soft = null;
}
 
Example 17
Source File: Ref.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Flushes the cached object.  Forces the next invocation of get() to
 * invoke reconstitute().
 */
public synchronized void flush() {
    SoftReference s = soft;
    if (s != null) s.clear();
    soft = null;
}
 
Example 18
Source File: Ref.java    From java-n-IDE-for-Android with Apache License 2.0 4 votes vote down vote up
/**
 * Flushes the cached object.  Forces the next invocation of get() to
 * invoke reconstitute().
 */
public synchronized void flush() {
    SoftReference s = soft;
    if (s != null) s.clear();
    soft = null;
}
 
Example 19
Source File: Ref.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Flushes the cached object.  Forces the next invocation of get() to
 * invoke reconstitute().
 */
public synchronized void flush() {
    SoftReference s = soft;
    if (s != null) s.clear();
    soft = null;
}
 
Example 20
Source File: Ref.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Flushes the cached object.  Forces the next invocation of get() to
 * invoke reconstitute().
 */
public synchronized void flush() {
    SoftReference s = soft;
    if (s != null) s.clear();
    soft = null;
}