KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > utils > ArrayEnumeration


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.core.internal.utils;
12
13 import java.util.Enumeration JavaDoc;
14 import java.util.NoSuchElementException JavaDoc;
15
16 /**
17  * An object which enumerates the elements of an array
18  */

19
20 public class ArrayEnumeration implements Enumeration JavaDoc {
21     int index;
22     int lastElement;
23     Object JavaDoc[] elements;
24
25     /**
26      * Returns new array enumeration over the given object array
27      */

28     public ArrayEnumeration(Object JavaDoc[] elements) {
29         this(elements, 0, elements.length - 1);
30     }
31
32     /**
33      * Returns new array enumeration over the given object array
34      */

35     public ArrayEnumeration(Object JavaDoc[] elements, int firstElement, int lastElement) {
36         super();
37         this.elements = elements;
38         index = firstElement;
39         this.lastElement = lastElement;
40     }
41
42     /**
43      * Returns true if this enumeration contains more elements.
44      */

45     public boolean hasMoreElements() {
46         return elements != null && index <= lastElement;
47     }
48
49     /**
50      * Returns the next element of this enumeration.
51      * @exception NoSuchElementException if no more elements exist.
52      */

53     public Object JavaDoc nextElement() throws NoSuchElementException JavaDoc {
54         if (!hasMoreElements())
55             throw new NoSuchElementException JavaDoc();
56         return elements[index++];
57     }
58 }
59
Popular Tags