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

net.sf.staccatocommons.collections.stream.internal.algorithms.PrependStream 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 static net.sf.staccatocommons.lang.tuple.Tuples.*;
import net.sf.staccatocommons.collections.stream.AbstractStream;
import net.sf.staccatocommons.collections.stream.Stream;
import net.sf.staccatocommons.defs.Thunk;
import net.sf.staccatocommons.defs.tuple.Tuple2;
import net.sf.staccatocommons.iterators.PrependThriterator;
import net.sf.staccatocommons.iterators.thriter.Thriterator;
import net.sf.staccatocommons.lang.thunk.Thunks;

/**
 * 
 * A {@link PrependStream} is a {@link Stream} that retrieves first a single
 * element - the head - and the elements from another {@link Iterable} - the
 * tail.
 * 
 * @author flbulgarelli
 * 
 * @param 
 */
public class PrependStream extends AbstractStream {

  private final A head;
  private final Stream source;

  /**
   * Creates a new {@link PrependStream}
   */
  public PrependStream(A head, Stream source) {
    this.source = source;
    this.head = head;
  }

  protected final Stream getSource() {
    return source;
  }

  public final int size() {
    return getSource().size() + 1;
  }

  public final boolean isEmpty() {
    return false;
  }

  public Thriterator iterator() {
    return new PrependThriterator(head(), tailIterator());
  }

  @Override
  public final Tuple2> decons() {
    return _(head(), tail());
  }

  @Override
  public Tuple2, Stream> delayedDecons() {
    return _(Thunks.constant(head), tail());
  }

  @Override
  public A head() {
    return head;
  }

  @Override
  public final Stream tail() {
    return getSource();
  }

  @Override
  public final A get(int n) {
    if (n == 0)
      return head();
    return getSource().get(n - 1);
  }

  protected final Thriterator tailIterator() {
    return getSource().iterator();
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy