* Instantiates an empty {@code BIT_STRING}.
*/
public BIT_STRING() {
+ set(new boolean[0]);
}
/**
* 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;
}
}
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)
*
* 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);
}
}