001/*
002// This software is subject to the terms of the Eclipse Public License v1.0
003// Agreement, available at the following URL:
004// http://www.eclipse.org/legal/epl-v10.html.
005// You must accept the terms of that agreement to use this software.
006//
007// Copyright (C) 2004-2005 TONBELLER AG
008// Copyright (C) 2006-2013 Pentaho
009// All Rights Reserved.
010*/
011package mondrian.rolap.cache;
012
013import org.apache.commons.collections.map.ReferenceMap;
014
015import java.util.*;
016
017/**
018 * An implementation of {@link SmartCacheImpl} which uses a
019 * {@link ReferenceMap} as a backing object. Both the key
020 * and the value are soft references, because of their
021 * cyclic nature.
022 *
023 * <p>This class does not enforce any synchronization, because
024 * this is handled by {@link SmartCacheImpl}.
025 *
026 * @author av, lboudreau
027 * @since Nov 3, 2005
028 */
029public class SoftSmartCache<K, V> extends SmartCacheImpl<K, V> {
030
031    @SuppressWarnings("unchecked")
032    private final Map<K, V> cache =
033        new ReferenceMap(ReferenceMap.SOFT, ReferenceMap.SOFT);
034
035    public V putImpl(K key, V value) {
036        // Null values are the same as a 'remove'
037        // Convert the operation because ReferenceMap doesn't
038        // like null values.
039        if (value == null) {
040            return cache.remove(key);
041        } else {
042            return cache.put(key, value);
043        }
044    }
045
046    public V getImpl(K key) {
047        return cache.get(key);
048    }
049
050    public V removeImpl(K key) {
051        return cache.remove(key);
052    }
053
054    public void clearImpl() {
055        cache.clear();
056    }
057
058    public int sizeImpl() {
059        return cache.size();
060    }
061
062    public Iterator<Map.Entry<K, V>> iteratorImpl() {
063        return cache.entrySet().iterator();
064    }
065}
066
067// End SoftSmartCache.java
068