Commit 56bb708d authored by Emeric Verschuur's avatar Emeric Verschuur

Variants refactoring

parent 26d51543
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.mbedsys</groupId>
<artifactId>vxon</artifactId>
<version>1.0</version>
<groupId>org.mbedsys</groupId>
<artifactId>jvar</artifactId>
<version>1.0</version>
<properties>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<organization>
<name>MbedSYS</name>
<url>http://mbedsys.org/</url>
</organization>
</project>
\ No newline at end of file
......@@ -22,6 +22,28 @@ package org.mbedsys.jvar;
* Copyright 2014 MbedSYS
*/
public class KeyNotFoundException extends RuntimeException {
public KeyNotFoundException(String key) {
/**
*
*/
private static final long serialVersionUID = 1L;
public KeyNotFoundException() {
super();
}
public KeyNotFoundException(String message, Throwable cause,
boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
public KeyNotFoundException(String message, Throwable cause) {
super(message, cause);
}
public KeyNotFoundException(Throwable cause) {
super(cause);
}
public KeyNotFoundException(String key) {
}
}
......@@ -19,7 +19,7 @@ package org.mbedsys.jvar;
import java.util.Random;
/**
* The Variant interface describes an object which acts like a union for the most
* The Variant abstract class describes an object which acts like a union for the most
* common data types.
*
* @author <a href="mailto:emericv@mbedsys.org">Emeric Verschuur</a>
......@@ -28,42 +28,42 @@ import java.util.Random;
public abstract class Variant implements Comparable<Object> {
public enum Type {
NULL,
BOOL,
MAP,
LIST,
BYTEARRAY,
STRING,
BOOL,
BYTE,
BYTEARRAY,
DATETIME,
DOUBLE,
INT,
LIST,
LONG,
MAP,
NULL,
STRING,
UINT,
ULONG,
DOUBLE,
DATETIME
ULONG
};
public static int FORMAT_JSON_COMPACT = 0x00000020;
public static int JSON_INDENT_MASK = 0x0000001F;
public static Variant IUD_GENERATOR = new VariantString() {
private volatile Random numberGenerator = new Random();
public static Variant IUD_GENERATOR = new VariantString("") {
private final long MSB = 0x8000000000000000L;
@Override
public Type type() {
return Type.STRING;
}
private volatile Random numberGenerator = new Random();
@Override
public String toString() {
return VariantNumber.toHexString(MSB | numberGenerator.nextLong())
+ VariantNumber.toHexString(MSB | numberGenerator.nextLong());
}
@Override
public Type type() {
return Type.STRING;
}
};
public static int JSON_INDENT_MASK = 0x0000001F;
/**
* Returns the variant as a boolean.
*
......@@ -100,22 +100,39 @@ public abstract class Variant implements Comparable<Object> {
}
/**
* Returns the variant as a short.
* Returns the variant as a double.
*
* Applicable to a number or string variant
*
* @return the variant as a short if the variant has {@link #type()} Double,
* @return the variant as a double if the variant has {@link #type()}
* Double, Float, Boolean, Integer, Long and String if the last one
* contains a textual representation (decimal, octal or binary
* format) of a double.
* @throws NumberFormatException
* - if the string does not contain a parsable double.
* @throws UnsupportedOperationException
* - if the convention is not possible.
*/
public double doubleValue() {
throw new UnsupportedOperationException("A variant with type " + type() + " cannot be converted to a double");
}
/**
* Returns the variant as a float.
*
* Applicable to a number or string variant
*
* @return the variant as a float if the variant has {@link #type()} Double,
* Float, Boolean, Integer, Long and String if the last one contains
* a textual representation (decimal, octal or binary format) of a
* short. Otherwise throws an instance of VariantFormatException
* runtime exception.
* float.
* @throws NumberFormatException
* - if the string does not contain a parsable byte.
* - if the string does not contain a parsable float.
* @throws UnsupportedOperationException
* - if the convention is not possible.
*/
public short shortValue() {
throw new UnsupportedOperationException("A variant with type " + type() + " cannot be converted to a short");
public float floatValue() {
throw new UnsupportedOperationException("A variant with type " + type() + " cannot be converted to a float");
}
/**
......@@ -136,6 +153,24 @@ public abstract class Variant implements Comparable<Object> {
throw new UnsupportedOperationException("A variant with type " + type() + " cannot be converted to an integer");
}
/**
* Get if the variant is empty
*
* Applicable to all the variant types
*
* @return true if the variant is empty, otherwise false
*/
public abstract boolean isEmpty();
/**
* Get if the variant is null/undefined
*
* Applicable to all the variant types
*
* @return true if the variant is null/undefined, otherwise false
*/
public abstract boolean isNull();
/**
* Returns the variant as a long.
*
......@@ -155,51 +190,24 @@ public abstract class Variant implements Comparable<Object> {
}
/**
* Returns the variant as a float.
* Returns the variant as a short.
*
* Applicable to a number or string variant
*
* @return the variant as a float if the variant has {@link #type()} Double,
* @return the variant as a short if the variant has {@link #type()} Double,
* Float, Boolean, Integer, Long and String if the last one contains
* a textual representation (decimal, octal or binary format) of a
* float.
* @throws NumberFormatException
* - if the string does not contain a parsable float.
* @throws UnsupportedOperationException
* - if the convention is not possible.
*/
public float floatValue() {
throw new UnsupportedOperationException("A variant with type " + type() + " cannot be converted to a float");
}
/**
* Returns the variant as a double.
*
* Applicable to a number or string variant
*
* @return the variant as a double if the variant has {@link #type()}
* Double, Float, Boolean, Integer, Long and String if the last one
* contains a textual representation (decimal, octal or binary
* format) of a double.
* short. Otherwise throws an instance of VariantFormatException
* runtime exception.
* @throws NumberFormatException
* - if the string does not contain a parsable double.
* - if the string does not contain a parsable byte.
* @throws UnsupportedOperationException
* - if the convention is not possible.
*/
public double doubleValue() {
throw new UnsupportedOperationException("A variant with type " + type() + " cannot be converted to a double");
public short shortValue() {
throw new UnsupportedOperationException("A variant with type " + type() + " cannot be converted to a short");
}
/**
* Returns the variant as a String.
*
* Applicable to all the variant types
*
* @return the variant as a String
*/
@Override
public abstract String toString();
/**
* Returns the variant as a byte array.
*
......@@ -213,23 +221,23 @@ public abstract class Variant implements Comparable<Object> {
/**
* Get the original object reference.
*
* Applicable to all the variant types
*
*
* Applicable to type List and Map
*
* @return an Object
*/
public VariantMap toMap() {
public VariantList toList() {
throw new UnsupportedOperationException("A variant with type " + type() + " cannot be converted to a Variant");
}
/**
* Get the original object reference.
*
* Applicable to type List and Map
*
*
* Applicable to all the variant types
*
* @return an Object
*/
public VariantList toList() {
public VariantMap toMap() {
throw new UnsupportedOperationException("A variant with type " + type() + " cannot be converted to a Variant");
}
......@@ -245,22 +253,25 @@ public abstract class Variant implements Comparable<Object> {
}
/**
* Get if the variant is empty
*
* Applicable to all the variant types
*
* @return true if the variant is empty, otherwise false
* Get variant as a VariantDate
*
* Applicable to types: (U)Int, (U)Long, Double, Byte and Null
*
* @return an Object
*/
public abstract boolean isEmpty();
public VariantDateTime toDate() {
throw new UnsupportedOperationException("A variant with type " + type() + " cannot be converted to a VariantNumber");
}
/**
* Get if the variant is null/undefined
* Returns the variant as a String.
*
* Applicable to all the variant types
*
* @return true if the variant is null/undefined, otherwise false
* @return the variant as a String
*/
public abstract boolean isNull();
@Override
public abstract String toString();
/**
* Return the variant type
......
......@@ -16,60 +16,30 @@
package org.mbedsys.jvar;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.text.Format;
/**
* Byte Variant
*
* @author <a href="mailto:emericv@mbedsys.org">Emeric Verschuur</a>
* Copyright 2014 MbedSYS
* @author <a href="mailto:emericv@mbedsys.org">Emeric Verschuur</a> Copyright
* 2014 MbedSYS
*/
public class VariantByte extends VariantNumber implements Comparable<Object> {
protected byte data;
/**
* Default constructor.
*/
public VariantByte() {
}
public class VariantByte extends VariantNumber {
/**
* Constructor from byte array input stream
*
* @param input ByteArrayInputStream
*/
public VariantByte(ByteArrayInputStream input) {
data = (byte) input.read();
}
protected byte data;
/**
* Constructor from value
* Byte variant constructor from value
*
* @param value
* @param value initial value
*/
public VariantByte(byte value) {
data = value;
}
/**
* Get boolean value
*
* @return a boolean
*/
@Override
public boolean booleanValue() {
return data != 0;
}
/**
* Get byte value
*
* @return a byte
*/
@Override
public byte byteValue() {
return data;
......@@ -82,102 +52,51 @@ public class VariantByte extends VariantNumber implements Comparable<Object> {
* @return
*/
public byte byteValue(byte mask) {
return (byte)(data & mask);
return (byte) (data & mask);
}
/**
* Get the sub value
*
* @param offset sub value bit offset
* @param length sub value bit length
* @param offset
* sub value bit offset
* @param length
* sub value bit length
* @return
*/
public byte byteValue(int offset, int length) {
byte mask = 0;
for(int i=0; i < length; i++) {
for (int i = 0; i < length; i++) {
mask |= 1 << (offset + i);
}
return (byte)((data & mask) >> offset);
return (byte) ((data & mask) >> offset);
}
/**
* Get the short integer value
*
* @return an integer
*/
@Override
public short shortValue() {
return data;
}
/**
* Get the integer value
*
* @return an integer
*/
@Override
public int intValue() {
return shortValue();
}
/**
* Get the long integer value
*
* @return a long integer
*/
@Override
public long longValue() {
return shortValue();
}
/**
* Get the a float value
*
* @return a float
*/
@Override
public float floatValue() {
return shortValue();
}
/**
* Get the double value
*
* @return a double
*/
@Override
public double doubleValue() {
return shortValue();
}
/**
* Convert to a byte array
*
* @return a byte[]
*/
@Override
public byte[] toByteArray() {
ByteArrayOutputStream output = new ByteArrayOutputStream();
writeTo(output);
return output.toByteArray();
}
/**
* Get the value as a Object
*
* @return the value as a Object
*/
@Override
public Object toObject() {
return new Byte(data);
}
/**
* Get the state of a single value bit
*
* @param indexbit index
* @return true: 1, false: 0
*/
@Override
public boolean isBitActive(int index) {
return (data & (1 << index)) != 0;
......@@ -186,53 +105,22 @@ public class VariantByte extends VariantNumber implements Comparable<Object> {
/**
* Test if the value match with a given mask
*
* @param mask mask to test
* @param mask
* mask to test
* @return true: match succeed, false: match fails
*/
public boolean match(byte mask) {
return ((data & mask) == mask);
}
/**
* Write the value to an output stream
*
* @param output output stream
* @return number of written bytes
*/
@Override
public final void writeTo(ByteArrayOutputStream output) {
output.write(data);
}
/**
* Return the human readable representation of the frame variant
*
* @return a string representation of the frame variant
*/
@Override
public String toString() {
return String.valueOf(data);
}
/**
* Return the variant as a string hexadecimal value
*
* @return a string hexadecimal value
*/
@Override
public String toHexString() {
return Format.toHexString(data);
}
/**
* Write the value in JSON format
*
* @param writer output stream writer
* @throws IOException
*/
@Override
public void writeJSONTo(OutputStreamWriter writer, int flags) throws IOException {
writer.write(String.valueOf(data));
return VariantNumber.toHexString(data);
}
@Override
......@@ -243,31 +131,29 @@ public class VariantByte extends VariantNumber implements Comparable<Object> {
return result;
}
/**
* Test if this object to an other variant
*
* @param other other variant
* @return true if the two objects are equals, otherwise false
*/
@Override
public boolean equals(Object other) {
if (!(other instanceof Variant)) {
return false;
}
return data == ((Variant)other).byteValue();
return data == ((Variant) other).byteValue();
}
/**
* Compare this variant to an other variant
*
* @param other other variant
* @return the difference as an integer value
*/
@Override
public int compareTo(Object other) {
if (!(other instanceof Variant)) {
return -1;
}
return data - ((Variant)other).byteValue();
return data - ((Variant) other).byteValue();
}
@Override
public boolean isNull() {
return data == 0;
}
@Override
public Type type() {
return Type.BYTE;
}
}
......@@ -16,10 +16,6 @@
package org.mbedsys.jvar;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
/**
*
......@@ -30,12 +26,6 @@ public class VariantByteArray extends Variant {
protected byte[] data;
private byte lengthType = LENGTH_UNDEFINED;
public static final byte LENGTH_NONE = (byte)0x00;
public static final byte LENGTH_UNDEFINED = (byte)0x01;
public static final byte LENGTH_DEF_8BIT = (byte)0x02;
protected static int toDigit(final char ch, final int index) {
final int digit = Character.digit(ch, 16);
if (digit == -1) {
......@@ -67,95 +57,19 @@ public class VariantByteArray extends Variant {
}
/**
* Variable sized frame field constructor from a byte array
* Byte array variant constructor from a value
*
* @param data source byte array
*/
public VariantByteArray(byte[] data) {
this.data = data;
}
/**
* Variable sized frame field constructor from an input stream
*
* @param input source input stream
* @param length length
* @param lengthType length type
*/
public VariantByteArray(ByteArrayInputStream input, int length, byte lengthType) {
this.lengthType = lengthType;
data = new byte[length];
try {
input.read(data);
} catch (IOException e) {} // cannot fail
}
/**
* Variable sized frame field constructor from an input stream
*
* @param input source input stream
* @param lengthType length type
*/
public VariantByteArray(ByteArrayInputStream input, byte lengthType) {
this.lengthType = lengthType;
switch (lengthType) {
case LENGTH_NONE:
data = new byte[input.available()];
try {
input.read(data);
} catch (IOException e) {} // cannot fail
break;
case LENGTH_DEF_8BIT:
data = new byte[input.read()];
try {
input.read(data);
} catch (IOException e) {} // cannot fail
break;
default:
}
}
/**
* Convert to a byte array
*
* @return a byte[]
*/
@Override
public byte[] toByteArray() {
return data;
}
/**
* Get the value as a Object
*
* @return the value as a Object
*/
@Override
public Object toObject() {
return data;
}
/**
* Write data to an output stream
*
* @param output output stream
* @return number of written bytes
*/
@Override
public final void writeTo(ByteArrayOutputStream output) {
try {
if (lengthType == LENGTH_DEF_8BIT) {
output.write((byte)(data.length));
}
output.write(data);
} catch (IOException e) {} // cannot fail
}
/**
* Return the human readable representation of the frame
*
* @return a string
*/
@Override
public String toString() {
String dispFrame = "";
......@@ -164,36 +78,24 @@ public class VariantByteArray extends Variant {
}
return dispFrame;
}
/**
* Write the value in JSON format
*
* @param writer output stream writer
* @throws IOException
*/
@Override