001    /*
002     *  Licensed to the Apache Software Foundation (ASF) under one
003     *  or more contributor license agreements.  See the NOTICE file
004     *  distributed with this work for additional information
005     *  regarding copyright ownership.  The ASF licenses this file
006     *  to you under the Apache License, Version 2.0 (the
007     *  "License"); you may not use this file except in compliance
008     *  with the License.  You may obtain a copy of the License at
009     *  
010     *    http://www.apache.org/licenses/LICENSE-2.0
011     *  
012     *  Unless required by applicable law or agreed to in writing,
013     *  software distributed under the License is distributed on an
014     *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     *  KIND, either express or implied.  See the License for the
016     *  specific language governing permissions and limitations
017     *  under the License. 
018     *  
019     */
020    package org.apache.directory.shared.ldap.util;
021    
022    
023    import javax.naming.NamingEnumeration;
024    
025    import java.util.NoSuchElementException;
026    
027    
028    /**
029     * A NamingEnumeration over a single element.
030     * 
031     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
032     * @version $Revision: 575496 $
033     */
034    public class SingletonEnumeration<T> implements NamingEnumeration<T>
035    {
036        /** The singleton element to return */
037        private final T element;
038    
039        /** Can we return a element */
040        private boolean hasMore = true;
041    
042    
043        /**
044         * Creates a NamingEnumeration over a single element.
045         * 
046         * @param element
047         *            TODO
048         */
049        public SingletonEnumeration(final T element)
050        {
051            this.element = element;
052        }
053    
054    
055        /**
056         * Makes calls to hasMore to false even if we had more.
057         * 
058         * @see javax.naming.NamingEnumeration#close()
059         */
060        public void close()
061        {
062            hasMore = false;
063        }
064    
065    
066        /**
067         * @see javax.naming.NamingEnumeration#hasMore()
068         */
069        public boolean hasMore()
070        {
071            return hasMore;
072        }
073    
074    
075        /**
076         * @see javax.naming.NamingEnumeration#next()
077         */
078        public T next()
079        {
080            if ( hasMore )
081            {
082                hasMore = false;
083                return element;
084            }
085    
086            throw new NoSuchElementException();
087        }
088    
089    
090        /**
091         * @see java.util.Enumeration#hasMoreElements()
092         */
093        public boolean hasMoreElements()
094        {
095            return hasMore;
096        }
097    
098    
099        /**
100         * @see java.util.Enumeration#nextElement()
101         */
102        public T nextElement()
103        {
104            return next();
105        }
106    }