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

net.sf.staccatocommons.collections.stream.impl.internal.MemorizedStream Maven / Gradle / Ivy

/*
 Copyright (c) 2011, The Staccato-Commons 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.impl.internal;

import java.util.NoSuchElementException;

import net.sf.staccatocommons.collections.stream.AbstractStream;
import net.sf.staccatocommons.collections.stream.Stream;
import net.sf.staccatocommons.defs.Thunk;
import net.sf.staccatocommons.iterators.EmptyThriterator;
import net.sf.staccatocommons.iterators.thriter.AdvanceThriterator;
import net.sf.staccatocommons.iterators.thriter.Thriterator;

/**
 * @author flbulgarelli
 * 
 */
public class MemorizedStream extends AbstractStream {

  private SingleLinkedDelayedQueue previous = new SingleLinkedDelayedQueue();

  private Thriterator remaining;

  /**
   * 
   * Creates a new {@link MemorizedStream}
   */
  public MemorizedStream(Thriterator source) {
    this.remaining = source;
  }

  @Override
  public boolean isEmpty() {
    return previous.isEmpty() && !remaining.hasNext();
  }

  @Override
  public Stream memorize() {
    return this;
  }

  @Override
  public Thriterator iterator() {
    if (remaining.isEmpty()) {
      if (previous.isEmpty())
        return EmptyThriterator.empty();
      return previous.iterator();
    }
    final Thriterator previousIter = previous.iterator();
    return new AdvanceThriterator() {
      private Thunk current;
      private Thriterator iter = previousIter;
      private boolean remainingIterationStarted = false;

      public boolean hasNext() {
        if (remainingIterationStarted)
          return iter.hasNext();
        return remaining.hasNext();
      }

      public void advanceNext() throws NoSuchElementException {
        if (!remainingIterationStarted && !iter.hasNext()) {
          iter = newRemaningIterator();
          remainingIterationStarted = true;
        }
        iter.advanceNext();
      }

      public A current() {
        return iter.current();
      }

      @Override
      public Thunk delayedCurrent() {
        return iter.delayedCurrent();
      }

      public Thriterator newRemaningIterator() {
        return new AdvanceThriterator() {
          public boolean hasNext() {
            return remaining.hasNext();
          }

          public void advanceNext() throws NoSuchElementException {
            remaining.advanceNext();
            current = remaining.delayedCurrent();
            previous.add(current);
          }

          public A current() {
            return delayedCurrent().value();
          }

          @Override
          public Thunk delayedCurrent() {
            return current;
          }
        };
      }
    };
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy