KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > util > ArrayEnumeration


1 /**
2  * Redistribution and use of this software and associated documentation
3  * ("Software"), with or without modification, are permitted provided
4  * that the following conditions are met:
5  *
6  * 1. Redistributions of source code must retain copyright
7  * statements and notices. Redistributions must also contain a
8  * copy of this document.
9  *
10  * 2. Redistributions in binary form must reproduce the
11  * above copyright notice, this list of conditions and the
12  * following disclaimer in the documentation and/or other
13  * materials provided with the distribution.
14  *
15  * 3. The name "Exolab" must not be used to endorse or promote
16  * products derived from this Software without prior written
17  * permission of Exoffice Technologies. For written permission,
18  * please contact info@exolab.org.
19  *
20  * 4. Products derived from this Software may not be called "Exolab"
21  * nor may "Exolab" appear in their names without prior written
22  * permission of Exoffice Technologies. Exolab is a registered
23  * trademark of Exoffice Technologies.
24  *
25  * 5. Due credit should be given to the Exolab Project
26  * (http://www.exolab.org/).
27  *
28  * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32  * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39  * OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * Copyright 1999 (C) Exoffice Technologies Inc. All Rights Reserved.
42  *
43  * $Id: ArrayEnumeration.java 1096 2004-03-26 21:41:16Z dblevins $
44  */

45 package org.openejb.util;
46
47 import java.io.Externalizable JavaDoc;
48 import java.io.IOException JavaDoc;
49 import java.io.ObjectInput JavaDoc;
50 import java.io.ObjectOutput JavaDoc;
51 import java.util.Enumeration JavaDoc;
52 import java.util.NoSuchElementException JavaDoc;
53 import java.util.Vector JavaDoc;
54
55 /**
56  * An Externalizable Enumeration.
57  *
58  * Mainly used for returning enumerations from the finder methods in the home interface of entity beans.
59  *
60  * @author David Blevins
61  */

62 public final class ArrayEnumeration implements Enumeration JavaDoc, Externalizable JavaDoc{
63     static final long serialVersionUID = -1194966576855523042L;
64
65     private Object JavaDoc[] elements;
66     private int elementsIndex;
67
68     public ArrayEnumeration(Vector JavaDoc elements){
69         this.elements = new Object JavaDoc[elements.size()];
70         elements.copyInto(this.elements);
71     }
72
73     public ArrayEnumeration(java.util.List JavaDoc list){
74         this.elements = new Object JavaDoc[list.size()];
75         list.toArray(this.elements);
76     }
77     
78     // This is required for Externalization.
79
public ArrayEnumeration() {
80     }
81
82     // These methods are borrowed from the List interface
83
// They are needed to avoid unnecessary object creation in
84
// the finder methods
85
public java.lang.Object JavaDoc get(int index) {
86         return elements[index];
87     }
88
89     public void set(int index, java.lang.Object JavaDoc o) {
90         elements[index] = o;
91     }
92
93     public int size() {
94         return elements.length;
95     }
96     
97     //=========================================
98
// java.util.Enumeration interface methods
99
//
100

101     /**
102      * Tests if this enumeration contains more elements.
103      *
104      * @return <code>true</code> if and only if this enumeration object
105      * contains at least one more element to provide;
106      * <code>false</code> otherwise.
107      */

108     public boolean hasMoreElements(){
109         return ( elementsIndex < elements.length );
110     }
111
112     /**
113      * Returns the next element of this enumeration if this enumeration
114      * object has at least one more element to provide.
115      *
116      * @return the next element of this enumeration.
117      * @exception NoSuchElementException if no more elements exist.
118      */

119     public Object JavaDoc nextElement(){
120         if ( !hasMoreElements()) throw new NoSuchElementException JavaDoc("No more elements exist");
121         return elements[elementsIndex++];
122     }
123
124     //
125
// java.util.Enumeration interface methods
126
//=========================================
127

128     //==========================================
129
// java.io.Externalizable interface methods
130
//
131

132     /**
133      * The object implements the writeExternal method to save its contents
134      * by calling the methods of DataOutput for its primitive values or
135      * calling the writeObject method of ObjectOutput for objects, strings,
136      * and arrays.
137      *
138      * @serialData Overriding methods should use this tag to describe
139      * the data layout of this Externalizable object.
140      * List the sequence of element types and, if possible,
141      * relate the element to a public/protected field and/or
142      * method of this Externalizable class.
143      *
144      * @exception IOException Includes any I/O exceptions that may occur
145      */

146     public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc{
147         out.writeInt(elements.length);
148         out.writeInt(elementsIndex);
149         for (int i=0; i < elements.length; i++) {
150             out.writeObject(elements[i]);
151         }
152     }
153
154     /**
155      * The object implements the readExternal method to restore its
156      * contents by calling the methods of DataInput for primitive
157      * types and readObject for objects, strings and arrays. The
158      * readExternal method must read the values in the same sequence
159      * and with the same types as were written by writeExternal.
160      * @exception ClassNotFoundException If the class for an object being
161      * restored cannot be found.
162      */

163     public void readExternal(ObjectInput JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc{
164         elements = new Object JavaDoc[in.readInt()];
165         elementsIndex = in.readInt();
166         for (int i=0; i < elements.length; i++) {
167             elements[i] = in.readObject();
168         }
169     }
170
171     //
172
// java.io.Externalizable interface methods
173
//==========================================
174

175
176 }
177
178
Popular Tags