All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.helger.commons.collection.iterate.MapperIterator Maven / Gradle / Ivy

There is a newer version: 11.1.10
Show newest version
/*
 * Copyright (C) 2014-2022 Philip Helger (www.helger.com)
 * philip[at]helger[dot]com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.helger.commons.collection.iterate;

import java.util.Iterator;
import java.util.function.Function;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import com.helger.commons.ValueEnforcer;
import com.helger.commons.string.ToStringGenerator;

/**
 * A simple iterator that changes the object type from a source type to a
 * destination type.
 *
 * @author Philip Helger
 * @param 
 *        The type of the source iterator
 * @param 
 *        The type of this iterator
 */
public class MapperIterator  implements IIterableIterator 
{
  // base iterator
  private final Iterator  m_aBaseIter;
  // the converter to use
  private final Function  m_aConverter;

  /**
   * Constructor.
   *
   * @param aBaseIter
   *        The base iterable iterator to use. May not be null.
   * @param aConverter
   *        The converter to be used. May not be null.
   */
  public MapperIterator (@Nonnull final IIterableIterator  aBaseIter,
                         @Nonnull final Function  aConverter)
  {
    this (aBaseIter.iterator (), aConverter);
  }

  /**
   * Constructor.
   *
   * @param aBaseCont
   *        The collection to iterate. May not be null.
   * @param aConverter
   *        The converter to be used. May not be null.
   */
  public MapperIterator (@Nonnull final Iterable  aBaseCont,
                         @Nonnull final Function  aConverter)
  {
    this (aBaseCont.iterator (), aConverter);
  }

  /**
   * Constructor.
   *
   * @param aBaseIter
   *        The base iterator to use. May not be null.
   * @param aConverter
   *        The converter to be used. May not be null.
   */
  public MapperIterator (@Nonnull final Iterator  aBaseIter,
                         @Nonnull final Function  aConverter)
  {
    m_aBaseIter = ValueEnforcer.notNull (aBaseIter, "BaseIterator");
    m_aConverter = ValueEnforcer.notNull (aConverter, "Filter");
  }

  /**
   * @return The converter as specified in the constructor.
   */
  @Nonnull
  public Function  getConverter ()
  {
    return m_aConverter;
  }

  public boolean hasNext ()
  {
    return m_aBaseIter.hasNext ();
  }

  @Nullable
  public ELEMENTTYPE next ()
  {
    final SRCTYPE ret = m_aBaseIter.next ();
    return m_aConverter.apply (ret);
  }

  @Override
  public void remove ()
  {
    m_aBaseIter.remove ();
  }

  // equals and hashCode wont work, because standard Java iterators don't
  // implement this!

  @Override
  public String toString ()
  {
    return new ToStringGenerator (this).append ("BaseIter", m_aBaseIter).append ("Converter", m_aConverter).getToString ();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy