OSDN Git Service

Version 0.11
[bm-asn1/bm-asn1.git] / jp / bitmeister / asn1 / type / builtin / BIT_STRING.java
index 69dbe2f..86b2a7e 100644 (file)
@@ -152,6 +152,7 @@ public class BIT_STRING extends PrimitiveType<boolean[]> implements
         * Instantiates an empty {@code BIT_STRING}.
         */
        public BIT_STRING() {
+               set(new boolean[0]);
        }
 
        /**
@@ -209,39 +210,39 @@ public class BIT_STRING extends PrimitiveType<boolean[]> implements
         *            The index of a bit.
         */
        public void set(int index) {
-               if (value() == null) {
-                       set(new boolean[index + 1]);
-               } else if (size() <= index) {
-                       boolean[] newValue = new boolean[index + 1];
-                       System.arraycopy(value(), 0, newValue, 0, size());
-                       set(newValue);
-               }
+               expand(index);
                value()[index] = true;
        }
 
        /**
-        * Sets {@code false} to a bit specified by the index number. If the index
-        * specifies the last bit of this bitstring value, the {@code boolean} array
-        * of the value of this data will be contracted to enough size to hold the
-        * last {@code true} bit.
+        * Sets {@code false} to a bit specified by the index number. If the
+        * {@code boolean} array of the value of this data does not have enough size
+        * to hold the bit specified by the index, it will be expanded
+        * automatically.
         * 
         * @param index
         *            The index of a bit.
         */
        public void unset(int index) {
-               if (value() == null) {
-                       set(new boolean[0]);
-               } else if (size() == index + 1) {
-                       for (index--; index >= 0; index--) {
-                               if (value()[index]) {
-                                       break;
-                               }
+               expand(index);
+               value()[index] = false;
+       }
+       
+       /**
+        * Contracts the {@code boolean} array of the value of this data to enough
+        * size to hold the last {@code true} bit.
+        */
+       public void contract() {
+               int index = size();
+               for (index--; index >= 0; index--) {
+                       if (value()[index]) {
+                               break;
                        }
+               }
+               if (index + 1 != size()) {
                        boolean[] newValue = new boolean[index + 1];
                        System.arraycopy(value(), 0, newValue, 0, newValue.length);
                        set(newValue);
-               } else if (size() > index) {
-                       value()[index] = false;
                }
        }
 
@@ -279,6 +280,21 @@ public class BIT_STRING extends PrimitiveType<boolean[]> implements
                return getNamedBitMap(getClass()).get(index);
        }
 
+       /**
+        * Expands the {@code boolean} array of the value of this data to
+        * enough size to hold the bit specified by the index.
+        * 
+        * @param index
+        *            The index of a bit.
+        */
+       private void expand(int index) {
+               if (size() <= index) {
+                       boolean[] newValue = new boolean[index + 1];
+                       System.arraycopy(value(), 0, newValue, 0, size());
+                       set(newValue);
+               }
+       }
+       
        /*
         * (non-Javadoc)
         * 
@@ -357,8 +373,8 @@ public class BIT_STRING extends PrimitiveType<boolean[]> implements
         * ASN1Visitor)
         */
        @Override
-       public <E extends Throwable> void accept(ASN1Visitor<E> visitor) throws E {
-               visitor.visit(this);
+       public <R, E extends Throwable> R accept(ASN1Visitor<R, E> visitor) throws E {
+               return visitor.visit(this);
        }
 
 }