ReadStream.java

/**
* Copyright 2010,2021 Nikolas S Boyd.
Permission is granted to copy this work provided this copyright statement is retained in all copies.

*/
package Hoot.Streams;

import Hoot.Runtime.Functions.*;
import Hoot.Runtime.Faces.*;
import Hoot.Runtime.Values.*;
import Hoot.Runtime.Blocks.*;
import Smalltalk.Core.*;
import Smalltalk.Blocks.*;
import Smalltalk.Magnitudes.*;
import Hoot.Behaviors.*;
import Hoot.Behaviors.Nil;
import Hoot.Behaviors.Object;
import Hoot.Behaviors.True;
import Hoot.Behaviors.False;
import Hoot.Behaviors.Boolean;
import Hoot.Magnitudes.*;
import Hoot.Magnitudes.Integer;
import Hoot.Magnitudes.Character;
import Hoot.Magnitudes.Float;
import Hoot.Magnitudes.Double;
import Hoot.Collections.*;
import Hoot.Collections.String;
import Hoot.Collections.OrderedCollection;
import Hoot.Runtime.Names.Primitive;
import Smalltalk.Magnitudes.Ordinal;
import Smalltalk.Streams.StreamReader;
import Smalltalk.Collections.*;
import Smalltalk.Blocks.*;
import Smalltalk.Core.Subject;

public class ReadStream<ElementType extends Object> extends CollectionStream<ElementType> implements StreamReader<ElementType>
{

  public static Metaclass type() { return (Metaclass)Metaclass.$class; }
  @Override public Metaclass $class() { return (Metaclass)Metaclass.$class; }
  public static class Metaclass extends CollectionStream.Metaclass implements StreamReader.Metatype
  {
    static final ReadStream.Metaclass $class = new ReadStream.Metaclass();
    public Metaclass() {
      this(ReadStream.Metaclass.class);
    }

    public Metaclass(java.lang.Class aClass) {
      super(aClass);
    }

    @Override public java.lang.Class outerClass() { return ReadStream.class; }


    /**
     * @return 
     */
    public  <ElementType extends Subject> ReadStream on(final CollectedReadably<ElementType> aCollection)
    {
      java.lang.String exitID = "ReadStreamMetatype>>on";
      Frame f0 = new Frame(exitID);
      return f0.evaluate(() -> {
      return (ReadStream)f0.exit(exitID, new ReadStream(aCollection));
      });
    }
  }



  /**
   * 
   */
  protected    ReadStream(final CollectedReadably<ElementType> aCollection)
  {
    super(aCollection);
    java.lang.String exitID = "ReadStream>>ReadStream";
    Frame f0 = new Frame(exitID);
  }

  /**
   * @return 
   */
  @Override public   CollectedReadably<ElementType> contents()
  {
    java.lang.String exitID = "ReadStream>>contents";
    Frame f0 = new Frame(exitID);
    return (CollectedReadably<ElementType>)((CollectedReadably)contents);
  }

  /**
   * @return 
   */
  public   ReadStream $do(final MonadicValuable aBlock)
  {
    java.lang.String exitID = "ReadStream>>$do";
    Frame f0 = new Frame(exitID);
    Predicate.with(f2 -> {
      return this.atEnd();
    }, "").whileFalse(Closure.with(f2 -> {
      return aBlock.value(this.next());
    }, ""));
    return (ReadStream)this;
  }

  /**
   * @return 
   */
  public   ReadStream skip(final Ordinal count)
  {
    java.lang.String exitID = "ReadStream>>skip";
    Frame f0 = new Frame(exitID);
     position = (position.plus(count));
    return (ReadStream)this;
  }

  /**
   * @return 
   */
  public   Boolean skipTo(final ElementType element)
  {
    java.lang.String exitID = "ReadStream>>skipTo";
    Frame f0 = new Frame(exitID);
    Predicate.with(f2 -> {
      return (this.next().equals(element));
    }, "").whileFalse(Closure.with(f2 -> {
      
    }, ""));
    return (Boolean)this.atEnd().not();
  }

  /**
   * @return 
   */
  public   ElementType next()
  {
    java.lang.String exitID = "ReadStream>>next";
    Frame f0 = new Frame(exitID);
     position = (SmallInteger.from(1).plus(position));
    return (ElementType)this.contents().at(position);
  }

  /**
   * @return 
   */
  public   String nextLine()
  {
    java.lang.String exitID = "ReadStream>>nextLine";
    Frame f0 = new Frame(exitID);
    return (String)String.from("");
  }

  /**
   * @return 
   */
  public   OrderedCollection<ElementType> next(final Ordinal count)
  {
    java.lang.String exitID = "ReadStream>>next";
    Frame f0 = new Frame(exitID);
    OrderedCollection<ElementType> result = OrderedCollection.type().$new(count);
    SmallInteger.from(1).to_do(count, Closure.with(f2 -> {
      Ordinal index = f2.getValue(0).value();
      return result.at_put(index, this.next());
    }, "index"));
    return (OrderedCollection<ElementType>)result;
  }

  /**
   * @return 
   */
  public   Boolean nextMatchFor(final ElementType element)
  {
    java.lang.String exitID = "ReadStream>>nextMatchFor";
    Frame f0 = new Frame(exitID);
    return (Boolean)Boolean.from((this.next().equals(element)));
  }

  /**
   * @return 
   */
  public   ElementType peek()
  {
    java.lang.String exitID = "ReadStream>>peek";
    Frame f0 = new Frame(exitID);
    return f0.evaluate(() -> {
    this.atEnd().ifTrue(Closure.with(f2 -> {
      return (ElementType)f0.exit(exitID, Nil.literal());
    }, ""));
    return (ElementType)f0.exit(exitID, this.contents().at((SmallInteger.from(1).plus(position))));
    });
  }

  /**
   * @return 
   */
  public   Boolean peekFor(final ElementType element)
  {
    java.lang.String exitID = "ReadStream>>peekFor";
    Frame f0 = new Frame(exitID);
    return (Boolean)Boolean.from((this.peek().equals(element)));
  }

  /**
   * @return 
   */
  public   CollectedReadably<ElementType> upTo(final ElementType candidate)
  {
    java.lang.String exitID = "ReadStream>>upTo";
    Frame f0 = new Frame(exitID);
    OrderedCollection<ElementType> result = OrderedCollection.type().$new();
    while(!(this.atEnd().primitiveBoolean())) {
      ElementType element = this.next();
      if (element.equals(candidate).primitiveBoolean()) {
        return result;
      };
      result.add(element);
    };
    return (CollectedReadably<ElementType>)result;
  }
}