/* XBN Java: Generically useful, non-GUI Java code. http://sourceforge.net/projects/xbnjava Copyright (C) 1997-2003, Jeff Epstein All rights reserved. Modifications: No Redistribution in binary form, with or without modifications, are permitted provided that the following conditions are met: * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * If modifications are made to source code then this license should indicate that fact in the "Modifications" section above. * Neither the author, nor the contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. [NOTE: This license contains NO advertising clause.] */ package xbn.array; import xbn.XBNObject; import java.util.Vector; /**
Wraps around a Vector, to aid and enhance the process of creating arrays of unknown length. See java.util.Vector.
Source code: VectorWrapper.java. Example code See the example code for VWInt.
This convenience class manages a Vector internally: creates it, validates added objects, and is ultimately converted to an array of the desired type. The order in which objects are added is preserved in the resulting array.
@version 0.9b @author Jeff Epstein, http://sourceforge.net/projects/xbnjava. **/ public abstract class VectorWrapper extends XBNObject implements Cloneable { private Vector vctr = null; private boolean bAddInOrder = false; private boolean bOrderDirAscDesc = false; /**Create a VectorWrapper with default settings.
Equal to VectorWrapper(new Vector())
Create a VectorWrapper.
Equal to VectorWrapper(v_ector, false, false)
Create a VectorWrapper.
Equal to VectorWrapper((new Vector()), b_orderDirAscDesc)
Create a VectorWrapper.
Equal to VectorWrapper(v_ector, true, b_orderDirAscDesc)
Create a VectorWrapper.
Equal to VectorWrapper((new Vector()), b_addInOrder, b_orderDirAscDesc)
Create a VectorWrapper.
@param v_ector The vector to add elements to. May not be null. It is assumed that this is appropriate for this VectorWrapper (for example, ordered in the proper way, as dictated by b_addInOrder and b orderDirAscDesc). If not, this class may behave unpredictably. See getVector(). @param b_addInOrder If true, when a [thing] is added, then add it so that the resulting (internally-held) Vector is ordered by [whatever is appropriate in the sub-class], in the direction defined by b_orderDirAscDesc. See doAddInOrder @param bOrderDirAscDesc If b_addInOrder is true, then this defines the direction in which ordering should occur. If true, then ordering is by ascending name (1, 2, 3). If false, then descending (3, 2, 1). See getOrderDirAscDesc. **/ public VectorWrapper(Vector v_ector, boolean b_addInOrder, boolean b_orderDirAscDesc) { throwAXIfNull(v_ector, "v_ector", sCNSTR); vctr = v_ector; bAddInOrder = b_addInOrder; bOrderDirAscDesc = b_orderDirAscDesc; } /**Get the vector for direct manipulation.
**/ public Vector getVector() { return vctr; } /**When elements are added, should they be added in order of something?
@return b_addInOrder Exactly as provided in the constructor. **/ public final boolean doAddInOrder() { return bAddInOrder; } /**When elements are added and definitely ordered, in which direction should ordering occur?
@return b_orderDirAscDesc Exactly as provided in the constructor. **/ public final boolean getOrderDirAscDesc() { return bOrderDirAscDesc; } /**Get the requested element.
@param i_dx The Vector index. Must range 0..[size() - 1]
, inclusive.
**/
public Object elementAt(int i_dx) {
try {
return getVector().elementAt(i_dx);
} catch(ArrayIndexOutOfBoundsException aioobx) {
throwAX("elementAt: i_dx (" + i_dx + ") is invalid. size()=" + size() + ".");
}
//Never reached. Needed for complie.
return null;
}
/**
Initialize the vector.
Use this when you want to start from scratch, with zero elements.
**/ public void initializeVector() { vctr = null; vctr = new Vector(); } /**How many elements are in the internal Vector?
@returngetVector().size()
**/
public int size() {
return getVector().size();
}
/**
Get the element at the requested index, as a string.
@param i_dx The Vector index to retrieve. Must range 0..[size() - 1]
, inclusive.
**/
public abstract String getString(int i_dx);
/**
Is the element at the requested index null?.
@param i_dx The Vector index to analyze. Must range 0..[size() - 1]
, inclusive.
@param true If the requested Vector element is null.
Get a list of all elements. This is not the most efficient, because it always calls getString. It's good for debugging, though.
**/ public String getList(String s_divider) { StringBuffer sb = new StringBuffer(); for(int i = 0; i < size(); i++) { sb.append(getString(i)); if(i < (size() - 1)) { sb.append(s_divider); } } return sb.toString(); } }