KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > naming > context > NamingEnumerationImpl


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@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: NamingEnumerationImpl.java 118 2006-03-05 19:47:51Z benoitf $
23  * --------------------------------------------------------------------------
24  */

25
26
27 package org.objectweb.easybeans.naming.context;
28
29 import java.util.Enumeration JavaDoc;
30 import java.util.Hashtable JavaDoc;
31 import java.util.NoSuchElementException JavaDoc;
32
33 import javax.naming.NameClassPair JavaDoc;
34 import javax.naming.NamingEnumeration JavaDoc;
35 import javax.naming.NamingException JavaDoc;
36
37 /**
38  * Implementation of the NamingEnumeration for list operations Each element is
39  * of type NameClassPair.
40  * @author Florent Benoit
41  */

42 public class NamingEnumerationImpl implements NamingEnumeration JavaDoc<NameClassPair JavaDoc> {
43
44     /**
45      * list of names.
46      */

47     private Enumeration JavaDoc names;
48
49     /**
50      * List of bindings.
51      */

52     private Hashtable JavaDoc bindings;
53
54     /**
55      * Constructor. Called by list()
56      * @param bindings list of bindings
57      */

58     NamingEnumerationImpl(final Hashtable JavaDoc bindings) {
59         this.bindings = bindings;
60         this.names = bindings.keys();
61     }
62
63     /**
64      * Determines whether there are any more elements in the enumeration.
65      * @return true if there is more in the enumeration ; false otherwise.
66      * @throws NamingException If a naming exception is encountered while
67      * attempting to determine whether there is another element in the
68      * enumeration.
69      */

70     public boolean hasMore() throws NamingException JavaDoc {
71         return names.hasMoreElements();
72     }
73
74     /**
75      * Retrieves the next element in the enumeration.
76      * @return The possibly null element in the enumeration. null is only valid
77      * for enumerations that can return null (e.g. Attribute.getAll()
78      * returns an enumeration of attribute values, and an attribute
79      * value can be null).
80      * @throws NamingException If a naming exception is encountered while
81      * attempting to retrieve the next element. See NamingException and
82      * its subclasses for the possible naming exceptions.
83      */

84     public NameClassPair JavaDoc next() throws NamingException JavaDoc {
85         String JavaDoc name = (String JavaDoc) names.nextElement();
86         String JavaDoc className = bindings.get(name).getClass().getName();
87         return new NameClassPair JavaDoc(name, className);
88     }
89
90     /**
91      * Closes this enumeration.
92      */

93     public void close() {
94     }
95
96     /**
97      * Returns the next element of this enumeration if this enumeration object
98      * has at least one more element to provide.
99      * @return the next element of this enumeration.
100      */

101     public NameClassPair JavaDoc nextElement() {
102         try {
103             return next();
104         } catch (NamingException JavaDoc e) {
105             throw new NoSuchElementException JavaDoc(e.toString());
106         }
107     }
108
109     /**
110      * Tests if this enumeration contains more elements.
111      * @return <code>true</code> if and only if this enumeration object
112      * contains at least one more element to provide; <code>false</code>
113      * otherwise.
114      */

115     public boolean hasMoreElements() {
116         try {
117             return hasMore();
118         } catch (NamingException JavaDoc e) {
119             return false;
120         }
121     }
122
123 }
124
Popular Tags