lowentry.ue4.libs.jackson.dataformat.javaprop.util.JPropNode Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of lowentryue4 Show documentation
Show all versions of lowentryue4 Show documentation
A Java library for the Low Entry UE4 plugins.
package lowentry.ue4.libs.jackson.dataformat.javaprop.util;
import java.util.*;
/**
* Value in an ordered tree presentation built from an arbitrarily ordered
* set of flat input values. Since either index- OR name-based access is to
* be supported (similar to, say, Javascript objects) -- but only one, not both --
* storage is bit of a hybrid. In addition, branches may also have values.
* So, code does bit coercion as necessary, trying to maintain something
* consistent and usable at all times, without failure.
*/
@SuppressWarnings("all")
public class JPropNode
{
/**
* Value for the path, for leaf nodes; usually null for branches.
* If both children and value exists, typically need to construct
* bogus value with empty String key.
*/
protected String _value;
/**
* Child entries with integral number index, if any.
*/
protected TreeMap _byIndex;
/**
* Child entries accessed with String property name, if any.
*/
protected Map _byName;
protected boolean _hasContents = false;
public JPropNode setValue(String v) {
// should we care about overwrite?
_value = v;
return this;
}
public JPropNode addByIndex(int index) {
// if we already have named entries, coerce into name
if (_byName != null) {
return addByName(String.valueOf(index));
}
_hasContents = true;
if (_byIndex == null) {
_byIndex = new TreeMap<>();
}
Integer key = Integer.valueOf(index);
JPropNode n = _byIndex.get(key);
if (n == null) {
n = new JPropNode();
_byIndex.put(key, n);
}
return n;
}
public JPropNode addByName(String name) {
// if former index entries, first coerce them
_hasContents = true;
if (_byIndex != null) {
if (_byName == null) {
_byName = new LinkedHashMap<>();
}
for (Map.Entry entry : _byIndex.entrySet()) {
_byName.put(entry.getKey().toString(), entry.getValue());
}
_byIndex = null;
}
if (_byName == null) {
_byName = new LinkedHashMap<>();
} else {
JPropNode old = _byName.get(name);
if (old != null) {
return old;
}
}
JPropNode result = new JPropNode();
_byName.put(name, result);
return result;
}
public boolean isLeaf() {
return !_hasContents && (_value != null);
}
public boolean isArray() {
return _byIndex != null;
}
public String getValue() {
return _value;
}
public Iterator arrayContents() {
// should never be called if `_byIndex` is null, hence no checks
return _byIndex.values().iterator();
}
/**
* Child entries accessed with String property name, if any.
*/
public Iterator> objectContents() {
if (_byName == null) { // only value, most likely
return Collections.emptyIterator();
}
return _byName.entrySet().iterator();
}
/**
* Helper method, mostly for debugging/testing, to convert structure contained
* into simple List/Map/String equivalent.
*/
public Object asRaw() {
if (isArray()) {
List