OSDN Git Service

Fix some bugs on BIT_STRING.
authorjwat <jwat@users.sourceforge.jp>
Thu, 7 Jul 2011 06:09:03 +0000 (15:09 +0900)
committerjwat <jwat@users.sourceforge.jp>
Thu, 7 Jul 2011 06:09:03 +0000 (15:09 +0900)
jp/bitmeister/asn1/type/builtin/BIT_STRING.java

index 86b2a7e..7a4bb37 100644 (file)
@@ -152,7 +152,6 @@ public class BIT_STRING extends PrimitiveType<boolean[]> implements
         * Instantiates an empty {@code BIT_STRING}.
         */
        public BIT_STRING() {
-               set(new boolean[0]);
        }
 
        /**
@@ -182,12 +181,10 @@ public class BIT_STRING extends PrimitiveType<boolean[]> implements
         * specified by the indexes.
         * 
         * @param indexes
-        *            The value to be assigned.
+        *            Indexes of bits to be set true.
         */
        public BIT_STRING(int... indexes) {
-               for (int e : indexes) {
-                       set(e);
-               }
+               set(indexes);
        }
 
        /**
@@ -201,31 +198,37 @@ public class BIT_STRING extends PrimitiveType<boolean[]> implements
        }
 
        /**
-        * Sets {@code true} to a bit specified by the index number. If the
+        * Sets {@code true} to bits specified by index numbers. 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
+        * to hold the bit specified by the biggest index, it will be expanded
         * automatically.
         * 
         * @param index
-        *            The index of a bit.
+        *            Indexes of bits to be set true.
         */
-       public void set(int index) {
-               expand(index);
-               value()[index] = true;
+       public void set(int ...indexes) {
+               Arrays.sort(indexes);
+               expand(indexes[indexes.length - 1]);
+               for (int e: indexes) {
+                       value()[e] = true;
+               }
        }
 
        /**
-        * Sets {@code false} to a bit specified by the index number. If the
+        * Sets {@code false} to bits specified by index numbers. 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
+        * to hold the bit specified by the biggest index, it will be expanded
         * automatically.
         * 
         * @param index
-        *            The index of a bit.
+        *            Indexes of bits to be set false.
         */
-       public void unset(int index) {
-               expand(index);
-               value()[index] = false;
+       public void unset(int ...indexes) {
+               Arrays.sort(indexes);
+               expand(indexes[indexes.length - 1]);
+               for (int e: indexes) {
+                       value()[e] = false;
+               }
        }
        
        /**
@@ -233,6 +236,10 @@ public class BIT_STRING extends PrimitiveType<boolean[]> implements
         * size to hold the last {@code true} bit.
         */
        public void contract() {
+               if (value() == null) {
+                       set(new boolean[0]);
+                       return;
+               }
                int index = size();
                for (index--; index >= 0; index--) {
                        if (value()[index]) {
@@ -290,7 +297,9 @@ public class BIT_STRING extends PrimitiveType<boolean[]> implements
        private void expand(int index) {
                if (size() <= index) {
                        boolean[] newValue = new boolean[index + 1];
-                       System.arraycopy(value(), 0, newValue, 0, size());
+                       if (size() > 0) {
+                               System.arraycopy(value(), 0, newValue, 0, size());
+                       }
                        set(newValue);
                }
        }
@@ -335,6 +344,9 @@ public class BIT_STRING extends PrimitiveType<boolean[]> implements
         */
        @Override
        public int size() {
+               if (value() == null) {
+                       return 0;
+               }
                return value().length;
        }
 
@@ -345,6 +357,9 @@ public class BIT_STRING extends PrimitiveType<boolean[]> implements
         */
        @Override
        protected boolean[] cloneValue() {
+               if (value() == null) {
+                       return null;
+               }
                return value().clone();
        }