KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas_ejb > lib > CollectionEnum


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999-2004 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: CollectionEnum.java,v 1.3 2004/09/17 08:25:02 joaninh Exp $
23  * --------------------------------------------------------------------------
24  */

25
26
27 package org.objectweb.jonas_ejb.lib;
28
29 import java.io.Serializable JavaDoc;
30 import java.lang.Object JavaDoc;
31 import java.util.Enumeration JavaDoc;
32 import java.util.NoSuchElementException JavaDoc;
33 import java.util.Vector JavaDoc;
34
35 /**
36  * This class implements the java.util.Enumeration and the java.io.Serializable interfaces.
37  * <br>
38  * This class is used as the return value type of the implementation of the finder methods
39  * which return a collection.
40  * <br>
41  * Indeed, the EJB spec. tells that the type for a collection is the java.util.Enumeration.
42  * In addition the return value type must be legal type for Java RMI (ie. serializable type).
43  * @author Helene Joanin : Initial developer
44  */

45
46 public class CollectionEnum implements Serializable JavaDoc, Enumeration JavaDoc {
47
48     /**
49      * @serial
50      */

51     private Vector JavaDoc mCollection = null;
52     /**
53      * @serial
54      */

55     private int mIndex = 0;
56
57     /**
58      * Create an empty CollectionEnum
59      *
60      */

61     public CollectionEnum() {
62     mCollection = new Vector JavaDoc();
63     mIndex = 0;
64     }
65
66     /**
67      * Add an element
68      */

69     public synchronized void addElement(Object JavaDoc obj) {
70     mCollection.addElement(obj);
71     }
72
73     /**
74      * Create a CollectionEnum from a vector
75      */

76     public CollectionEnum(Vector JavaDoc v) {
77     mCollection = new Vector JavaDoc();
78     for (int i=0; i<v.size(); i++) {
79         mCollection.addElement(v.elementAt(i));
80     }
81     mIndex = 0;
82     }
83
84     /**
85      * Implements Enumeration.hasMoreElements()
86      */

87     public boolean hasMoreElements() {
88     return(mIndex<mCollection.size());
89     }
90
91     /**
92      * Implements Enumeration.nextElement()
93      */

94     public Object JavaDoc nextElement() throws NoSuchElementException JavaDoc {
95     if (mIndex>=mCollection.size()) {
96         throw new NoSuchElementException JavaDoc("CollectionEnum ("+mIndex+">="+mCollection.size()+")");
97     }
98     mIndex++;
99     return(mCollection.elementAt(mIndex-1));
100     }
101
102 }
103
104
Popular Tags