ByteArray.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.Collections;

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.Magnitudes.Integer;
import Hoot.Runtime.Names.Primitive;
import org.apache.commons.codec.binary.*;
import Smalltalk.Core.Subject;
import Smalltalk.Collections.*;
import Smalltalk.Magnitudes.Ordinal;

public class ByteArray extends ArrayedCollection
{

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

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

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


    /**
     * @return 
     */
    @Override public   ByteArray $new()
    {
      java.lang.String exitID = "ByteArrayMetatype>>$new";
      Frame f0 = new Frame(exitID);
      return f0.evaluate(() -> {
      return (ByteArray)f0.exit(exitID, this.$new(SmallInteger.from(0)));
      });
    }

    /**
     * @return 
     */
    @Override public   ByteArray $new(final Ordinal capacity)
    {
      java.lang.String exitID = "ByteArrayMetatype>>$new";
      Frame f0 = new Frame(exitID);
      return f0.evaluate(() -> {
      return (ByteArray)f0.exit(exitID, new ByteArray(capacity));
      });
    }

    /**
     * @return 
     */
    public   ByteArray withAll(final byte ... elements)
    {
      java.lang.String exitID = "ByteArrayMetatype>>withAll";
      Frame f0 = new Frame(exitID);
      return f0.evaluate(() -> {
      return (ByteArray)f0.exit(exitID, new ByteArray(elements));
      });
    }

    /**
     * @return 
     */
    public   ByteArray withAll(final Array<Integer> byteValues)
    {
      java.lang.String exitID = "ByteArrayMetatype>>withAll";
      Frame f0 = new Frame(exitID);
      return f0.evaluate(() -> {
      ByteArray results = ByteArray.type().$new(byteValues.size());

      FastInteger index = SmallInteger.from(1).faster();
      byteValues.$do(Closure.with(f3 -> {
        Integer v = f3.getValue(0).value();
        results.at_put(index, ((Ordinal)v));
      index.increment();
      }, "v"));
      return (ByteArray)f0.exit(exitID, results);
      });
    }
  }


  protected byte [] contents = {
  };

  /**
   * 
   */
  protected    ByteArray()
  {
    java.lang.String exitID = "ByteArray>>ByteArray";
    Frame f0 = new Frame(exitID);
  }

  /**
   * 
   */
  protected    ByteArray(final byte ... elements)
  {
    java.lang.String exitID = "ByteArray>>ByteArray";
    Frame f0 = new Frame(exitID);
     contents = elements;
  }

  /**
   * 
   */
  protected    ByteArray(final Ordinal capacity)
  {
    java.lang.String exitID = "ByteArray>>ByteArray";
    Frame f0 = new Frame(exitID);
     contents = new byte[capacity.intValue()];
  }

  /**
   * @return 
   */
  public   byte [] primitiveContents()
  {
    java.lang.String exitID = "ByteArray>>primitiveContents";
    Frame f0 = new Frame(exitID);
    return (byte [])contents;
  }

  /**
   * @return 
   */
  @Override public   Integer size()
  {
    java.lang.String exitID = "ByteArray>>size";
    Frame f0 = new Frame(exitID);
    return (Integer)SmallInteger.from(Primitive.length(contents));
  }

  /**
   * @return 
   */
  @Override protected   Integer get(final int index)
  {
    java.lang.String exitID = "ByteArray>>get";
    Frame f0 = new Frame(exitID);
    return (Integer)SmallInteger.from(contents[index]);
  }

  /**
   * @return 
   */
  protected   Integer add_at(final Object element, final int index)
  {
    java.lang.String exitID = "ByteArray>>add_at";
    Frame f0 = new Frame(exitID);
    contents[index] = ((byte)((Ordinal)element).intValue());
    return (Integer)SmallInteger.from(((Ordinal)element).intValue());
  }

  /**
   * @return 
   */
  @Override public   Integer at_ifAbsent(final Ordinal index, final NiladicValuable aBlock)
  {
    java.lang.String exitID = "ByteArray>>at_ifAbsent";
    Frame f0 = new Frame(exitID);
    return (Integer)this.get(this.checkIndex_ifAbsent(index, aBlock));
  }

  /**
   * @return 
   */
  public   Integer at_put(final Ordinal index, final Ordinal element)
  {
    java.lang.String exitID = "ByteArray>>at_put";
    Frame f0 = new Frame(exitID);
    return (Integer)this.add_at(((Magnitude)element), this.checkIndex(index));
  }

  /**
   * @return 
   */
  @Override public   int hashCode()
  {
    java.lang.String exitID = "ByteArray>>hashCode";
    Frame f0 = new Frame(exitID);
    return (int)this.primitiveContents().hashCode();
  }

  /**
   * @return 
   */
  public   Boolean equals(final ByteArray aCollection)
  {
    java.lang.String exitID = "ByteArray>>equals";
    Frame f0 = new Frame(exitID);
    return (Boolean)Boolean.from(contents.equals(aCollection.primitiveContents()));
  }

  /**
   * @return 
   */
  @Override public   Boolean equals(final Collected aCollection)
  {
    java.lang.String exitID = "ByteArray>>equals";
    Frame f0 = new Frame(exitID);
    if (aCollection instanceof ByteArray) {
      return this.equals(((ByteArray)aCollection));
    };
    return (Boolean)aCollection.asArray().equals(this);
  }

  /**
   * @return 
   */
  @Override public   ByteArray $do(final MonadicValuable aBlock)
  {
    java.lang.String exitID = "ByteArray>>$do";
    Frame f0 = new Frame(exitID);
    SmallInteger.from(1).to_do(this.size(), Closure.with(f2 -> {
      Integer index = f2.getValue(0).value();
      return aBlock.value(this.get((index.minus(SmallInteger.from(1))).primitiveInteger()));
    }, "index"));
    return (ByteArray)this;
  }

  /**
   * @return 
   */
  protected   java.lang.String rawHex()
  {
    java.lang.String exitID = "ByteArray>>rawHex";
    Frame f0 = new Frame(exitID);
    return (java.lang.String)Hex.encodeHexString(this.primitiveContents());
  }

  /**
   * @return 
   */
  public   String hexString()
  {
    java.lang.String exitID = "ByteArray>>hexString";
    Frame f0 = new Frame(exitID);
    return (String)String.from(this.rawHex());
  }

  /**
   * @return 
   */
  @Override public   String printString()
  {
    java.lang.String exitID = "ByteArray>>printString";
    Frame f0 = new Frame(exitID);
    return (String)(String.from("#").append(this.hexString()));
  }

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

  /**
   * @return 
   */
  public   Subject removeFirst()
  {
    java.lang.String exitID = "ByteArray>>removeFirst";
    Frame f0 = new Frame(exitID);
    this.immutabilityError();
    return (Subject)SmallInteger.from(0);
  }

  /**
   * @return 
   */
  public   Subject removeLast()
  {
    java.lang.String exitID = "ByteArray>>removeLast";
    Frame f0 = new Frame(exitID);
    this.immutabilityError();
    return (Subject)SmallInteger.from(0);
  }

  /**
   * @return 
   */
  public   Subject removeAtIndex(final Ordinal index)
  {
    java.lang.String exitID = "ByteArray>>removeAtIndex";
    Frame f0 = new Frame(exitID);
    this.immutabilityError();
    return (Subject)SmallInteger.from(0);
  }

  /**
   * @return 
   */
  public   Subject add(final Subject element)
  {
    java.lang.String exitID = "ByteArray>>add";
    Frame f0 = new Frame(exitID);
    this.immutabilityError();
    return (Subject)SmallInteger.from(0);
  }
}