001: // ============================================================================
002: // $Id: ArrayUtils.java,v 1.3 2006/10/19 01:43:44 davidahall Exp $
003: // Copyright (c) 2004-2005 David A. Hall
004: // ============================================================================
005: // The contents of this file are subject to the Common Development and
006: // Distribution License (CDDL), Version 1.0 (the License); you may not use this
007: // file except in compliance with the License. You should have received a copy
008: // of the the License along with this file: if not, a copy of the License is
009: // available from Sun Microsystems, Inc.
010: //
011: // http://www.sun.com/cddl/cddl.html
012: //
013: // From time to time, the license steward (initially Sun Microsystems, Inc.) may
014: // publish revised and/or new versions of the License. You may not use,
015: // distribute, or otherwise make this file available under subsequent versions
016: // of the License.
017: //
018: // Alternatively, the contents of this file may be used under the terms of the
019: // GNU Lesser General Public License Version 2.1 or later (the "LGPL"), in which
020: // case the provisions of the LGPL are applicable instead of those above. If you
021: // wish to allow use of your version of this file only under the terms of the
022: // LGPL, and not to allow others to use your version of this file under the
023: // terms of the CDDL, indicate your decision by deleting the provisions above
024: // and replace them with the notice and other provisions required by the LGPL.
025: // If you do not delete the provisions above, a recipient may use your version
026: // of this file under the terms of either the CDDL or the LGPL.
027: //
028: // This library is distributed in the hope that it will be useful,
029: // but WITHOUT ANY WARRANTY; without even the implied warranty of
030: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
031: // ============================================================================
032:
033: package net.sf.jga.util;
034:
035: import java.util.Iterator;
036: import java.util.ListIterator;
037: import java.util.NoSuchElementException;
038:
039: /**
040: * Miscellaneous utilities for working with arrays.
041: * <p>
042: * Copyright © 2004-2005 David A. Hall
043: * @author <a href="mailto:davidahall@users.sf.net">David A. Hall</a>
044: */
045:
046: public class ArrayUtils {
047:
048: // Older versions of java lack a method that does this.
049: static public String toString(Object[] arr) {
050: StringBuffer buf = new StringBuffer();
051: buf.append("[");
052: for (int i = 0; i < arr.length; ++i) {
053: if (i > 0)
054: buf.append(',');
055:
056: buf.append(arr[i]);
057: }
058:
059: buf.append("]");
060: return buf.toString();
061: }
062:
063: /**
064: * Produce an ArrayIterator over the given array.
065: */
066: static public <E> ArrayIterator<E> iterate(E[] arr) {
067: return new ArrayIterator<E>(arr);
068: }
069:
070: /**
071: * Adapts the array to the Iterable interface
072: */
073: static public <T> /*@net.sf.jga.util.*/Iterable<T>/*@*/iterable(
074: T[] ts) {
075: return new ArrayIterable<T>(ts);
076: }
077:
078: /**
079: * Iterable wrapper around a given array.
080: */
081: static public class ArrayIterable<T> implements
082: /*@net.sf.jga.util.*/Iterable/*@*/<T> {
083:
084: // The array being iterated over
085: private T[] _delegate;
086:
087: public ArrayIterable(T[] ts) {
088: _delegate = ts;
089: }
090:
091: // - - - - - - - - - - - -
092: // Iterable implementation
093: // - - - - - - - - - - - -
094:
095: public Iterator<T> iterator() {
096: if (_delegate == null)
097: return new EmptyIterator<T>();
098: else
099: return new ArrayIterator<T>(_delegate);
100: }
101: }
102:
103: /**
104: * Iterates over an array of objects. Not safe for use by multiple threads.
105: */
106: static public class ArrayIterator<T> implements ListIterator<T> {
107:
108: // the array being iterated over
109: private T[] _array;
110:
111: // the current pointer into the array
112: private int _idx;
113:
114: // Null on initialization, TRUE when next() was the last move called,
115: // FALSE when previous() was the last move called.
116: private Boolean _goingForward;
117:
118: /**
119: * Builds an ArrayIterator for the given array
120: */
121: public ArrayIterator(T[] array) {
122: _array = array;
123: }
124:
125: // - - - - - - - - - - - - - -
126: // ListIterator implementation
127: // - - - - - - - - - - - - - -
128:
129: public boolean hasNext() {
130: return _idx < _array.length;
131: }
132:
133: public boolean hasPrevious() {
134: return _idx > 0;
135: }
136:
137: public int nextIndex() {
138: return _idx;
139: }
140:
141: public int previousIndex() {
142: return _idx - 1;
143: }
144:
145: public T next() {
146: if (_idx >= _array.length)
147: throw new NoSuchElementException();
148:
149: _goingForward = Boolean.TRUE;
150: return _array[_idx++];
151: }
152:
153: public T previous() {
154: if (_idx <= 0)
155: throw new NoSuchElementException();
156:
157: _goingForward = Boolean.FALSE;
158: return _array[--_idx];
159: }
160:
161: public void set(T value) {
162: if (_goingForward == null)
163: throw new IllegalStateException();
164:
165: if (_goingForward.booleanValue()) // to compile w/ 1.4 for retro test
166: _array[_idx - 1] = value;
167: else
168: _array[_idx] = value;
169: }
170:
171: // Can't support either of these, as an array is a fixed size structure
172: public void add(T value) {
173: _goingForward = null;
174: throw new UnsupportedOperationException();
175: }
176:
177: public void remove() {
178: _goingForward = null;
179: throw new UnsupportedOperationException();
180: }
181: }
182: }
|