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.
/*
* Copyright (c) 2017 Rumen Nikiforov
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.github.unafraid.telegrambot.util;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author UnAfraid
*/publicfinalclassMapUtil{
privatestaticfinal Logger LOGGER = LoggerFactory.getLogger(MapUtil.class);
privatefinal Map values;
public MapUtil() {
values = new LinkedHashMap<>();
}
public MapUtil(Map map) {
values = map;
}
/**
* Returns the set of values
*
* @return HashMap
*/publicfinal Map getInternalMap() {
return values;
}
/**
* Add a set of couple values in the current set
*
* @param newSet : MapSet pointing out the list of couples to add in the current set
*/public void merge(MapUtil newSet) {
Map newMap = newSet.getInternalMap();
for (Entry entry : newMap.entrySet()) {
values.put(entry.getKey(), entry.getValue());
}
}
/**
* Puts key-value pair inside the internal map, replacing previously existing key
*
* @param name String designating the key in the set
* @param value value associated to the key
* @return the same instance of current MapSet so it can be chained like: mapSet.put("key", "value").put("another key", "another value");
*/public MapUtil put(String name, Object value) {
if (value != null) {
values.put(name, value);
}
return this;
}
/**
* Return the boolean associated to the key put in parameter ("name")
*
* @param name : String designating the key in the set
* @return boolean : value associated to the key
*/public boolean getBoolean(String name) {
Object val = values.get(name);
if (val == null) {
thrownew IllegalArgumentException("Boolean value required, but not specified");
}
if (val instanceof Boolean) {
return (Boolean) val;
}
try {
return Boolean.parseBoolean((String) val);
} catch (Exception e) {
thrownew IllegalArgumentException("Boolean value required, but found: " + val);
}
}
/**
* Return the boolean associated to the key put in parameter ("name"). If the value associated to the key is null, this method returns the value of the parameter defaultValue.
*
* @param name : String designating the key in the set
* @param defaultValue : boolean designating the default value if value associated with the key is null
* @return boolean : value of the key
*/public boolean getBoolean(String name, boolean defaultValue) {
final Object val = values.get(name);
if (val == null) {
return defaultValue;
}
if (val instanceof Boolean) {
return (Boolean) val;
}
try {
return Boolean.parseBoolean((String) val);
} catch (Exception e) {
thrownew IllegalArgumentException("Boolean value required, but found: " + val);
}
}
/**
* Returns the int associated to the key put in parameter ("name"). If the value associated to the key is null, this method returns the value of the parameter defaultValue.
*
* @param name : String designating the key in the set
* @param defaultValue : byte designating the default value if value associated with the key is null
* @return byte : value associated to the key
*/public byte getByte(String name, byte defaultValue) {
Object val = values.get(name);
if (val == null) {
return defaultValue;
}
if (val instanceof Number) {
return ((Number) val).byteValue();
}
try {
return Byte.parseByte((String) val);
} catch (Exception e) {
thrownew IllegalArgumentException("Byte value required, but found: " + val);
}
}
/**
* Returns the byte associated to the key put in parameter ("name").
*
* @param name : String designating the key in the set
* @return byte : value associated to the key
*/public byte getByte(String name) {
Object val = values.get(name);
if (val == null) {
thrownew IllegalArgumentException("Byte value required, but not specified");
}
if (val instanceof Number) {
return ((Number) val).byteValue();
}
try {
return Byte.parseByte((String) val);
} catch (Exception e) {
thrownew IllegalArgumentException("Byte value required, but found: " + val);
}
}
/**
* Returns the short associated to the key put in parameter ("name"). If the value associated to the key is null, this method returns the value of the parameter defaultValue.
*
* @param name : String designating the key in the set
* @param defaultValue : short designating the default value if value associated with the key is null
* @return short : value associated to the key
*/public short getShort(String name, short defaultValue) {
Object val = values.get(name);
if (val == null) {
return defaultValue;
}
if (val instanceof Number) {
return ((Number) val).shortValue();
}
try {
return Short.parseShort((String) val);
} catch (Exception e) {
thrownew IllegalArgumentException("Short value required, but found: " + val);
}
}
/**
* Returns the short associated to the key put in parameter ("name").
*
* @param name : String designating the key in the set
* @return short : value associated to the key
*/public short getShort(String name) {
final Object val = values.get(name);
if (val == null) {
thrownew IllegalArgumentException("Short value required, but not specified");
}
if (val instanceof Number) {
return ((Number) val).shortValue();
}
try {
return Short.parseShort((String) val);
} catch (Exception e) {
thrownew IllegalArgumentException("Short value required, but found: " + val);
}
}
/**
* Returns the int associated to the key put in parameter ("name").
*
* @param name : String designating the key in the set
* @return int : value associated to the key
*/public int getInt(String name) {
final Object val = values.get(name);
if (val == null) {
thrownew IllegalArgumentException("Integer value required, but not specified");
}
if (val instanceof Number) {
return ((Number) val).intValue();
}
try {
return Integer.parseInt((String) val);
} catch (Exception e) {
thrownew IllegalArgumentException("Integer value required, but found: " + val);
}
}
/**
* Returns the int associated to the key put in parameter ("name"). If the value associated to the key is null, this method returns the value of the parameter defaultValue.
*
* @param name : String designating the key in the set
* @param defaultValue : int designating the default value if value associated with the key is null
* @return int : value associated to the key
*/public int getInteger(String name, int defaultValue) {
final Object val = values.get(name);
if (val == null) {
return defaultValue;
}
if (val instanceof Number) {
return ((Number) val).intValue();
}
try {
return Integer.parseInt((String) val);
} catch (Exception e) {
thrownew IllegalArgumentException("Integer value required, but found: " + val);
}
}
/**
* Returns the int[] associated to the key put in parameter ("name"). If the value associated to the key is null, this method returns the value of the parameter defaultValue.
*
* @param name : String designating the key in the set
* @param delimiter the delimiter
* @return int[] : value associated to the key
*/public int[] getIntArray(String name, String delimiter) {
final Object val = values.get(name);
if (val == null) {
returnnull;
}
if (val instanceof Number) {
returnnew int[]
{
((Number) val).intValue()
};
}
int c = 0;
String[] vals = ((String) val).split(delimiter);
int[] result = new int[vals.length];
for (String v : vals) {
try {
result[c++] = Integer.parseInt(v);
} catch (Exception e) {
thrownew IllegalArgumentException("Integer value required, but found: " + val);
}
}
return result;
}
public float[] getFloatArray(String name, String delimiter) {
final Object val = values.get(name);
if (val == null) {
returnnull;
}
if (val instanceof Number) {
returnnew float[]
{
((Number) val).floatValue()
};
}
int c = 0;
String[] vals = ((String) val).split(delimiter);
float[] result = new float[vals.length];
for (String v : vals) {
try {
result[c++] = Float.parseFloat(v);
} catch (Exception e) {
thrownew IllegalArgumentException("Float value required, but found: " + val);
}
}
return result;
}
/**
* Returns the long associated to the key put in parameter ("name").
*
* @param name : String designating the key in the set
* @return long : value associated to the key
*/public long getLong(String name) {
final Object val = values.get(name);
if (val == null) {
thrownew IllegalArgumentException("Integer value required, but not specified");
}
if (val instanceof Number) {
return ((Number) val).longValue();
}
try {
return Long.parseLong((String) val);
} catch (Exception e) {
thrownew IllegalArgumentException("Integer value required, but found: " + val);
}
}
/**
* Returns the long associated to the key put in parameter ("name"). If the value associated to the key is null, this method returns the value of the parameter defaultValue.
*
* @param name : String designating the key in the set
* @param defaultValue : long designating the default value if value associated with the key is null
* @return long : value associated to the key
*/public long getLong(String name, int defaultValue) {
final Object val = values.get(name);
if (val == null) {
return defaultValue;
}
if (val instanceof Number) {
return ((Number) val).longValue();
}
try {
return Long.parseLong((String) val);
} catch (Exception e) {
thrownew IllegalArgumentException("Integer value required, but found: " + val);
}
}
/**
* Returns the float associated to the key put in parameter ("name").
*
* @param name : String designating the key in the set
* @return float : value associated to the key
*/public float getFloat(String name) {
final Object val = values.get(name);
if (val == null) {
thrownew IllegalArgumentException("Float value required, but not specified");
}
if (val instanceof Number) {
return ((Number) val).floatValue();
}
try {
return Float.parseFloat((String) val);
} catch (Exception e) {
thrownew IllegalArgumentException("Float value required, but found: " + val);
}
}
/**
* Returns the float associated to the key put in parameter ("name"). If the value associated to the key is null, this method returns the value of the parameter defaultValue.
*
* @param name : String designating the key in the set
* @param defaultValue : float designating the default value if value associated with the key is null
* @return float : value associated to the key
*/public float getFloat(String name, float defaultValue) {
final Object val = values.get(name);
if (val == null) {
return defaultValue;
}
if (val instanceof Number) {
return ((Number) val).floatValue();
}
try {
return Float.parseFloat((String) val);
} catch (Exception e) {
thrownew IllegalArgumentException("Float value required, but found: " + val);
}
}
/**
* Returns the double associated to the key put in parameter ("name").
*
* @param name : String designating the key in the set
* @return double : value associated to the key
*/public double getDouble(String name) {
final Object val = values.get(name);
if (val == null) {
thrownew IllegalArgumentException("Float value required, but not specified");
}
if (val instanceof Number) {
return ((Number) val).doubleValue();
}
try {
return Double.parseDouble((String) val);
} catch (Exception e) {
thrownew IllegalArgumentException("Float value required, but found: " + val);
}
}
/**
* Returns the double associated to the key put in parameter ("name"). If the value associated to the key is null, this method returns the value of the parameter defaultValue.
*
* @param name : String designating the key in the set
* @param defaultValue : float designating the default value if value associated with the key is null
* @return double : value associated to the key
*/public double getDouble(String name, float defaultValue) {
final Object val = values.get(name);
if (val == null) {
return defaultValue;
}
if (val instanceof Number) {
return ((Number) val).doubleValue();
}
try {
return Double.parseDouble((String) val);
} catch (Exception e) {
thrownew IllegalArgumentException("Float value required, but found: " + val);
}
}
/**
* Returns the String associated to the key put in parameter ("name").
*
* @param name : String designating the key in the set
* @return String : value associated to the key
*/public String getString(String name) {
final Object val = values.get(name);
if (val == null) {
thrownew IllegalArgumentException("String '" + name + "' value required, but not specified");
}
return String.valueOf(val);
}
/**
* Returns the String associated to the key put in parameter ("name"). If the value associated to the key is null, this method returns the value of the parameter defaultValue.
*
* @param name : String designating the key in the set
* @param defaultValue : String designating the default value if value associated with the key is null
* @return String : value associated to the key
*/public String getString(String name, String defaultValue) {
final Object val = values.get(name);
if (val == null) {
return defaultValue;
}
return String.valueOf(val);
}
/**
* Returns an enumeration of <T> from the set
*
* @param : Class of the enumeration returned
* @param name : String designating the key in the set
* @param enumClass : Class designating the class of the value associated with the key in the set
* @return enum of type T
*/
@SuppressWarnings("unchecked")
public> T getEnum(String name, ClassenumClass) {
final Object val = values.get(name);
if (val == null) {
thrownew IllegalArgumentException("Enum value of type " + enumClass.getName() + " required, but not specified");
}
if (enumClass.isInstance(val)) {
return (T) val;
}
try {
return Enum.valueOf(enumClass, String.valueOf(val));
} catch (Exception e) {
thrownew IllegalArgumentException("Enum value of type " + enumClass.getName() + " required, but found: " + val);
}
}
/**
* Returns an enumeration of <T> from the set. If the enumeration is empty, the method returns the value of the parameter "defaultValue".
*
* @param : Class of the enumeration returned
* @param name : String designating the key in the set
* @param enumClass : Class designating the class of the value associated with the key in the set
* @param defaultValue : T designating the value by default
* @return enum of type T
*/
@SuppressWarnings("unchecked")
public> T getEnum(String name, ClassenumClass, TdefaultValue) {
final Object val = values.get(name);
if (val == null) {
return defaultValue;
}
if (enumClass.isInstance(val)) {
return (T) val;
}
try {
return Enum.valueOf(enumClass, String.valueOf(val));
} catch (Exception e) {
thrownew IllegalArgumentException("Enum value of type " + enumClass.getName() + "required, but found: " + val);
}
}
@SuppressWarnings("unchecked")
publicfinal A getObject(String name, Classtype) {
Object obj = values.get(name);
if ((obj == null) || !type.isAssignableFrom(obj.getClass())) {
returnnull;
}
return (A) obj;
}
@SuppressWarnings("unchecked")
publicList getList(String key, Classclazz) {
Objects.requireNonNull(key);
Objects.requireNonNull(clazz);
final Object obj = values.get(key);
if (!(obj instanceofList)) {
returnnull;
}
finalList