Primitive Types in PolyJava

As of version 0.1.5, PolyJava (formerly PJava) allows parameterized types to be instantiated with Java primitive types, or Java array types as their parameters. For the purpose of constraint satisfation, the PolyJava translator behaves as though these primitive types implemented a number of methods. For example, consider the following code:
class Summation[[NumT]] where NumT { NumT add(NumT); }
{
   public NumT val;

   public Summation(NumT initial_value) { val = initial_value; }
   public void AddValue(NumT value) { val = val.add(value); }
}
public class Demo {
   public static void main(String[] args) {
      Summation[[int]] SI = new Summation[[int]](3);
      SI.AddValue(4);
      SI.AddValue(3);
      System.out.println(SI.val);
   }
}
Note that the type int satisfies the constraint on NumT, even though Java primitive types do not have methods of their own. As another example, consider the following generic bubble-sort routine.
class Sorter[[ContainerT,ElemT]] 
           where ContainerT { ElemT fetch(int);
                              void store(ElemT,int);
                              int length(); }
           // a.compareTo(b) returns -1 if a < b, 0 if a == b, 1 if a > b.
           where ElemT { int compareTo(ElemT); }
{
   public static void BubbleSort(ContainerT c) {
      boolean ordered = false;
      while (!ordered) {
         ordered = true;
         for(int i = 0; i < c.length()-1; ++i) {
            if (c.fetch(i).compareTo(c.fetch(i+1)) > 0) {
               ordered = false;
               ElemT elem = c.fetch(i);;
               c.store(c.fetch(i+1), i);
               c.store(elem, i+1);
            }
         }
      }
   }
}
public class Demo
{
   public static void main(String[] args) {
      long[] longArray = new long[4];
      longArray[0]=5; longArray[1]=4; longArray[2]=1; longArray[3]=4;
      Sorter[[long[],long]].BubbleSort(longArray);
      for(int i = 0; i < 4; ++i) {
         System.out.println(longArray[i]);
      }
   }
}

Methods

All of the primitive types and array types act as if they implemented a number of methods for the purpose of constraint satisfaction.

All types

All primitive types and array types implement the following methods:
Object clone()
Returns a copy of this Object. Since primitive types are immutable, this is an identity function for them. For arrays, it preforms a shallow copy of elements.
boolen equals(Object obj)
Returns true iff this is equal to obj. For booleans and characters, tests strict equality. For all numeric types, returns true iff obj is a wrapped-numeric type representing the same number as this. For arrays, returns true if this and obj are the same object.
String toString()
Returns a string representing this. For primitive types, this method calls an appropriate toString method (i.e. java.lang.Integer.toString). For arrays, this method calls java.lang.Object.toString
int hashCode()
Returns a hashCode for this object. For primitive types, this method works the same way as the appropriate hashCode method (i.e. java.lang.Integer.hashCode. For arrays, this method calls java.lang.Object.hashCode.

Numeric types

The types byte, short, int, long, float, double, and char are defined to be numeric. In the method signatures below, "Numeric" should be replaced by the appropriate numeric type.
boolean equals(long l)
Returns true iff this, when cast to a long, is equal to l.
boolean equals(double d)
Returns true iff this, when cast to a double, is equal to d.
byte byteValue()
short shortValue()
int intValue()
long longValue()
float floatValue()
double doubleValue()
Return the value of this, cast to the appropriate type.
Numeric max(Numeric n)
Return this iff this is greater than n. Otherwise return n.
Numeric min(Numeric n)
Return this iff this is less than n. Otherwise return n.
int signum()
Return -1 if this is less than 0, 1 if this is greater than 0, and 0 if this is equal to 0.
Numeric add(Numeric n)
Return the sum of this and n.
Numeric subtract(Numeric n)
Return the difference of this and n.
Numeric multiply(Numeric n)
Return the product of this and n.
Numeric divide(Numeric n)
Return the quotient of this and n.
Numeric negate()
Return the inverse of this.
Numeric abs()
Return the absolute value of this.
int compareTo(Numeric n)
Returns -1 if this is less than n, 1 if this is greater than n, and 0 if this is equal to n. Equivalent to subtract(n).signum()
char charValue()
Returns the character equivalent to this. This method is only provided for the types byte, short, int, and char.
Numeric types also have the following constructors:
Numeric()
Creates a numeric type initialized to that type's default value.
Numeric(Numeric n)
Creates a numeric type initialized to the value of n.
Numeric(double d)
Creates a new numeric type initialized to the value of d.
char(int i)
Creates a new character initialized to the value of i.
float(long l)
double(long l)
Creates a new floating-point type initialized to the value of l.

Logical types

All types to which the logical operators apply (byte, short, int, long, char, and boolean have the following methods. (These methods are bitwise operations for all types but boolean, for which they are logical operations.)
Logical and(Logical l)
Returns the and of this and l.
Logical or(Logical l)
Returns the or of this and l.
Logical xor(Logical l)
Returns the xor of this and l.
Logical not()
Returns the not of this.

Integer

All types which represent integer numbers (byte, short, int, long, char) have the following method:
Integer mod(Integer i)
returns this modulo i.

Boolean

The type boolean has the following methods:
boolean(boolean b)
Creates a new boolean value equal to b.
booleanValue()
Creates a boolean value equal to this.

Array

All array types Element[] have the following methods:
Array(Array a)
Creates a new array with the same elements as a.
Array(int i)
Creates a new array with n elements.
int length()
Returns the length of this.
void setElementAt(Element e, int i)
void store(Element e, int i)
Set the i'th element of this to e.
Element elementAt(int i)
Element fetch(int i)
Returns the i'th element of this.