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> {
|
||||
private final Iterator<Value> iterator = ((ArrayValue) value).iterator();
|
||||
private final Iterator<Value> iterator = value.asArray().iterator();
|
||||
|
||||
@Override
|
||||
public Value next() {
|
||||
@@ -70,13 +70,13 @@ public abstract class ValueDumper implements Dumper {
|
||||
return iterator.hasNext();
|
||||
}
|
||||
|
||||
public ArrayStackLevel(ArrayValue value) {
|
||||
public ArrayStackLevel(Value value) {
|
||||
super(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
|
||||
public Map.Entry<String, Value> next() {
|
||||
@@ -89,7 +89,7 @@ public abstract class ValueDumper implements Dumper {
|
||||
return iterator.hasNext();
|
||||
}
|
||||
|
||||
public ObjectStackLevel(ObjectValue value) {
|
||||
public ObjectStackLevel(Value value) {
|
||||
super(value);
|
||||
}
|
||||
}
|
||||
|
@@ -106,33 +106,31 @@ public class JSONDumper extends ValueDumper {
|
||||
stringValue(v.asString());
|
||||
break;
|
||||
case ARRAY:
|
||||
ArrayValue arrayValue = WorthUtils.dynamicCast(v, ArrayValue.class);
|
||||
if(ids != null && (id = ids.get(new ValueIdentity(arrayValue))) != null) {
|
||||
if(ids != null && (id = ids.get(new ValueIdentity(v))) != null) {
|
||||
if(dumpedId.add(id)) {
|
||||
stack.push(new ArrayStackLevel(arrayValue));
|
||||
stack.push(new ArrayStackLevel(v));
|
||||
valueId(id);
|
||||
beginArray(arrayValue.size());
|
||||
beginArray(v.size());
|
||||
} else {
|
||||
valueReference(id);
|
||||
}
|
||||
} else {
|
||||
stack.push(new ArrayStackLevel(arrayValue));
|
||||
beginArray(arrayValue.size());
|
||||
stack.push(new ArrayStackLevel(v));
|
||||
beginArray(v.size());
|
||||
}
|
||||
break;
|
||||
case OBJECT:
|
||||
ObjectValue objectValue = WorthUtils.dynamicCast(v, ObjectValue.class);
|
||||
if(ids != null && (id = ids.get(new ValueIdentity(objectValue))) != null) {
|
||||
if(ids != null && (id = ids.get(new ValueIdentity(v))) != null) {
|
||||
if(dumpedId.add(id)) {
|
||||
stack.push(new ObjectStackLevel(WorthUtils.dynamicCast(v, ObjectValue.class)));
|
||||
stack.push(new ObjectStackLevel(v));
|
||||
valueId(id);
|
||||
beginObject(objectValue.size());
|
||||
beginObject(v.size());
|
||||
} else {
|
||||
valueReference(id);
|
||||
}
|
||||
} else {
|
||||
stack.push(new ObjectStackLevel(WorthUtils.dynamicCast(v, ObjectValue.class)));
|
||||
beginObject(objectValue.size());
|
||||
stack.push(new ObjectStackLevel(v));
|
||||
beginObject(v.size());
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void set(int index, Value value) {
|
||||
this.value.set(index, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Value get(int index) {
|
||||
int sz = size();
|
||||
|
@@ -85,8 +85,8 @@ abstract class MapObjectValue implements ObjectValue {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Value> asObject() {
|
||||
return Collections.unmodifiableMap(value);
|
||||
public Iterable<Map.Entry<String, Value>> asObject() {
|
||||
return () -> value.entrySet().iterator();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -168,12 +168,8 @@ class ListObjectValue implements ObjectValue {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Value> asObject() {
|
||||
Map<String, Value> result = new LinkedHashMap<>();
|
||||
for (Map.Entry<String, Value> entry : value) {
|
||||
result.put(entry.getKey(), entry.getValue());
|
||||
}
|
||||
return Collections.unmodifiableMap(result);
|
||||
public Iterable<Map.Entry<String, Value>> asObject() {
|
||||
return value::iterator;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -34,11 +34,11 @@ public interface Value {
|
||||
throw new TypeException("Not a String");
|
||||
}
|
||||
|
||||
default List<Value> asArray() {
|
||||
default Iterable<Value> asArray() {
|
||||
throw new TypeException("Not an array");
|
||||
}
|
||||
|
||||
default Map<String, Value> asObject() {
|
||||
default Iterable<Map.Entry<String, Value>> asObject() {
|
||||
throw new TypeException("Not an object");
|
||||
}
|
||||
|
||||
@@ -50,6 +50,10 @@ public interface Value {
|
||||
throw new TypeException("Not an array");
|
||||
}
|
||||
|
||||
default void set(int index, Value value) {
|
||||
throw new TypeException("Not an array");
|
||||
}
|
||||
|
||||
default Value pop() {
|
||||
throw new TypeException("Not an array");
|
||||
}
|
||||
|
Reference in New Issue
Block a user