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

com.welemski.wrench.StrDictionary 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.util.HashMap;
import java.util.Map;
import com.welemski.wrench.spanner.DateTimeExtractor;
import com.welemski.wrench.spanner.DefaultObjectExtractor;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import com.welemski.wrench.spanner.ObjectExtractor;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Set;
import com.welemski.wrench.spanner.ExtractableDictionary;
import java.util.function.BiFunction;
import java.util.function.Function;

/**
 *
 * @author welemski
 */
public class StrDictionary implements Map, ExtractableDictionary{
    
    private String identifier;
    private Map content = null;
    
    public StrDictionary(){
        content = new HashMap<>();
    }
    public StrDictionary(String identifer){
        content = new HashMap<>();
        this.identifier = identifer;
    }
    
    public StrDictionary(boolean ordered){
        content = ordered ? new LinkedHashMap<>() : new HashMap<>();
    }
    public StrDictionary(boolean ordered,String identifier){
        content = ordered ? new LinkedHashMap<>() : new HashMap<>();
        this.identifier = identifier;
    }
    
    public StrDictionary(Map m){
        content = m;
    }
    public StrDictionary(Map m,boolean ordered){
        content = ordered ? new LinkedHashMap<>(m) : new HashMap<>(m);
    }
    
    public StrDictionary(int initialCapacity, float loadFactor){
        content = new HashMap<>(initialCapacity, loadFactor);
    }
    
    public StrDictionary(int initialCapacity, float loadFactor, boolean ordered){
        content = ordered ? new LinkedHashMap<>(initialCapacity, loadFactor) : new HashMap<>(initialCapacity, loadFactor);
    }
    
    public StrDictionary(int initialCapacity){
        content = new HashMap<>(initialCapacity);
    }
    
    public StrDictionary ordered(boolean ordered){
        if(content.isEmpty()){
            content = ordered ? new LinkedHashMap<>() : new HashMap<>();
            return this;
        }
        
        content = ordered ? new LinkedHashMap<>(content) : new HashMap<>(content);
        return this;
    }
    
    public Map toMap(){
        return this.content;
    }
    
    /**
     * Convenience finder to recursively find objects based on identifier.
     * 
     * @param identifier an identifier
     * @return 
     */
    public Object find(String identifier){
        
        if(this.identifier != null && this.identifier.equals(identifier)){
            return this;
        }
        
        for (Map.Entry entry : this.entrySet()) {
            
            if(entry.getValue() instanceof StrDictionaries ){
                StrDictionaries sds = (StrDictionaries)entry.getValue();
                Object found = sds.find(identifier);
                if(found != null){
                    return found;
                }
            }
            
            if(entry.getValue() instanceof StrDictionary){
                StrDictionary sd = (StrDictionary)entry.getValue();
                Object found = sd.find(identifier);
                if(found != null){
                    return found;
                }
            }
        }
        
        return null;
    }
    
    /**
     * Convenience method to add item to a list from N depth levels of structured data.
     * Useful for generating JSON string.
     * 
     * @param keyName a key
     * @param value default value
     * @return 
     */
    public StrDictionary addItem(String keyName, StrDictionary value){
        StrDictionaries list = null;
        
        Object item = this.get(keyName);
        if(item == null){
            list = new StrDictionaries();
        } else {
            list = (StrDictionaries) item;
        }
        list.add(value);
        this.put(keyName, list);
        return this;
    }
    
    public static StrDictionary create(){
        return new StrDictionary();
    }
    
    public static StrDictionary create(String identifier){
        return new StrDictionary(identifier);
    }
    
    public static StrDictionary ordered(String identifier){
        return new StrDictionary(true, identifier);
    }
    
    /**
     * @return the identifier
     */
    public String getIdentifier() {
        return identifier;
    }

    /**
     * @param identifier the identifier to set
     */
    public void setIdentifier(String identifier) {
        this.identifier = identifier;
    }
    
    public StrDictionary setValue(String forKey, Object value){
        this.put(forKey, value);
        return this;
    }

    @Override
    public int size() {
        return content.size();
    }

    @Override
    public boolean isEmpty() {
        return content.isEmpty();
    }

    @Override
    public boolean containsKey(Object key) {
        return content.containsKey(key);
    }

    @Override
    public boolean containsValue(Object value) {
        return content.containsValue(value);
    }

    @Override
    public Object get(Object key) {
        return content.get(key);
    }

    @Override
    public Object put(String key, Object value) {
        return content.put(key, value);
    }

    @Override
    public Object remove(Object key) {
        return content.remove(key);
    }

    @Override
    public void putAll(Map m) {
        content.putAll(m);
    }

    @Override
    public void clear() {
        content.clear();
    }

    @Override
    public Set keySet() {
        return content.keySet();
    }

    @Override
    public Collection values() {
        return content.values();
    }

    @Override
    public Set> entrySet() {
        return content.entrySet();
    }
    
    
    

    @Override
    public String getString(String key, String value, ObjectExtractor extractor) {
        return extractor.extract(this.get(key), value);
    }

    @Override
    public String getString(String key, DefaultObjectExtractor extractor) {
        return extractor.extract(this.get(key));
    }

    @Override
    public Boolean getBoolean(String key, Boolean value, ObjectExtractor extractor) {
        return extractor.extract(this.get(key), value);
    }

    @Override
    public Boolean getBoolean(String key, DefaultObjectExtractor extractor) {
        return extractor.extract(this.get(key));
    }

    @Override
    public Integer getInteger(String key, Integer value, ObjectExtractor extractor) {
        return extractor.extract(this.get(key), value);
    }

    @Override
    public Integer getInteger(String key, DefaultObjectExtractor extractor) {
        return extractor.extract(this.get(key));
    }

    @Override
    public Long getLong(String key, Long value, ObjectExtractor extractor) {
        return extractor.extract(this.get(key), value);
    }

    @Override
    public Long getLong(String key, DefaultObjectExtractor extractor) {
        return extractor.extract(this.get(key));
    }

    @Override
    public Double getDouble(String key, Double value, ObjectExtractor extractor) {
        return extractor.extract(this.get(key), value);
    }

    @Override
    public Double getDouble(String key, DefaultObjectExtractor extractor) {
        return extractor.extract(this.get(key));
    }

    @Override
    public LocalDateTime getDateTime(String key, LocalDateTime value, ObjectExtractor extractor) {
        return extractor.extract(this.get(key), value);
    }

    @Override
    public LocalDateTime getDateTime(String key, DateTimeFormatter formatter, LocalDateTime value, DateTimeExtractor extractor) {
        return extractor.extract(this.get(key), value, formatter);
    }

    @Override
    public List getStringList(String key, List value, ObjectExtractor, Object> extractor) {
        return extractor.extract(this.get(key), value);
    }

    @Override
    public List getStringList(String key, DefaultObjectExtractor, Object> extractor) {
        return extractor.extract(this.get(key));
    }

    @Override
    public String[] getStringArray(String key, String[] value, ObjectExtractor extractor) {
        return extractor.extract(this.get(key), value);
    }

    @Override
    public String[] getStringArray(String key, DefaultObjectExtractor extractor) {
        return extractor.extract(this.get(key));
    }

    @Override
    public Map getStringMap(String key, Map value, ObjectExtractor, Object> extractor) {
        return extractor.extract(this.get(key), value);
    }

    @Override
    public Map getStringMap(String key, DefaultObjectExtractor, Object> extractor) {
        return extractor.extract(this.get(key));
    }

    @Override
    public HashMap getStringHashMap(String key, HashMap value, ObjectExtractor, Object> extractor) {
        return extractor.extract(this.get(key), value);
    }

    @Override
    public HashMap getStringHashMap(String key, DefaultObjectExtractor, Object> extractor) {
        return extractor.extract(this.get(key));
    }

    @Override
    public StrDictionary getStrDictionary(String key, StrDictionary value, ObjectExtractor extractor) {
        return extractor.extract(this.get(key), value);
    }

    @Override
    public StrDictionary getStrDictionary(String key, DefaultObjectExtractor extractor) {
        return extractor.extract(this.get(key));
    }

    @Override
    public ObjectArrayList getObjectArrayList(String key, ObjectArrayList value, ObjectExtractor extractor) {
       return extractor.extract(this.get(key), value);
    }

    @Override
    public ObjectArrayList getObjectArrayList(String key, DefaultObjectExtractor extractor) {
        return extractor.extract(this.get(key));
    }

    @Override
    public List getObjectList(String key, List value, ObjectExtractor, Object> extractor) {
        return extractor.extract(this.get(key), value);
    }

    @Override
    public List getObjectList(String key, DefaultObjectExtractor, Object> extractor) {
        return extractor.extract(this.get(key));
    }

    @Override
    public Object[] getObjectArray(String key, Object[] value, ObjectExtractor extractor) {
        return extractor.extract(this.get(key), value);
    }

    @Override
    public Object[] getObjectArray(String key, DefaultObjectExtractor extractor) {
        return extractor.extract(this.get(key));
    }

    @Override
    public Map getObjectMap(String key, Map value, ObjectExtractor, Object> extractor) {
        return extractor.extract(this.get(key), value);
    }

    @Override
    public Map getObjectMap(String key, DefaultObjectExtractor, Object> extractor) {
        return extractor.extract(this.get(key));
    }

    @Override
    public LinkedTreeMap getLinkedTreeMap(String key, LinkedTreeMap value, ObjectExtractor, Object> extractor) {
        return extractor.extract(this.get(key), value);
    }

    @Override
    public LinkedTreeMap getLinkedTreeMap(String key, DefaultObjectExtractor, Object> extractor) {
        return extractor.extract(this.get(key));
    }

    @Override
    public Object customExtract(String key, Object defaultValue, BiFunction function) {
        return function.apply(this.get(key), defaultValue);
    }

    @Override
    public Object customExtract(String key, Function function) {
        return function.apply(this.get(key));
    }


}