1: doctype "../grammars/EJ.g.etl";
     2: /// Rational number
     3: /// @verion 0.0.0
     4: class public final strictfp Rational extends Number 
     5:                                      implements Comparable
     6: {
     7:   /// a divident
     8:   var public final int dividend;
     9:   /// a divisor
    10:   var public final int divisor;
    11:   /// a constructor
    12:   /// @param divident divident in rational number
    13:   /// @param divisor divisor in rational number
    14:   maker public Rational(final int dividend, final int divisor)
    15:   {
    16:     super();
    17:     this.divident = dividend;
    18:     this.divisor = divisor;
    19:   };
    20:   /// @see java.lang.Number#intValue()
    21:   to public int intValue()
    22:   {
    23:     return dividend / divisor;
    24:   };
    25:   /// @see java.lang.Number#longValue()
    26:   to public long longValue()
    27:   {
    28:     return dividend as long / divisor;
    29:   };
    30:   /// @see java.lang.Number#floatValue()
    31:   to public float floatValue()
    32:   {
    33:     return dividend as float / divisor;
    34:   };
    35:   /// @see java.lang.Number#doubleValue()
    36:   to public double doubleValue()
    37:   {
    38:     return dividend as double / divisor;
    39:   };
    40:   /// @see java.lang.Comparable#compareTo(java.lang.Object)
    41:   to public int compareTo(final Object o)
    42:   {
    43:     var final Rational r = o as Rational; 
    44:     var final long v = dividend as long * r.divisor 
    45:                      - r.dividend as long * divisor; 
    46:     return v < 0 ? -1 : v > 0 ? 1 : 0;
    47:   };
    48: 
    49: };
    50: 
    51: /// This is a contrived sample class
    52: class abstract AbstractTest extends java.lang.Object
    53: {
    54:   /// constant demo
    55:   var public final static String TEST_ID = "testId";
    56:   /// private variable sample
    57:   var private boolean tested = false;
    58:   var private int testCache;
    59:   var private final int xorValue;
    60:   /// abstract method sample
    61:   to abstract protected int test(String value);
    62:   to private void ensureTested()
    63:   {
    64:     if(!tested) 
    65:     {
    66:       testCache = test(TEST_ID);
    67:       tested = true;
    68:     };
    69:   };
    70:   to final public int test()
    71:   {
    72:     ensureTested();
    73:     return testCache ^ xorValue;
    74:   };
    75:   to [T extends Comparable[ ? super T ]] public static T max(T a, T b)
    76:   {
    77:     return a.compareTo(b) > 0 ? a : b;
    78:   };
    79:   maker AbstractTest(int xorValue)
    80:   {
    81:     super();
    82:     this.xorValue = xorValue;
    83:   };
    84: 
    85:   // test sample
    86:   to [T] public static void copyList(List[ ? super T ] dest, List[ ? extends T ] src) 
    87:   {
    88:     var int length = src.size();
    89:     var ListIterator[ ? super T ] di = dest.listIterator();
    90:     var ListIterator[ ? extends T ] si=src.listIterator();
    91:     for (var int i = 0 : i<length : i++) 
    92:     {
    93:       di.next();
    94:       di.set(si.next());
    95:     };
    96:   };
    97: 
    98: };
    99: 
   100: /// template class
   101: class Seq[A] 
   102: {
   103:   var A head;
   104:   var Seq[A] tail;
   105:   maker Seq() { this(null, null); };
   106:   maker Seq(A head, Seq[A] tail) { 
   107:     this.head = head; this.tail = tail; 
   108:   };
   109:   to boolean isEmpty() { return tail == null; };
   110:   class Zipper[B] 
   111:   {
   112:     to Seq[Pair[A,B]] zip(Seq[B] that) 
   113:     {
   114:       if (this.isEmpty() || that.isEmpty())
   115:       {
   116:           return new Seq[Pair[A,B]]();
   117:       }
   118:       else
   119:       {
   120:         return new Seq[Pair[A,B]](
   121:             new Pair[A,B](this.head, that.head),
   122:             this.tail.zip(that.tail));
   123:       };
   124:     };
   125:   };
   126: };