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;
}
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());
}
};
}
};
}
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);
}
}
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);
}
}
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;
}
Aggregations