Example usage for java.lang.ref SoftReference SoftReference

List of usage examples for java.lang.ref SoftReference SoftReference

Introduction

In this page you can find the example usage for java.lang.ref SoftReference SoftReference.

Prototype

public SoftReference(T referent) 

Source Link

Document

Creates a new soft reference that refers to the given object.

Usage

From source file:com.twapime.app.util.AsyncImageLoader.java

/**
 * @param context/*from   www  .j  a  v a 2  s .  co  m*/
 */
private AsyncImageLoader(Context context) {
    this.context = context;
    //
    urlsBeingDownloaded = new Vector<String>();
    callbacks = new HashMap<String, List<ImageLoaderCallback>>();
    hardCache = new LinkedHashMap<String, Drawable>(HARD_CACHE_CAPACITY, 0.75f, true) {
        private static final long serialVersionUID = -4795168423098442895L;

        @Override
        protected boolean removeEldestEntry(LinkedHashMap.Entry<String, Drawable> eldest) {
            if (size() > HARD_CACHE_CAPACITY) {
                softCache.put(eldest.getKey(), new SoftReference<Drawable>(eldest.getValue()));
                //
                return true;
            } else
                return false;
        }
    };
    softCache = new ConcurrentHashMap<String, SoftReference<Drawable>>(HARD_CACHE_CAPACITY / 2);
    //
    loadCacheDir();
}

From source file:com.irurueta.server.commons.email.ApacheMailSender.java

/**
 * Returns or creates singleton instance of this class.
 * @return singleton of this class./* www.  ja  v  a 2s .c  o m*/
 */
public static synchronized ApacheMailSender getInstance() {
    ApacheMailSender sender;
    if (mReference == null || (sender = mReference.get()) == null) {
        sender = new ApacheMailSender();
        mReference = new SoftReference<>(sender);
    }
    return sender;
}

From source file:org.diorite.impl.world.io.ChunkRegionCache.java

public ChunkRegion getChunkRegion(final int regionX, final int regionZ) {
    final long key = BigEndianUtils.toLong(regionX, regionZ);
    final File file = new File(this.regionDir, "r." + regionX + "." + regionZ + this.extension);

    final Reference<ChunkRegion> ref = this.cache.get(key);

    if ((ref != null) && (ref.get() != null)) {
        return ref.get();
    }/*w w  w. j a v  a2 s . co m*/

    if (!this.regionDir.isDirectory() && !this.regionDir.mkdirs()) {
        System.err.println("[WorldIO] Failed to create directory: " + this.regionDir);
    }

    if (this.cache.size() >= this.maxCacheSize) {
        this.clear();
    }

    final ChunkRegion reg = this.createNewRegion(file, regionX, regionZ);
    synchronized (this.cache) {
        this.cache.put(key, new SoftReference<>(reg));
    }
    return reg;
}

From source file:com.taobao.common.tedis.cache.DefaultLocalCache.java

public V put(K key, V value) {
    SoftReference<V> result = getCache(key).put(key, new SoftReference<V>(value));
    expiryCache.put(key, -1L);/*from  ww  w. j  a v a  2s  .c  o m*/

    return result == null ? null : result.get();
}

From source file:com.slytechs.capture.file.indexer.SoftTable.java

/**
 * @param i//from   w  w  w  . ja  v a  2s  .  co m
 * @param index
 * @throws IOException
 */
private long[] fetchIndexes() throws IOException {
    if (this.positions.get() != null) {
        return positions.get(); // Nothing to do
    }

    final long[] positions = new long[length];
    // Arrays.fill(positions, -1);

    final BasicRecordIterator iterator = new BasicRecordIterator(loader, loader.getLengthGetter());

    iterator.setPosition(first);

    for (int i = 0; i < length; i++) {
        positions[i] = iterator.getPosition();

        if (iterator.hasNext() == false) {
            throw new IllegalStateException("Not enough records in loader, expected upto " + length);
        }

        iterator.skip();
    }

    this.positions = new SoftReference<long[]>(positions);

    //      logger.info("fetched: [" + positions[0] + "-" + (positions[length - 1])
    //          + "]");

    return positions;
}

From source file:org.jboss.windup.metadata.type.XmlMetadata.java

public void setParsedDocument(Document parsedDocument) {
    this.parsedDocumentRef = new SoftReference<Document>(parsedDocument);
}

From source file:com.abelsky.idea.geekandpoke.entries.Entry.java

@Nullable
public BufferedImage getImage() {
    if (imageRef == null) {
        log.assertTrue(isDeleted());/*from  w ww  .  j  ava 2  s  .  c o m*/
        return null;
    }

    log.assertTrue(!isDeleted());

    @Nullable
    BufferedImage image = imageRef.get();
    if (image == null) {
        imageRef = new SoftReference<BufferedImage>(image = fetchCachedImage());
    }
    return image;
}

From source file:com.googlecode.fightinglayoutbugs.ScreenshotCache.java

public @Nonnull Screenshot getScreenshot(Condition condition) {
    SoftReference<Screenshot> softReference = _memoryCache.get(condition);
    Screenshot screenshot;//www. ja v  a  2 s.  c o  m
    if (softReference == null) {
        // Cache miss, take screenshot ...
        screenshot = takeScreenshot(condition);
        // ... and cache it ...
        _memoryCache.put(condition, new SoftReference<Screenshot>(screenshot));
        _diskCache.put(condition, saveToTempFile(screenshot));
    } else {
        screenshot = softReference.get();
        if (screenshot == null) {
            // screenshot in _memoryCache was garbage collected, read it from _diskCache ...
            File file = _diskCache.get(condition);
            screenshot = readFromFile(file);
            // ... and put it into _memoryCache again ...
            _memoryCache.put(condition, new SoftReference<Screenshot>(screenshot));
        }
    }
    return screenshot;
}

From source file:com.guang.eunormia.common.cache.DefaultLocalCache.java

@Override
public V put(K key, V value) {
    SoftReference<V> result = getCache(key).put(key, new SoftReference<V>(value));
    expiryCache.put(key, -1L);//from w  w  w  .ja  v  a 2 s .c o m

    return result == null ? null : result.get();
}

From source file:org.videolan.vlc.util.HttpImageLoader.java

@Nullable
public static Bitmap downloadBitmap(String imageUrl) {
    if (iconsMap.containsKey(imageUrl)) {
        Bitmap bd = iconsMap.get(imageUrl).get();
        if (bd != null) {
            return bd;
        } else/*from w  w w  .ja  va 2s. c o  m*/
            iconsMap.remove(imageUrl);
    }
    HttpURLConnection urlConnection = null;
    Bitmap icon = null;
    try {
        URL url = new URL(imageUrl);
        if (url.getPort() <= 0)
            return null;
        urlConnection = (HttpURLConnection) url.openConnection();
        InputStream in = new BufferedInputStream(urlConnection.getInputStream());
        icon = BitmapFactory.decodeStream(in);
        iconsMap.put(imageUrl, new SoftReference<>(icon));
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (urlConnection != null)
            urlConnection.disconnect();
    }
    return icon;
}