step.core.collections.DocumentObject Maven / Gradle / Ivy
The newest version!
/*******************************************************************************
* Copyright (C) 2020, exense GmbH
*
* This file is part of STEP
*
* STEP is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* STEP is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with STEP. If not, see .
******************************************************************************/
package step.core.collections;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.stream.Collectors;
public class DocumentObject implements Map {
private final Map map;
public DocumentObject() {
this(new HashMap<>());
}
public DocumentObject(Map map) {
super();
this.map = map;
}
@SuppressWarnings("unchecked")
public DocumentObject getObject(String key) {
Object object = get(key);
if (object != null) {
if (object instanceof Map) {
return new DocumentObject((Map) object);
} else {
throw new RuntimeException("Value of " + key + " is not a map but " + object.getClass().getName());
}
} else {
return null;
}
}
@SuppressWarnings("unchecked")
public List getArray(String key) {
Object object = get(key);
if (object != null) {
if (object instanceof List) {
return (List) ((List>) object).stream()
.map(e -> new DocumentObject((Map) e)).collect(Collectors.toList());
} else {
throw new RuntimeException("Value of " + key + " is not a map but " + object.getClass().getName());
}
} else {
return null;
}
}
public String getString(String key) {
Object object = get(key);
return object != null ? object.toString() : null;
}
public boolean getBoolean(String key) {
Object object = get(key);
if (object != null) {
if (object instanceof Boolean) {
return (Boolean) object;
} else if (object instanceof String) {
return Boolean.parseBoolean((String) object);
} else {
throw new RuntimeException("Value " + object + " is not a boolean");
}
} else {
return false;
}
}
public int size() {
return map.size();
}
public boolean isEmpty() {
return map.isEmpty();
}
public boolean containsKey(Object key) {
return map.containsKey(key);
}
public boolean containsValue(Object value) {
return map.containsValue(value);
}
public Object get(Object key) {
return map.get(key);
}
public Object put(String key, Object value) {
return map.put(key, value);
}
public Object remove(Object key) {
return map.remove(key);
}
public void putAll(Map extends String, ? extends Object> m) {
map.putAll(m);
}
public void clear() {
map.clear();
}
public Set keySet() {
return map.keySet();
}
public Collection