Commit 763605df authored by Emeric Verschuur's avatar Emeric Verschuur

Adding VariantShort & VariantUShort

parent 38971903
......@@ -17,15 +17,6 @@ import java.io.IOException;
%{
private StringBuffer string = new StringBuffer();
private Variant mkVariantNumber() {
long v = Long.parseLong(yytext());
if ((v & 0xFFFFFFFF00000000L) == 0) {
return new VariantInt((int)v);
} else {
return new VariantLong(v);
}
}
public boolean ready() throws IOException {
while (true) {
if ((zzCurrentPos + 1) < zzEndRead) {
......@@ -110,7 +101,7 @@ escape_horiz_tab = \\t
{kw_nan} { return new JSONTocken(JSONTocken.TVARIANT, Variant.NULL); }
{kw_infinity} { return new JSONTocken(JSONTocken.TVARIANT, Variant.NULL); }
{number} { return new JSONTocken(JSONTocken.TVARIANT, mkVariantNumber()); }
{number} { return new JSONTocken(JSONTocken.TVARIANT, VariantNumber.optimize(Long.parseLong(yytext())); }
{number_ext} { return new JSONTocken(JSONTocken.TVARIANT, new VariantDouble(Double.parseDouble(yytext()))); }
{square_bracket_open} { return new JSONTocken(JSONTocken.TARRBEGIN); }
......
......@@ -264,15 +264,6 @@ class JSONScanner {
/* user code: */
private StringBuffer string = new StringBuffer();
private Variant mkVariantNumber() {
long v = Long.parseLong(yytext());
if ((v & 0xFFFFFFFF00000000L) == 0) {
return new VariantInt((int)v);
} else {
return new VariantLong(v);
}
}
public boolean ready() throws IOException {
while (true) {
if ((zzCurrentPos + 1) < zzEndRead) {
......@@ -671,7 +662,7 @@ class JSONScanner {
}
case 26: break;
case 3:
{ return new JSONTocken(JSONTocken.TVARIANT, mkVariantNumber());
{ return new JSONTocken(JSONTocken.TVARIANT, VariantNumber.optimize(Long.parseLong(yytext())));
}
case 27: break;
case 4:
......
......@@ -42,7 +42,7 @@ import java.util.Random;
public abstract class Variant implements Comparable<Object> {
public enum Type {
BOOL, BYTE, BYTEARRAY, DATETIME, DOUBLE, INT, LIST, LONG, MAP, NULL, STRING, UINT, ULONG
BOOL, BYTE, BYTEARRAY, DATETIME, DOUBLE, SHORT, INT, LIST, LONG, MAP, NULL, STRING, USHORT, UINT, ULONG
}
public enum Format {
......@@ -97,14 +97,16 @@ public abstract class Variant implements Comparable<Object> {
private static final byte BCON_TOKEN_TRUE = (byte) 0x02;
private static final byte BCON_TOKEN_FALSE = (byte) 0x03;
private static final byte BCON_TOKEN_BYTE = (byte) 0x04;
private static final byte BCON_TOKEN_INT32 = (byte) 0x05;
private static final byte BCON_TOKEN_INT64 = (byte) 0x06;
private static final byte BCON_TOKEN_UINT32 = (byte) 0x07;
private static final byte BCON_TOKEN_UINT64 = (byte) 0x08;
private static final byte BCON_TOKEN_DOUBLE = (byte) 0x0A;
private static final byte BCON_TOKEN_DATETIME = (byte) 0x0B;
private static final byte BCON_TOKEN_MAP = (byte) 0x0C;
private static final byte BCON_TOKEN_LIST = (byte) 0x0D;
private static final byte BCON_TOKEN_INT16 = (byte) 0x05;
private static final byte BCON_TOKEN_UINT16 = (byte) 0x06;
private static final byte BCON_TOKEN_INT32 = (byte) 0x07;
private static final byte BCON_TOKEN_UINT32 = (byte) 0x08;
private static final byte BCON_TOKEN_INT64 = (byte) 0x09;
private static final byte BCON_TOKEN_UINT64 = (byte) 0x0A;
private static final byte BCON_TOKEN_DOUBLE = (byte) 0x0B;
private static final byte BCON_TOKEN_DATETIME = (byte) 0x0C;
private static final byte BCON_TOKEN_LIST = (byte) 0x0E;
private static final byte BCON_TOKEN_MAP = (byte) 0x0F;
private static final byte BCON_TOKEN_DATA6 = (byte) 0xA0;
private static final byte BCON_TOKEN_STRING6 = (byte) 0xC0;
private static final byte BCON_TOKEN_DATA12 = (byte) 0x10;
......@@ -570,6 +572,8 @@ public abstract class Variant implements Comparable<Object> {
case NULL:
writer.append("null");
break;
case SHORT:
case USHORT:
case INT:
case UINT:
case LONG:
......@@ -600,6 +604,13 @@ public abstract class Variant implements Comparable<Object> {
}
}
private static void write16(OutputStream output, byte type, short value)
throws IOException {
output.write(type);
output.write(((byte) ((value & 0xFF))));
output.write(((byte) (((value >> 8) & 0xFF))));
}
private static void write32(OutputStream output, byte type, int value)
throws IOException {
output.write(type);
......@@ -641,22 +652,30 @@ public abstract class Variant implements Comparable<Object> {
output.write(BCON_TOKEN_BYTE);
output.write(variant.byteValue());
break;
case UINT: {
write32(output, BCON_TOKEN_UINT32, variant.intValue());
case SHORT: {
write16(output, BCON_TOKEN_INT32, variant.shortValue());
break;
}
case ULONG: {
write64(output, BCON_TOKEN_UINT64, variant.intValue());
case USHORT: {
write16(output, BCON_TOKEN_UINT32, variant.shortValue());
break;
}
case INT: {
write32(output, BCON_TOKEN_INT32, variant.intValue());
break;
}
case UINT: {
write32(output, BCON_TOKEN_UINT32, variant.intValue());
break;
}
case LONG: {
write64(output, BCON_TOKEN_INT64, variant.longValue());
break;
}
case ULONG: {
write64(output, BCON_TOKEN_UINT64, variant.intValue());
break;
}
case DOUBLE: {
write64(output, BCON_TOKEN_DOUBLE, variant.longValue());
break;
......@@ -897,6 +916,10 @@ public abstract class Variant implements Comparable<Object> {
| ((input.read() & 0xFF) << 16) | ((input.read() & 0xFF) << 24);
}
private static short read16(InputStream input) throws IOException {
return (short) ((input.read() & 0xFF) | ((input.read() & 0xFF) << 8));
}
private static long read64(InputStream input) throws IOException {
return (long) (input.read() & 0xFF)
| ((long) (input.read() & 0xFF) << 8)
......@@ -965,15 +988,21 @@ public abstract class Variant implements Comparable<Object> {
case BCON_TOKEN_BYTE:
ret = new VariantByte((byte) input.read());
break;
case BCON_TOKEN_INT16:
ret = new VariantShort(read16(input));
break;
case BCON_TOKEN_UINT16:
ret = new VariantUShort(read16(input));
break;
case BCON_TOKEN_INT32:
ret = new VariantInt(read32(input));
break;
case BCON_TOKEN_INT64:
ret = new VariantLong(read64(input));
break;
case BCON_TOKEN_UINT32:
ret = new VariantUInt(read32(input));
break;
case BCON_TOKEN_INT64:
ret = new VariantLong(read64(input));
break;
case BCON_TOKEN_UINT64:
ret = new VariantULong(read64(input));
break;
......
......@@ -66,21 +66,6 @@ public class VariantInt extends VariantNumber {
return data & mask;
}
/**
* Get the sub value
*
* @param offset sub value bit offset
* @param length sub value bit length
* @return a short value
*/
public int intValue(int offset, int length) {
int mask = 0;
for(int i=0; i < length; i++) {
mask |= 1 << (offset + i);
}
return (data & mask) >> offset;
}
@Override
public long longValue() {
return data;
......
......@@ -62,6 +62,38 @@ public abstract class VariantNumber extends Variant {
+ Integer.toHexString(0x10000 | (int)((value >> 16) & 0xffff)).substring(1).toUpperCase()
+ Integer.toHexString(0x10000 | (int)(value & 0xffff)).substring(1).toUpperCase();
}
public static VariantNumber optimize(int number) {
if ((number & 0x7FFFFF80) == 0) {
return new VariantByte((byte)number);
} else if ((number & 0xFFFF8000) == 0) {
return new VariantUShort((short)number);
} else if ((number & 0x7FFF8000) == 0) {
return new VariantShort((short)number);
} else if ((number & 0x80000000) == 0) {
return new VariantUInt(number);
} else {
return new VariantInt(number);
}
}
public static VariantNumber optimize(long number) {
if ((number & 0x7FFFFFFFFFFFFF80L) == 0) {
return new VariantByte((byte)number);
} else if ((number & 0xFFFFFFFFFFFF8000L) == 0) {
return new VariantUShort((short)number);
} else if ((number & 0x7FFFFFFFFFFF8000L) == 0) {
return new VariantShort((short)number);
} else if ((number & 0xFFFFFFFF80000000L) == 0) {
return new VariantUInt((int)number);
} else if ((number & 0x7FFFFFFF80000000L) == 0) {
return new VariantInt((int)number);
} else if ((number & 0x8000000000000000L) == 0) {
return new VariantULong(number);
} else {
return new VariantLong(number);
}
}
/**
* Get the state of a single value bit
......
/*
* Copyright 2014 Emeric Verschuur <emericv@mbedsys.org>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mbedsys.jvar;
/**
* Signed short integer variant
*
* @author <a href="mailto:emericv@mbedsys.org">Emeric Verschuur</a>
* Copyright 2014 MbedSYS
*/
public class VariantShort extends VariantNumber {
protected short data;
/**
* Short variant constructor from a value
*
* @param value
*/
public VariantShort(short value) {
data = value;
}
@Override
public boolean booleanValue() {
return data != 0;
}
@Override
public byte byteValue() {
return (byte) data;
}
@Override
public short shortValue() {
return (short) data;
}
@Override
public int intValue() {
return data;
}
/**
* Get the short value using a binary mask
*
* @param mask mask to apply
* @return a short value
*/
public int intValue(int mask) {
return data & mask;
}
@Override
public long longValue() {
return data;
}
@Override
public float floatValue() {
return data;
}
@Override
public double doubleValue() {
return data;
}
@Override
public boolean isBitActive(int index) {
return (data & (1 << index)) != 0;
}
/**
* Test if the value match with a given mask
*
* @param mask mask to test
* @return true: match succeed, false: match fails
*/
public boolean match(int mask) {
return (data & mask) != 0;
}
@Override
public String toString() {
return String.valueOf(data);
}
@Override
public String toHexString() {
return toHexString(data);
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + data;
return result;
}
@Override
public boolean equals(Object other) {
if (!(other instanceof Variant)) {
return false;
}
return data == ((Variant)other).intValue();
}
@Override
public int compareTo(Object other) {
if (!(other instanceof Variant)) {
return -1;
}
return data - ((Variant)other).intValue();
}
@Override
public boolean isNull() {
return data == 0;
}
@Override
public Type type() {
return Type.INT;
}
}
/*
* Copyright 2014 Emeric Verschuur <emericv@mbedsys.org>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mbedsys.jvar;
/**
* Unsigned short integer variant
*
* @author <a href="mailto:emericv@mbedsys.org">Emeric Verschuur</a>
* Copyright 2014 MbedSYS
*/
public class VariantUShort extends VariantNumber {
protected short data;
/**
* Unsigned short integer variant constructor from a value
*
* @param value
*/
public VariantUShort(short value) {
data = value;
}
@Override
public boolean booleanValue() {
return data != 0;
}
@Override
public byte byteValue() {
return (byte) data;
}
@Override
public short shortValue() {
return data;
}
@Override
public int intValue() {
return data & 0xFFFF;
}
/**
* Get the short value using a binary mask
*
* @param mask mask to apply
* @return a short value
*/
public int intValue(int mask) {
return data & mask;
}
/**
* Get the sub value
*
* @param offset sub value bit offset
* @param length sub value bit length
* @return a short value
*/
public short shortValue(int offset, int length) {
int mask = 0;
for(int i=0; i < length; i++) {
mask |= 1 << (offset + i);
}
return (short) ((data & mask) >> offset);
}
@Override
public long longValue() {
return data & 0xFFFFFFFFL;
}
@Override
public float floatValue() {
return data;
}
@Override
public double doubleValue() {
return data;
}
@Override
public boolean isBitActive(int index) {
return (data & (1 << index)) != 0;
}
/**
* Test if the value match with a given mask
*
* @param mask mask to test
* @return true: match succeed, false: match fails
*/
public boolean match(int mask) {
return (data & mask) != 0;
}
@Override
public String toString() {
return String.valueOf(data);
}
@Override
public String toHexString() {
return toHexString(data);
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + data;
return result;
}
@Override
public boolean equals(Object other) {
if (!(other instanceof Variant)) {
return false;
}
return data == ((Variant)other).intValue();
}
@Override
public int compareTo(Object other) {
if (!(other instanceof Variant)) {
return -1;
}
return data - ((Variant)other).intValue();
}
@Override
public boolean isNull() {
return data == 0;
}
@Override
public Type type() {
return Type.UINT;
}
}
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