All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.onetwo.common.utils.map.BaseMap Maven / Gradle / Ivy

There is a newer version: 4.7.2
Show newest version
package org.onetwo.common.utils.map;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.onetwo.common.convert.Types;
import org.onetwo.common.utils.CUtils;

/****
 * 提供类型转换的get方法
 * @author way
 *
 * @param 
 * @param 
 */
public class BaseMap implements Map{
	
	final private Map delegateMap;
	
	public BaseMap(){
		this.delegateMap = new HashMap<>();
	}
	
	public BaseMap(int initialCapacity) {
		this.delegateMap = new HashMap<>(initialCapacity);
	}
	
	public BaseMap(Map map){
		delegateMap = map;
	}

	public Object get(Object key, Object def){
		Object val = get(key);
		if(val==null)
			return def;
		return val;
	}

	public K getKey(V val){
		for(Map.Entry entry : this.entrySet()){
			if(val==null && val==entry.getValue())
				return entry.getKey();
			else if(val!=null && val.equals(entry.getValue()))
				return entry.getKey();
		}
		return null;
	}
	
	public String getString(Object key){
		return getString(key, null);
	}
	
	public String getString(Object key, String def){
		Object val = get(key, def);
		return convert(val, String.class, def);
	}
	
	public Boolean getBoolean(Object key){
		Object val = get(key);
		return convert(val, Boolean.class, false);
	}
	
	public Boolean getBoolean(Object key, Boolean def){
		Object val = get(key, def);
		return convert(val, Boolean.class, def);
	}
	
	public Long getLong(String key){
		return this.getLong(key, null);
	}
	
	public Long getLong(String key, Long def){
		Object val = get(key);
		return convert(val, Long.class, def);
	}
	
	public Double getDouble(String key, Double def){
		Object val = get(key);
		return convert(val, Double.class, def);
	}
	
	public Float getFloat(String key, Float def){
		Object val = get(key);
		return convert(val, Float.class, def);
	}
	
	protected  T convert(Object val, Class toType, T def){
//		return MyUtils.simpleConvert(val, toType, def);
		return Types.convertValue(val, toType, def);
	}
	
	public Integer getInteger(String key){
		return getInteger(key, null);
	}
	
	public Integer getInteger(String key, Integer def){
		Object val = get(key);
		return convert(val, Integer.class, def);
	}
	
	public Object[] toArray(){
		return CUtils.toList(this).toArray();
	}

	public int size() {
		return delegateMap.size();
	}

	public boolean isEmpty() {
		return delegateMap.isEmpty();
	}

	public boolean containsKey(Object key) {
		return delegateMap.containsKey(key);
	}

	public boolean containsValue(Object value) {
		return delegateMap.containsValue(value);
	}

	public V get(Object key) {
		return delegateMap.get(key);
	}

	public V put(K key, V value) {
		return delegateMap.put(key, value);
	}

	public V remove(Object key) {
		return delegateMap.remove(key);
	}

	public void putAll(Map m) {
		delegateMap.putAll(m);
	}

	public void clear() {
		delegateMap.clear();
	}

	public Set keySet() {
		return delegateMap.keySet();
	}

	public Collection values() {
		return delegateMap.values();
	}

	public Set> entrySet() {
		return delegateMap.entrySet();
	}

	public boolean equals(Object o) {
		return delegateMap.equals(o);
	}

	public int hashCode() {
		return delegateMap.hashCode();
	}
	
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy