001    /*--------------------------------------------------------------------------+
002    $Id: MemoryEfficientStringMap.java 29399 2010-07-27 15:03:17Z juergens $
003    |                                                                          |
004    | Copyright 2005-2010 Technische Universitaet Muenchen                     |
005    |                                                                          |
006    | Licensed under the Apache License, Version 2.0 (the "License");          |
007    | you may not use this file except in compliance with the License.         |
008    | You may obtain a copy of the License at                                  |
009    |                                                                          |
010    |    http://www.apache.org/licenses/LICENSE-2.0                            |
011    |                                                                          |
012    | Unless required by applicable law or agreed to in writing, software      |
013    | distributed under the License is distributed on an "AS IS" BASIS,        |
014    | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
015    | See the License for the specific language governing permissions and      |
016    | limitations under the License.                                           |
017    +--------------------------------------------------------------------------*/
018    package edu.tum.cs.commons.collections;
019    
020    import java.util.HashMap;
021    import java.util.Map;
022    
023    /**
024     * A map implementation of a map using string keys. This is based on a hybrid
025     * map which uses an {@link ArrayBackedStringMap} while only a small number of
026     * keys are present and switches to a {@link HashMap} after a certain size has
027     * been reached.
028     * 
029     * @author hummelb
030     * @author $Author: juergens $
031     * @version $Rev: 29399 $
032     * @levd.rating GREEN Hash: B82C7BCF35689181816346E57A25D854
033     */
034    public class MemoryEfficientStringMap<V> extends HybridMapBase<String, V> {
035    
036            /** The maximal size of the map before switching is performed. */
037            private static final int SWITCHING_SIZE = 16;
038    
039            /** Constructor. */
040            public MemoryEfficientStringMap() {
041                    super(new ArrayBackedStringMap<V>(4));
042            }
043    
044            /** Constructor. */
045            public MemoryEfficientStringMap(Map<? extends String, ? extends V> map) {
046                    this();
047                    putAll(map);
048            }
049    
050            /** {@inheritDoc} */
051            @Override
052            protected Map<String, V> obtainNewMap() {
053                    return new HashMap<String, V>();
054            }
055    
056            /** {@inheritDoc} */
057            @Override
058            protected boolean shouldSwitch(Map<String, V> map) {
059                    return map.size() == SWITCHING_SIZE
060                                    && map instanceof ArrayBackedMap<?, ?>;
061            }
062    }