Synthia
Generic and flexible data structure generator
ComparableList.java
Go to the documentation of this file.
1 /*
2  Synthia, a data structure generator
3  Copyright (C) 2019-2021 Laboratoire d'informatique formelle
4  Université du Québec à Chicoutimi, Canada
5 
6  This program is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Lesser General Public License as published
8  by the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Lesser General Public License for more details.
15 
16  You should have received a copy of the GNU Lesser General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 package ca.uqac.lif.synthia.collection;
20 
21 import java.util.ArrayList;
22 import java.util.List;
23 
24 /**
25  * Class that extends Java's ArrayList class to implements the Comparable interface.
26  *
27  * @param <T> The type of the elements of the list.
28  * @author Marc-Antoine Plourde
29  * @ingroup API
30  */
31 public class ComparableList<T> extends ArrayList<T> implements Comparable<List<T>>
32 {
33  /**
34  * Dummy UID.
35  */
36  private static final long serialVersionUID = 1L;
37 
38  /**
39  * Default constructor.
40  */
41  public ComparableList()
42  {
43  super();
44  }
45 
46  /**
47  * Constructor who takes a <code>List&lt;T&gt;</code> as parameter.
48  *
49  * @param l The reference list used to compare to an other list.
50  */
51  public ComparableList(List<T> l)
52  {
53  super(l);
54  }
55 
56  /**
57  * Public method to compare a list to the reference one.
58  *
59  * @param o An <code>ArrayList&lt;T&gt;</code> to compare with the reference one.
60  * @return <ul>
61  * <li>a negative integer if the reference list is considered smaller than the second one.</li>
62  * <li>0 if the reference list is considered equal to the second one.</li>
63  * <li>A posisitive integer supperior to 0 if the reference list is considered greater than the second one..</li>
64  * <ul/>
65  */
66  @SuppressWarnings("unchecked")
67  @Override public int compareTo(List<T> o)
68  {
69  if (this.size() < o.size())
70  {
71  return -1;
72  }
73  else if (this.size() > o.size())
74  {
75  return 1;
76  }
77  else
78  {
79  int answer = 0;
80  int i = 0;
81 
82  while ((i < this.size()) && (answer == 0))
83  {
84 
85  if (((this.get(i)) instanceof Comparable) && ((o.get(i)) instanceof Comparable))
86  {
87  try
88  {
89  answer = ((Comparable<T>) this.get(i)).compareTo(o.get(i));
90  }
91  catch (ClassCastException exception)
92  {
93  answer = 0;
94  }
95  }
96  else if (((this.get(i)) instanceof List) && ((o.get(i)) instanceof List))
97  {
98  answer = new ComparableList<T>((List<T>) this.get(i)).compareTo((ComparableList<T>) o.get(i));
99  }
100  i++;
101  }
102  return answer;
103  }
104  }
105 }
ca.uqac.lif.synthia.collection.ComparableList.compareTo
int compareTo(List< T > o)
Public method to compare a list to the reference one.
Definition: ComparableList.java:67
ca.uqac.lif.synthia.collection.ComparableList
Class that extends Java's ArrayList class to implements the Comparable interface.
Definition: ComparableList.java:31
ca.uqac.lif.synthia.collection.ComparableList.ComparableList
ComparableList()
Default constructor.
Definition: ComparableList.java:41
ca.uqac.lif.synthia.collection.ComparableList.ComparableList
ComparableList(List< T > l)
Constructor who takes a List<T> as parameter.
Definition: ComparableList.java:51