diff options
Diffstat (limited to 'qpid/java/common/src/main/java/org/apache/qpid/framing/AMQTypedValue.java')
-rw-r--r-- | qpid/java/common/src/main/java/org/apache/qpid/framing/AMQTypedValue.java | 230 |
1 files changed, 185 insertions, 45 deletions
diff --git a/qpid/java/common/src/main/java/org/apache/qpid/framing/AMQTypedValue.java b/qpid/java/common/src/main/java/org/apache/qpid/framing/AMQTypedValue.java index 1dbedca362..f64164c10b 100644 --- a/qpid/java/common/src/main/java/org/apache/qpid/framing/AMQTypedValue.java +++ b/qpid/java/common/src/main/java/org/apache/qpid/framing/AMQTypedValue.java @@ -20,9 +20,7 @@ */ package org.apache.qpid.framing; -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.IOException; +import java.io.*; import java.util.Date; import java.util.Map; import java.math.BigDecimal; @@ -42,81 +40,223 @@ import java.math.BigDecimal; * <tr><td> Extract the value from a fully typed AMQP value. * </table> */ -public class AMQTypedValue +public abstract class AMQTypedValue { - /** The type of the value. */ - private final AMQType _type; - /** The Java native representation of the AMQP typed value. */ - private final Object _value; + public abstract AMQType getType(); - public AMQTypedValue(AMQType type, Object value) + public abstract Object getValue(); + + public abstract void writeToBuffer(DataOutput buffer) throws IOException; + + public abstract int getEncodingSize(); + + + private static final class GenericTypedValue extends AMQTypedValue { - if (type == null) + /** The type of the value. */ + private final AMQType _type; + + /** The Java native representation of the AMQP typed value. */ + private final Object _value; + + private GenericTypedValue(AMQType type, Object value) { - throw new NullPointerException("Cannot create a typed value with null type"); + if (type == null) + { + throw new NullPointerException("Cannot create a typed value with null type"); + } + + _type = type; + _value = type.toNativeValue(value); } - _type = type; - _value = type.toNativeValue(value); - } + private GenericTypedValue(AMQType type, DataInput buffer) throws IOException + { + _type = type; + _value = type.readValueFromBuffer(buffer); + } + + + public AMQType getType() + { + return _type; + } + + public Object getValue() + { + return _value; + } + + public void writeToBuffer(DataOutput buffer) throws IOException + { + _type.writeToBuffer(_value, buffer); + } + + public int getEncodingSize() + { + return _type.getEncodingSize(_value); + } + + + public String toString() + { + return "[" + getType() + ": " + getValue() + "]"; + } + + + public boolean equals(Object o) + { + if(o instanceof GenericTypedValue) + { + GenericTypedValue other = (GenericTypedValue) o; + return _type == other._type && (_value == null ? other._value == null : _value.equals(other._value)); + } + else + { + return false; + } + } + + public int hashCode() + { + return _type.hashCode() ^ (_value == null ? 0 : _value.hashCode()); + } - private AMQTypedValue(AMQType type, DataInputStream buffer) throws IOException - { - _type = type; - _value = type.readValueFromBuffer(buffer); } - public AMQType getType() + private static final class LongTypedValue extends AMQTypedValue { - return _type; + + private final long _value; + + private LongTypedValue(long value) + { + _value = value; + } + + public LongTypedValue(DataInput buffer) throws IOException + { + _value = EncodingUtils.readLong(buffer); + } + + public AMQType getType() + { + return AMQType.LONG; + } + + + public Object getValue() + { + return _value; + } + + public void writeToBuffer(DataOutput buffer) throws IOException + { + EncodingUtils.writeByte(buffer,AMQType.LONG.identifier()); + EncodingUtils.writeLong(buffer,_value); + } + + + public int getEncodingSize() + { + return EncodingUtils.encodedLongLength(); + } } - public Object getValue() + private static final class IntTypedValue extends AMQTypedValue { - return _value; + @Override + public String toString() + { + return "[INT: " + String.valueOf(_value) + "]"; + } + + private final int _value; + + public IntTypedValue(int value) + { + _value = value; + } + + public AMQType getType() + { + return AMQType.INT; + } + + + public Object getValue() + { + return _value; + } + + public void writeToBuffer(DataOutput buffer) throws IOException + { + EncodingUtils.writeByte(buffer,AMQType.INT.identifier()); + EncodingUtils.writeInteger(buffer, _value); + } + + + public int getEncodingSize() + { + return EncodingUtils.encodedIntegerLength(); + } } - public void writeToBuffer(DataOutputStream buffer) throws IOException + + public static AMQTypedValue readFromBuffer(DataInput buffer) throws IOException { - _type.writeToBuffer(_value, buffer); + AMQType type = AMQTypeMap.getType(buffer.readByte()); + + switch(type) + { + case LONG: + return new LongTypedValue(buffer); + + case INT: + int value = EncodingUtils.readInteger(buffer); + return createAMQTypedValue(value); + + default: + return new GenericTypedValue(type, buffer); + } + } - public int getEncodingSize() + private static final AMQTypedValue[] INT_VALUES = new AMQTypedValue[16]; + static { - return _type.getEncodingSize(_value); + for(int i = 0 ; i < 16; i ++) + { + INT_VALUES[i] = new IntTypedValue(i); + } } - public static AMQTypedValue readFromBuffer(DataInputStream buffer) throws IOException + public static AMQTypedValue createAMQTypedValue(int i) { - AMQType type = AMQTypeMap.getType(buffer.readByte()); - - return new AMQTypedValue(type, buffer); + return (i & 0x0f) == i ? INT_VALUES[i] : new IntTypedValue(i); } - public String toString() + + public static AMQTypedValue createAMQTypedValue(long value) { - return "[" + getType() + ": " + getValue() + "]"; + return new LongTypedValue(value); } - - public boolean equals(Object o) + public static AMQTypedValue createAMQTypedValue(AMQType type, Object value) { - if(o instanceof AMQTypedValue) + switch(type) { - AMQTypedValue other = (AMQTypedValue) o; - return _type == other._type && (_value == null ? other._value == null : _value.equals(other._value)); - } - else - { - return false; + case LONG: + return new LongTypedValue((Long) AMQType.LONG.toNativeValue(value)); + case INT: + return new IntTypedValue((Integer) AMQType.INT.toNativeValue(value)); + + default: + return new GenericTypedValue(type, value); } } - public int hashCode() - { - return _type.hashCode() ^ (_value == null ? 0 : _value.hashCode()); - } public static AMQTypedValue toTypedValue(Object val) |