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