1   /****************************************************************************
2    * This demo file is part of yFiles for Java 2.14.
3    * Copyright (c) 2000-2017 by yWorks GmbH, Vor dem Kreuzberg 28,
4    * 72070 Tuebingen, Germany. All rights reserved.
5    * 
6    * yFiles demo files exhibit yFiles for Java functionalities. Any redistribution
7    * of demo files in source code or binary form, with or without
8    * modification, is not permitted.
9    * 
10   * Owners of a valid software license for a yFiles for Java version that this
11   * demo is shipped with are allowed to use the demo source code as basis
12   * for their own yFiles for Java powered applications. Use of such programs is
13   * governed by the rights and conditions as set out in the yFiles for Java
14   * license agreement.
15   * 
16   * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY EXPRESS OR IMPLIED
17   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18   * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
19   * NO EVENT SHALL yWorks BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
21   * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22   * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23   * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24   * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26   *
27   ***************************************************************************/
28  package demo.base;
29  
30  import y.base.ListCell;
31  import y.base.YCursor;
32  import y.base.YList;
33  
34  import java.util.Comparator;
35  
36  /**
37   * Demonstrates how to use the linked list data type YList and the YCursor interface.
38   * <p>
39   * <b>usage:</b> java demo.base.ListDemo
40   * </p>
41  
42   * @see <a href="http://docs.yworks.com/yfiles/doc/api/index.html#/dguide/containers#containers" target="_blank">Section Containers</a> in the yFiles for Java Developer's Guide
43   * @see <a href="http://docs.yworks.com/yfiles/doc/api/index.html#/dguide/iteration#iteration" target="_blank">Section Iteration Mechanisms</a> in the yFiles for Java Developer's Guide
44   */
45  public class ListDemo 
46  {
47    public ListDemo()
48    {
49      //create new YList instance
50      YList list = new YList();
51      
52      //add numbered String elements to list 
53      for(int i = 0; i < 20; i++)
54        list.addLast(""+i);
55      
56      //iterate over list from first to last
57      System.out.println("List elements from front to back");
58      for(YCursor c = list.cursor(); c.ok(); c.next())
59      {
60        //output element at cursor position
61        System.out.println(c.current());
62      }
63      
64      //iterate over list from last to first
65      System.out.println("List elements from back to front");
66      YCursor rc = list.cursor();
67      for(rc.toLast(); rc.ok(); rc.prev())
68      {
69        //output element at cursor position
70        System.out.println(rc.current());
71      }
72      
73      //sort list lexicografically
74      list.sort(new Comparator() 
75                {
76                  public int compare(Object a, Object b)
77                    {
78                      return ((String)a).compareTo((String)b);
79                    }
80                });
81      
82      
83      //iterate over list from first to last
84      System.out.println("Lexicographically sorted list");
85      for(YCursor c = list.cursor(); c.ok(); c.next())
86      {
87        //output element at cursor position
88        System.out.println(c.current());
89      }
90      
91      //low level iteration on list cells (non-const iteration)
92      for(ListCell cell = list.firstCell(); cell != null; cell = cell.succ())
93      {
94        String s = (String)cell.getInfo();
95        //remove all Strings from list that have length == 1 
96        if(s.length() == 1)
97        {
98          list.removeCell(cell); 
99          //note that cell is still half-valid, i.e it's succ() and pred() 
100         //pointers are still unchanged. therefore cell = cell.succ() is
101         //valid in the for-statement
102       }
103     }
104     
105     System.out.println("list after element removal");
106     System.out.println(list);
107     
108     //initialize list2 with the elements from list
109     YList list2 = new YList(list.cursor());
110     System.out.println("list2 after creation");
111     System.out.println(list2);
112     
113     //reverse element order in list2
114     list2.reverse();
115     System.out.println("list2 after reversal");
116     System.out.println(list2);
117     
118     //move all elements of list2 to the end of list
119     list.splice(list2);
120     
121     System.out.println("list after splicing");
122     System.out.println(list);
123     System.out.println("list2 after splicing");
124     System.out.println(list2);
125     
126   }
127   
128   public static void main(String[] args)
129   {
130     new ListDemo();
131   }
132   
133 }
134