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

org.intermine.util.SoftReferenceMap Maven / Gradle / Ivy

package org.intermine.util;

/*
 * Copyright (C) 2002-2022 FlyMine
 *
 * This code may be freely distributed and modified under the
 * terms of the GNU Lesser General Public Licence.  This should
 * be distributed with the code.  See the LICENSE file for more
 * information or http://www.gnu.org/copyleft/lesser.html.
 *
 */

import java.lang.ref.ReferenceQueue;
import java.lang.ref.SoftReference;

/**
 * This is a Map implementation designed specifically for people intending to create a cache.
 * The keys are held strongly, but the values are held softly, so the values can be
 * garbage-collected. When an entry is garbage-collected, its key is removed from the Map on
 * the next Map activity. Subclasses of this map are required to provide the actual Map
 * functionality.
 * 

* The entrySet() and values() methods of this class do not work. * * @see java.lang.ref.SoftReference * @param the type of keys maintained by this map * @param the type of mapped values * @author Matthew Wakeling */ public abstract class SoftReferenceMap extends ReferenceMap { /** * Returns a new SoftReferenceWithKey object for the given objects. * * @param value an Object * @param queue a ReferenceQueue * @param key an Object * @return a SoftReferenceWithKey object */ @Override protected SoftReferenceWithKey newRef(Object value, ReferenceQueue queue, K key) { return new SoftReferenceWithKey(value, queue, key); } private static class SoftReferenceWithKey extends SoftReference implements ReferenceWithKey { private K key; SoftReferenceWithKey(Object value, ReferenceQueue queue, K key) { super(value, queue); this.key = key; } @Override public K getKey() { return key; } } }