Commit f8a7125f authored by Emeric Verschuur's avatar Emeric Verschuur

Variant implementation

parent 56bb708d
......@@ -16,6 +16,11 @@
package org.mbedsys.jvar;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;
/**
......@@ -41,7 +46,9 @@ public abstract class Variant implements Comparable<Object> {
STRING,
UINT,
ULONG
};
}
public static final Variant NULL = new VariantNull();
public static int FORMAT_JSON_COMPACT = 0x00000020;
......@@ -281,4 +288,113 @@ public abstract class Variant implements Comparable<Object> {
* @return the type as Type
*/
public abstract Type type();
/**
* Write the value in JSON format
*
* @param writer output stream writer
* @throws IOException
*/
private static void serializeJSONList(OutputStreamWriter writer, List<Variant> list, int flags) throws IOException {
boolean conpact = (flags & FORMAT_JSON_COMPACT) != 0;
int indentStep = flags & JSON_INDENT_MASK;
int indentOff = (flags >> 16) + indentStep;
flags = (flags & 0xFFFF) | (indentOff << 16);
Iterator<Variant> it = list.iterator();
writer.write('[');
if (it.hasNext()) {
if (indentOff != 0) {
writer.write('\n');
appendSpaces(writer, indentOff);
}
serializeJSONElt(writer, it.next(), flags);
while (it.hasNext()) {
writer.write(',');
if (indentOff != 0) {
writer.write('\n');
appendSpaces(writer, indentOff);
} else if (!conpact) {
writer.write(' ');
}
serializeJSONElt(writer, it.next(), flags);
}
if (indentOff != 0) {
writer.write('\n');
appendSpaces(writer, indentOff - indentStep);
}
}
writer.write(']');
}
/**
* Write the value in JSON format
*
* @param writer output stream writer
* @throws IOException
*/
public static void serializeJSONMap(OutputStreamWriter writer, Map<String, Variant> map, int flags) throws IOException {
Iterator<String> keys = map.keySet().iterator();
boolean conpact = (flags & FORMAT_JSON_COMPACT) != 0;
int indentStep = flags & JSON_INDENT_MASK;
int indentOff = (flags >> 16) + indentStep;
flags = (flags & 0xFFFF) | (indentOff << 16);
String key;
writer.write('{');
if (keys.hasNext()) {
if (indentOff != 0) {
writer.write('\n');
appendSpaces(writer, indentOff);
}
key = keys.next();
VariantString.writeJSONTo(writer, key);
writer.write(':');
if (!conpact) {
writer.write(' ');
}
serializeJSONElt(writer, map.get(key), flags);
while (keys.hasNext()) {
key = keys.next();
writer.write(',');
if (indentOff != 0) {
writer.write('\n');
appendSpaces(writer, indentOff);
} else if (!conpact) {
writer.write(' ');
}
VariantString.writeJSONTo(writer, key);
writer.write(':');
if (!conpact) {
writer.write(' ');
}
serializeJSONElt(writer, map.get(key), flags);
}
if (indentOff != 0) {
writer.write('\n');
appendSpaces(writer, indentOff - indentStep);
}
}
writer.write('}');
}
protected static void appendSpaces(OutputStreamWriter writer, int cout) {
// TODO Auto-generated method stub
}
protected static void serializeJSONElt(OutputStreamWriter writer,
Variant next, int flags) {
// TODO Auto-generated method stub
}
protected static void serializeJSON(OutputStreamWriter writer,
VariantList variantList, int i) {
// TODO Auto-generated method stub
}
protected static String[] split(String path, char sep) {
// TODO Auto-generated method stub
return null;
}
}
......@@ -19,8 +19,8 @@ package org.mbedsys.jvar;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
......@@ -30,245 +30,186 @@ import java.util.ListIterator;
* @author <a href="mailto:emericv@mbedsys.org">Emeric Verschuur</a>
* Copyright 2014 MbedSYS
*/
public class VariantList implements Variant, List<Variant> {
public class VariantList extends Variant implements List<Variant> {
List<Variant> data;
public VariantList(List emptyList) {
// TODO Auto-generated constructor stub
public VariantList(List<Variant> value) {
if (value == null)
throw new IllegalArgumentException("value argument cannot be null");
data = value;
}
/**
* Write the value in JSON format
*
* @param writer output stream writer
* @throws IOException
*/
@Override
public void writeJSONTo(OutputStreamWriter writer, int flags) throws IOException {
boolean conpact = (flags & FORMAT_JSON_COMPACT) != 0;
int indentStep = flags & JSON_INDENT_MASK;
int indentOff = (flags >> 16) + indentStep;
flags = (flags & 0xFFFF) | (indentOff << 16);
Iterator<Variant> it = data.iterator();
writer.write('[');
if (it.hasNext()) {
if (indentOff != 0) {
writer.write('\n');
Utils.appendSpaces(writer, indentOff);
}
it.next().writeJSONTo(writer, flags);
while (it.hasNext()) {
writer.write(',');
if (indentOff != 0) {
writer.write('\n');
Utils.appendSpaces(writer, indentOff);
} else if (!conpact) {
writer.write(' ');
}
it.next().writeJSONTo(writer, flags);
}
if (indentOff != 0) {
writer.write('\n');
Utils.appendSpaces(writer, indentOff - indentStep);
}
}
writer.write(']');
public VariantList add(boolean value) {
data.add(new VariantBool(value));
return this;
}
public VariantList add(boolean value) {
data.add(new BooleanVariant(value));
public VariantList add(double value) {
data.add(new VariantDouble(value));
return this;
}
public VariantList add(int value) {
data.add(new IntegerVariant(value));
data.add(new VariantInt(value));
return this;
}
@Override
public void add(int index, Variant element) {
data.add(index, element);
}
public VariantList add(long value) {
data.add(new LongVariant(value));
data.add(new VariantLong(value));
return this;
}
public VariantList add(double value) {
data.add(new DoubleVariant(value));
public VariantList add(String value) {
data.add(new VariantString(value));
return this;
}
public VariantList add_(Object value) {
if (value == null) {
data.add(NullVariant.NULL);
} else if (value instanceof Variant) {
data.add((Variant) value);
} else if (value instanceof String) {
data.add(new StringVariant((String) value));
} else {
data.add(new GenericVariant(value));
}
public VariantList add(Date value) {
data.add(new VariantDateTime(value));
return this;
}
@Override
public String toString_() {
ByteArrayOutputStream output = new ByteArrayOutputStream();
OutputStreamWriter writer = new OutputStreamWriter(output);
try {
writeJSONTo(writer, 4);
writer.flush();
} catch (IOException e) {
System.out.println("AbstractVariant.toJSONString()\n"
+ PfException.getStackTraceToString(e));
}
return new String(output.toByteArray());
public boolean add(Variant e) {
return data.add(e);
}
@Override
public final String toJSONString_() {
ByteArrayOutputStream output = new ByteArrayOutputStream();
OutputStreamWriter writer = new OutputStreamWriter(output);
try {
writeJSONTo(writer, FORMAT_JSON_COMPACT);
writer.flush();
} catch (IOException e) {
throw new Error(e);
}
return new String(output.toByteArray());
@Override
public boolean addAll(Collection<? extends Variant> c) {
return data.addAll(c);
}
@Override
public int size() {
// TODO Auto-generated method stub
return 0;
public boolean addAll(int index, Collection<? extends Variant> c) {
return data.addAll(index, c);
}
@Override
public boolean isEmpty() {
// TODO Auto-generated method stub
return false;
public void clear() {
data.clear();
}
@Override
public int compareTo(Object o) {
throw new UnsupportedOperationException("VariantList cannot be compared to an other Variant");
}
@Override
public boolean contains(Object o) {
// TODO Auto-generated method stub
return false;
return data.contains(o);
}
@Override
public Iterator<Variant> iterator() {
// TODO Auto-generated method stub
return null;
public boolean containsAll(Collection<?> c) {
return data.containsAll(c);
}
@Override
public Object[] toArray() {
// TODO Auto-generated method stub
return null;
public Variant get(int index) {
return data.get(index);
}
@Override
public <T> T[] toArray(T[] a) {
// TODO Auto-generated method stub
return null;
public int indexOf(Object o) {
return data.indexOf(o);
}
@Override
public boolean add(Variant e) {
// TODO Auto-generated method stub
return false;
public boolean isEmpty() {
return data.isEmpty();
}
@Override
public boolean remove(Object o) {
// TODO Auto-generated method stub
public boolean isNull() {
return false;
}
@Override
public boolean containsAll(Collection<?> c) {
// TODO Auto-generated method stub
return false;
public Iterator<Variant> iterator() {
return data.iterator();
}
@Override
public boolean addAll(Collection<? extends Variant> c) {
// TODO Auto-generated method stub
return false;
public int lastIndexOf(Object o) {
return data.lastIndexOf(o);
}
@Override
public boolean addAll(int index, Collection<? extends Variant> c) {
// TODO Auto-generated method stub
return false;
public ListIterator<Variant> listIterator() {
return data.listIterator();
}
@Override
public boolean removeAll(Collection<?> c) {
// TODO Auto-generated method stub
return false;
public ListIterator<Variant> listIterator(int index) {
return data.listIterator(index);
}
@Override
public boolean retainAll(Collection<?> c) {
// TODO Auto-generated method stub
return false;
public Variant remove(int index) {
return data.remove(index);
}
@Override
public void clear() {
// TODO Auto-generated method stub
public boolean remove(Object o) {
return data.remove(o);
}
@Override
public Variant get(int index) {
// TODO Auto-generated method stub
return null;
public boolean removeAll(Collection<?> c) {
return data.removeAll(c);
}
@Override
public Variant set(int index, Variant element) {
// TODO Auto-generated method stub
return null;
public boolean retainAll(Collection<?> c) {
return data.retainAll(c);
}
@Override
public void add(int index, Variant element) {
// TODO Auto-generated method stub
public Variant set(int index, Variant element) {
return data.set(index, element);
}
@Override
public Variant remove(int index) {
// TODO Auto-generated method stub
return null;
public int size() {
return data.size();
}
@Override
public int indexOf(Object o) {
// TODO Auto-generated method stub
return 0;
public List<Variant> subList(int fromIndex, int toIndex) {
return data.subList(fromIndex, toIndex);
}
@Override
public int lastIndexOf(Object o) {
// TODO Auto-generated method stub
return 0;
public Object[] toArray() {
return data.toArray();
}
@Override
public ListIterator<Variant> listIterator() {
// TODO Auto-generated method stub
return null;
public <T> T[] toArray(T[] a) {
return data.toArray(a);
}
@Override
public ListIterator<Variant> listIterator(int index) {
// TODO Auto-generated method stub
return null;
public String toString() {
ByteArrayOutputStream output = new ByteArrayOutputStream();
OutputStreamWriter writer = new OutputStreamWriter(output);
try {
serializeJSON(writer, this, 4);
writer.flush();
} catch (IOException e) {
throw new RuntimeException(e);
}
return new String(output.toByteArray());
}
@Override
public List<Variant> subList(int fromIndex, int toIndex) {
// TODO Auto-generated method stub
return null;
public Type type() {
return Type.LIST;
}
}
......@@ -23,15 +23,15 @@ import java.util.Collection;
import java.util.Dictionary;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
/**
*
* @author <a href="mailto:emericv@mbedsys.org">Emeric Verschuur</a>
* Copyright 2014 MbedSYS
*/
public class VariantMap implements Variant, Map<String, Variant> {
public class VariantMap extends Variant implements Map<String, Variant> {
private HashMap<String, Variant> data;
......@@ -48,59 +48,9 @@ public class VariantMap implements Variant, Map<String, Variant> {
data = new HashMap<>();
}
public VariantMap(Map<String, Variant> properties) {
data = new HashMap<>(properties);
}
/**
* Write the value in JSON format
*
* @param writer output stream writer
* @throws IOException
*/
@Override
public void writeJSONTo(OutputStreamWriter writer, int flags) throws IOException {
Iterator<String> keys = data.keySet().iterator();
boolean conpact = (flags & FORMAT_JSON_COMPACT) != 0;
int indentStep = flags & JSON_INDENT_MASK;
int indentOff = (flags >> 16) + indentStep;
flags = (flags & 0xFFFF) | (indentOff << 16);
String key;
writer.write('{');
if (keys.hasNext()) {
if (indentOff != 0) {
writer.write('\n');
appendSpaces(writer, indentOff);
}
key = keys.next();
VariantString.writeJSONTo(writer, key);
writer.write(':');
if (!conpact) {
writer.write(' ');
}
data.get(key).writeJSONTo(writer, flags);
while (keys.hasNext()) {
key = keys.next();
writer.write(',');
if (indentOff != 0) {
writer.write('\n');
appendSpaces(writer, indentOff);
} else if (!conpact) {
writer.write(' ');
}
VariantString.writeJSONTo(writer, key);
writer.write(':');
if (!conpact) {
writer.write(' ');
}
data.get(key).writeJSONTo(writer, flags);
}
if (indentOff != 0) {
writer.write('\n');
appendSpaces(writer, indentOff - indentStep);
}
}
writer.write('}');
public VariantMap(Map<String, Variant> value) {
if (value == null)
throw new IllegalArgumentException("value argument cannot be null");
}
@Override
......@@ -130,46 +80,32 @@ public class VariantMap implements Variant, Map<String, Variant> {
public VariantMap put(String key, Object value) {
if (value == null) {
data.put(key, NullVariant.NULL);
data.put(key, Variant.NULL);
} else if (value instanceof Variant) {
data.put(key, (Variant) value);
} else if (value instanceof String) {
data.put(key, new VariantString((String) value));
} else {
data.put(key, new GenericVariant(value));
throw new IllegalArgumentException("Unsupported value type");
}
return this;
}
@Override
public String toString_() {
ByteArrayOutputStream output = new ByteArrayOutputStream();
OutputStreamWriter writer = new OutputStreamWriter(output);
try {
writeJSONTo(writer, 4);
writer.flush();
} catch (IOException e) {
System.out.println("AbstractVariant.toJSONString()\n"
+ PfException.getStackTraceToString(e));
}
return new String(output.toByteArray());
}
public static Variant resolvePath(Variant data, String path) {
public static Variant resolvePath(VariantMap data, String path) {
return resolvePath(data, path, '.');
}
public static Variant resolvePath(Variant data, String path, char sep) {
for (String key: Utils.split(path, sep)) {
public static VariantMap resolvePath(VariantMap data, String path, char sep) {
for (String key: split(path, sep)) {
if (!data.containsKey(key)) {
return null;
}
data = data.get(key);
data = data.get(key).toMap();
}
return data;
}
private static void updatePath(VariantMap node, String[] pathParts, Variant value, int i) {
private static void updatePath(VariantMap node, String[] pathParts, Variant value, int i) {
String k = pathParts[i++];
if (i == pathParts.length) {
if (value == null) {
......@@ -195,18 +131,88 @@ public class VariantMap implements Variant, Map<String, Variant> {
}
public static Variant updatePath(Variant data, String path, Variant value) {
return updatePath(data, Utils.split(path, '.'), value);
return updatePath(data, split(path, '.'), value);
}
@Override
public final String toJSONString_() {
@Override
public int compareTo(Object o) {
throw new UnsupportedOperationException("VariantMap cannot be compared to an other Variant");
}
@Override
public int size() {
return data.size();
}
@Override
public boolean containsKey(Object key) {
return data.containsKey(key);
}
@Override
public boolean containsValue(Object value) {
return data.containsValue(value);
}
@Override
public Variant get(Object key) {
return data.get(key);
}
@Override
public Variant put(String key, Variant value) {
return data.put(key, value);
}
@Override
public Variant remove(Object key) {
return data.remove(key);
}
@Override
public void putAll(Map<? extends String, ? extends Variant> m) {
data.putAll(m);
}
@Override
public void clear() {
data.clear();
}
@Override
public Set<String> keySet() {
return data.keySet();
}
@Override
public Collection<Variant> values() {
return data.values();
}
@Override
public Set<java.util.Map.Entry<String, Variant>> entrySet() {
return data.entrySet();
}
@Override
public boolean isEmpty() {
return data.isEmpty();
}
@Override
public boolean isNull() {
return false;
}
@Override
public String toString() {
ByteArrayOutputStream output = new ByteArrayOutputStream();
OutputStreamWriter writer = new OutputStreamWriter(output);
try {
writeJSONTo(writer, FORMAT_JSON_COMPACT);
serializeJSONElt(writer, this, 4);
writer.flush();
} catch (IOException e) {
throw new Error(e);
throw new RuntimeException(e);
}
return new String(output.toByteArray());
}
......
......@@ -21,212 +21,136 @@ import java.io.OutputStreamWriter;
/**
*
* @author <a href="mailto:emericv@mbedsys.org">Emeric Verschuur</a>
* Copyright 2014 MbedSYS
* @author <a href="mailto:emericv@mbedsys.org">Emeric Verschuur</a> Copyright