
com.welemski.wrench.StringDictionary Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of wrench Show documentation
Show all versions of wrench Show documentation
Convenience class library
/*
* The MIT License
*
* Copyright 2016 welemski.
*
* 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.welemski.wrench;
import com.google.gson.internal.LinkedTreeMap;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
/**
*
* @author welemski
*/
public class StringDictionary extends HashMap{
private static final long serialVersionUID = -7788558778538248182L;
private String dateFormat = "yyy-MM-dd HH:mm:ss";
private Locale locale = Locale.getDefault();
public StringDictionary() {
super();
}
public StringDictionary(Map attr) {
super(attr);
}
public StringDictionary(int initialCapacity) {
super(initialCapacity);
}
public StringDictionary(int initialCapacity, float loadFactor) {
super(initialCapacity, loadFactor);
}
public String getString(String key, String defaultValue) {
Object obj = this.get(key);
if (obj == null) {
return defaultValue;
}
if (obj instanceof String) {
return (String) obj;
}
if (obj instanceof Integer) {
return ((Integer) obj).toString();
}
if (obj instanceof Boolean) {
return ((Boolean) obj).toString();
}
if (obj instanceof Double) {
return ((Double) obj).toString();
}
if (obj instanceof Date) {
SimpleDateFormat sdf = new SimpleDateFormat(this.dateFormat, this.locale);
return sdf.format((Date) obj);
}
if (obj instanceof Long) {
return ((Long) obj).toString();
}
return defaultValue;
}
public String getString(String key) {
return this.getString(key, null);
}
public Integer getInteger(String key, Integer defaultValue) {
Object obj = this.get(key);
if (obj == null) {
return defaultValue;
}
if (obj instanceof String) {
try {
return Integer.parseInt((String) obj);
} catch (NumberFormatException ex) {
return defaultValue;
}
}
if (obj instanceof Integer) {
return ((Integer) obj);
}
if (obj instanceof Boolean) {
return ((Boolean) obj) ? 1 : 0;
}
if (obj instanceof Double) {
return ((Double) obj).intValue();
}
if (obj instanceof Date) {
Long l = ((Date) obj).getTime();
return l.intValue();
}
if (obj instanceof Long) {
return ((Long) obj).intValue();
}
return defaultValue;
}
public Integer getInteger(String key) {
return this.getInteger(key, null);
}
public Double getDouble(String key, Double defaultValue) {
Object obj = this.get(key);
if (obj == null) {
return defaultValue;
}
if (obj instanceof Double) {
return ((Double) obj);
}
if (obj instanceof String) {
try {
return Double.parseDouble(((String) obj));
} catch (NumberFormatException ex) {
return defaultValue;
}
}
if (obj instanceof Integer) {
return ((Integer) obj).doubleValue();
}
if (obj instanceof Boolean) {
return ((Boolean) obj) ? 1.0 : 0.0;
}
if (obj instanceof Date) {
Long l = ((Date) obj).getTime();
return l.doubleValue();
}
if (obj instanceof Long) {
return ((Long) obj).doubleValue();
}
return defaultValue;
}
public Double getDouble(String key) {
return this.getDouble(key, null);
}
public Date getDate(String key, Date defaultDate) {
Object obj = this.get(key);
if (obj == null) {
return defaultDate;
}
if (obj instanceof Date) {
return ((Date) obj);
}
if (obj instanceof String) {
SimpleDateFormat sdf = new SimpleDateFormat(this.dateFormat, this.locale);
try {
Date dt = sdf.parse((String) obj);
return dt;
} catch (ParseException ex) {
return defaultDate;
}
}
if (obj instanceof Double) {
return defaultDate;
}
if (obj instanceof Long) {
Date dt = new Date(((Long) obj));
return dt;
}
if (obj instanceof Integer) {
Date dt = new Date(((Integer) obj).longValue());
return dt;
}
return defaultDate;
}
public Date getDate(String key) {
return this.getDate(key, null);
}
public Long getLong(String key, Long defaultValue) {
Object obj = this.get(key);
if (obj == null) {
return defaultValue;
}
if (obj instanceof Long) {
return (Long) obj;
}
if (obj instanceof String) {
try {
return Long.parseLong(((String) obj));
} catch (NumberFormatException ex) {
return defaultValue;
}
}
if (obj instanceof Integer) {
return ((Integer) obj).longValue();
}
if (obj instanceof Double) {
return ((Double) obj).longValue();
}
if (obj instanceof Date) {
return ((Date) obj).getTime();
}
return defaultValue;
}
public Long getLong(String key) {
return this.getLong(key, null);
}
public List getStrings(String key) {
Object obj = this.get(key);
if (obj == null) {
return new ArrayList<>();
}
if (obj instanceof ArrayList>) {
List> list = ((ArrayList>) obj);
if (list.isEmpty()) {
return new ArrayList<>();
}
Object item = list.get(0);
if (item instanceof String) {
return (ArrayList) obj;
}
}
if (obj instanceof List>) {
List> list = (List>) obj;
if (list.isEmpty()) {
return new ArrayList<>();
}
Object item = list.get(0);
if (item instanceof String) {
return ((ArrayList) obj);
}
}
return new ArrayList<>();
}
public List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy