View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.api.util;
21  
22  
23  import javax.naming.NamingEnumeration;
24  
25  import java.util.NoSuchElementException;
26  
27  
28  /**
29   * A NamingEnumeration over a single element.
30   * 
31   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
32   */
33  public class SingletonEnumeration<T> implements NamingEnumeration<T>
34  {
35      /** The singleton element to return */
36      private final T element;
37  
38      /** Can we return a element */
39      private boolean hasMore = true;
40  
41  
42      /**
43       * Creates a NamingEnumeration over a single element.
44       * 
45       * @param element
46       *            TODO
47       */
48      public SingletonEnumeration( final T element )
49      {
50          this.element = element;
51      }
52  
53  
54      /**
55       * Makes calls to hasMore to false even if we had more.
56       * 
57       * @see javax.naming.NamingEnumeration#close()
58       */
59      public void close()
60      {
61          hasMore = false;
62      }
63  
64  
65      /**
66       * @see javax.naming.NamingEnumeration#hasMore()
67       */
68      public boolean hasMore()
69      {
70          return hasMore;
71      }
72  
73  
74      /**
75       * @see javax.naming.NamingEnumeration#next()
76       */
77      public T next()
78      {
79          if ( hasMore )
80          {
81              hasMore = false;
82              return element;
83          }
84  
85          throw new NoSuchElementException();
86      }
87  
88  
89      /**
90       * @see java.util.Enumeration#hasMoreElements()
91       */
92      public boolean hasMoreElements()
93      {
94          return hasMore;
95      }
96  
97  
98      /**
99       * @see java.util.Enumeration#nextElement()
100      */
101     public T nextElement()
102     {
103         return next();
104     }
105 }