added system property to switch object value implementation

This commit is contained in:
Walter Oggioni
2019-07-16 16:56:07 +02:00
committed by Walter Oggioni
parent 7f50efb4dc
commit a2e5e669f1
13 changed files with 108 additions and 57 deletions

View File

@@ -24,7 +24,7 @@ public abstract class ValueDumper implements Dumper {
}
protected static class ArrayStackLevel extends StackLevel implements Iterator<Value> {
private final Iterator<Value> iterator = value.asArray().iterator();
private final Iterator<Value> iterator = ((ArrayValue) value).iterator();
@Override
public Value next() {
@@ -43,7 +43,7 @@ public abstract class ValueDumper implements Dumper {
}
protected static class ObjectStackLevel extends StackLevel implements Iterator<Map.Entry<String, Value>> {
private final Iterator<Map.Entry<String, Value>> iterator = value.asObject().entrySet().iterator();
private final Iterator<Map.Entry<String, Value>> iterator = ((ObjectValue) value).iterator();
@Override
public Map.Entry<String, Value> next() {

View File

@@ -4,8 +4,8 @@ import lombok.RequiredArgsConstructor;
import net.woggioni.worth.exception.NotImplementedException;
import net.woggioni.worth.utils.WorthUtils;
import net.woggioni.worth.value.*;
import net.woggioni.worth.xface.Value;
import net.woggioni.worth.xface.Parser;
import net.woggioni.worth.xface.Value;
import java.io.InputStream;
import java.io.InputStreamReader;

View File

@@ -2,13 +2,13 @@ package net.woggioni.worth.serialization.binary;
import lombok.SneakyThrows;
import net.woggioni.worth.exception.NotImplementedException;
import net.woggioni.worth.serialization.ValueDumper;
import net.woggioni.worth.utils.Leb128;
import net.woggioni.worth.utils.WorthUtils;
import net.woggioni.worth.value.ArrayValue;
import net.woggioni.worth.value.ObjectValue;
import net.woggioni.worth.xface.Value;
import net.woggioni.worth.serialization.ValueDumper;
import net.woggioni.worth.xface.Dumper;
import net.woggioni.worth.xface.Value;
import java.io.OutputStream;
import java.io.Writer;

View File

@@ -1,13 +1,13 @@
package net.woggioni.worth.serialization.binary;
import lombok.SneakyThrows;
import net.woggioni.worth.utils.Leb128;
import net.woggioni.worth.utils.WorthUtils;
import net.woggioni.worth.xface.Value;
import net.woggioni.worth.buffer.LookAheadInputStream;
import net.woggioni.worth.exception.ParseException;
import net.woggioni.worth.serialization.ValueParser;
import net.woggioni.worth.utils.Leb128;
import net.woggioni.worth.utils.WorthUtils;
import net.woggioni.worth.xface.Parser;
import net.woggioni.worth.xface.Value;
import java.io.InputStream;
import java.util.function.Function;

View File

@@ -1,12 +1,12 @@
package net.woggioni.worth.serialization.json;
import lombok.SneakyThrows;
import net.woggioni.worth.serialization.ValueDumper;
import net.woggioni.worth.utils.WorthUtils;
import net.woggioni.worth.value.ArrayValue;
import net.woggioni.worth.value.ObjectValue;
import net.woggioni.worth.xface.Value;
import net.woggioni.worth.serialization.ValueDumper;
import net.woggioni.worth.xface.Dumper;
import net.woggioni.worth.xface.Value;
import java.io.OutputStream;
import java.io.OutputStreamWriter;

View File

@@ -1,7 +1,6 @@
package net.woggioni.worth.serialization.json;
import lombok.SneakyThrows;
import net.woggioni.worth.xface.Value;
import net.woggioni.worth.buffer.LookAheadTextInputStream;
import net.woggioni.worth.exception.IOException;
import net.woggioni.worth.exception.NotImplementedException;
@@ -9,6 +8,7 @@ import net.woggioni.worth.exception.ParseException;
import net.woggioni.worth.serialization.ValueParser;
import net.woggioni.worth.utils.WorthUtils;
import net.woggioni.worth.xface.Parser;
import net.woggioni.worth.xface.Value;
import java.io.InputStream;
import java.io.InputStreamReader;

View File

@@ -4,6 +4,7 @@ import lombok.EqualsAndHashCode;
import net.woggioni.worth.xface.Value;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
@@ -59,10 +60,9 @@ public class ArrayValue implements Value, Iterable<Value> {
@Override
public List<Value> asArray() {
return value;
return Collections.unmodifiableList(value);
}
@Override
public Iterator<Value> iterator() {
return value.iterator();

View File

@@ -2,29 +2,48 @@ package net.woggioni.worth.value;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import net.woggioni.worth.utils.WorthUtils;
import net.woggioni.worth.xface.Value;
import java.util.*;
public interface ObjectValue extends Value, Iterable<Map.Entry<String, Value>> {
boolean listBasedImplementation = Boolean.valueOf(
System.getProperty(ObjectValue.class.getName() + ".listBasedImplementation", "false"));
boolean preserveKeyOrder = Boolean.valueOf(
System.getProperty(ObjectValue.class.getName() + ".preserveKeyOrder", "false"));
Implementation implementation = Implementation.valueOf(
System.getProperty(ObjectValue.class.getName() + ".implementation", "TreeMap"));
static ObjectValue newInstance() {
if (listBasedImplementation) {
return new ListObjectValue();
} else {
return new MapObjectValue();
ObjectValue result;
switch(implementation) {
case ArrayList:
result = new ListObjectValue();
break;
case TreeMap:
result = new TreeMapObjectValue();
break;
case HashMap:
result = new HashMapObjectValue();
break;
case LinkedHashMap:
result = new LinkedHashMapObjectValue();
break;
default:
throw WorthUtils.newThrowable(IllegalArgumentException.class,
"Unknown value of %s: %s",
Implementation.class.getName(),
implementation);
}
return result;
}
@Override
default Type type() {
return Type.OBJECT;
}
enum Implementation {
ArrayList, TreeMap, HashMap, LinkedHashMap
}
}
final class ObjectEntry<K, V> implements Map.Entry<K, V> {
@@ -55,14 +74,10 @@ final class ObjectEntry<K, V> implements Map.Entry<K, V> {
}
@EqualsAndHashCode
class MapObjectValue implements ObjectValue {
abstract class MapObjectValue implements ObjectValue {
private final Map<String, Value> value;
public MapObjectValue() {
this.value = ObjectValue.preserveKeyOrder ? new LinkedHashMap<>() : new HashMap<>();
}
public MapObjectValue(Map<String, Value> value) {
this.value = value;
}
@@ -114,6 +129,31 @@ class MapObjectValue implements ObjectValue {
}
}
@EqualsAndHashCode
class HashMapObjectValue extends MapObjectValue {
public HashMapObjectValue() {
super(new HashMap<>());
}
}
@EqualsAndHashCode
class LinkedHashMapObjectValue extends MapObjectValue {
public LinkedHashMapObjectValue() {
super(new LinkedHashMap<>());
}
}
@EqualsAndHashCode
class TreeMapObjectValue extends MapObjectValue {
public TreeMapObjectValue() {
super(new TreeMap<>());
}
}
@NoArgsConstructor
@EqualsAndHashCode
class ListObjectValue implements ObjectValue {
@@ -126,7 +166,7 @@ class ListObjectValue implements ObjectValue {
@Override
public Map<String, Value> asObject() {
Map<String, Value> result = preserveKeyOrder ? new LinkedHashMap<>() : new HashMap<>();
Map<String, Value> result = new LinkedHashMap<>();
for (Map.Entry<String, Value> entry : value) {
result.put(entry.getKey(), entry.getValue());
}