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.message;
021
022
023 import java.util.NoSuchElementException;
024 import javax.naming.NamingEnumeration;
025
026
027 /**
028 * A NamingEnumeration over an array of objects.
029 *
030 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
031 * @version $Rev: 494173 $
032 */
033 public class ArrayNamingEnumeration<T> implements NamingEnumeration<T>
034 {
035 /** the objects to enumerate */
036 private final T[] objects;
037
038 /** the index pointing into the array */
039 private int index = 0;
040
041
042 /**
043 * Creates a NamingEnumeration over an array of objects.
044 *
045 * @param objects
046 * the objects to enumerate over
047 */
048 public ArrayNamingEnumeration( T[] objects )
049 {
050 this.objects = objects;
051 }
052
053
054 public void close()
055 {
056 if ( objects != null )
057 {
058 index = objects.length;
059 }
060 }
061
062
063 public boolean hasMore()
064 {
065 if ( objects == null || objects.length == 0 )
066 {
067 return false;
068 }
069
070 return index < objects.length;
071 }
072
073
074 public T next()
075 {
076 if ( objects == null || objects.length == 0 || index >= objects.length )
077 {
078 throw new NoSuchElementException();
079 }
080
081 T retval = objects[index];
082 index++;
083 return retval;
084 }
085
086
087 public boolean hasMoreElements()
088 {
089 return hasMore();
090 }
091
092
093 public T nextElement()
094 {
095 return next();
096 }
097 }