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

net.sf.staccatocommons.collections.stream.internal.algorithms.MapStream Maven / Gradle / Ivy

Go to download

Collections library of the Staccatissimo project, focused on providing new abstractions that mix object oriented and functional programming style for dealing with iterable objects.

The newest version!
/**
 *  Copyright (c) 2010-2012, The StaccatoCommons Team
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation; version 3 of the License.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 */

package net.sf.staccatocommons.collections.stream.internal.algorithms;

import net.sf.staccatocommons.collections.internal.iterator.MapIterator;
import net.sf.staccatocommons.collections.stream.AbstractStream;
import net.sf.staccatocommons.collections.stream.Stream;
import net.sf.staccatocommons.defs.function.Function;
import net.sf.staccatocommons.iterators.thriter.Thriterator;
import net.sf.staccatocommons.restrictions.check.NonNull;

/**
 * @author flbulgarelli
 * 
 * @param 
 */
public final class MapStream extends AbstractStream {
  private final Stream stream;
  private final Function function;

  /**
   * Creates a new {@link MapStream}
   */
  public MapStream(@NonNull Stream stream, @NonNull Function function) {
    this.stream = stream;
    this.function = function;
  }

  public Thriterator iterator() {
    return new MapIterator(function, stream.iterator());
  }

  @Override
  public B get(int n) {
    return function.apply(stream.get(n));
  }

  @Override
  public int size() {
    return stream.size();
  }

  @Override
  public boolean isEmpty() {
    return stream.isEmpty();
  }

  @Override
  public  Stream map(final Function function) {
    return new MapStream(stream, function.of(this.function));
  }

}