Search in sources :

Example 21 with SoftReference

use of java.lang.ref.SoftReference in project jdk8u_jdk by JetBrains.

the class Ref method setThing.

/**
     * Sets the thing to the specified object.
     * @param thing the specified object
     */
public synchronized void setThing(Object thing) {
    flush();
    soft = new SoftReference(thing);
}
Also used : SoftReference(java.lang.ref.SoftReference)

Example 22 with SoftReference

use of java.lang.ref.SoftReference in project jdk8u_jdk by JetBrains.

the class Ref method flush.

/**
     * 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;
}
Also used : SoftReference(java.lang.ref.SoftReference)

Example 23 with SoftReference

use of java.lang.ref.SoftReference in project jdk8u_jdk by JetBrains.

the class Font2D method getStrike.

FontStrike getStrike(FontStrikeDesc desc, boolean copy) {
    /* Before looking in the map, see if the descriptor matches the
         * last strike returned from this Font2D. This should often be a win
         * since its common for the same font, in the same size to be
         * used frequently, for example in many parts of a UI.
         *
         * If its not the same then we use the descriptor to locate a
         * Reference to the strike. If it exists and points to a strike,
         * then we update the last strike to refer to that and return it.
         *
         * If the key isn't in the map, or its reference object has been
         * collected, then we create a new strike, put it in the map and
         * set it to be the last strike.
         */
    FontStrike strike = (FontStrike) lastFontStrike.get();
    if (strike != null && desc.equals(strike.desc)) {
        //strike.lastlookupTime = System.currentTimeMillis();
        return strike;
    } else {
        Reference strikeRef = strikeCache.get(desc);
        if (strikeRef != null) {
            strike = (FontStrike) strikeRef.get();
            if (strike != null) {
                //strike.lastlookupTime = System.currentTimeMillis();
                lastFontStrike = new SoftReference(strike);
                StrikeCache.refStrike(strike);
                return strike;
            }
        }
        /* When we create a new FontStrike instance, we *must*
             * ask the StrikeCache for a reference. We must then ensure
             * this reference remains reachable, by storing it in the
             * Font2D's strikeCache map.
             * So long as the Reference is there (reachable) then if the
             * reference is cleared, it will be enqueued for disposal.
             * If for some reason we explicitly remove this reference, it
             * must only be done when holding a strong reference to the
             * referent (the FontStrike), or if the reference is cleared,
             * then we must explicitly "dispose" of the native resources.
             * The only place this currently happens is in this same method,
             * where we find a cleared reference and need to overwrite it
             * here with a new reference.
             * Clearing the whilst holding a strong reference, should only
             * be done if the
             */
        if (copy) {
            desc = new FontStrikeDesc(desc);
        }
        strike = createStrike(desc);
        //StrikeCache.addStrike();
        /* If we are creating many strikes on this font which
             * involve non-quadrant rotations, or more general
             * transforms which include shears, then force the use
             * of weak references rather than soft references.
             * This means that it won't live much beyond the next GC,
             * which is what we want for what is likely a transient strike.
             */
        int txType = desc.glyphTx.getType();
        if (txType == AffineTransform.TYPE_GENERAL_TRANSFORM || (txType & AffineTransform.TYPE_GENERAL_ROTATION) != 0 && strikeCache.size() > 10) {
            strikeRef = StrikeCache.getStrikeRef(strike, true);
        } else {
            strikeRef = StrikeCache.getStrikeRef(strike);
        }
        strikeCache.put(desc, strikeRef);
        //strike.lastlookupTime = System.currentTimeMillis();
        lastFontStrike = new SoftReference(strike);
        StrikeCache.refStrike(strike);
        return strike;
    }
}
Also used : SoftReference(java.lang.ref.SoftReference) Reference(java.lang.ref.Reference) SoftReference(java.lang.ref.SoftReference)

Example 24 with SoftReference

use of java.lang.ref.SoftReference in project jdk8u_jdk by JetBrains.

the class FontUtilities method getCompositeFontUIResource.

public static FontUIResource getCompositeFontUIResource(Font font) {
    FontUIResource fuir = new FontUIResource(font);
    Font2D font2D = FontUtilities.getFont2D(font);
    if (!(font2D instanceof PhysicalFont)) {
        /* Swing should only be calling this when a font is obtained
             * from desktop properties, so should generally be a physical font,
             * an exception might be for names like "MS Serif" which are
             * automatically mapped to "Serif", so there's no need to do
             * anything special in that case. But note that suggested usage
             * is first to call fontSupportsDefaultEncoding(Font) and this
             * method should not be called if that were to return true.
             */
        return fuir;
    }
    FontManager fm = FontManagerFactory.getInstance();
    Font2D dialog = fm.findFont2D("dialog", font.getStyle(), FontManager.NO_FALLBACK);
    // Should never be null, but MACOSX fonts are not CompositeFonts
    if (dialog == null || !(dialog instanceof CompositeFont)) {
        return fuir;
    }
    CompositeFont dialog2D = (CompositeFont) dialog;
    PhysicalFont physicalFont = (PhysicalFont) font2D;
    ConcurrentHashMap<PhysicalFont, CompositeFont> compMap = compMapRef.get();
    if (compMap == null) {
        // Its been collected.
        compMap = new ConcurrentHashMap<PhysicalFont, CompositeFont>();
        compMapRef = new SoftReference(compMap);
    }
    CompositeFont compFont = compMap.get(physicalFont);
    if (compFont == null) {
        compFont = new CompositeFont(physicalFont, dialog2D);
        compMap.put(physicalFont, compFont);
    }
    FontAccess.getFontAccess().setFont2D(fuir, compFont.handle);
    /* marking this as a created font is needed as only created fonts
         * copy their creator's handles.
         */
    FontAccess.getFontAccess().setCreatedFont(fuir);
    return fuir;
}
Also used : SoftReference(java.lang.ref.SoftReference) FontUIResource(javax.swing.plaf.FontUIResource)

Example 25 with SoftReference

use of java.lang.ref.SoftReference in project jdk8u_jdk by JetBrains.

the class StandardGlyphVector method getGlyphVisualBounds.

public Shape getGlyphVisualBounds(int ix) {
    if (ix < 0 || ix >= glyphs.length) {
        throw new IndexOutOfBoundsException("ix = " + ix);
    }
    Shape[] vbcache;
    if (vbcacheRef == null || (vbcache = (Shape[]) vbcacheRef.get()) == null) {
        vbcache = new Shape[glyphs.length];
        vbcacheRef = new SoftReference(vbcache);
    }
    Shape result = vbcache[ix];
    if (result == null) {
        result = new DelegatingShape(getGlyphOutlineBounds(ix));
        vbcache[ix] = result;
    }
    return result;
}
Also used : Shape(java.awt.Shape) SoftReference(java.lang.ref.SoftReference)

Aggregations

SoftReference (java.lang.ref.SoftReference)56 ReferenceQueue (java.lang.ref.ReferenceQueue)7 Reference (java.lang.ref.Reference)6 WeakReference (java.lang.ref.WeakReference)6 PhantomReference (java.lang.ref.PhantomReference)5 Random (java.util.Random)5 IOException (java.io.IOException)4 Bitmap (android.graphics.Bitmap)3 Context (android.content.Context)2 BitmapFactory (android.graphics.BitmapFactory)2 Handler (android.os.Handler)2 Message (android.os.Message)2 MetaClass (groovy.lang.MetaClass)2 Shape (java.awt.Shape)2 File (java.io.File)2 InputStream (java.io.InputStream)2 UndeclaredThrowableException (java.lang.reflect.UndeclaredThrowableException)2 PrivilegedAction (java.security.PrivilegedAction)2 PrivilegedActionException (java.security.PrivilegedActionException)2 SecureClassLoader (java.security.SecureClassLoader)2