View Javadoc

1   /*
2    * Created on Dec 19, 2003
3    * 
4    * Copyright (c) 2003, 2004 Delft University of Technology Jaffalaan 5, 2628 BX
5    * Delft, the Netherlands All rights reserved.
6    * 
7    * This software is proprietary information of Delft University of Technology
8    * The code is published under the General Public License
9    */
10  
11  package nl.tudelft.simulation.traffic.controlpoint.util;
12  
13  import java.util.Comparator;
14  import java.util.Iterator;
15  import java.util.SortedSet;
16  import java.util.TreeSet;
17  
18  import nl.tudelft.simulation.traffic.controlpoint.ControlPointInterface;
19  
20  /***
21   * This class implements a list containing controlPoints. It is an sorted set
22   * that can only be used for controlPoints.
23   * 
24   * @author <a
25   * href="http://www.tbm.tudelft.nl/webstaf/alexandv/index.htm">Alexander
26   * Verbraeck </a> <br>
27   */
28  public class ControlPointsList
29  {
30      /*** the list of control points */
31      private SortedSet list = new TreeSet(new ProgressionComparator());
32  
33      /***
34       * Add a control point
35       * 
36       * @param cp control point to add
37       */
38      public void add(final ControlPointInterface cp)
39      {
40          this.list.add(cp);
41      }
42  
43      /***
44       * Does the control point list contain a certain control point?
45       * 
46       * @param cp
47       * @return boolean
48       */
49      public boolean contains(final ControlPointInterface cp)
50      {
51          return this.list.contains(cp);
52      }
53  
54      /***
55       * @param cp
56       */
57      public void remove(final ControlPointInterface cp)
58      {
59          this.list.remove(cp);
60      }
61  
62      /***
63       * @return size
64       */
65      public int size()
66      {
67          return this.list.size();
68      }
69  
70      /***
71       * @return Iterator
72       */
73      public Iterator iterator()
74      {
75          return this.list.iterator();
76      }
77  
78      /***
79       * @return boolean
80       */
81      public boolean isEmpty()
82      {
83          return this.list.isEmpty();
84      }
85  
86      /***
87       * This method clears the entire list
88       */
89      public void clear()
90      {
91          this.list.clear();
92      }
93  
94      /***
95       * @param cpl
96       */
97      public void addAll(final ControlPointsList cpl)
98      {
99          this.list.addAll(cpl.getList());
100     }
101 
102     /***
103      * @return set
104      */
105     public SortedSet getList()
106     {
107         return this.list;
108     }
109 
110     /***
111      * @return cloned list
112      */
113     public ControlPointsList cloneList()
114     {
115         ControlPointsList clone = new ControlPointsList();
116         clone.addAll(this);
117         return clone;
118     }
119 
120     /***
121      * @param removeList
122      */
123     public void removeAll(final ControlPointsList removeList)
124     {
125         this.list.removeAll(removeList.getList());
126     }
127     
128     /***
129      * @return first control point in this list
130      */
131     public ControlPointInterface first()
132     {
133         return (ControlPointInterface) this.list.first();
134     }
135 
136     /***
137      * Inner class ProgressionComparator. Makes sure that the control points
138      * are sorted on their progression along the Track. <br>
139      * (c) copyright 2003-2004 <a href="http://www.simulation.tudelft.nl">Delft
140      * University of Technology </a>, the Netherlands. <br>
141      * See for project information <a href="http://www.simulation.tudelft.nl">
142      * www.simulation.tudelft.nl </a> <br>
143      * License of use: <a href="http://www.gnu.org/copyleft/gpl.html">General
144      * Public License (GPL) </a>, no warranty <br>
145      * 
146      * @version Jun 19, 2004 <br>
147      * @author <a
148      * href="http://www.tbm.tudelft.nl/webstaf/alexandv/index.htm">Alexander
149      * Verbraeck </a>
150      */
151     private class ProgressionComparator implements Comparator
152     {
153         /***
154          * @see java.util.Comparator#compare(java.lang.Object,
155          * java.lang.Object)
156          */
157         public int compare(final Object arg0, final Object arg1)
158         {
159             ControlPointInterface cp0 = (ControlPointInterface) arg0;
160             ControlPointInterface cp1 = (ControlPointInterface) arg1;
161             if (cp0.getProgression() < cp1.getProgression())
162                 return -1;
163             if (cp0.getProgression() > cp1.getProgression())
164                 return 1;
165             if (cp0.hashCode() < cp1.hashCode())
166                 return -1;
167             if (cp0.hashCode() > cp1.hashCode())
168                 return 1;
169             return 0; // probably really the same control points
170         }
171     }
172 }