17 |
ilm |
1 |
/*
|
|
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
|
|
3 |
*
|
|
|
4 |
* Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
|
|
|
5 |
*
|
|
|
6 |
* The contents of this file are subject to the terms of the GNU General Public License Version 3
|
|
|
7 |
* only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
|
|
|
8 |
* copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
|
|
|
9 |
* language governing permissions and limitations under the License.
|
|
|
10 |
*
|
|
|
11 |
* When distributing the software, include this License Header Notice in each file.
|
|
|
12 |
*/
|
|
|
13 |
|
|
|
14 |
/*
|
|
|
15 |
* Créé le 14 avr. 2005
|
|
|
16 |
*/
|
|
|
17 |
package org.openconcerto.utils;
|
|
|
18 |
|
|
|
19 |
import org.openconcerto.utils.cc.ITransformer;
|
|
|
20 |
|
20 |
ilm |
21 |
import java.math.BigDecimal;
|
17 |
ilm |
22 |
import java.util.Comparator;
|
|
|
23 |
import java.util.List;
|
|
|
24 |
|
|
|
25 |
/**
|
|
|
26 |
* @author Sylvain CUAZ
|
|
|
27 |
*/
|
|
|
28 |
public class CompareUtils {
|
|
|
29 |
|
|
|
30 |
/**
|
|
|
31 |
* Compare 2 nombres entier avec longValue().
|
|
|
32 |
*
|
|
|
33 |
* @param n1 le premier nombre.
|
|
|
34 |
* @param n2 le deuxième nombre.
|
|
|
35 |
* @return 0 si ==, >0 si n1>2.
|
|
|
36 |
*/
|
|
|
37 |
public static final int compareIntNumbers(Number n1, Number n2) {
|
|
|
38 |
return compareLong(n1.longValue(), n2.longValue());
|
|
|
39 |
}
|
|
|
40 |
|
|
|
41 |
static public final int compareInt(int int1, int int2) {
|
|
|
42 |
if (int1 < int2)
|
|
|
43 |
return -1;
|
|
|
44 |
else if (int1 == int2)
|
|
|
45 |
return 0;
|
|
|
46 |
else
|
|
|
47 |
return +1;
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
static public final int compareLong(long int1, long int2) {
|
|
|
51 |
if (int1 < int2)
|
|
|
52 |
return -1;
|
|
|
53 |
else if (int1 == int2)
|
|
|
54 |
return 0;
|
|
|
55 |
else
|
|
|
56 |
return +1;
|
|
|
57 |
}
|
|
|
58 |
|
|
|
59 |
/**
|
25 |
ilm |
60 |
* Compare two objects if they're numbers or comparable.
|
|
|
61 |
*
|
|
|
62 |
* @param o1 first object.
|
|
|
63 |
* @param o2 second object.
|
|
|
64 |
* @return a negative integer, zero, or a positive integer as o1 is less than, equal to, or
|
|
|
65 |
* greater than o2.
|
|
|
66 |
* @throws ClassCastException if o1 is neither a {@link Number} nor a {@link Comparable}, or if
|
|
|
67 |
* o2's type prevents it from being compared to o1.
|
|
|
68 |
* @throws NullPointerException if o1 or o2 is <code>null</code>.
|
|
|
69 |
* @see Comparable#compareTo(Object)
|
|
|
70 |
* @see NumberUtils#compare(Number, Number)
|
|
|
71 |
*/
|
|
|
72 |
static public final int compare(final Object o1, final Object o2) throws ClassCastException {
|
|
|
73 |
if (o1 == null || o2 == null)
|
|
|
74 |
throw new NullPointerException();
|
|
|
75 |
if (o1 instanceof Number && o2 instanceof Number) {
|
|
|
76 |
return NumberUtils.compare((Number) o1, (Number) o2);
|
|
|
77 |
} else {
|
|
|
78 |
// see Arrays.mergeSort()
|
|
|
79 |
@SuppressWarnings({ "rawtypes", "unchecked" })
|
|
|
80 |
final int res = ((Comparable) o1).compareTo(o2);
|
|
|
81 |
return res;
|
|
|
82 |
}
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
/**
|
17 |
ilm |
86 |
* Renvoie un comparateur qui utilise successivement la liste passée tant que les objets sont
|
|
|
87 |
* égaux.
|
|
|
88 |
*
|
|
|
89 |
* @param comparators une liste de Comparator.
|
|
|
90 |
* @return le Comparator demandé.
|
|
|
91 |
* @param <T> type of comparator
|
|
|
92 |
*/
|
|
|
93 |
static public final <T> Comparator<T> createComparator(final List<? extends Comparator<T>> comparators) {
|
|
|
94 |
return new Comparator<T>() {
|
|
|
95 |
public String toString() {
|
|
|
96 |
return "CompareUtils comparator with " + comparators;
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
public int compare(T o1, T o2) {
|
|
|
100 |
int result = 0;
|
|
|
101 |
int i = 0;
|
|
|
102 |
while (i < comparators.size() && result == 0) {
|
|
|
103 |
final Comparator<T> transf = comparators.get(i);
|
|
|
104 |
result = transf.compare(o1, o2);
|
|
|
105 |
i++;
|
|
|
106 |
}
|
|
|
107 |
return result;
|
|
|
108 |
}
|
|
|
109 |
};
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
/**
|
|
|
113 |
* Compare 2 objets pouvant être <code>null</code>.
|
|
|
114 |
*
|
|
|
115 |
* @param o1 the first object, can be <code>null</code>.
|
|
|
116 |
* @param o2 the second object, can be <code>null</code>.
|
|
|
117 |
* @return <code>true</code> if both are <code>null</code> or if o1.equals(o2).
|
20 |
ilm |
118 |
* @see Object#equals(Object)
|
17 |
ilm |
119 |
*/
|
|
|
120 |
static public final boolean equals(Object o1, Object o2) {
|
|
|
121 |
if (o1 == null && o2 == null)
|
|
|
122 |
return true;
|
|
|
123 |
if (o1 == null || o2 == null)
|
|
|
124 |
return false;
|
|
|
125 |
return o1.equals(o2);
|
|
|
126 |
}
|
|
|
127 |
|
20 |
ilm |
128 |
/**
|
|
|
129 |
* Compare 2 objets pouvant être <code>null</code> avec compareTo(). Useful since for some
|
|
|
130 |
* classes equals() is more specific than compareTo()==0, e.g. {@link BigDecimal#equals(Object)}
|
|
|
131 |
* doesn't compare the numeric value but instance variables (1E2 is not equal to 100 or 100.00).
|
|
|
132 |
*
|
|
|
133 |
* @param o1 the first object, can be <code>null</code>.
|
|
|
134 |
* @param o2 the second object, can be <code>null</code>.
|
|
|
135 |
* @return <code>true</code> if both are <code>null</code> or if o1.compareTo(o2) == 0.
|
|
|
136 |
* @see Comparable#compareTo(Object)
|
|
|
137 |
*/
|
|
|
138 |
static public final <T> boolean equalsWithCompareTo(Comparable<T> o1, T o2) {
|
|
|
139 |
if (o1 == null && o2 == null)
|
|
|
140 |
return true;
|
|
|
141 |
if (o1 == null || o2 == null)
|
|
|
142 |
return false;
|
|
|
143 |
return o1.compareTo(o2) == 0;
|
|
|
144 |
}
|
|
|
145 |
|
17 |
ilm |
146 |
static public interface Equalizer<T> {
|
|
|
147 |
public boolean equals(T o1, T o2);
|
|
|
148 |
}
|
|
|
149 |
|
|
|
150 |
static public final <T> boolean equals(List<T> l1, List<T> l2, Equalizer<? super T> comp) {
|
|
|
151 |
return compare(l1, l2, comp, null) == null;
|
|
|
152 |
}
|
|
|
153 |
|
|
|
154 |
/**
|
|
|
155 |
* Compare two lists using the provided comparator.
|
|
|
156 |
*
|
|
|
157 |
* @param <T> type of items
|
|
|
158 |
* @param l1 the first list.
|
|
|
159 |
* @param l2 the second list.
|
|
|
160 |
* @param comp how to compare each item.
|
|
|
161 |
* @param toString how to dispay items, can be <code>null</code>.
|
|
|
162 |
* @return <code>null</code> if the two lists are equal, otherwise a String explaining the
|
|
|
163 |
* difference.
|
|
|
164 |
*/
|
|
|
165 |
static public final <T> String compare(List<T> l1, List<T> l2, Equalizer<? super T> comp, final ITransformer<? super T, String> toString) {
|
|
|
166 |
final int size = l1.size();
|
|
|
167 |
if (size != l2.size())
|
|
|
168 |
return "unequal size";
|
|
|
169 |
for (int i = 0; i < size; i++) {
|
|
|
170 |
final T o1 = l1.get(i);
|
|
|
171 |
final T o2 = l2.get(i);
|
|
|
172 |
if (!comp.equals(o1, o2)) {
|
|
|
173 |
final String s1 = toString == null ? String.valueOf(o1) : toString.transformChecked(o1);
|
|
|
174 |
final String s2 = toString == null ? String.valueOf(o2) : toString.transformChecked(o2);
|
|
|
175 |
return "unequal at " + i + ": " + s1 + " != " + s2;
|
|
|
176 |
}
|
|
|
177 |
}
|
|
|
178 |
return null;
|
|
|
179 |
}
|
|
|
180 |
}
|