KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jga > util > ArrayUtils


1 // ============================================================================
2
// $Id: ArrayUtils.java,v 1.3 2006/10/19 01:43:44 davidahall Exp $
3
// Copyright (c) 2004-2005 David A. Hall
4
// ============================================================================
5
// The contents of this file are subject to the Common Development and
6
// Distribution License (CDDL), Version 1.0 (the License); you may not use this
7
// file except in compliance with the License. You should have received a copy
8
// of the the License along with this file: if not, a copy of the License is
9
// available from Sun Microsystems, Inc.
10
//
11
// http://www.sun.com/cddl/cddl.html
12
//
13
// From time to time, the license steward (initially Sun Microsystems, Inc.) may
14
// publish revised and/or new versions of the License. You may not use,
15
// distribute, or otherwise make this file available under subsequent versions
16
// of the License.
17
//
18
// Alternatively, the contents of this file may be used under the terms of the
19
// GNU Lesser General Public License Version 2.1 or later (the "LGPL"), in which
20
// case the provisions of the LGPL are applicable instead of those above. If you
21
// wish to allow use of your version of this file only under the terms of the
22
// LGPL, and not to allow others to use your version of this file under the
23
// terms of the CDDL, indicate your decision by deleting the provisions above
24
// and replace them with the notice and other provisions required by the LGPL.
25
// If you do not delete the provisions above, a recipient may use your version
26
// of this file under the terms of either the CDDL or the LGPL.
27
//
28
// This library is distributed in the hope that it will be useful,
29
// but WITHOUT ANY WARRANTY; without even the implied warranty of
30
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
31
// ============================================================================
32

33 package net.sf.jga.util;
34
35 import java.util.Iterator JavaDoc;
36 import java.util.ListIterator JavaDoc;
37 import java.util.NoSuchElementException JavaDoc;
38
39 /**
40  * Miscellaneous utilities for working with arrays.
41  * <p>
42  * Copyright &copy; 2004-2005 David A. Hall
43  * @author <a HREF="mailto:davidahall@users.sf.net">David A. Hall</a>
44  */

45
46 public class ArrayUtils {
47
48     // Older versions of java lack a method that does this.
49
static public String JavaDoc toString(Object JavaDoc[] arr) {
50         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
51         buf.append("[");
52         for (int i = 0; i < arr.length; ++i) {
53             if (i > 0)
54                 buf.append(',');
55             
56             buf.append(arr[i]);
57         }
58
59         buf.append("]");
60         return buf.toString();
61     }
62
63
64     /**
65      * Produce an ArrayIterator over the given array.
66      */

67     static public <E> ArrayIterator<E> iterate(E[] arr) {
68         return new ArrayIterator<E>(arr);
69     }
70
71     
72     /**
73      * Adapts the array to the Iterable interface
74      */

75     static public <T> /*@net.sf.jga.util.*/Iterable JavaDoc<T>/*@*/ iterable(T[] ts) {
76         return new ArrayIterable<T>(ts);
77     }
78
79
80     /**
81      * Iterable wrapper around a given array.
82      */

83     static public class ArrayIterable<T> implements /*@net.sf.jga.util.*/Iterable JavaDoc/*@*/<T> {
84
85         // The array being iterated over
86
private T[] _delegate;
87
88         public ArrayIterable(T[] ts) { _delegate = ts; }
89
90         // - - - - - - - - - - - -
91
// Iterable implementation
92
// - - - - - - - - - - - -
93

94         public Iterator JavaDoc<T> iterator() {
95             if (_delegate == null)
96                 return new EmptyIterator<T>();
97             else
98                 return new ArrayIterator<T>(_delegate);
99         }
100     }
101
102     /**
103      * Iterates over an array of objects. Not safe for use by multiple threads.
104      */

105     static public class ArrayIterator<T> implements ListIterator JavaDoc<T> {
106         
107         // the array being iterated over
108
private T[] _array;
109
110         // the current pointer into the array
111
private int _idx;
112
113         // Null on initialization, TRUE when next() was the last move called,
114
// FALSE when previous() was the last move called.
115
private Boolean JavaDoc _goingForward;
116         
117         /**
118          * Builds an ArrayIterator for the given array
119          */

120         public ArrayIterator(T[] array) { _array = array; }
121
122         // - - - - - - - - - - - - - -
123
// ListIterator implementation
124
// - - - - - - - - - - - - - -
125

126         public boolean hasNext() { return _idx < _array.length; }
127
128         public boolean hasPrevious() { return _idx > 0; }
129
130         public int nextIndex() { return _idx; }
131
132         public int previousIndex() { return _idx - 1; }
133
134         public T next() {
135             if (_idx >= _array.length )
136                 throw new NoSuchElementException JavaDoc();
137
138             _goingForward = Boolean.TRUE;
139             return _array[_idx++];
140         }
141
142         public T previous() {
143             if (_idx <= 0)
144                 throw new NoSuchElementException JavaDoc();
145
146             _goingForward = Boolean.FALSE;
147             return _array[--_idx];
148         }
149
150         public void set(T value) {
151             if (_goingForward == null)
152                 throw new IllegalStateException JavaDoc();
153
154             if (_goingForward.booleanValue()) // to compile w/ 1.4 for retro test
155
_array[_idx - 1] = value;
156             else
157                 _array[_idx] = value;
158         }
159
160         // Can't support either of these, as an array is a fixed size structure
161
public void add(T value) {
162             _goingForward = null;
163             throw new UnsupportedOperationException JavaDoc();
164         }
165     
166         public void remove() {
167             _goingForward = null;
168             throw new UnsupportedOperationException JavaDoc();
169         }
170     }
171 }
172
Popular Tags