001    /*
002     * Copyright (C) 2008-2010 by Holger Arndt
003     *
004     * This file is part of the Universal Java Matrix Package (UJMP).
005     * See the NOTICE file distributed with this work for additional
006     * information regarding copyright ownership and licensing.
007     *
008     * UJMP is free software; you can redistribute it and/or modify
009     * it under the terms of the GNU Lesser General Public License as
010     * published by the Free Software Foundation; either version 2
011     * of the License, or (at your option) any later version.
012     *
013     * UJMP is distributed in the hope that it will be useful,
014     * but WITHOUT ANY WARRANTY; without even the implied warranty of
015     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016     * GNU Lesser General Public License for more details.
017     *
018     * You should have received a copy of the GNU Lesser General Public
019     * License along with UJMP; if not, write to the
020     * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
021     * Boston, MA  02110-1301  USA
022     */
023    
024    package org.ujmp.core.collections;
025    
026    import java.lang.ref.SoftReference;
027    import java.util.HashMap;
028    import java.util.Map;
029    import java.util.Set;
030    
031    public class SoftHashMap<K, V> extends AbstractMap<K, V> {
032            private static final long serialVersionUID = 6784629567536455622L;
033    
034            private transient Map<K, SoftReference<V>> map = null;
035    
036            public SoftHashMap() {
037            }
038    
039            public SoftHashMap(Map<? extends K, ? extends V> map) {
040                    this();
041                    putAll(map);
042            }
043    
044            private Map<K, SoftReference<V>> getMap() {
045                    if (map == null) {
046                            map = new HashMap<K, SoftReference<V>>();
047                    }
048                    return map;
049            }
050    
051            public void clear() {
052                    getMap().clear();
053            }
054    
055            public V get(Object key) {
056                    SoftReference<V> v = getMap().get(key);
057                    return v == null ? null : v.get();
058            }
059    
060            public Set<K> keySet() {
061                    return getMap().keySet();
062            }
063    
064            public V put(K key, V value) {
065                    do {
066                            try {
067                                    SoftReference<V> v = getMap().put(key, new SoftReference<V>(value));
068                                    return v == null ? null : v.get();
069                            } catch (OutOfMemoryError e) {
070                                    getMap().remove(getMap().keySet().iterator().next());
071                            }
072                    } while (!getMap().isEmpty());
073                    throw new OutOfMemoryError("removing all entries from Map could not avoid OutOfMemoryError");
074            }
075    
076            public V remove(Object key) {
077                    SoftReference<V> v = getMap().remove(key);
078                    return v == null ? null : v.get();
079            }
080    
081            public int size() {
082                    return getMap().size();
083            }
084    
085    }