001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.harmony.jndi.provider.ldap;
019:
020: import java.util.ArrayList;
021: import java.util.Collection;
022: import java.util.NoSuchElementException;
023:
024: import javax.naming.NamingEnumeration;
025: import javax.naming.NamingException;
026:
027: /**
028: * TODO: dynamic load elements from server
029: */
030: public class LdapNamingEnumeration<T> implements NamingEnumeration<T> {
031:
032: private ArrayList<T> values;
033:
034: private int currentIndex;
035:
036: private NamingException exception;
037:
038: /**
039: * <code>list</code> and <code>ex</code> both can be <code>null</code>,
040: * <code>null</code> of <code>list</code> will be treated as empty List.
041: *
042: * @param list
043: * All elements to be enumerate
044: * @param ex
045: * exception would be thrown when over iterate
046: */
047: public LdapNamingEnumeration(Collection<T> list, NamingException ex) {
048: if (list == null) {
049: values = new ArrayList<T>();
050: } else {
051: values = new ArrayList<T>(list);
052: }
053:
054: exception = ex;
055: currentIndex = 0;
056: }
057:
058: /**
059: * release all relative resources, current implementation just set
060: * enumeration values to <code>null</code>.
061: */
062: public void close() throws NamingException {
063: // no other resources need to release
064: values = null;
065: }
066:
067: public boolean hasMore() throws NamingException {
068: if (values == null) {
069: return false;
070: }
071:
072: if (currentIndex < values.size()) {
073: return true;
074: }
075: // no elemnts to iterate, release resource first
076: close();
077: if (exception != null) {
078: throw exception;
079: }
080: return false;
081: }
082:
083: /**
084: * Retrieves the next element. <code>NoSuchElementException</code> will be
085: * thrown, if there is no other elements or <code>close()</code> has been
086: * invoked.
087: */
088: public T next() throws NamingException {
089: if (values == null || currentIndex >= values.size()) {
090: throw new NoSuchElementException();
091: }
092:
093: return values.get(currentIndex++);
094: }
095:
096: public boolean hasMoreElements() {
097: if (values == null) {
098: return false;
099: }
100:
101: if (currentIndex < values.size()) {
102: return true;
103: }
104: return false;
105: }
106:
107: public T nextElement() {
108: if (values == null || currentIndex >= values.size()) {
109: throw new NoSuchElementException();
110: }
111:
112: return values.get(currentIndex++);
113: }
114:
115: protected void setException(NamingException exception) {
116: this .exception = exception;
117: }
118:
119: void add(T pair) {
120: values.add(pair);
121: }
122:
123: }
|