001/*
002 * CDDL HEADER START
003 *
004 * The contents of this file are subject to the terms of the
005 * Common Development and Distribution License, Version 1.0 only
006 * (the "License").  You may not use this file except in compliance
007 * with the License.
008 *
009 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
010 * or http://forgerock.org/license/CDDLv1.0.html.
011 * See the License for the specific language governing permissions
012 * and limitations under the License.
013 *
014 * When distributing Covered Code, include this CDDL HEADER in each
015 * file and include the License file at legal-notices/CDDLv1_0.txt.
016 * If applicable, add the following below this CDDL HEADER, with the
017 * fields enclosed by brackets "[]" replaced with your own identifying
018 * information:
019 *      Portions Copyright [yyyy] [name of copyright owner]
020 *
021 * CDDL HEADER END
022 *
023 *
024 *      Copyright 2006-2008 Sun Microsystems, Inc.
025 */
026package org.opends.server.backends.jeb;
027
028import java.util.Iterator;
029import java.util.NoSuchElementException;
030
031/**
032 * Iterator for a set of Entry IDs.  It must return values in order of ID.
033 */
034public class IDSetIterator implements Iterator<EntryID>
035{
036  /**
037   * An array of ID values in order of ID.
038   */
039  private long[] entryIDList;
040
041  /**
042   * Current position of the iterator as an index into the array of IDs.
043   */
044  private int i;
045
046  /**
047   * Create a new iterator for a given array of entry IDs.
048   * @param entryIDList An array of IDs in order or ID.
049   */
050  public IDSetIterator(long[] entryIDList)
051  {
052    this.entryIDList = entryIDList;
053  }
054
055  /**
056   * Create a new iterator for a given array of entry IDs.
057   * @param entryIDList An array of IDs in order or ID.
058   * @param begin The entry ID of the first entry that should be returned, or
059   *              {@code null} if it should start at the beginning of the list.
060   */
061  public IDSetIterator(long[] entryIDList, EntryID begin)
062  {
063    this.entryIDList = entryIDList;
064
065    if (begin == null)
066    {
067      i = 0;
068    }
069    else
070    {
071      for (i=0; i < entryIDList.length; i++)
072      {
073        if (entryIDList[i] == begin.longValue())
074        {
075          break;
076        }
077      }
078
079      if (i >= entryIDList.length)
080      {
081        i = 0;
082      }
083    }
084  }
085
086  /**
087   * Returns <tt>true</tt> if the iteration has more elements. (In other
088   * words, returns <tt>true</tt> if <tt>next</tt> would return an element
089   * rather than throwing an exception.)
090   *
091   * @return <tt>true</tt> if the iterator has more elements.
092   */
093  public boolean hasNext()
094  {
095    return i < entryIDList.length;
096  }
097
098  /**
099   * Returns the next element in the iteration.  Calling this method
100   * repeatedly until the {@link #hasNext()} method returns false will
101   * return each element in the underlying collection exactly once.
102   *
103   * @return the next element in the iteration.
104   * @throws java.util.NoSuchElementException
105   *          iteration has no more elements.
106   */
107  public EntryID next()
108       throws NoSuchElementException
109  {
110    if (i < entryIDList.length)
111    {
112      return new EntryID(entryIDList[i++]);
113    }
114    throw new NoSuchElementException();
115  }
116
117  /**
118   *
119   * Removes from the underlying collection the last element returned by the
120   * iterator (optional operation).  This method can be called only once per
121   * call to <tt>next</tt>.  The behavior of an iterator is unspecified if
122   * the underlying collection is modified while the iteration is in
123   * progress in any way other than by calling this method.
124   *
125   * @exception UnsupportedOperationException if the <tt>remove</tt>
126   *            operation is not supported by this Iterator.
127   */
128  public void remove() throws UnsupportedOperationException
129  {
130    throw new UnsupportedOperationException();
131  }
132}