001/*
002 * JPPF.
003 * Copyright (C) 2005-2018 JPPF Team.
004 * http://www.jppf.org
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * 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, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.jppf.utils.collections;
019
020import java.util.*;
021
022/**
023 * Wraps an Iterator as an Enumeration
024 * @param <T> the type of the enumerated objects.
025 */
026public class IteratorEnumeration<T> implements Enumeration<T>
027{
028  /**
029   * The iterator to wrap as an enumeration.
030   */
031  private Iterator<T> iterator = null;
032
033  /**
034   * Initialize this enumeration with the specified iterator.
035   * @param iterator the iterator to wrap as an enumeration.
036   */
037  public IteratorEnumeration(final Iterator<T> iterator)
038  {
039    this.iterator = iterator;
040  }
041
042  /**
043   * Tests if this enumeration contains more elements.
044   * @return true if and only if this enumeration object contains at least one more element to provide, false otherwise.
045   * @see java.util.Enumeration#hasMoreElements()
046   */
047  @Override
048  public boolean hasMoreElements()
049  {
050    return iterator.hasNext();
051  }
052
053  /**
054   * Returns the next element of this enumeration if this enumeration object has at least one more element to provide.
055   * @return the next element of this enumeration.
056   * @exception NoSuchElementException if no more elements exist.
057   * @see java.util.Enumeration#nextElement()
058   */
059  @Override
060  public T nextElement()
061  {
062    if (!iterator.hasNext()) throw new NoSuchElementException("this enumeration has no more element to provide");
063    return iterator.next();
064  }
065}