com.welemski.wrench.StrDictionary 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.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 extends String, ? extends Object> m) {
content.putAll(m);
}
@Override
public void clear() {
content.clear();
}
@Override
public Set keySet() {
return content.keySet();
}
@Override
public Collection