qpid-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From acon...@apache.org
Subject [14/50] [abbrv] qpid-proton git commit: PROTON-1016: Support for encoding of java.math.BigInteger
Date Fri, 23 Oct 2015 14:36:16 GMT
PROTON-1016: Support for encoding of java.math.BigInteger


Project: http://git-wip-us.apache.org/repos/asf/qpid-proton/repo
Commit: http://git-wip-us.apache.org/repos/asf/qpid-proton/commit/6091e9e2
Tree: http://git-wip-us.apache.org/repos/asf/qpid-proton/tree/6091e9e2
Diff: http://git-wip-us.apache.org/repos/asf/qpid-proton/diff/6091e9e2

Branch: refs/heads/go1
Commit: 6091e9e2c57d2dd94cfb5b87d61ca354ce84362e
Parents: 1ae893e
Author: Bozo Dragojevic <bozzo@digiverse.si>
Authored: Thu Oct 8 17:18:38 2015 +0200
Committer: Bozo Dragojevic <bozzo@digiverse.si>
Committed: Thu Oct 8 17:54:24 2015 +0200

----------------------------------------------------------------------
 .../qpid/proton/codec/BigIntegerType.java       | 196 +++++++++++++++++++
 .../apache/qpid/proton/codec/EncoderImpl.java   |   2 +
 2 files changed, 198 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/6091e9e2/proton-j/src/main/java/org/apache/qpid/proton/codec/BigIntegerType.java
----------------------------------------------------------------------
diff --git a/proton-j/src/main/java/org/apache/qpid/proton/codec/BigIntegerType.java b/proton-j/src/main/java/org/apache/qpid/proton/codec/BigIntegerType.java
new file mode 100644
index 0000000..f74e80b
--- /dev/null
+++ b/proton-j/src/main/java/org/apache/qpid/proton/codec/BigIntegerType.java
@@ -0,0 +1,196 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.apache.qpid.proton.codec;
+
+import java.math.BigInteger;
+import java.util.Arrays;
+import java.util.Collection;
+
+public class BigIntegerType extends AbstractPrimitiveType<BigInteger> {
+
+    public static interface BigIntegerEncoding extends PrimitiveTypeEncoding<BigInteger>
+    {
+        void write(BigInteger l);
+        void writeValue(BigInteger l);
+        public BigInteger readPrimitiveValue();
+    }
+
+    private static final BigInteger BIG_BYTE_MIN = BigInteger.valueOf(Byte.MIN_VALUE);
+    private static final BigInteger BIG_BYTE_MAX = BigInteger.valueOf(Byte.MAX_VALUE);
+    private static final BigInteger BIG_LONG_MIN = BigInteger.valueOf(Long.MIN_VALUE);;
+    private static final BigInteger BIG_LONG_MAX = BigInteger.valueOf(Long.MAX_VALUE);;
+
+    private BigIntegerEncoding _BigIntegerEncoding;
+    private BigIntegerEncoding _smallBigIntegerEncoding;
+
+    BigIntegerType(final EncoderImpl encoder, final DecoderImpl decoder)
+    {
+        _BigIntegerEncoding = new AllBigIntegerEncoding(encoder, decoder);
+        _smallBigIntegerEncoding = new SmallBigIntegerEncoding(encoder, decoder);
+        encoder.register(BigInteger.class, this);
+    }
+
+    public Class<BigInteger> getTypeClass()
+    {
+        return BigInteger.class;
+    }
+
+    public BigIntegerEncoding getEncoding(final BigInteger l)
+    {
+        return (l.compareTo(BIG_BYTE_MIN) >= 0 && l.compareTo(BIG_BYTE_MAX) <=
0) ? _smallBigIntegerEncoding : _BigIntegerEncoding;
+    }
+
+
+    public BigIntegerEncoding getCanonicalEncoding()
+    {
+        return _BigIntegerEncoding;
+    }
+
+    public Collection<BigIntegerEncoding> getAllEncodings()
+    {
+        return Arrays.asList(_smallBigIntegerEncoding, _BigIntegerEncoding);
+    }
+
+    private long longValueExact(final BigInteger val) {
+        if (val.compareTo(BIG_LONG_MIN) < 0 || val.compareTo(BIG_LONG_MAX) > 0) {
+            throw new ArithmeticException("cannot encode BigInteger not representable as
long");
+        }
+        return val.longValue();
+    }
+
+    private class AllBigIntegerEncoding extends FixedSizePrimitiveTypeEncoding<BigInteger>
implements BigIntegerEncoding
+    {
+
+        public AllBigIntegerEncoding(final EncoderImpl encoder, final DecoderImpl decoder)
+        {
+            super(encoder, decoder);
+        }
+
+        @Override
+        protected int getFixedSize()
+        {
+            return 8;
+        }
+
+        @Override
+        public byte getEncodingCode()
+        {
+            return EncodingCodes.LONG;
+        }
+
+        public BigIntegerType getType()
+        {
+            return BigIntegerType.this;
+        }
+
+        public void writeValue(final BigInteger val)
+        {
+            getEncoder().writeRaw(longValueExact(val));
+        }
+        
+        public void write(final BigInteger l)
+        {
+            writeConstructor();
+            getEncoder().writeRaw(longValueExact(l));
+            
+        }
+
+        public boolean encodesSuperset(final TypeEncoding<BigInteger> encoding)
+        {
+            return (getType() == encoding.getType());
+        }
+
+        public BigInteger readValue()
+        {
+            return readPrimitiveValue();
+        }
+
+        public BigInteger readPrimitiveValue()
+        {
+            return BigInteger.valueOf(getDecoder().readLong());
+        }
+
+
+        @Override
+        public boolean encodesJavaPrimitive()
+        {
+            return true;
+        }
+    }
+
+    private class SmallBigIntegerEncoding  extends FixedSizePrimitiveTypeEncoding<BigInteger>
implements BigIntegerEncoding
+    {
+        public SmallBigIntegerEncoding(final EncoderImpl encoder, final DecoderImpl decoder)
+        {
+            super(encoder, decoder);
+        }
+
+        @Override
+        public byte getEncodingCode()
+        {
+            return EncodingCodes.SMALLLONG;
+        }
+
+        @Override
+        protected int getFixedSize()
+        {
+            return 1;
+        }
+
+        public void write(final BigInteger l)
+        {
+            writeConstructor();
+            getEncoder().writeRaw(l.byteValue());
+        }
+
+        public BigInteger readPrimitiveValue()
+        {
+            return BigInteger.valueOf(getDecoder().readRawByte());
+        }
+
+        public BigIntegerType getType()
+        {
+            return BigIntegerType.this;
+        }
+
+        public void writeValue(final BigInteger val)
+        {
+            getEncoder().writeRaw(val.byteValue());
+        }
+
+        public boolean encodesSuperset(final TypeEncoding<BigInteger> encoder)
+        {
+            return encoder == this;
+        }
+
+        public BigInteger readValue()
+        {
+            return readPrimitiveValue();
+        }
+
+
+        @Override
+        public boolean encodesJavaPrimitive()
+        {
+            return true;
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/6091e9e2/proton-j/src/main/java/org/apache/qpid/proton/codec/EncoderImpl.java
----------------------------------------------------------------------
diff --git a/proton-j/src/main/java/org/apache/qpid/proton/codec/EncoderImpl.java b/proton-j/src/main/java/org/apache/qpid/proton/codec/EncoderImpl.java
index fd0be07..97e1005 100644
--- a/proton-j/src/main/java/org/apache/qpid/proton/codec/EncoderImpl.java
+++ b/proton-j/src/main/java/org/apache/qpid/proton/codec/EncoderImpl.java
@@ -59,6 +59,7 @@ public final class EncoderImpl implements ByteBufferEncoder
     private final UnsignedIntegerType   _unsignedIntegerType;
     private final LongType              _longType;
     private final UnsignedLongType      _unsignedLongType;
+    private final BigIntegerType        _bigIntegerType;
 
     private final CharacterType         _characterType;
     private final FloatType             _floatType;
@@ -98,6 +99,7 @@ public final class EncoderImpl implements ByteBufferEncoder
         _unsignedIntegerType    = new UnsignedIntegerType(this, decoder);
         _longType               = new LongType(this, decoder);
         _unsignedLongType       = new UnsignedLongType(this, decoder);
+        _bigIntegerType         = new BigIntegerType(this, decoder);
 
         _characterType          = new CharacterType(this, decoder);
         _floatType              = new FloatType(this, decoder);


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@qpid.apache.org
For additional commands, e-mail: commits-help@qpid.apache.org


Mime
View raw message