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