1   /****************************************************************************
2    **
3    ** This file is part of yFiles-2.9. 
4    ** 
5    ** yWorks proprietary/confidential. Use is subject to license terms.
6    **
7    ** Redistribution of this file or of an unauthorized byte-code version
8    ** of this file is strictly forbidden.
9    **
10   ** Copyright (c) 2000-2011 by yWorks GmbH, Vor dem Kreuzberg 28, 
11   ** 72070 Tuebingen, Germany. All rights reserved.
12   **
13   ***************************************************************************/
14  package demo.base;
15  
16  import y.base.ListCell;
17  import y.base.YCursor;
18  import y.base.YList;
19  
20  import java.util.Comparator;
21  
22  /**
23   * Demonstrates how to use the linked list data type YList and the YCursor interface.
24   * <p>
25   * <b>usage:</b> java demo.base.ListDemo
26   * </p>
27  
28   * @see <a href="http://docs.yworks.com/yfiles/doc/developers-guide/containers.html#containers">Section Containers</a> in the yFiles for Java Developer's Guide
29   * @see <a href="http://docs.yworks.com/yfiles/doc/developers-guide/iteration.html#iteration">Section Iteration Mechanisms</a> in the yFiles for Java Developer's Guide
30   */
31  public class ListDemo 
32  {
33    public ListDemo()
34    {
35      //create new YList instance
36      YList list = new YList();
37      
38      //add numbered String elements to list 
39      for(int i = 0; i < 20; i++)
40        list.addLast(""+i);
41      
42      //iterate over list from first to last
43      System.out.println("List elements from front to back");
44      for(YCursor c = list.cursor(); c.ok(); c.next())
45      {
46        //output element at cursor position
47        System.out.println(c.current());
48      }
49      
50      //iterate over list from last to first
51      System.out.println("List elements from back to front");
52      YCursor rc = list.cursor();
53      for(rc.toLast(); rc.ok(); rc.prev())
54      {
55        //output element at cursor position
56        System.out.println(rc.current());
57      }
58      
59      //sort list lexicografically
60      list.sort(new Comparator() 
61                {
62                  public int compare(Object a, Object b)
63                    {
64                      return ((String)a).compareTo((String)b);
65                    }
66                });
67      
68      
69      //iterate over list from first to last
70      System.out.println("Lexicographically sorted list");
71      for(YCursor c = list.cursor(); c.ok(); c.next())
72      {
73        //output element at cursor position
74        System.out.println(c.current());
75      }
76      
77      //low level iteration on list cells (non-const iteration)
78      for(ListCell cell = list.firstCell(); cell != null; cell = cell.succ())
79      {
80        String s = (String)cell.getInfo();
81        //remove all Strings from list that have length == 1 
82        if(s.length() == 1)
83        {
84          list.removeCell(cell); 
85          //note that cell is still half-valid, i.e it's succ() and pred() 
86          //pointers are still unchanged. therefore cell = cell.succ() is
87          //valid in the for-statement
88        }
89      }
90      
91      System.out.println("list after element removal");
92      System.out.println(list);
93      
94      //initialize list2 with the elements from list
95      YList list2 = new YList(list.cursor());
96      System.out.println("list2 after creation");
97      System.out.println(list2);
98      
99      //reverse element order in list2
100     list2.reverse();
101     System.out.println("list2 after reversal");
102     System.out.println(list2);
103     
104     //move all elements of list2 to the end of list
105     list.splice(list2);
106     
107     System.out.println("list after splicing");
108     System.out.println(list);
109     System.out.println("list2 after splicing");
110     System.out.println(list2);
111     
112   }
113   
114   public static void main(String[] args)
115   {
116     new ListDemo();
117   }
118   
119 }