
// ListsTester.java
// https://www.demo2s.com/java/apache-commons-transformedlist-transformedlist-final-list-e-list-fin.html
// Andrew Davison, ad@coe.psu.ac.th, May 2023

/* Various List data structures:
      * CursorableLinkedList
      * FixedSizeList
      * GrowthList
      * LazyList
      * PredicatedList
      * SetUniqueList
      * TransformedList

  Usage:
    > compile ListsTester.java
    > run ListsTester
*/

import java.util.*;

import org.apache.commons.collections4.list.*;


public class ListsTester 
{

  public static void main(String[] args)
  {
    List<Integer> list = new ArrayList<>(
         Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8));
    System.out.println("Original mutable list: " + list);

    // ----------------------------

    CursorableLinkedList<Integer> cll = new CursorableLinkedList<>(
                  new ArrayList<Integer>(list) );   // copy the list
    CursorableLinkedList.Cursor<Integer> cursor = cll.cursor();
    System.out.println("\nIterate and remove cursorable: ");
    while (cursor.hasNext()) {
      System.out.print(cursor.next() + " ");
      cursor.remove();
    }
    System.out.println();
    System.out.println("Resulting list: " + cll);

    // ----------------------------

    FixedSizeList<Integer> fsl = FixedSizeList.fixedSizeList(
                    new ArrayList<Integer>(list));   // copy the list
    fsl.set(0, 0);
    System.out.println("\nFixed size list: " + fsl);
    System.out.println("Try to remove an element");
    try {
      fsl.remove(0); 
    }
    catch(UnsupportedOperationException e)
    {  System.out.println(e);  }

    // ----------------------------

    GrowthList<Integer> gl = GrowthList.growthList(
                      new ArrayList<Integer>(list));  // copy the list
    System.out.println("\nGrowth list: " + gl); 
    System.out.println("Add an element way past end of list");
    gl.add(12, Integer.valueOf(10));  // index, element
    System.out.println("Result: " + gl); 
                  // [1, 2, 3, 4, 5, 6, 7, 8, null, ...,  null, 10]

    // ----------------------------

    LazyList<Integer> ll = LazyList.lazyList(
                                   new ArrayList<>(), v -> v);
    System.out.println("\nInitial Lazy list: " + ll);
    System.out.println("Get 5th value: " + ll.get(4)); // 4
    System.out.println("Get 10th value: " + ll.get(9)); // 9

    // ----------------------------

    PredicatedList<Integer> pl = PredicatedList.predicatedList(
                     new ArrayList<Integer>(list), i -> i < 10);  // copy
    System.out.println("\nPredicated (vals < 10) list: " + pl);
    System.out.println("Try to add an element >= 10: 15");
    try {
      pl.add(15); 
    }
    catch(IllegalArgumentException e)
    {  System.out.println(e);  }

    // ----------------------------

    List<Integer> multsList = new ArrayList<Integer>(
                      Arrays.asList(1, 2, 2, 3, 3, 3, 2, 2));    // mutable
    System.out.println("\nOriginal list: " + multsList);
    SetUniqueList<Integer> sul = SetUniqueList.setUniqueList(multsList);
    System.out.println("Set list version: " + sul);
    System.out.println("Add 4: " + sul.add(4));  // true
    System.out.println("Add 4: " + sul.add(4));  // false

    // ----------------------------

    List<Integer> tl = TransformedList.transformedList(
                   new ArrayList<Integer>(list),  i -> i * 2);  // copy list
    System.out.println("\nTransformed list using (*2) into : " + tl); 
    System.out.println("Add -1");
    tl.add(-1);
    System.out.println(" " + tl);  // [2, 4, 6, 8, 10, 12, 14, 16, -2]
  }  // end of main()

}  // end of ListsTester class