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

com.googlecode.jpattern.org.cojen.util.WeakValuedHashMap Maven / Gradle / Ivy

Go to download

This is a copy of the good Cojen project from http://cojen.sourceforge.net/ with package name changed

The newest version!
/*
 *  Copyright 2006-2010 Brian S O'Neill
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package com.googlecode.jpattern.org.cojen.util;

import java.lang.ref.Reference;
import java.lang.ref.WeakReference;
import java.util.Map;

/**
 * A Map that weakly references its values and can be used as a simple cache.
 * WeakValuedHashMap is not thread-safe and must be wrapped with
 * Collections.synchronizedMap to be made thread-safe.
 * 

* Note: Weakly referenced entries may be automatically removed during * either accessor or mutator operations, possibly causing a concurrent * modification to be detected. Therefore, even if multiple threads are only * accessing this map, be sure to synchronize this map first. Also, do not * rely on the value returned by size() when using an iterator from this map. * The iterators may return less entries than the amount reported by size(). * * @author Brian S O'Neill * @since 2.1 */ public class WeakValuedHashMap extends ReferencedValueHashMap { /** * Constructs a new, empty map with the specified initial * capacity and the specified load factor. * * @param initialCapacity the initial capacity of the HashMap. * @param loadFactor the load factor of the HashMap * @throws IllegalArgumentException if the initial capacity is less * than zero, or if the load factor is nonpositive. */ public WeakValuedHashMap(int initialCapacity, float loadFactor) { super(initialCapacity, loadFactor); } /** * Constructs a new, empty map with the specified initial capacity * and default load factor, which is 0.75. * * @param initialCapacity the initial capacity of the HashMap. * @throws IllegalArgumentException if the initial capacity is less * than zero. */ public WeakValuedHashMap(int initialCapacity) { super(initialCapacity); } /** * Constructs a new, empty map with a default capacity and load * factor, which is 0.75. */ public WeakValuedHashMap() { super(); } /** * Constructs a new map with the same mappings as the given map. The * map is created with a capacity of twice the number of mappings in * the given map or 11 (whichever is greater), and a default load factor, * which is 0.75. */ public WeakValuedHashMap(Map t) { super(t); } Entry newEntry(int hash, K key, V value, Entry next) { return new WeakEntry(hash, key, value, next); } static class WeakEntry extends ReferencedValueHashMap.Entry { WeakEntry(int hash, K key, V value, Entry next) { super(hash, key, value, next); } WeakEntry(int hash, K key, Reference value, Entry next) { super(hash, key, value, next); } Entry newEntry(int hash, K key, Reference value, Entry next) { return new WeakEntry(hash, key, value, next); } Reference newReference(V value) { return new WeakReference(value); } } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy