directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dran...@apache.org
Subject directory-kerby git commit: ASN1. Simplified a bit some codes for collection types
Date Tue, 01 Dec 2015 09:43:15 GMT
Repository: directory-kerby
Updated Branches:
  refs/heads/master f98e113b8 -> 54c10a31e


ASN1. Simplified a bit some codes for collection types


Project: http://git-wip-us.apache.org/repos/asf/directory-kerby/repo
Commit: http://git-wip-us.apache.org/repos/asf/directory-kerby/commit/54c10a31
Tree: http://git-wip-us.apache.org/repos/asf/directory-kerby/tree/54c10a31
Diff: http://git-wip-us.apache.org/repos/asf/directory-kerby/diff/54c10a31

Branch: refs/heads/master
Commit: 54c10a31edbb77ad75fc0edcf4692db556c016fe
Parents: f98e113
Author: Kai Zheng <kai.zheng@intel.com>
Authored: Tue Dec 1 17:42:58 2015 +0800
Committer: Kai Zheng <kai.zheng@intel.com>
Committed: Tue Dec 1 17:42:58 2015 +0800

----------------------------------------------------------------------
 .../apache/kerby/asn1/type/Asn1Collection.java  | 22 ++++++--------
 .../kerby/asn1/type/Asn1CollectionOf.java       | 32 ++++++++------------
 .../kerby/asn1/type/Asn1CollectionType.java     |  3 +-
 .../apache/kerby/asn1/TestAsn1Collection.java   | 10 +++---
 4 files changed, 29 insertions(+), 38 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/54c10a31/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Collection.java
----------------------------------------------------------------------
diff --git a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Collection.java b/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Collection.java
index 94bd2f5..3bbb770 100644
--- a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Collection.java
+++ b/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Collection.java
@@ -30,12 +30,12 @@ import java.util.List;
 /**
  * ASN1 complex type, may be better named.
  */
-public class Asn1Collection extends AbstractAsn1Type<List<Asn1Item>> {
+public class Asn1Collection extends AbstractAsn1Type<List<Asn1Type>> {
     private boolean lazy = false;
 
     public Asn1Collection(UniversalTag universalTag) {
         super(universalTag);
-        setValue(new ArrayList<Asn1Item>());
+        setValue(new ArrayList<Asn1Type>());
         usePrimitive(false);
     }
 
@@ -48,11 +48,7 @@ public class Asn1Collection extends AbstractAsn1Type<List<Asn1Item>>
{
     }
 
     public void addItem(Asn1Type value) {
-        if (value instanceof Asn1Item) {
-            getValue().add((Asn1Item) value);
-        } else {
-            getValue().add(new Asn1Item(value));
-        }
+        getValue().add(value);
     }
 
     public void clear() {
@@ -61,9 +57,9 @@ public class Asn1Collection extends AbstractAsn1Type<List<Asn1Item>>
{
 
     @Override
     protected int encodingBodyLength() {
-        List<Asn1Item> valueItems = getValue();
+        List<Asn1Type> valueItems = getValue();
         int allLen = 0;
-        for (Asn1Item item : valueItems) {
+        for (Asn1Type item : valueItems) {
             if (item != null) {
                 allLen += item.encodingLength();
             }
@@ -73,8 +69,8 @@ public class Asn1Collection extends AbstractAsn1Type<List<Asn1Item>>
{
 
     @Override
     protected void encodeBody(ByteBuffer buffer) {
-        List<Asn1Item> valueItems = getValue();
-        for (Asn1Item item : valueItems) {
+        List<Asn1Type> valueItems = getValue();
+        for (Asn1Type item : valueItems) {
             if (item != null) {
                 item.encode(buffer);
             }
@@ -88,8 +84,10 @@ public class Asn1Collection extends AbstractAsn1Type<List<Asn1Item>>
{
             if (item != null) {
                 if (item.isSimple() && !isLazy()) {
                     item.decodeValueAsSimple();
+                    addItem(item.getValue());
+                } else {
+                    addItem(item);
                 }
-                addItem(item);
             }
         }
     }

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/54c10a31/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1CollectionOf.java
----------------------------------------------------------------------
diff --git a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1CollectionOf.java b/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1CollectionOf.java
index 6d7f36b..26acc0b 100644
--- a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1CollectionOf.java
+++ b/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1CollectionOf.java
@@ -33,19 +33,23 @@ public abstract class Asn1CollectionOf<T extends Asn1Type> extends
Asn1Collectio
     }
 
     public List<T> getElements() {
-        List<Asn1Item> items = getValue();
+        List<Asn1Type> items = getValue();
         int nElements = items != null ? items.size() : 0;
         List<T> results = new ArrayList<T>(nElements);
         if (nElements > 0) {
-            for (Asn1Item item : items) {
-                if (!item.isFullyDecoded()) {
-                    try {
-                        item.decodeValueAs(getElementType());
-                    } catch (IOException e) {
-                        throw new RuntimeException(e);
+            for (Asn1Type itemObj : items) {
+                if (itemObj instanceof Asn1Item) {
+                    Asn1Item item = (Asn1Item) itemObj;
+                    if (!item.isFullyDecoded()) {
+                        try {
+                            item.decodeValueAs(getElementType());
+                        } catch (IOException e) {
+                            throw new RuntimeException(e);
+                        }
                     }
+                    itemObj = item.getValue();
                 }
-                results.add((T) item.getValue());
+                results.add((T) itemObj);
             }
         }
         return results;
@@ -69,18 +73,6 @@ public abstract class Asn1CollectionOf<T extends Asn1Type> extends
Asn1Collectio
         super.addItem(element);
     }
 
-    @Override
-    public void addItem(Asn1Type value) {
-        Class<T> eleType = getElementType();
-        if (value instanceof Asn1Item) {
-            super.addItem(value);
-        } else if (!eleType.isInstance(value)) {
-            throw new RuntimeException("Unexpected element type " + value.getClass().getCanonicalName());
-        } else {
-            addElement((T) value);
-        }
-    }
-
     protected Class<T> getElementType() {
         Class<T> elementType = (Class<T>) ((ParameterizedType)
                 getClass().getGenericSuperclass()).getActualTypeArguments()[0];

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/54c10a31/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1CollectionType.java
----------------------------------------------------------------------
diff --git a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1CollectionType.java b/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1CollectionType.java
index bd3040e..3226441 100644
--- a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1CollectionType.java
+++ b/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1CollectionType.java
@@ -83,8 +83,9 @@ public abstract class Asn1CollectionType extends AbstractAsn1Type<Asn1Collection
         coll.decode(tag(), content);
 
         int lastPos = -1, foundPos = -1;
-        for (Asn1Item item : coll.getValue()) {
+        for (Asn1Type itemObj : coll.getValue()) {
             foundPos = -1;
+            Asn1Item item = (Asn1Item) itemObj;
             for (int i = lastPos + 1; i < fieldInfos.length; ++i) {
                 if (item.isContextSpecific()) {
                     if (fieldInfos[i].getTagNo() == item.tagNo()) {

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/54c10a31/kerby-asn1/src/test/java/org/apache/kerby/asn1/TestAsn1Collection.java
----------------------------------------------------------------------
diff --git a/kerby-asn1/src/test/java/org/apache/kerby/asn1/TestAsn1Collection.java b/kerby-asn1/src/test/java/org/apache/kerby/asn1/TestAsn1Collection.java
index f1cd35c..2ae62e7 100644
--- a/kerby-asn1/src/test/java/org/apache/kerby/asn1/TestAsn1Collection.java
+++ b/kerby-asn1/src/test/java/org/apache/kerby/asn1/TestAsn1Collection.java
@@ -19,10 +19,10 @@
  */
 package org.apache.kerby.asn1;
 
-import org.apache.kerby.asn1.type.AbstractAsn1Type;
 import org.apache.kerby.asn1.type.Asn1Boolean;
 import org.apache.kerby.asn1.type.Asn1IA5String;
 import org.apache.kerby.asn1.type.Asn1Sequence;
+import org.apache.kerby.asn1.type.Asn1String;
 import org.junit.Test;
 
 import java.io.IOException;
@@ -50,11 +50,11 @@ public class TestAsn1Collection {
     public void testSequenceDecoding() throws IOException {
         Asn1Sequence seq = new Asn1Sequence();
         seq.decode(EXPECTED_BYTES);
-        AbstractAsn1Type<?> field =
-            (AbstractAsn1Type<?>) seq.getValue().get(0).getValue();
+        Asn1String field =
+            (Asn1String) seq.getValue().get(0);
         assertThat(field.getValue()).isEqualTo(TEST_STR);
 
-        field = (AbstractAsn1Type<?>) seq.getValue().get(1).getValue();
-        assertThat(field.getValue()).isEqualTo(TEST_BOOL);
+        Asn1Boolean field2 = (Asn1Boolean) seq.getValue().get(1);
+        assertThat(field2.getValue()).isEqualTo(TEST_BOOL);
     }
 }


Mime
View raw message