public void cons(X elem) { assertUnpublished(); if (head.isEmpty()) { this.end = new BoundedList<>(elem); this.head = this.end; } else { this.head = new BoundedList<>(elem, this.head); } }
public int size() { int size = 0; Seq<X> seq = this; while (seq.nonEmpty()) { assert seq.isEmpty() || seq instanceof BoundedList; seq = seq.tail(); size++; } return size; }
public void add(X elem) { assertUnpublished(); List<X> newEnd = new BoundedList<>(elem); if (head.isEmpty()) { this.head = newEnd; this.end = newEnd; } else { this.end.tail = newEnd; this.end = newEnd; } }
/** * A covariant constructor for adding an element at the front of a Seq. * * @param elem an element reference. Can be null. * @param tail any type of Seq. Cannot be null. * @param <Y> type of the element added at the front of the given Seq. * @return a new Seq made of the given arguments using the List class. */ public static <Y> Seq<Y> cons(Y elem, Seq<? extends Y> tail) { if (tail.isEmpty() || tail instanceof BoundedList) return new BoundedList<>(elem, tail); else return new List<>(elem, tail); }