added dependency from jwo
This commit is contained in:
@@ -16,14 +16,16 @@ import org.antlr.v4.runtime.CommonTokenStream;
|
|||||||
import org.antlr.v4.runtime.tree.ParseTreeWalker;
|
import org.antlr.v4.runtime.tree.ParseTreeWalker;
|
||||||
import org.tukaani.xz.XZInputStream;
|
import org.tukaani.xz.XZInputStream;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.BufferedInputStream;
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
import java.util.function.Function;
|
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
@@ -153,7 +155,7 @@ public class Main {
|
|||||||
Optional<Method> targetMethod = Arrays.stream(methods)
|
Optional<Method> targetMethod = Arrays.stream(methods)
|
||||||
.filter(method -> Objects.equals(benchmarkName, method.getName()))
|
.filter(method -> Objects.equals(benchmarkName, method.getName()))
|
||||||
.findFirst();
|
.findFirst();
|
||||||
targetMethod.ifPresent(new Consumer<>() {
|
targetMethod.ifPresent(new Consumer<Method>() {
|
||||||
@Override
|
@Override
|
||||||
@SneakyThrows
|
@SneakyThrows
|
||||||
public void accept(Method method) {
|
public void accept(Method method) {
|
||||||
|
@@ -18,6 +18,7 @@ git.useGitDescribe := true
|
|||||||
//javaOptions in Test += "-Xmx14G"
|
//javaOptions in Test += "-Xmx14G"
|
||||||
//scalafmtOnCompile := true
|
//scalafmtOnCompile := true
|
||||||
libraryDependencies += "org.projectlombok" % "lombok" % "1.18.8" % Provided
|
libraryDependencies += "org.projectlombok" % "lombok" % "1.18.8" % Provided
|
||||||
|
libraryDependencies += "net.woggioni" % "jwo" % "1.0" % Compile
|
||||||
|
|
||||||
val testDependencies = Seq("com.novocode" % "junit-interface" % "0.11" % Test,
|
val testDependencies = Seq("com.novocode" % "junit-interface" % "0.11" % Test,
|
||||||
"com.fasterxml.jackson.core" % "jackson-databind" % "2.9.6" % Test,
|
"com.fasterxml.jackson.core" % "jackson-databind" % "2.9.6" % Test,
|
||||||
|
@@ -18,7 +18,7 @@ import java.util.HashMap;
|
|||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import static net.woggioni.worth.utils.WorthUtils.tail;
|
import static net.woggioni.jwo.JWO.tail;
|
||||||
|
|
||||||
public abstract class ValueDumper implements Dumper {
|
public abstract class ValueDumper implements Dumper {
|
||||||
|
|
||||||
|
@@ -4,7 +4,6 @@ import lombok.RequiredArgsConstructor;
|
|||||||
import net.woggioni.worth.exception.MaxDepthExceededException;
|
import net.woggioni.worth.exception.MaxDepthExceededException;
|
||||||
import net.woggioni.worth.exception.NotImplementedException;
|
import net.woggioni.worth.exception.NotImplementedException;
|
||||||
import net.woggioni.worth.exception.ParseException;
|
import net.woggioni.worth.exception.ParseException;
|
||||||
import net.woggioni.worth.utils.WorthUtils;
|
|
||||||
import net.woggioni.worth.value.ArrayValue;
|
import net.woggioni.worth.value.ArrayValue;
|
||||||
import net.woggioni.worth.value.BooleanValue;
|
import net.woggioni.worth.value.BooleanValue;
|
||||||
import net.woggioni.worth.value.FloatValue;
|
import net.woggioni.worth.value.FloatValue;
|
||||||
@@ -23,7 +22,7 @@ import java.util.HashMap;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
|
|
||||||
import static net.woggioni.worth.utils.WorthUtils.newThrowable;
|
import static net.woggioni.jwo.JWO.newThrowable;
|
||||||
|
|
||||||
public class ValueParser implements Parser {
|
public class ValueParser implements Parser {
|
||||||
|
|
||||||
@@ -56,9 +55,11 @@ public class ValueParser implements Parser {
|
|||||||
StackLevel last = stack.getFirst();
|
StackLevel last = stack.getFirst();
|
||||||
ArrayStackLevel asl;
|
ArrayStackLevel asl;
|
||||||
ObjectStackLevel osl;
|
ObjectStackLevel osl;
|
||||||
if ((asl = WorthUtils.dynamicCast(last, ArrayStackLevel.class)) != null)
|
if (last instanceof ArrayStackLevel) {
|
||||||
|
asl = (ArrayStackLevel) last;
|
||||||
asl.value.add(value);
|
asl.value.add(value);
|
||||||
else if ((osl = WorthUtils.dynamicCast(last, ObjectStackLevel.class)) != null) {
|
} else if (last instanceof ObjectStackLevel) {
|
||||||
|
osl = (ObjectStackLevel) last;
|
||||||
osl.value.put(osl.currentKey, value);
|
osl.value.put(osl.currentKey, value);
|
||||||
osl.currentKey = null;
|
osl.currentKey = null;
|
||||||
}
|
}
|
||||||
@@ -73,7 +74,7 @@ public class ValueParser implements Parser {
|
|||||||
if (cfg.serializeReferences) {
|
if (cfg.serializeReferences) {
|
||||||
idMap = new HashMap<>();
|
idMap = new HashMap<>();
|
||||||
}
|
}
|
||||||
stack = new ArrayDeque<>() {
|
stack = new ArrayDeque<StackLevel>() {
|
||||||
@Override
|
@Override
|
||||||
public void push(StackLevel stackLevel) {
|
public void push(StackLevel stackLevel) {
|
||||||
if (size() == cfg.maxDepth) {
|
if (size() == cfg.maxDepth) {
|
||||||
|
@@ -3,10 +3,8 @@ package net.woggioni.worth.serialization.binary;
|
|||||||
import lombok.SneakyThrows;
|
import lombok.SneakyThrows;
|
||||||
import net.woggioni.worth.exception.NotImplementedException;
|
import net.woggioni.worth.exception.NotImplementedException;
|
||||||
import net.woggioni.worth.serialization.ValueDumper;
|
import net.woggioni.worth.serialization.ValueDumper;
|
||||||
import net.woggioni.worth.serialization.json.JSONDumper;
|
|
||||||
import net.woggioni.worth.traversal.ValueIdentity;
|
import net.woggioni.worth.traversal.ValueIdentity;
|
||||||
import net.woggioni.worth.utils.Leb128;
|
import net.woggioni.worth.utils.Leb128;
|
||||||
import net.woggioni.worth.utils.WorthUtils;
|
|
||||||
import net.woggioni.worth.value.ArrayValue;
|
import net.woggioni.worth.value.ArrayValue;
|
||||||
import net.woggioni.worth.value.ObjectValue;
|
import net.woggioni.worth.value.ObjectValue;
|
||||||
import net.woggioni.worth.xface.Dumper;
|
import net.woggioni.worth.xface.Dumper;
|
||||||
@@ -76,7 +74,7 @@ public class JBONDumper extends ValueDumper {
|
|||||||
stringValue(v.asString());
|
stringValue(v.asString());
|
||||||
break;
|
break;
|
||||||
case ARRAY:
|
case ARRAY:
|
||||||
ArrayValue arrayValue = WorthUtils.dynamicCast(v, ArrayValue.class);
|
ArrayValue arrayValue = (ArrayValue) v;
|
||||||
if(ids != null && (id = ids.get(new ValueIdentity(arrayValue))) != 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(arrayValue));
|
||||||
@@ -91,17 +89,17 @@ public class JBONDumper extends ValueDumper {
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case OBJECT:
|
case OBJECT:
|
||||||
ObjectValue objectValue = WorthUtils.dynamicCast(v, ObjectValue.class);
|
ObjectValue objectValue = (ObjectValue) v;
|
||||||
if(ids != null && (id = ids.get(new ValueIdentity(objectValue))) != 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(objectValue.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(objectValue.size());
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@@ -113,14 +111,16 @@ public class JBONDumper extends ValueDumper {
|
|||||||
StackLevel last = stack.getFirst();
|
StackLevel last = stack.getFirst();
|
||||||
ArrayStackLevel arrayStackLevel;
|
ArrayStackLevel arrayStackLevel;
|
||||||
ObjectStackLevel objectStackLevel;
|
ObjectStackLevel objectStackLevel;
|
||||||
if ((arrayStackLevel = WorthUtils.dynamicCast(last, ArrayStackLevel.class)) != null) {
|
if (last instanceof ArrayStackLevel) {
|
||||||
|
arrayStackLevel = (ArrayStackLevel) last;
|
||||||
if (arrayStackLevel.hasNext()) {
|
if (arrayStackLevel.hasNext()) {
|
||||||
handle_value.accept(arrayStackLevel.next());
|
handle_value.accept(arrayStackLevel.next());
|
||||||
} else {
|
} else {
|
||||||
endArray();
|
endArray();
|
||||||
stack.pop();
|
stack.pop();
|
||||||
}
|
}
|
||||||
} else if ((objectStackLevel = WorthUtils.dynamicCast(last, ObjectStackLevel.class)) != null) {
|
} else if (last instanceof ObjectStackLevel) {
|
||||||
|
objectStackLevel = (ObjectStackLevel) last;
|
||||||
if (objectStackLevel.hasNext()) {
|
if (objectStackLevel.hasNext()) {
|
||||||
Map.Entry<String, Value> entry = objectStackLevel.next();
|
Map.Entry<String, Value> entry = objectStackLevel.next();
|
||||||
objectKey(entry.getKey());
|
objectKey(entry.getKey());
|
||||||
|
@@ -40,8 +40,10 @@ public class JBONParser extends ValueParser {
|
|||||||
Integer currentId = null;
|
Integer currentId = null;
|
||||||
Leb128.Leb128Decoder decoder = new Leb128.Leb128Decoder(stream);
|
Leb128.Leb128Decoder decoder = new Leb128.Leb128Decoder(stream);
|
||||||
ObjectStackLevel osl;
|
ObjectStackLevel osl;
|
||||||
|
StackLevel sl;
|
||||||
while (true) {
|
while (true) {
|
||||||
if ((osl = WorthUtils.dynamicCast(stack.getFirst(), ObjectStackLevel.class)) != null && osl.currentKey == null) {
|
sl = stack.getFirst();
|
||||||
|
if (sl instanceof ObjectStackLevel && ((ObjectStackLevel) sl).currentKey == null) {
|
||||||
int size = (int) decoder.decode();
|
int size = (int) decoder.decode();
|
||||||
byte[] buffer = new byte[size];
|
byte[] buffer = new byte[size];
|
||||||
stream.read(buffer);
|
stream.read(buffer);
|
||||||
@@ -106,14 +108,13 @@ public class JBONParser extends ValueParser {
|
|||||||
throw new ParseException(String.format("Illegal byte at position %d: 0x%02x", cursor, c));
|
throw new ParseException(String.format("Illegal byte at position %d: 0x%02x", cursor, c));
|
||||||
}
|
}
|
||||||
while(stack.size() > 0) {
|
while(stack.size() > 0) {
|
||||||
if ((osl = WorthUtils.dynamicCast(stack.getFirst(), ObjectStackLevel.class)) != null
|
sl = stack.getFirst();
|
||||||
&& osl.value.size() == osl.expectedSize) {
|
if (sl instanceof ObjectStackLevel && (osl = (ObjectStackLevel) sl).value.size() == osl.expectedSize) {
|
||||||
endObject();
|
endObject();
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
ArrayStackLevel asl;
|
ArrayStackLevel asl;
|
||||||
if((asl = WorthUtils.dynamicCast(stack.getFirst(), ArrayStackLevel.class)) != null
|
if(sl instanceof ArrayStackLevel && (asl = (ArrayStackLevel) sl).value.size() == asl.expectedSize) {
|
||||||
&& asl.value.size() == asl.expectedSize) {
|
|
||||||
endArray();
|
endArray();
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@@ -141,7 +141,8 @@ public class JSONDumper extends ValueDumper {
|
|||||||
StackLevel last = stack.getFirst();
|
StackLevel last = stack.getFirst();
|
||||||
ArrayStackLevel arrayStackLevel;
|
ArrayStackLevel arrayStackLevel;
|
||||||
ObjectStackLevel objectStackLevel;
|
ObjectStackLevel objectStackLevel;
|
||||||
if ((arrayStackLevel = WorthUtils.dynamicCast(last, ArrayStackLevel.class)) != null) {
|
if (last instanceof ArrayStackLevel) {
|
||||||
|
arrayStackLevel = (ArrayStackLevel) last;
|
||||||
if (arrayStackLevel.hasNext()) {
|
if (arrayStackLevel.hasNext()) {
|
||||||
if (arrayStackLevel.index > 0) {
|
if (arrayStackLevel.index > 0) {
|
||||||
writer.write(",");
|
writer.write(",");
|
||||||
@@ -151,7 +152,8 @@ public class JSONDumper extends ValueDumper {
|
|||||||
endArray();
|
endArray();
|
||||||
stack.pop();
|
stack.pop();
|
||||||
}
|
}
|
||||||
} else if ((objectStackLevel = WorthUtils.dynamicCast(last, ObjectStackLevel.class)) != null) {
|
} else if (last instanceof ObjectStackLevel) {
|
||||||
|
objectStackLevel = (ObjectStackLevel) last;
|
||||||
if (objectStackLevel.hasNext()) {
|
if (objectStackLevel.hasNext()) {
|
||||||
if (objectStackLevel.index > 0) {
|
if (objectStackLevel.index > 0) {
|
||||||
writer.write(",");
|
writer.write(",");
|
||||||
|
@@ -230,8 +230,8 @@ public class JSONParser extends ValueParser {
|
|||||||
}
|
}
|
||||||
} else if (c == '\"') {
|
} else if (c == '\"') {
|
||||||
String text = readString(stream);
|
String text = readString(stream);
|
||||||
ObjectStackLevel osl;
|
StackLevel sl = stack.getFirst();
|
||||||
if ((osl = WorthUtils.dynamicCast(stack.getFirst(), ObjectStackLevel.class)) != null && osl.currentKey == null) {
|
if (sl instanceof ObjectStackLevel && ((ObjectStackLevel) sl).currentKey == null) {
|
||||||
objectKey(text);
|
objectKey(text);
|
||||||
} else {
|
} else {
|
||||||
stringValue(text);
|
stringValue(text);
|
||||||
|
@@ -6,7 +6,7 @@ import net.woggioni.worth.xface.Value;
|
|||||||
|
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
|
|
||||||
import static net.woggioni.worth.utils.WorthUtils.newThrowable;
|
import static net.woggioni.jwo.JWO.newThrowable;
|
||||||
|
|
||||||
class ArrayStackElement<T> extends AbstractStackElement<T> {
|
class ArrayStackElement<T> extends AbstractStackElement<T> {
|
||||||
|
|
||||||
|
@@ -3,7 +3,7 @@ package net.woggioni.worth.traversal;
|
|||||||
import net.woggioni.worth.exception.NotImplementedException;
|
import net.woggioni.worth.exception.NotImplementedException;
|
||||||
import net.woggioni.worth.xface.Value;
|
import net.woggioni.worth.xface.Value;
|
||||||
|
|
||||||
import static net.woggioni.worth.utils.WorthUtils.newThrowable;
|
import static net.woggioni.jwo.JWO.newThrowable;
|
||||||
|
|
||||||
class LeafStackElement<T> extends AbstractStackElement<T> {
|
class LeafStackElement<T> extends AbstractStackElement<T> {
|
||||||
public LeafStackElement(Value value) {
|
public LeafStackElement(Value value) {
|
||||||
|
@@ -10,9 +10,8 @@ import java.util.List;
|
|||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
|
|
||||||
import static net.woggioni.worth.utils.WorthUtils.dynamicCast;
|
import static net.woggioni.jwo.JWO.pop;
|
||||||
import static net.woggioni.worth.utils.WorthUtils.pop;
|
import static net.woggioni.jwo.JWO.tail;
|
||||||
import static net.woggioni.worth.utils.WorthUtils.tail;
|
|
||||||
|
|
||||||
class TraversalContextImpl<T> implements TraversalContext<T> {
|
class TraversalContextImpl<T> implements TraversalContext<T> {
|
||||||
private final List<StackElement<T>> immutableStack;
|
private final List<StackElement<T>> immutableStack;
|
||||||
@@ -29,14 +28,16 @@ class TraversalContextImpl<T> implements TraversalContext<T> {
|
|||||||
@Override
|
@Override
|
||||||
public String getPath() {
|
public String getPath() {
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
for (StackElement se : immutableStack) {
|
for (StackElement<T> se : immutableStack) {
|
||||||
ArrayStackElement ase;
|
ArrayStackElement<T> ase;
|
||||||
ObjectStackElement ose;
|
ObjectStackElement<T> ose;
|
||||||
if ((ase = dynamicCast(se, ArrayStackElement.class)) != null) {
|
if (se instanceof ArrayStackElement) {
|
||||||
|
ase = (ArrayStackElement<T>) se;
|
||||||
sb.append("[");
|
sb.append("[");
|
||||||
sb.append(ase.getCurrentIndex());
|
sb.append(ase.getCurrentIndex());
|
||||||
sb.append("]");
|
sb.append("]");
|
||||||
} else if ((ose = dynamicCast(se, ObjectStackElement.class)) != null) {
|
} else if (se instanceof ObjectStackElement) {
|
||||||
|
ose = (ObjectStackElement<T>) se;
|
||||||
sb.append("[\"");
|
sb.append("[\"");
|
||||||
sb.append(ose.getCurrentKey());
|
sb.append(ose.getCurrentKey());
|
||||||
sb.append("\"]");
|
sb.append("\"]");
|
||||||
@@ -119,12 +120,12 @@ public class ValueWalker {
|
|||||||
private static <T> AbstractStackElement<T> nextChildStackElement(StackElement<T> parent) {
|
private static <T> AbstractStackElement<T> nextChildStackElement(StackElement<T> parent) {
|
||||||
AbstractStackElement<T> result = null;
|
AbstractStackElement<T> result = null;
|
||||||
if (parent instanceof ArrayStackElement) {
|
if (parent instanceof ArrayStackElement) {
|
||||||
ArrayStackElement ase = (ArrayStackElement) parent;
|
ArrayStackElement<T> ase = (ArrayStackElement<T>) parent;
|
||||||
if (ase.hasNext()) {
|
if (ase.hasNext()) {
|
||||||
result = stackElementFromValue(ase.next());
|
result = stackElementFromValue(ase.next());
|
||||||
}
|
}
|
||||||
} else if (parent instanceof ObjectStackElement) {
|
} else if (parent instanceof ObjectStackElement) {
|
||||||
ObjectStackElement ose = (ObjectStackElement) parent;
|
ObjectStackElement<T> ose = (ObjectStackElement<T>) parent;
|
||||||
if (ose.hasNext()) {
|
if (ose.hasNext()) {
|
||||||
result = stackElementFromValue(ose.next());
|
result = stackElementFromValue(ose.next());
|
||||||
}
|
}
|
||||||
|
@@ -122,7 +122,7 @@ public class ListView<T> implements List<T> {
|
|||||||
public T get(int i) {
|
public T get(int i) {
|
||||||
int index = start + i;
|
int index = start + i;
|
||||||
if(end >= 0 && index < end) {
|
if(end >= 0 && index < end) {
|
||||||
throw new IndexOutOfBoundsException(i);
|
throw new IndexOutOfBoundsException(Integer.toString(i));
|
||||||
}
|
}
|
||||||
return delegate.get(start + i);
|
return delegate.get(start + i);
|
||||||
}
|
}
|
||||||
@@ -172,7 +172,7 @@ public class ListView<T> implements List<T> {
|
|||||||
@Override
|
@Override
|
||||||
public ListIterator<T> listIterator(int i) {
|
public ListIterator<T> listIterator(int i) {
|
||||||
if(i < 0 || i > size()) {
|
if(i < 0 || i > size()) {
|
||||||
throw new IndexOutOfBoundsException(0);
|
throw new IndexOutOfBoundsException(Integer.toString(0));
|
||||||
} else {
|
} else {
|
||||||
return new ListViewIterator<>(this, start);
|
return new ListViewIterator<>(this, start);
|
||||||
}
|
}
|
||||||
@@ -181,9 +181,9 @@ public class ListView<T> implements List<T> {
|
|||||||
@Override
|
@Override
|
||||||
public List<T> subList(int i, int i1) {
|
public List<T> subList(int i, int i1) {
|
||||||
if(i < 0) {
|
if(i < 0) {
|
||||||
throw new IndexOutOfBoundsException(0);
|
throw new IndexOutOfBoundsException(Integer.toString(0));
|
||||||
} else if(i1 > size()) {
|
} else if(i1 > size()) {
|
||||||
throw new IndexOutOfBoundsException(i1);
|
throw new IndexOutOfBoundsException(Integer.toString(i1));
|
||||||
} else {
|
} else {
|
||||||
return new ListView<>(delegate, start + i, start + i1);
|
return new ListView<>(delegate, start + i, start + i1);
|
||||||
}
|
}
|
||||||
|
@@ -3,101 +3,11 @@ package net.woggioni.worth.utils;
|
|||||||
import lombok.SneakyThrows;
|
import lombok.SneakyThrows;
|
||||||
import net.woggioni.worth.xface.Value;
|
import net.woggioni.worth.xface.Value;
|
||||||
|
|
||||||
import java.io.*;
|
|
||||||
import java.lang.reflect.Constructor;
|
|
||||||
import java.nio.file.Path;
|
|
||||||
import java.nio.file.Paths;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Optional;
|
|
||||||
import java.util.function.Consumer;
|
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
import java.util.function.Predicate;
|
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
import java.util.stream.Stream;
|
|
||||||
import java.util.stream.StreamSupport;
|
|
||||||
|
|
||||||
public class WorthUtils {
|
public class WorthUtils {
|
||||||
|
|
||||||
public static <T> T dynamicCast(final Object o, final Class<T> cls) {
|
|
||||||
if (cls.isInstance(o)) {
|
|
||||||
return (T) o;
|
|
||||||
} else {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static <T> Stream<T> iterable2stream(Iterable<T> iterable) {
|
|
||||||
return StreamSupport.stream(iterable.spliterator(), false);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void writeObject2File(String fileName, Object o) {
|
|
||||||
writeObject2File(Paths.get(fileName), o);
|
|
||||||
}
|
|
||||||
|
|
||||||
@SneakyThrows
|
|
||||||
public static void writeObject2File(Path file, Object o) {
|
|
||||||
try (Writer writer = new OutputStreamWriter(new FileOutputStream(file.toString()))) {
|
|
||||||
writer.write(o.toString());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@SneakyThrows
|
|
||||||
public static void writeBytes2File(Path file, byte[] bytes) {
|
|
||||||
try (OutputStream os = new FileOutputStream(file.toString())) {
|
|
||||||
os.write(bytes);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@SneakyThrows
|
|
||||||
public static String readFile2String(Path file) {
|
|
||||||
StringBuilder builder = new StringBuilder();
|
|
||||||
try (Reader reader = new InputStreamReader(new BufferedInputStream(new FileInputStream(file.toString())))) {
|
|
||||||
char[] buffer = new char[1024];
|
|
||||||
while (true) {
|
|
||||||
int read = reader.read(buffer);
|
|
||||||
builder.append(buffer, 0, read);
|
|
||||||
if (read < buffer.length) break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return builder.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
@SneakyThrows
|
|
||||||
public static String readResource2String(String classpath) {
|
|
||||||
StringBuilder sb = new StringBuilder();
|
|
||||||
try (Reader reader = new InputStreamReader(WorthUtils.class.getResourceAsStream(classpath))) {
|
|
||||||
char[] buffer = new char[1024];
|
|
||||||
while (true) {
|
|
||||||
int read = reader.read(buffer);
|
|
||||||
sb.append(buffer, 0, read);
|
|
||||||
if (read < buffer.length) break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return sb.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
@SneakyThrows
|
|
||||||
public static <T extends Throwable> T newThrowable(Class<T> cls, String format, Object... args) {
|
|
||||||
Constructor<T> constructor = cls.getConstructor(String.class);
|
|
||||||
return constructor.newInstance(String.format(format, args));
|
|
||||||
}
|
|
||||||
|
|
||||||
@SneakyThrows
|
|
||||||
public static <T extends Throwable> T newThrowable(Class<T> cls, Throwable throwable, String format, Object... args) {
|
|
||||||
Constructor<T> constructor = cls.getConstructor(String.class, Throwable.class);
|
|
||||||
return constructor.newInstance(String.format(format, args), throwable);
|
|
||||||
}
|
|
||||||
|
|
||||||
@SneakyThrows
|
|
||||||
public static <T extends Throwable> void raise(Class<T> cls, Throwable throwable, String format, Object... args) {
|
|
||||||
throw newThrowable(cls, throwable, format, args);
|
|
||||||
}
|
|
||||||
|
|
||||||
@SneakyThrows
|
|
||||||
public static <T extends Throwable> void raise(Class<T> cls, String format, Object... args) {
|
|
||||||
throw newThrowable(cls, format, args);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Value getOrNull(Value root, String... keys) {
|
public static Value getOrNull(Value root, String... keys) {
|
||||||
Value result = root;
|
Value result = root;
|
||||||
for (String key : keys) {
|
for (String key : keys) {
|
||||||
@@ -124,35 +34,4 @@ public class WorthUtils {
|
|||||||
return success.apply(result);
|
return success.apply(result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <T> Predicate<T> not(Predicate<T> p) {
|
|
||||||
return p.negate();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static <V, T> Stream<V> flatMap(Stream<T> stream,
|
|
||||||
Function<? super T, Optional<? extends V>> mappingFunction) {
|
|
||||||
return stream.map(mappingFunction).filter(Optional::isPresent).map(Optional::get);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static <T> Stream<T> optional2Stream(Optional<T> optional) {
|
|
||||||
return optional.map(Stream::of).orElse(Stream.empty());
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void setSystemPropertyIfNotDefined(String key, String value) {
|
|
||||||
if(System.getProperty(key) == null) {
|
|
||||||
System.setProperty(key, value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static <T> T tail(List<T> l) {
|
|
||||||
return tail(l, -1);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static <T> T tail(List<T> l, int offset) {
|
|
||||||
return l.get(l.size() + offset);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static <T> T pop(List<T> l) {
|
|
||||||
return l.remove(l.size() - 1);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@@ -2,7 +2,7 @@ package net.woggioni.worth.value;
|
|||||||
|
|
||||||
import lombok.EqualsAndHashCode;
|
import lombok.EqualsAndHashCode;
|
||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
import net.woggioni.worth.utils.WorthUtils;
|
import static net.woggioni.jwo.JWO.newThrowable;
|
||||||
import net.woggioni.worth.xface.Value;
|
import net.woggioni.worth.xface.Value;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
@@ -29,7 +29,7 @@ public interface ObjectValue extends Value, Iterable<Map.Entry<String, Value>> {
|
|||||||
result = new LinkedHashMapObjectValue();
|
result = new LinkedHashMapObjectValue();
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
throw WorthUtils.newThrowable(IllegalArgumentException.class,
|
throw newThrowable(IllegalArgumentException.class,
|
||||||
"Unknown value of %s: %s",
|
"Unknown value of %s: %s",
|
||||||
Implementation.class.getName(),
|
Implementation.class.getName(),
|
||||||
cfg.objectValueImplementation);
|
cfg.objectValueImplementation);
|
||||||
|
@@ -1,6 +1,7 @@
|
|||||||
package net.woggioni.worth.serialization;
|
package net.woggioni.worth.serialization;
|
||||||
|
|
||||||
import lombok.SneakyThrows;
|
import lombok.SneakyThrows;
|
||||||
|
import net.woggioni.jwo.JWO;
|
||||||
import net.woggioni.worth.serialization.binary.JBONDumper;
|
import net.woggioni.worth.serialization.binary.JBONDumper;
|
||||||
import net.woggioni.worth.serialization.binary.JBONParser;
|
import net.woggioni.worth.serialization.binary.JBONParser;
|
||||||
import net.woggioni.worth.serialization.json.JSONDumper;
|
import net.woggioni.worth.serialization.json.JSONDumper;
|
||||||
@@ -38,7 +39,7 @@ public class ReferenceTest {
|
|||||||
dumper.dump(value, baos);
|
dumper.dump(value, baos);
|
||||||
bytes = baos.toByteArray();
|
bytes = baos.toByteArray();
|
||||||
}
|
}
|
||||||
WorthUtils.writeBytes2File(Paths.get("/tmp/ciao.jbon"), bytes);
|
JWO.writeBytes2File(Paths.get("/tmp/ciao.jbon"), bytes);
|
||||||
Value reparsedValue;
|
Value reparsedValue;
|
||||||
try(ByteArrayInputStream bais = new ByteArrayInputStream(bytes)) {
|
try(ByteArrayInputStream bais = new ByteArrayInputStream(bytes)) {
|
||||||
Parser parser = parserConstructor.apply(cfg);
|
Parser parser = parserConstructor.apply(cfg);
|
||||||
|
@@ -61,8 +61,8 @@ public class JSONTest {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
case ARRAY:
|
case ARRAY:
|
||||||
ArrayValue array = WorthUtils.dynamicCast(value, ArrayValue.class);
|
ArrayValue array;
|
||||||
if (jsonNode.getNodeType() == JsonNodeType.ARRAY && array.size() == jsonNode.size()) {
|
if (jsonNode.getNodeType() == JsonNodeType.ARRAY && (array = (ArrayValue) value).size() == jsonNode.size()) {
|
||||||
for (int i = 0; i < array.size(); i++) {
|
for (int i = 0; i < array.size(); i++) {
|
||||||
if (!compareValueAndJsonNode(array.get(i), jsonNode.get(i))) {
|
if (!compareValueAndJsonNode(array.get(i), jsonNode.get(i))) {
|
||||||
return false;
|
return false;
|
||||||
@@ -73,8 +73,8 @@ public class JSONTest {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
case OBJECT:
|
case OBJECT:
|
||||||
ObjectValue object = WorthUtils.dynamicCast(value, ObjectValue.class);
|
|
||||||
if (jsonNode.getNodeType() == JsonNodeType.OBJECT) {
|
if (jsonNode.getNodeType() == JsonNodeType.OBJECT) {
|
||||||
|
ObjectValue object = (ObjectValue) value;
|
||||||
for (Map.Entry<String, Value> entry : object) {
|
for (Map.Entry<String, Value> entry : object) {
|
||||||
if (!jsonNode.has(entry.getKey())) {
|
if (!jsonNode.has(entry.getKey())) {
|
||||||
return false;
|
return false;
|
||||||
@@ -153,8 +153,9 @@ public class JSONTest {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
case ARRAY:
|
case ARRAY:
|
||||||
ArrayValue array = WorthUtils.dynamicCast(value, ArrayValue.class);
|
ArrayValue array;
|
||||||
if (jsonNode.getNodeType() == JsonNodeType.ARRAY && array.size() == jsonNode.size()) {
|
if (jsonNode.getNodeType() == JsonNodeType.ARRAY &&
|
||||||
|
(array = (ArrayValue) value).size() == jsonNode.size()) {
|
||||||
for (int i = 0; i < array.size(); i++) {
|
for (int i = 0; i < array.size(); i++) {
|
||||||
if (!compareValueAndJsonNode(array.get(i), jsonNode.get(i), cb)) {
|
if (!compareValueAndJsonNode(array.get(i), jsonNode.get(i), cb)) {
|
||||||
return false;
|
return false;
|
||||||
@@ -166,8 +167,8 @@ public class JSONTest {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
case OBJECT:
|
case OBJECT:
|
||||||
ObjectValue object = WorthUtils.dynamicCast(value, ObjectValue.class);
|
|
||||||
if (jsonNode.getNodeType() == JsonNodeType.OBJECT) {
|
if (jsonNode.getNodeType() == JsonNodeType.OBJECT) {
|
||||||
|
ObjectValue object = (ObjectValue) value;
|
||||||
for (Map.Entry<String, Value> entry : object) {
|
for (Map.Entry<String, Value> entry : object) {
|
||||||
if (!jsonNode.has(entry.getKey())) {
|
if (!jsonNode.has(entry.getKey())) {
|
||||||
cb.call(value, jsonNode);
|
cb.call(value, jsonNode);
|
||||||
|
@@ -1,11 +1,12 @@
|
|||||||
package net.woggioni.worth.traversal;
|
package net.woggioni.worth.traversal;
|
||||||
|
|
||||||
import lombok.SneakyThrows;
|
import lombok.SneakyThrows;
|
||||||
|
import net.woggioni.jwo.JWO;
|
||||||
import net.woggioni.worth.serialization.json.JSONParser;
|
import net.woggioni.worth.serialization.json.JSONParser;
|
||||||
import net.woggioni.worth.serialization.json.JSONTest;
|
import net.woggioni.worth.serialization.json.JSONTest;
|
||||||
import net.woggioni.worth.utils.Tuple2;
|
import net.woggioni.worth.utils.Tuple2;
|
||||||
import net.woggioni.worth.utils.WorthUtils;
|
import net.woggioni.worth.value.ArrayValue;
|
||||||
import net.woggioni.worth.value.*;
|
import net.woggioni.worth.value.ObjectValue;
|
||||||
import net.woggioni.worth.xface.Parser;
|
import net.woggioni.worth.xface.Parser;
|
||||||
import net.woggioni.worth.xface.Value;
|
import net.woggioni.worth.xface.Value;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
@@ -41,14 +42,14 @@ public class ValueWalkerTest {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean visitPre(TraversalContext<Void> ctx) {
|
public boolean visitPre(TraversalContext<Void> ctx) {
|
||||||
Value value = WorthUtils.tail(ctx.getStack()).getValue();
|
Value value = JWO.tail(ctx.getStack()).getValue();
|
||||||
preValues.add(value);
|
preValues.add(value);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visitPost(TraversalContext<Void> ctx) {
|
public void visitPost(TraversalContext<Void> ctx) {
|
||||||
Value value = WorthUtils.tail(ctx.getStack()).getValue();
|
Value value = JWO.tail(ctx.getStack()).getValue();
|
||||||
postValues.add(value);
|
postValues.add(value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -57,11 +58,13 @@ public class ValueWalkerTest {
|
|||||||
ObjectValue ov;
|
ObjectValue ov;
|
||||||
ArrayValue av;
|
ArrayValue av;
|
||||||
preResult.add(value);
|
preResult.add(value);
|
||||||
if((av = WorthUtils.dynamicCast(value, ArrayValue.class)) != null) {
|
if(value instanceof ArrayValue) {
|
||||||
|
av = (ArrayValue) value;
|
||||||
for(Value v : av) {
|
for(Value v : av) {
|
||||||
walk(preResult, postResult, v);
|
walk(preResult, postResult, v);
|
||||||
}
|
}
|
||||||
} else if((ov = WorthUtils.dynamicCast(value, ObjectValue.class)) != null) {
|
} else if(value instanceof ObjectValue) {
|
||||||
|
ov = (ObjectValue) value;
|
||||||
for(Map.Entry<String, Value> entry : ov) {
|
for(Map.Entry<String, Value> entry : ov) {
|
||||||
walk(preResult, postResult, entry.getValue());
|
walk(preResult, postResult, entry.getValue());
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user