/* * The contents of this file are subject to the Mozilla Public License Version 1.1 * (the "License"); you may not use this file except in compliance with the License. * You may obtain a copy of the License at . * * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT * WARRANTY OF ANY KIND, either express or implied. See the License for the specific * language governing rights and limitations under the License. * * The Original Code is the Venice Web Communities System. * * The Initial Developer of the Original Code is Eric J. Bowersox , * for Silverwrist Design Studios. Portions created by Eric J. Bowersox are * Copyright (C) 2001 Eric J. Bowersox/Silverwrist Design Studios. All Rights Reserved. * * Contributor(s): */ package com.silverwrist.util; import java.util.BitSet; /** * A variant of the BitSet that can express itself as a character string, where each * character represents a flag that is "set." Up to 91 flags can be specified pre OptionSet. * * @author Eric J. Bowersox <erbo@silcom.com> * @version X * @see java.util.BitSet */ public class OptionSet extends BitSet { /*-------------------------------------------------------------------------------- * Static data members *-------------------------------------------------------------------------------- */ // The alphabet to use to store individual flags. private static final String ALPHA = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" + "!#$%&()*+,-./:;<=>?@[]^_`{|}~"; /*-------------------------------------------------------------------------------- * Constructors *-------------------------------------------------------------------------------- */ /** * Creates a new OptionSet. All bits are initially false. */ public OptionSet() { super(); } // end constructor /** * Creates an OptionSet whose initial size is large enough to explicitly represent bits with * indices in the range 0 through nbits-1. All bits are initially false. The maximum * size of the OptionSet is 91. * * @param nbits The initial size of the bit set. * @exception java.lang.NegativeArraySizeException If the specified initial size is negative. */ public OptionSet(int nbits) { super(Math.min(nbits,ALPHA.length())); } // end constructor /** * Creates an OptionSet from an array of characters representing "set" options. * * @param options The options to be set in the new OptionSet. */ public OptionSet(char[] options) { super(); // initialize all bits to 0 for (int i=0; i=0) super.set(ndx); } // end for } // end constructor /** * Creates an OptionSet from a string of characters representing "set" options. * * @param options The options to be set in the new OptionSet. */ public OptionSet(String options) { this(options.toCharArray()); } // end constructor /*-------------------------------------------------------------------------------- * Internal functions *-------------------------------------------------------------------------------- */ /** * Returns a StringBuffer representing the current state of this OptionSet, * with one character in it for each bit that is set. * * @return A StringBuffer containing the current state of the OptionSet. */ private StringBuffer asStringBuffer() { StringBuffer b = new StringBuffer(); for (int i=0; ifalse. * * @param bitIndex The index of the bit to be cleared. * @exception java.lang.IndexOutOfBoundsException - If the specified index is negative or greater than the * maximum option for an OptionSet. */ public void clear(int bitIndex) { if (bitIndex>=ALPHA.length()) throw new IndexOutOfBoundsException(); super.clear(bitIndex); } // end clear /** * Returns the value of the bit with the specified index. The value is true if the bit with * the index bitIndex is currently set in this OptionSet; otherwise, the result * is false. * * @param bitIndex The bit index. * @return The value of the bit with the specified index. * @exception java.lang.IndexOutOfBoundsException - If the specified index is negative or greater than the * maximum option for an OptionSet. */ public boolean get(int bitIndex) { if (bitIndex>=ALPHA.length()) throw new IndexOutOfBoundsException(); return super.get(bitIndex); } // end get /** * Sets the bit specified by the index to true. * * @param bitIndex The index of the bit to be set. * @exception java.lang.IndexOutOfBoundsException - If the specified index is negative or greater than the * maximum option for an OptionSet. */ public void set(int bitIndex) { if (bitIndex>=ALPHA.length()) throw new IndexOutOfBoundsException(); super.set(bitIndex); } // end set /*-------------------------------------------------------------------------------- * External operations *-------------------------------------------------------------------------------- */ /** * Sets the value of the specified bit in the OptionSet to a specified Boolean * value, and returns an indication of whether that value was changed. * * @param ndx The index of the bit to be assigned. * @param val true to set the corresponding bit, false to clear it. * @return true if the value of the bit in the OptionSet was changed by this * operation, false if not. * @exception java.lang.IndexOutOfBoundsException - If the specified index is negative or greater than the * maximum option for an OptionSet. */ public boolean assign(int ndx, boolean val) { if (ndx>=ALPHA.length()) throw new IndexOutOfBoundsException(); boolean old = super.get(ndx); if (val) super.set(ndx); else super.clear(ndx); return (old!=val); } // end assign /** * Resets the state of this OptionSet from an array of characters representing "set" options. * * @param options The options to be set in the new OptionSet. All options not specified will * be cleared. */ public void assign(char[] options) { int i; for (i=0; i=0) super.set(ndx); } // end for } // end assign /** * Resets the state of this OptionSet from a string of characters representing "set" options. * * @param options The options to be set in the new OptionSet. All options not specified will * be cleared. */ public void assign(String options) { if (options!=null) assign(options.toCharArray()); } // end assign /** * Returns a character array representing the current state of this OptionSet, * with one character in it for each bit that is set. * * @return A character array containing the current state of the OptionSet. */ public char[] asCharArray() { return asStringBuffer().toString().toCharArray(); } // end asCharArray /** * Returns a string representing the current state of this OptionSet, * with one character in it for each bit that is set. * * @return A string containing the current state of the OptionSet. */ public String asString() { return asStringBuffer().toString(); } // end asString /*-------------------------------------------------------------------------------- * External static operations *-------------------------------------------------------------------------------- */ public static final char getOptionChar(int index) { if ((index<0) || (index>=ALPHA.length())) throw new IndexOutOfBoundsException(); return ALPHA.charAt(index); } // end getOptionChar } // end class OptionSet