small refactor
This commit is contained in:
@@ -57,7 +57,7 @@ public abstract class ValueDumper implements Dumper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected static class ArrayStackLevel extends StackLevel implements Iterator<Value> {
|
protected static class ArrayStackLevel extends StackLevel implements Iterator<Value> {
|
||||||
private final Iterator<Value> iterator = ((ArrayValue) value).iterator();
|
private final Iterator<Value> iterator = value.asArray().iterator();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Value next() {
|
public Value next() {
|
||||||
@@ -70,13 +70,13 @@ public abstract class ValueDumper implements Dumper {
|
|||||||
return iterator.hasNext();
|
return iterator.hasNext();
|
||||||
}
|
}
|
||||||
|
|
||||||
public ArrayStackLevel(ArrayValue value) {
|
public ArrayStackLevel(Value value) {
|
||||||
super(value);
|
super(value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static class ObjectStackLevel extends StackLevel implements Iterator<Map.Entry<String, Value>> {
|
protected static class ObjectStackLevel extends StackLevel implements Iterator<Map.Entry<String, Value>> {
|
||||||
private final Iterator<Map.Entry<String, Value>> iterator = ((ObjectValue) value).iterator();
|
private final Iterator<Map.Entry<String, Value>> iterator = value.asObject().iterator();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Map.Entry<String, Value> next() {
|
public Map.Entry<String, Value> next() {
|
||||||
@@ -89,7 +89,7 @@ public abstract class ValueDumper implements Dumper {
|
|||||||
return iterator.hasNext();
|
return iterator.hasNext();
|
||||||
}
|
}
|
||||||
|
|
||||||
public ObjectStackLevel(ObjectValue value) {
|
public ObjectStackLevel(Value value) {
|
||||||
super(value);
|
super(value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -106,33 +106,31 @@ public class JSONDumper extends ValueDumper {
|
|||||||
stringValue(v.asString());
|
stringValue(v.asString());
|
||||||
break;
|
break;
|
||||||
case ARRAY:
|
case ARRAY:
|
||||||
ArrayValue arrayValue = WorthUtils.dynamicCast(v, ArrayValue.class);
|
if(ids != null && (id = ids.get(new ValueIdentity(v))) != null) {
|
||||||
if(ids != null && (id = ids.get(new ValueIdentity(arrayValue))) != null) {
|
|
||||||
if(dumpedId.add(id)) {
|
if(dumpedId.add(id)) {
|
||||||
stack.push(new ArrayStackLevel(arrayValue));
|
stack.push(new ArrayStackLevel(v));
|
||||||
valueId(id);
|
valueId(id);
|
||||||
beginArray(arrayValue.size());
|
beginArray(v.size());
|
||||||
} else {
|
} else {
|
||||||
valueReference(id);
|
valueReference(id);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
stack.push(new ArrayStackLevel(arrayValue));
|
stack.push(new ArrayStackLevel(v));
|
||||||
beginArray(arrayValue.size());
|
beginArray(v.size());
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case OBJECT:
|
case OBJECT:
|
||||||
ObjectValue objectValue = WorthUtils.dynamicCast(v, ObjectValue.class);
|
if(ids != null && (id = ids.get(new ValueIdentity(v))) != null) {
|
||||||
if(ids != null && (id = ids.get(new ValueIdentity(objectValue))) != null) {
|
|
||||||
if(dumpedId.add(id)) {
|
if(dumpedId.add(id)) {
|
||||||
stack.push(new ObjectStackLevel(WorthUtils.dynamicCast(v, ObjectValue.class)));
|
stack.push(new ObjectStackLevel(v));
|
||||||
valueId(id);
|
valueId(id);
|
||||||
beginObject(objectValue.size());
|
beginObject(v.size());
|
||||||
} else {
|
} else {
|
||||||
valueReference(id);
|
valueReference(id);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
stack.push(new ObjectStackLevel(WorthUtils.dynamicCast(v, ObjectValue.class)));
|
stack.push(new ObjectStackLevel(v));
|
||||||
beginObject(objectValue.size());
|
beginObject(v.size());
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
11
src/main/java/net/woggioni/worth/utils/Tuple2.java
Normal file
11
src/main/java/net/woggioni/worth/utils/Tuple2.java
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
package net.woggioni.worth.utils;
|
||||||
|
|
||||||
|
import lombok.EqualsAndHashCode;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
|
||||||
|
@EqualsAndHashCode
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class Tuple2<T, U> {
|
||||||
|
public final T _1;
|
||||||
|
public final U _2;
|
||||||
|
}
|
@@ -31,6 +31,11 @@ public class ArrayValue implements Value, Iterable<Value> {
|
|||||||
this.value.add(value);
|
this.value.add(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void set(int index, Value value) {
|
||||||
|
this.value.set(index, value);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Value get(int index) {
|
public Value get(int index) {
|
||||||
int sz = size();
|
int sz = size();
|
||||||
|
@@ -85,8 +85,8 @@ abstract class MapObjectValue implements ObjectValue {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Map<String, Value> asObject() {
|
public Iterable<Map.Entry<String, Value>> asObject() {
|
||||||
return Collections.unmodifiableMap(value);
|
return () -> value.entrySet().iterator();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -168,12 +168,8 @@ class ListObjectValue implements ObjectValue {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Map<String, Value> asObject() {
|
public Iterable<Map.Entry<String, Value>> asObject() {
|
||||||
Map<String, Value> result = new LinkedHashMap<>();
|
return value::iterator;
|
||||||
for (Map.Entry<String, Value> entry : value) {
|
|
||||||
result.put(entry.getKey(), entry.getValue());
|
|
||||||
}
|
|
||||||
return Collections.unmodifiableMap(result);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@@ -34,11 +34,11 @@ public interface Value {
|
|||||||
throw new TypeException("Not a String");
|
throw new TypeException("Not a String");
|
||||||
}
|
}
|
||||||
|
|
||||||
default List<Value> asArray() {
|
default Iterable<Value> asArray() {
|
||||||
throw new TypeException("Not an array");
|
throw new TypeException("Not an array");
|
||||||
}
|
}
|
||||||
|
|
||||||
default Map<String, Value> asObject() {
|
default Iterable<Map.Entry<String, Value>> asObject() {
|
||||||
throw new TypeException("Not an object");
|
throw new TypeException("Not an object");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -50,6 +50,10 @@ public interface Value {
|
|||||||
throw new TypeException("Not an array");
|
throw new TypeException("Not an array");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
default void set(int index, Value value) {
|
||||||
|
throw new TypeException("Not an array");
|
||||||
|
}
|
||||||
|
|
||||||
default Value pop() {
|
default Value pop() {
|
||||||
throw new TypeException("Not an array");
|
throw new TypeException("Not an array");
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user