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

com.welemski.wrench.StringDictionary Maven / Gradle / Ivy

There is a newer version: 1.0.13
Show newest version
/*
 * 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 getObjects(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 Object) {
                return ((ArrayList) obj);
            }
        }

        if (obj instanceof List) {
            List list = ((ArrayList) obj);

            if (list.isEmpty()) {
                return new ArrayList<>();
            }

            Object item = list.get(0);

            if (item instanceof Object) {
                return ((List) obj);
            }
        }

        return new ArrayList<>();
    }

    public ObjectArray getArray(String key) {
        Object obj = this.get(key);

        if (obj == null) {
            return new ObjectArray();
        }

        if (obj instanceof ObjectArray) {
            return ((ObjectArray) obj);
        }

        if (obj instanceof ArrayList) {
            List list = (ArrayList) obj;

            return new ObjectArray(list);
        }

        if (obj instanceof List) {
            List list = (List) obj;

            return new ObjectArray(list);
        }

        return new ObjectArray();
    }

    public Map getObjectMap(String key) {
        Object obj = this.get(key);

        if (obj == null) {
            return null;
        }

        if (obj instanceof HashMap) {
            Map map = ((HashMap) obj);

            if (map.isEmpty()) {
                return null;
            }

            return ((HashMap) obj);
        }

        if (obj instanceof Map) {
            Map map = ((Map) obj);

            if (map.isEmpty()) {
                return null;
            }

            return (Map) obj;
        }

        return null;
    }

    public StringDictionary getStringDictionary(String key) {
        Object obj = this.get(key);

        if (obj == null) {
            return null;
        }

        if (obj instanceof StringDictionary) {
            return (StringDictionary) obj;
        }

        if (obj instanceof HashMap) {
            Map map = ((HashMap) obj);

            if (map.isEmpty()) {
                return null;
            }

            Entry next = map.entrySet().iterator().next();
            if (next.getValue() instanceof String) {
                return new StringDictionary((Map) obj);
            }
        }

        return null;
    }
    
    public Map getLinkedTreeMap(String key){
        Object obj = this.get(key);
        
        if(obj == null){
            return null;
        }
        
        if(obj instanceof LinkedTreeMap){
            Map map = ((LinkedTreeMap)obj);
            
            if(map.isEmpty()){
                return null;
            }
            
            Entry next = map.entrySet().iterator().next();
            
            if(next.getKey() instanceof String){
                return ((LinkedTreeMap)obj);
            }
        }
        
        return null;
    }
    
    public Map getStringMap(String key) {
        Object obj = this.get(key);

        if (obj == null) {
            return null;
        }

        if (obj instanceof HashMap) {
            Map map = ((HashMap) obj);

            if (map.isEmpty()) {
                return null;
            }

            Entry next = map.entrySet().iterator().next();
            if (next.getValue() instanceof String) {
                return ((HashMap) obj);
            }
        }

        if (obj instanceof Map) {
            Map map = ((Map) obj);

            if (map.isEmpty()) {
                return null;
            }

            Entry next = map.entrySet().iterator().next();
            if (next.getValue() instanceof String) {
                return ((Map) obj);
            }
        }

        return null;
    }
    
    public List> getStringMaps(String key){
        Object obj = this.get(key);
        
        if(obj == null){
            return new ArrayList<>();
        }
        
        if(obj instanceof ArrayList){
            List objs = (ArrayList)obj;
            if(objs.isEmpty()){
                return new ArrayList<>();
            }
            
            Object itemObj = objs.get(0);
            if(itemObj instanceof Map){
                Map m = (Map)itemObj;
                
                if(m.isEmpty()){
                    return new ArrayList<>();
                }
                
                Entry next = m.entrySet().iterator().next();
                if(next.getKey() instanceof String){
                    return (ArrayList>)obj;
                }
            }
        }
        
        if(obj instanceof List){
            List objs = (List)obj;
            if(objs.isEmpty()){
                return new ArrayList<>();
            }
            
            Object itemObj = objs.get(0);
            if(itemObj instanceof Map){
                Map m = (Map)itemObj;
                
                if(m.isEmpty()){
                    return new ArrayList<>();
                }
                
                Entry next = m.entrySet().iterator().next();
                if(next.getKey() instanceof String){
                    return (List>)obj;
                }
            }
        }
        
        return new ArrayList<>();
    }
    
}