All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.mygreen.supercsv.expression.ObjectCache Maven / Gradle / Ivy

Go to download

CSVのJavaライブラリであるSuperCSVに、アノテーション機能を追加したライブラリです。

There is a newer version: 2.3
Show newest version
package com.github.mygreen.supercsv.expression;

import java.lang.ref.SoftReference;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;


public class ObjectCache {
    
    private final Map> map = new HashMap>();
    
    private final LinkedList objectsLastAccessed = new LinkedList();
    private final int objectsToKeepCount;
    
    /**
     * Creates a new cache keeping all objects.
     */
    public ObjectCache() {
        this.objectsToKeepCount = -1;
    }
    
    /**
     * @param maxObjectsToKeep the number of cached objects that should stay in memory when GC 
     * starts removing SoftReferences to free memory 
     */
    public ObjectCache(final int maxObjectsToKeep) {
        this.objectsToKeepCount = maxObjectsToKeep;
    }
    
    public void compact() {
        for (final Map.Entry> entry : map.entrySet()) {
            final SoftReference ref = entry.getValue();
            if (ref.get() == null) map.remove(entry.getKey());
        }
    }
    
    public boolean contains(final K key) {
        return map.containsKey(key);
    }
    
    public V get(final K key) {
        final SoftReference softReference = map.get(key);
        if (softReference != null) {
            final V value = softReference.get();
            if (value == null) {
                map.remove(key);
            } else if (objectsToKeepCount > 0 && value != objectsLastAccessed.getFirst()) {
                objectsLastAccessed.remove(value);
                objectsLastAccessed.addFirst(value);
                if (objectsLastAccessed.size() > objectsToKeepCount) objectsLastAccessed.removeLast();
            }
            return softReference.get();
        }
        return null;
    }
    
    public void put(final K key, final V value) {
        map.remove(key);
        map.put(key, new SoftReference(value));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy