
de.vandermeer.skb.collections.MapStrategy Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of skb-collections Show documentation
Show all versions of skb-collections Show documentation
Strategies for Java collections
The newest version!
/* Copyright 2014 Sven van der Meer
*
* 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 de.vandermeer.skb.collections;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.IdentityHashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.TreeMap;
import java.util.WeakHashMap;
import java.util.concurrent.ConcurrentHashMap;
/**
* Strategies for maps.
*
* @author Sven van der Meer <[email protected]>
* @version v0.0.4 build 150619 (19-Jun-15) for Java 1.8
*/
public enum MapStrategy implements IsMapStrategy {
/** Default map, results in an HashMap */
DEFAULT,
/** HashMap from java-util. */
HASH_MAP,
/** Hashtable from java-util. */
HASH_TABLE,
/** LinkedHashMap from java-util. */
LINKED_HASH_MAP,
/** IdentityHashMap from java-util. */
IDENTITY_HASH_MAP,
/** ConcurrentHashMap from java-util. */
CONCURRENT_HASH_MAP,
/** WeakHashMap from java-util. */
WEAK_HASH_MAP,
/** TreeMap from java-util. */
TREE_MAP,
;
/**
* Returns a new map for the used strategy (enum) for the given class T as map <String, T>.
* @param class to be used for initialising the map
* @return new map for the given class
*/
@Override
public Map get(Class> T) {
switch(this){
case LINKED_HASH_MAP:
return new LinkedHashMap();
case TREE_MAP:
return new TreeMap();
case HASH_TABLE:
return new Hashtable();
case IDENTITY_HASH_MAP:
return new IdentityHashMap();
case WEAK_HASH_MAP:
return new WeakHashMap();
case CONCURRENT_HASH_MAP:
return new ConcurrentHashMap();
case HASH_MAP:
case DEFAULT:
default:
return new HashMap();
}
}
/**
* Returns a new map view for the given map of type <String, T>.
* @param type for the map values
* @param map input map
* @return new map view
*/
@Override
public Map get(Map map){
Map ret;
switch(this){
case LINKED_HASH_MAP:
ret = new LinkedHashMap(map);
break;
case TREE_MAP:
ret = new TreeMap(map);
break;
case HASH_TABLE:
ret = new Hashtable(map);
break;
case IDENTITY_HASH_MAP:
ret = new IdentityHashMap(map);
break;
case WEAK_HASH_MAP:
ret = new WeakHashMap(map);
break;
case CONCURRENT_HASH_MAP:
ret = new ConcurrentHashMap(map);
break;
case HASH_MAP:
case DEFAULT:
default:
ret = new HashMap(map);
break;
}
return ret;
}
@Override
public String toString() {
return this.name();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy