Commit 983e2bd8 authored by Emeric Verschuur's avatar Emeric Verschuur

Add Variant.clone() method

parent 80ab56d7
......@@ -48,4 +48,13 @@
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
\ No newline at end of file
......@@ -42,7 +42,7 @@ import java.util.Random;
public abstract class Variant implements Comparable<Object> {
public enum Type {
BOOL, BYTE, BYTEARRAY, DATETIME, DOUBLE, SHORT, INT, LIST, LONG, MAP, NULL, STRING, USHORT, UINT, ULONG
VOID, ANY, BOOL, BYTE, BYTEARRAY, DATETIME, DOUBLE, SHORT, INT, LIST, LONG, MAP, NULL, STRING, USHORT, UINT, ULONG
}
public enum Format {
......@@ -51,8 +51,21 @@ public abstract class Variant implements Comparable<Object> {
public static final Variant NULL = new VariantNull();
/**
* Compact JSON format, without white spaces (flag used for serialization)
*/
public static int FORMAT_JSON_COMPACT = 0x00000020;
/**
* Deep copy (flag used for clone method)
*/
public static int DEEP_COPY = 0x00000001;
/**
* Make unmodifiable variant (applicable to cloned maps and lists)
*/
public static int UNMODIFIABLE = 0x00000002;
public static Variant IUD_GENERATOR = new VariantString("") {
private final long MSB = 0x8000000000000000L;
......@@ -374,6 +387,14 @@ public abstract class Variant implements Comparable<Object> {
* @return the type as Type
*/
public abstract Type type();
/**
* Clone this variant (make a copy of)
*
* @param flags
* @return the cloned variant
*/
public abstract Variant clone(int flags);
/**
* Abstract parser
......
......@@ -125,4 +125,9 @@ public class VariantBool extends VariantNumber {
public Type type() {
return Type.BOOL;
}
@Override
public Variant clone(int flags) {
return new VariantBool(data);
}
}
......@@ -49,7 +49,7 @@ public class VariantByte extends VariantNumber {
* Get the byte value using a binary mask
*
* @param mask
* @return
* @return a byte value
*/
public byte byteValue(byte mask) {
return (byte) (data & mask);
......@@ -62,7 +62,7 @@ public class VariantByte extends VariantNumber {
* sub value bit offset
* @param length
* sub value bit length
* @return
* @return a byte value
*/
public byte byteValue(int offset, int length) {
byte mask = 0;
......@@ -156,4 +156,9 @@ public class VariantByte extends VariantNumber {
public Type type() {
return Type.BYTE;
}
@Override
public Variant clone(int flags) {
return new VariantByte(data);
}
}
......@@ -98,4 +98,9 @@ public class VariantByteArray extends Variant {
public boolean isNull() {
return data == null;
}
@Override
public Variant clone(int flags) {
return new VariantByteArray(data);
}
}
......@@ -181,4 +181,9 @@ public class VariantDateTime extends Variant {
public Type type() {
return Type.DATETIME;
}
@Override
public Variant clone(int flags) {
return new VariantDateTime(data);
}
}
......@@ -118,4 +118,9 @@ public class VariantDouble extends VariantNumber {
public Type type() {
return Type.DOUBLE;
}
@Override
public Variant clone(int flags) {
return new VariantDouble(data);
}
}
......@@ -139,4 +139,9 @@ public class VariantInt extends VariantNumber {
public Type type() {
return Type.INT;
}
@Override
public Variant clone(int flags) {
return new VariantInt(data);
}
}
......@@ -21,6 +21,7 @@ import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
......@@ -222,4 +223,21 @@ public class VariantList extends Variant implements List<Variant> {
public Type type() {
return Type.LIST;
}
@Override
public Variant clone(int flags) {
VariantList list;
if ((flags & DEEP_COPY) != 0) {
list = new VariantList();
for (Variant elt: data) {
list.add(elt.clone(flags));
}
} else {
list = new VariantList(data);
}
if ((flags & UNMODIFIABLE) != 0) {
list.data = Collections.unmodifiableList(list.data);
}
return list;
}
}
......@@ -154,4 +154,9 @@ public class VariantLong extends VariantNumber {
public Type type() {
return Type.LONG;
}
@Override
public Variant clone(int flags) {
return new VariantLong(data);
}
}
......@@ -20,6 +20,7 @@ import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Collection;
import java.util.Collections;
import java.util.Dictionary;
import java.util.Enumeration;
import java.util.HashMap;
......@@ -227,4 +228,21 @@ public class VariantMap extends Variant implements Map<String, Variant> {
public VariantList toList() {
return new VariantList(data.values());
}
@Override
public Variant clone(int flags) {
VariantMap list;
if ((flags & DEEP_COPY) != 0) {
list = new VariantMap();
for (Entry<String, Variant> elt: data.entrySet()) {
list.put(elt.getKey(), elt.getValue().clone(flags));
}
} else {
list = new VariantMap(data);
}
if ((flags & UNMODIFIABLE) != 0) {
list.data = Collections.unmodifiableMap(list.data);
}
return list;
}
}
......@@ -119,4 +119,9 @@ public class VariantNull extends VariantNumber {
public String toHexString() {
return "0";
}
@Override
public Variant clone(int flags) {
return new VariantNull();
}
}
......@@ -139,4 +139,9 @@ public class VariantShort extends VariantNumber {
public Type type() {
return Type.INT;
}
@Override
public Variant clone(int flags) {
return new VariantShort(data);
}
}
......@@ -224,4 +224,9 @@ public class VariantString extends Variant {
}
return ret;
}
@Override
public Variant clone(int flags) {
return new VariantString(data);
}
}
......@@ -154,4 +154,9 @@ public class VariantUInt extends VariantNumber {
public Type type() {
return Type.UINT;
}
@Override
public Variant clone(int flags) {
return new VariantUInt(data);
}
}
......@@ -154,4 +154,9 @@ public class VariantULong extends VariantNumber {
public Type type() {
return Type.ULONG;
}
@Override
public Variant clone(int flags) {
return new VariantULong(data);
}
}
......@@ -154,4 +154,9 @@ public class VariantUShort extends VariantNumber {
public Type type() {
return Type.UINT;
}
@Override
public Variant clone(int flags) {
return new VariantUShort(data);
}
}
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.mbedsys.jvar.Variant;
public class Test {
public static void main(String[] args) {
try {
Variant value = Variant.newParser(new FileInputStream("rs/test.json"), Variant.Format.JSON).next();
// Variant value = Variant.newParser(new FileInputStream("rs/test.bson"), Variant.Format.BSON).next();
// Variant value = Variant.newParser(new FileInputStream("rs/test.bcon"), Variant.Format.BCON).next();
Variant.serializeBCON(new FileOutputStream("rs/test_out.bcon"), value);
Variant.serializeBSON(new FileOutputStream("rs/test_out.bson"), value);
System.out.println(value);
} catch (IOException e) {
e.printStackTrace();
}
}
}
package org.mbedsys;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* Unit test for simple App.
*/
public class VariantTest extends TestCase {
/**
* Create the test case
*
* @param testName
* name of the test case
*/
public VariantTest(String testName) {
super(testName);
}
/**
* @return the suite of tests being tested
*/
public static Test suite() {
return new TestSuite(VariantTest.class);
}
public void test() throws Exception {
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment