All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.github.lontime.extcommons.lang3.StringObjectMap Maven / Gradle / Ivy
package com.github.lontime.extcommons.lang3;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.BiFunction;
import java.util.function.Function;
/**
* StringObjectMap.
* @author lontime
* @since 1.0
*/
public class StringObjectMap {
private static final int DEFAULT_SIZE = 8;
private final Map map;
//-------------static---------------------
/**
* create.
* @return StringObjectMap
*/
public static StringObjectMap create() {
return create(DEFAULT_SIZE);
}
public static StringObjectMap create(int initialCapacity) {
return new StringObjectMap(initialCapacity, false);
}
public static StringObjectMap create(Map objectMap) {
return new StringObjectMap(new HashMap<>(objectMap));
}
public static StringObjectMap createSorted() {
return createSorted(Collections.emptyMap());
}
public static StringObjectMap createSorted(Comparator comparator) {
return createSorted(Collections.emptyMap(), comparator);
}
public static StringObjectMap createSorted(Map objectMap) {
return createSorted(objectMap, null);
}
public static StringObjectMap createSorted(Map objectMap, Comparator comparator) {
final TreeMap treeMap = new TreeMap<>(comparator);
treeMap.putAll(objectMap);
return new StringObjectMap(treeMap);
}
public static StringObjectMap createSafe() {
return createSafe(DEFAULT_SIZE);
}
public static StringObjectMap createSafe(int initialCapacity) {
return new StringObjectMap(initialCapacity, true);
}
public static StringObjectMap createSafe(Map objectMap) {
return new StringObjectMap(new ConcurrentHashMap<>(objectMap));
}
//-------------static---------------------
public StringObjectMap(int initialCapacity, boolean threadSafe) {
this.map = threadSafe ? new ConcurrentHashMap<>(initialCapacity)
: new HashMap<>(initialCapacity);
}
public StringObjectMap() {
this(DEFAULT_SIZE, false);
}
public StringObjectMap(boolean threadSafe) {
this(DEFAULT_SIZE, threadSafe);
}
public StringObjectMap(Map objectMap) {
this.map = objectMap;
}
public void put(String key, Object value) {
map.put(key, value);
}
public Object get(String key) {
return map.get(key);
}
public boolean isEmpty() {
return map.isEmpty();
}
public boolean containsKey(String key) {
return map.containsKey(key);
}
public Set keySet() {
return map.keySet();
}
public Object getOrDefault(String key, Object defaultValue) {
return map.getOrDefault(key, defaultValue);
}
public Object computeIfAbsent(String key, Function mappingFunction) {
return map.computeIfAbsent(key, mappingFunction);
}
public Object computeIfPresent(String key, BiFunction remappingFunction) {
return map.computeIfPresent(key, remappingFunction);
}
public Object putIfAbsent(String key, Object value) {
return map.putIfAbsent(key, value);
}
public void putAll(Map m) {
map.putAll(m);
}
public Object remove(String key) {
return map.remove(key);
}
public boolean remove(String key, Object value) {
return map.remove(key, value);
}
public Object replace(String key, Object value) {
return map.replace(key, value);
}
public boolean replace(String key, Object oldValue, Object newValue) {
return map.replace(key, oldValue, newValue);
}
public Map toMap() {
return Collections.unmodifiableMap(map);
}
@Override
public String toString() {
return map.toString();
}
@Override
public int hashCode() {
return map.hashCode();
}
@Override
public boolean equals(Object o) {
if (!(o instanceof StringObjectMap)) {
return false;
}
StringObjectMap m = (StringObjectMap) o;
return map.equals(m.toMap());
}
/////////////////////extend//////////////////
public T getAsClass(String key) {
return (T) map.get(key);
}
public boolean getAsBoolean(String key) {
final Object value = map.get(key);
if (isBoolean(value)) {
return ((Boolean) value).booleanValue();
}
// Check to see if the value as a String is "true" in any case.
return Boolean.parseBoolean(getAsString(key));
}
public String getAsString(String key) {
final Object value = map.get(key);
if (isNumber(value)) {
return getAsNumber(key).toString();
} else if (isBoolean(value)) {
return ((Boolean) value).toString();
} else {
return (String) value;
}
}
public Number getAsNumber(String key) {
final Object value = map.get(key);
return value instanceof String ? new LazilyParsedNumber((String) value) : (Number) value;
}
public BigDecimal getAsBigDecimal(String key) {
final Object value = map.get(key);
return value instanceof BigDecimal ? (BigDecimal) value : new BigDecimal(value.toString());
}
public BigInteger getAsBigInteger(String key) {
final Object value = map.get(key);
return value instanceof BigInteger ?
(BigInteger) value : new BigInteger(value.toString());
}
public int getAsInt(String key) {
final Object value = map.get(key);
return isNumber(value) ? getAsNumber(key).intValue() : Integer.parseInt(getAsString(key));
}
public byte getAsByte(String key) {
final Object value = map.get(key);
return isNumber(value) ? getAsNumber(key).byteValue() : Byte.parseByte(getAsString(key));
}
public short getAsShort(String key) {
final Object value = map.get(key);
return isNumber(value) ? getAsNumber(key).shortValue() : Short.parseShort(getAsString(key));
}
public long getAsLong(String key) {
final Object value = map.get(key);
return isNumber(value) ? getAsNumber(key).longValue() : Long.parseLong(getAsString(key));
}
public float getAsFloat(String key) {
final Object value = map.get(key);
return isNumber(value) ? getAsNumber(key).floatValue() : Float.parseFloat(getAsString(key));
}
public double getAsDouble(String key) {
final Object value = map.get(key);
return isNumber(value) ? getAsNumber(key).doubleValue() : Double.parseDouble(getAsString(key));
}
public char getAsCharacter(String key) {
return getAsString(key).charAt(0);
}
/**
* Check whether this primitive contains a Number.
* @param value value
* @return true if this primitive contains a Number, false otherwise.
*/
public boolean isNumber(Object value) {
return value instanceof Number;
}
/**
* Check whether this primitive contains a boolean value.
* @param value value
* @return true if this primitive contains a boolean value, false otherwise.
*/
public boolean isBoolean(Object value) {
return value instanceof Boolean;
}
/**
* Check whether this primitive contains a String value.
* @param value value
* @return true if this primitive contains a String value, false otherwise.
*/
public boolean isString(String value) {
return value instanceof String;
}
}