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 */
020package org.apache.directory.shared.util;
021
022
023import javax.naming.NamingEnumeration;
024
025import 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 */
033public class SingletonEnumeration<T> implements NamingEnumeration<T>
034{
035    /** The singleton element to return */
036    private final T element;
037
038    /** Can we return a element */
039    private boolean hasMore = true;
040
041
042    /**
043     * Creates a NamingEnumeration over a single element.
044     * 
045     * @param element
046     *            TODO
047     */
048    public SingletonEnumeration(final T element)
049    {
050        this.element = element;
051    }
052
053
054    /**
055     * Makes calls to hasMore to false even if we had more.
056     * 
057     * @see javax.naming.NamingEnumeration#close()
058     */
059    public void close()
060    {
061        hasMore = false;
062    }
063
064
065    /**
066     * @see javax.naming.NamingEnumeration#hasMore()
067     */
068    public boolean hasMore()
069    {
070        return hasMore;
071    }
072
073
074    /**
075     * @see javax.naming.NamingEnumeration#next()
076     */
077    public T next()
078    {
079        if ( hasMore )
080        {
081            hasMore = false;
082            return element;
083        }
084
085        throw new NoSuchElementException();
086    }
087
088
089    /**
090     * @see java.util.Enumeration#hasMoreElements()
091     */
092    public boolean hasMoreElements()
093    {
094        return hasMore;
095    }
096
097
098    /**
099     * @see java.util.Enumeration#nextElement()
100     */
101    public T nextElement()
102    {
103        return next();
104    }
105}