WriteStream.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.Core.Subject;
import Smalltalk.Streams.StreamWriter;
import Smalltalk.Collections.*;

public class WriteStream<ElementType extends Object> extends CollectionStream<ElementType> implements StreamWriter<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 StreamWriter.Metatype
  {
    static final WriteStream.Metaclass $class = new WriteStream.Metaclass();
    public Metaclass() {
      this(WriteStream.Metaclass.class);
    }

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

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


    /**
     * @return 
     */
    public  <ElementType extends Subject> WriteStream with(final CollectedVariably<ElementType> aCollection)
    {
      java.lang.String exitID = "WriteStreamMetatype>>with";
      Frame f0 = new Frame(exitID);
      return f0.evaluate(() -> {
      return (WriteStream)f0.exit(exitID, new WriteStream(aCollection));
      });
    }

    /**
     * @return 
     */
    public   String lineSeparator()
    {
      java.lang.String exitID = "WriteStreamMetatype>>lineSeparator";
      Frame f0 = new Frame(exitID);
      return f0.evaluate(() -> {
      return (String)f0.exit(exitID, LineSeparator);
      });
    }
  }


  protected static final java.lang.String SeparatorName = "line.separator";
  protected static final String LineSeparator = String.from(Primitive.systemValue(SeparatorName));

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

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

  /**
   * @return 
   */
  public   WriteStream flush()
  {
    java.lang.String exitID = "WriteStream>>flush";
    Frame f0 = new Frame(exitID);
    return (WriteStream)this;
  }

  /**
   * @return 
   */
  public   WriteStream cr()
  {
    java.lang.String exitID = "WriteStream>>cr";
    Frame f0 = new Frame(exitID);
    this.nextPutAll(((CollectedReadably)LineSeparator));
    return (WriteStream)this;
  }

  /**
   * @return 
   */
  public   WriteStream tab()
  {
    java.lang.String exitID = "WriteStream>>tab";
    Frame f0 = new Frame(exitID);
    this.nextPut(((ElementType)Character.type().tab()));
    return (WriteStream)this;
  }

  /**
   * @return 
   */
  public   WriteStream space()
  {
    java.lang.String exitID = "WriteStream>>space";
    Frame f0 = new Frame(exitID);
    this.nextPut(((ElementType)Character.type().space()));
    return (WriteStream)this;
  }

  /**
   * @return 
   */
  public   WriteStream nextPut(final ElementType element)
  {
    java.lang.String exitID = "WriteStream>>nextPut";
    Frame f0 = new Frame(exitID);
    this.collectsElements(((Object)element)).ifTrue(Closure.with(f2 -> {
      this.contents().add(element);

     position = (position.plus(SmallInteger.from(1)));
    }, ""));
    return (WriteStream)this;
  }

  /**
   * @return 
   */
  public   WriteStream nextPutAll(final CollectedReadably<ElementType> elements)
  {
    java.lang.String exitID = "WriteStream>>nextPutAll";
    Frame f0 = new Frame(exitID);
    elements.notEmpty().ifTrue(Closure.with(f2 -> {
      return this.collectsElements(((Object)elements.first())).ifTrue(Closure.with(f3 -> {
      this.contents().addAll(elements);

     position = (position.plus(elements.size()));
    }, ""));
    }, ""));
    return (WriteStream)this;
  }
}