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 * https://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 * @param <T> The element in the enumeration 32 * 33 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 34 */ 35 public class SingletonEnumeration<T> implements NamingEnumeration<T> 36 { 37 /** The singleton element to return */ 38 private final T element; 39 40 /** Can we return a element */ 41 private boolean hasMore = true; 42 43 44 /** 45 * Creates a NamingEnumeration over a single element. 46 * 47 * @param element The element to store in this enumeration 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 @Override 61 public void close() 62 { 63 hasMore = false; 64 } 65 66 67 /** 68 * @see javax.naming.NamingEnumeration#hasMore() 69 */ 70 @Override 71 public boolean hasMore() 72 { 73 return hasMore; 74 } 75 76 77 /** 78 * @see javax.naming.NamingEnumeration#next() 79 */ 80 @Override 81 public T next() 82 { 83 if ( hasMore ) 84 { 85 hasMore = false; 86 return element; 87 } 88 89 throw new NoSuchElementException(); 90 } 91 92 93 /** 94 * @see java.util.Enumeration#hasMoreElements() 95 */ 96 @Override 97 public boolean hasMoreElements() 98 { 99 return hasMore; 100 } 101 102 103 /** 104 * @see java.util.Enumeration#nextElement() 105 */ 106 @Override 107 public T nextElement() 108 { 109 return next(); 110 } 111 }