1 /**
2  * The utillib library.
3  * More information is available at http://www.jinchess.com/.
4  * Copyright (C) 2002 Alexander Maryanovsky.
5  * All rights reserved.
6  *
7  * The utillib library is free software; you can redistribute
8  * it and/or modify it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; either version 2 of the
10  * License, or (at your option) any later version.
11  *
12  * The utillib library is distributed in the hope that it will
13  * be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with utillib library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21 
22 package free.util;
23 
24 import java.util.Enumeration;
25 import java.util.NoSuchElementException;
26 
27 
28 /**
29  * An implementation of the Enumeration interface which enumerates the items of
30  * an array. Note: This class is not thread safe.
31  */
32 
33 public class ArrayEnumeration implements Enumeration{
34 
35 
36   /**
37    * The array.
38    */
39 
40   private Object [] arr;
41 
42 
43 
44   /**
45    * The index of the first enumerated item.
46    */
47 
48   private final int offset;
49 
50 
51 
52   /**
53    * The amount of the enumerated items.
54    */
55 
56   private final int count;
57 
58 
59 
60   /**
61    * The index of the next returned item.
62    */
63 
64   private int curIndex;
65 
66 
67 
68   /**
69    * Creates a new ArrayEnumeration which enumerates the items of the given
70    * array. The first <code>count</code> items starting at index
71    * <code>offset</code> are enumerated.
72    *
73    * @throws IllegalArgumentException if the offset and/or count parameters are
74    * invalid.
75    */
76 
ArrayEnumeration(Object [] arr, int offset, int count)77   public ArrayEnumeration(Object [] arr, int offset, int count){
78     if ((offset < 0) || (offset + count > arr.length) || (count < 0))
79       throw new IllegalArgumentException("Invalid enumeration range");
80 
81     this.arr = new Object[arr.length];
82     System.arraycopy(arr, 0, this.arr, 0, arr.length);
83     this.offset = offset;
84     this.count = count;
85 
86     curIndex = offset;
87   }
88 
89 
90 
91   /**
92    * Creates a new ArrayEnumeration which enumerates all the items of the given
93    * array.
94    */
95 
ArrayEnumeration(Object [] arr)96   public ArrayEnumeration(Object [] arr){
97     this(arr, 0, arr.length);
98   }
99 
100 
101 
102   /**
103    * Returns the next element in the enumeration.
104    */
105 
nextElement()106   public Object nextElement(){
107     if (!hasMoreElements())
108       throw new NoSuchElementException();
109 
110     Object item = arr[curIndex];
111     arr[curIndex++] = null; // We don't want to keep a reference to it any longer than we have to.
112     if (!hasMoreElements())
113       arr = null; // Neither do we need this any more.
114     return item;
115   }
116 
117 
118 
119   /**
120    * Returns true if there are more elements in the enumeration.
121    */
122 
hasMoreElements()123   public boolean hasMoreElements(){
124     return curIndex < offset + count;
125   }
126 
127 
128 
129 }
130