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