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 * @param <T> The element in the enumeration 33 */ 34 public class SingletonEnumeration<T> implements NamingEnumeration<T> 35 { 36 /** The singleton element to return */ 37 private final T element; 38 39 /** Can we return a element */ 40 private boolean hasMore = true; 41 42 43 /** 44 * Creates a NamingEnumeration over a single element. 45 * 46 * @param element 47 * TODO 48 */ 49 public SingletonEnumeration( final T element ) 50 { 51 this.element = element; 52 } 53 54 55 /** 56 * Makes calls to hasMore to false even if we had more. 57 * 58 * @see javax.naming.NamingEnumeration#close() 59 */ 60 public void close() 61 { 62 hasMore = false; 63 } 64 65 66 /** 67 * @see javax.naming.NamingEnumeration#hasMore() 68 */ 69 public boolean hasMore() 70 { 71 return hasMore; 72 } 73 74 75 /** 76 * @see javax.naming.NamingEnumeration#next() 77 */ 78 public T next() 79 { 80 if ( hasMore ) 81 { 82 hasMore = false; 83 return element; 84 } 85 86 throw new NoSuchElementException(); 87 } 88 89 90 /** 91 * @see java.util.Enumeration#hasMoreElements() 92 */ 93 public boolean hasMoreElements() 94 { 95 return hasMore; 96 } 97 98 99 /** 100 * @see java.util.Enumeration#nextElement() 101 */ 102 public T nextElement() 103 { 104 return next(); 105 } 106 }