TextReadStream.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 Smalltalk.Magnitudes.Code;
import Smalltalk.Collections.*;

public class TextReadStream extends ReadStream<Character>
{

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

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

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


    /**
     * @return 
     */
    public   TextReadStream on(final String aString)
    {
      java.lang.String exitID = "TextReadStreamMetatype>>on";
      Frame f0 = new Frame(exitID);
      return f0.evaluate(() -> {
      return (TextReadStream)f0.exit(exitID, this.with(aString));
      });
    }

    /**
     * @return 
     */
    public   TextReadStream with(final String aString)
    {
      java.lang.String exitID = "TextReadStreamMetatype>>with";
      Frame f0 = new Frame(exitID);
      return f0.evaluate(() -> {
      return (TextReadStream)f0.exit(exitID, new TextReadStream(aString));
      });
    }
  }



  /**
   * 
   */
  protected    TextReadStream(final String aString)
  {
    super(aString);
    java.lang.String exitID = "TextReadStream>>TextReadStream";
    Frame f0 = new Frame(exitID);
  }

  /**
   * @return 
   */
  @Override public   String contents()
  {
    java.lang.String exitID = "TextReadStream>>contents";
    Frame f0 = new Frame(exitID);
    return (String)String.from(contents.toString());
  }

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

  /**
   * @return 
   */
  @Override public   String nextLine()
  {
    java.lang.String exitID = "TextReadStream>>nextLine";
    Frame f0 = new Frame(exitID);
    return (String)this.upTo(Character.type().newLine());
  }

  /**
   * @return 
   */
  public   String upTo(final Character target)
  {
    java.lang.String exitID = "TextReadStream>>upTo";
    Frame f0 = new Frame(exitID);
    String result = String.emptyString();

    FastInteger check = SmallInteger.from(0).faster();
    Predicate.with(f2 -> {
      return this.atEnd().or(Predicate.with(f3 -> {
      return (check.moreThan(SmallInteger.from(0)));
    }, ""));
    }, "").whileFalse(Closure.with(f2 -> {
      
    Character c = this.next();
    (target.equals(c)).ifTrue_ifFalse(Closure.with(f3 -> {
      check.increment();
    }, ""), Closure.with(f3 -> {
      result.at_put((result.size().plus(SmallInteger.from(1))), c);
    }, ""));
    }, ""));
    return (String)result;
  }
}