OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 41 | Rev 93 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
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
 
80 ilm 85
    static private final Comparator<Comparable<Object>> NATURAL_COMPARATOR = new Comparator<Comparable<Object>>() {
86
        @Override
87
        public int compare(Comparable<Object> o1, Comparable<Object> o2) {
88
            return o1.compareTo(o2);
89
        }
90
    };
91
 
92
    // added in Comparator in Java 8
93
    @SuppressWarnings("unchecked")
94
    static public final <T extends Comparable<? super T>> Comparator<T> naturalOrder() {
95
        return (Comparator<T>) NATURAL_COMPARATOR;
96
    }
97
 
25 ilm 98
    /**
17 ilm 99
     * Renvoie un comparateur qui utilise successivement la liste passée tant que les objets sont
100
     * égaux.
101
     *
102
     * @param comparators une liste de Comparator.
103
     * @return le Comparator demandé.
104
     * @param <T> type of comparator
105
     */
106
    static public final <T> Comparator<T> createComparator(final List<? extends Comparator<T>> comparators) {
107
        return new Comparator<T>() {
108
            public String toString() {
109
                return "CompareUtils comparator with " + comparators;
110
            }
111
 
112
            public int compare(T o1, T o2) {
113
                int result = 0;
114
                int i = 0;
115
                while (i < comparators.size() && result == 0) {
116
                    final Comparator<T> transf = comparators.get(i);
117
                    result = transf.compare(o1, o2);
118
                    i++;
119
                }
120
                return result;
121
            }
122
        };
123
    }
124
 
125
    /**
126
     * Compare 2 objets pouvant être <code>null</code>.
127
     *
128
     * @param o1 the first object, can be <code>null</code>.
129
     * @param o2 the second object, can be <code>null</code>.
130
     * @return <code>true</code> if both are <code>null</code> or if o1.equals(o2).
20 ilm 131
     * @see Object#equals(Object)
17 ilm 132
     */
133
    static public final boolean equals(Object o1, Object o2) {
134
        if (o1 == null && o2 == null)
135
            return true;
136
        if (o1 == null || o2 == null)
137
            return false;
138
        return o1.equals(o2);
139
    }
140
 
20 ilm 141
    /**
142
     * Compare 2 objets pouvant être <code>null</code> avec compareTo(). Useful since for some
143
     * classes equals() is more specific than compareTo()==0, e.g. {@link BigDecimal#equals(Object)}
144
     * doesn't compare the numeric value but instance variables (1E2 is not equal to 100 or 100.00).
145
     *
146
     * @param o1 the first object, can be <code>null</code>.
147
     * @param o2 the second object, can be <code>null</code>.
148
     * @return <code>true</code> if both are <code>null</code> or if o1.compareTo(o2) == 0.
149
     * @see Comparable#compareTo(Object)
150
     */
151
    static public final <T> boolean equalsWithCompareTo(Comparable<T> o1, T o2) {
152
        if (o1 == null && o2 == null)
153
            return true;
154
        if (o1 == null || o2 == null)
155
            return false;
156
        return o1.compareTo(o2) == 0;
157
    }
158
 
17 ilm 159
    static public interface Equalizer<T> {
160
        public boolean equals(T o1, T o2);
161
    }
162
 
41 ilm 163
    static public final Equalizer<Object> OBJECT_EQ = new Equalizer<Object>() {
164
        public boolean equals(Object o1, Object o2) {
165
            return CompareUtils.equals(o1, o2);
166
        }
167
    };
168
 
17 ilm 169
    static public final <T> boolean equals(List<T> l1, List<T> l2, Equalizer<? super T> comp) {
170
        return compare(l1, l2, comp, null) == null;
171
    }
172
 
173
    /**
174
     * Compare two lists using the provided comparator.
175
     *
176
     * @param <T> type of items
177
     * @param l1 the first list.
178
     * @param l2 the second list.
179
     * @param comp how to compare each item.
180
     * @param toString how to dispay items, can be <code>null</code>.
181
     * @return <code>null</code> if the two lists are equal, otherwise a String explaining the
182
     *         difference.
183
     */
184
    static public final <T> String compare(List<T> l1, List<T> l2, Equalizer<? super T> comp, final ITransformer<? super T, String> toString) {
185
        final int size = l1.size();
186
        if (size != l2.size())
187
            return "unequal size";
188
        for (int i = 0; i < size; i++) {
189
            final T o1 = l1.get(i);
190
            final T o2 = l2.get(i);
191
            if (!comp.equals(o1, o2)) {
192
                final String s1 = toString == null ? String.valueOf(o1) : toString.transformChecked(o1);
193
                final String s2 = toString == null ? String.valueOf(o2) : toString.transformChecked(o2);
194
                return "unequal at " + i + ": " + s1 + " != " + s2;
195
            }
196
        }
197
        return null;
198
    }
199
}