Search in sources :

Example 51 with SoftReference

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

the class StandardGlyphVector method getGlyphLogicalBounds.

public Shape getGlyphLogicalBounds(int ix) {
    if (ix < 0 || ix >= glyphs.length) {
        throw new IndexOutOfBoundsException("ix = " + ix);
    }
    Shape[] lbcache;
    if (lbcacheRef == null || (lbcache = (Shape[]) lbcacheRef.get()) == null) {
        lbcache = new Shape[glyphs.length];
        lbcacheRef = new SoftReference(lbcache);
    }
    Shape result = lbcache[ix];
    if (result == null) {
        setFRCTX();
        initPositions();
        // !!! ought to return a rectangle2d for simple cases, though the following works for all
        // get the position, the tx offset, and the x,y advance and x,y adl.  The
        // shape is the box formed by adv (width) and adl (height) offset by
        // the position plus the tx offset minus the ascent.
        ADL adl = new ADL();
        GlyphStrike gs = getGlyphStrike(ix);
        gs.getADL(adl);
        Point2D.Float adv = gs.strike.getGlyphMetrics(glyphs[ix]);
        float wx = adv.x;
        float wy = adv.y;
        float hx = adl.descentX + adl.leadingX + adl.ascentX;
        float hy = adl.descentY + adl.leadingY + adl.ascentY;
        float x = positions[ix * 2] + gs.dx - adl.ascentX;
        float y = positions[ix * 2 + 1] + gs.dy - adl.ascentY;
        GeneralPath gp = new GeneralPath();
        gp.moveTo(x, y);
        gp.lineTo(x + wx, y + wy);
        gp.lineTo(x + wx + hx, y + wy + hy);
        gp.lineTo(x + hx, y + hy);
        gp.closePath();
        result = new DelegatingShape(gp);
        lbcache[ix] = result;
    }
    return result;
}
Also used : Shape(java.awt.Shape) SoftReference(java.lang.ref.SoftReference) GeneralPath(java.awt.geom.GeneralPath) Point2D(java.awt.geom.Point2D)

Example 52 with SoftReference

use of java.lang.ref.SoftReference in project bnd by bndtools.

the class PersistentMap method entrySet.

public Set<java.util.Map.Entry<String, V>> entrySet() {
    return new AbstractSet<Map.Entry<String, V>>() {

        public int size() {
            init();
            return cache.size();
        }

        public Iterator<java.util.Map.Entry<String, V>> iterator() {
            init();
            return new Iterator<Map.Entry<String, V>>() {

                Iterator<java.util.Map.Entry<String, SoftReference<V>>> it = cache.entrySet().iterator();

                java.util.Map.Entry<String, SoftReference<V>> entry;

                public boolean hasNext() {
                    return it.hasNext();
                }

                @SuppressWarnings("unchecked")
                public java.util.Map.Entry<String, V> next() {
                    try {
                        entry = it.next();
                        SoftReference<V> ref = entry.getValue();
                        V value = null;
                        if (ref != null)
                            value = ref.get();
                        if (value == null) {
                            File file = new File(data, entry.getKey());
                            value = (V) codec.dec().from(file).get(type);
                            entry.setValue(new SoftReference<V>(value));
                        }
                        final V v = value;
                        return new Map.Entry<String, V>() {

                            public String getKey() {
                                return entry.getKey();
                            }

                            public V getValue() {
                                return v;
                            }

                            public V setValue(V value) {
                                return put(entry.getKey(), value);
                            }
                        };
                    } catch (Exception e) {
                        e.printStackTrace();
                        throw new RuntimeException(e);
                    }
                }

                public void remove() {
                    PersistentMap.this.remove(entry.getKey());
                }
            };
        }
    };
}
Also used : AbstractSet(java.util.AbstractSet) OverlappingFileLockException(java.nio.channels.OverlappingFileLockException) IOException(java.io.IOException) SoftReference(java.lang.ref.SoftReference) Iterator(java.util.Iterator) HashMap(java.util.HashMap) AbstractMap(java.util.AbstractMap) Map(java.util.Map) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Example 53 with SoftReference

use of java.lang.ref.SoftReference in project sling by apache.

the class ScriptCacheImpl method onChange.

@Override
public void onChange(@Nonnull List<ResourceChange> list) {
    for (final ResourceChange change : list) {
        Runnable eventTask = new Runnable() {

            @Override
            public void run() {
                String path = change.getPath();
                writeLock.lock();
                try {
                    final boolean removed = internalMap.remove(path) != null;
                    LOGGER.debug("Detected script change for {} - removed entry from the cache.", path);
                    if (!removed && change.getType() == ChangeType.REMOVED) {
                        final String prefix = path + "/";
                        final Set<String> removal = new HashSet<>();
                        for (final Map.Entry<String, SoftReference<CachedScript>> entry : internalMap.entrySet()) {
                            if (entry.getKey().startsWith(prefix)) {
                                removal.add(entry.getKey());
                            }
                        }
                        for (final String key : removal) {
                            internalMap.remove(key);
                            LOGGER.debug("Detected removal for {} - removed entry {} from the cache.", path, key);
                        }
                    }
                } finally {
                    writeLock.unlock();
                }
            }
        };
        threadPool.execute(eventTask);
    }
}
Also used : SoftReference(java.lang.ref.SoftReference) ResourceChange(org.apache.sling.api.resource.observation.ResourceChange) CachingMap(org.apache.sling.scripting.core.impl.helper.CachingMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 54 with SoftReference

use of java.lang.ref.SoftReference in project android_frameworks_base by crdroidandroid.

the class HeapTest method xxtestSoftRefPartialClean.

public void xxtestSoftRefPartialClean() throws Exception {
    final int NUM_REFS = 128;
    Object[] objects = new Object[NUM_REFS];
    SoftReference<Object>[] refs = new SoftReference[objects.length];
    /* Create a bunch of objects and a parallel array
        * of SoftReferences.
        */
    makeRefs(objects, refs);
    Runtime.getRuntime().gc();
    /* Let go of the hard references to the objects so that
        * the references can be cleared.
        */
    clearRefs(objects);
    /* Start creating a bunch of temporary and permanent objects
        * to drive GC.
        */
    final int NUM_OBJECTS = 64 * 1024;
    Object[] junk = new Object[NUM_OBJECTS];
    Random random = new Random();
    int i = 0;
    int mod = 0;
    int totalSize = 0;
    int cleared = -1;
    while (i < junk.length && totalSize < 8 * 1024 * 1024) {
        int r = random.nextInt(64 * 1024) + 128;
        Object o = (Object) new byte[r];
        if (++mod % 16 == 0) {
            junk[i++] = o;
            totalSize += r * 4;
        }
        cleared = checkRefs(refs, cleared);
    }
}
Also used : SoftReference(java.lang.ref.SoftReference) Random(java.util.Random)

Example 55 with SoftReference

use of java.lang.ref.SoftReference in project Lucee by lucee.

the class SoftHashMap method get.

@Override
public Object get(Object key) {
    Object result = null;
    // We get the SoftReference represented by that key
    SoftReference soft_ref = (SoftReference) hash.get(key);
    if (soft_ref != null) {
        result = soft_ref.get();
        if (result == null) {
            hash.remove(key);
        } else {
            hardCache.addFirst(result);
            // Remove the last entry if list longer than HARD_SIZE
            if (HARD_SIZE > 0 && hardCache.size() > HARD_SIZE) {
                hardCache.removeLast();
            }
        }
    }
    return result;
}
Also used : 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