OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 17 | Rev 25 | 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
    /**
60
     * Renvoie un comparateur qui utilise successivement la liste passée tant que les objets sont
61
     * égaux.
62
     *
63
     * @param comparators une liste de Comparator.
64
     * @return le Comparator demandé.
65
     * @param <T> type of comparator
66
     */
67
    static public final <T> Comparator<T> createComparator(final List<? extends Comparator<T>> comparators) {
68
        return new Comparator<T>() {
69
            public String toString() {
70
                return "CompareUtils comparator with " + comparators;
71
            }
72
 
73
            public int compare(T o1, T o2) {
74
                int result = 0;
75
                int i = 0;
76
                while (i < comparators.size() && result == 0) {
77
                    final Comparator<T> transf = comparators.get(i);
78
                    result = transf.compare(o1, o2);
79
                    i++;
80
                }
81
                return result;
82
            }
83
        };
84
    }
85
 
86
    /**
87
     * Compare 2 objets pouvant être <code>null</code>.
88
     *
89
     * @param o1 the first object, can be <code>null</code>.
90
     * @param o2 the second object, can be <code>null</code>.
91
     * @return <code>true</code> if both are <code>null</code> or if o1.equals(o2).
20 ilm 92
     * @see Object#equals(Object)
17 ilm 93
     */
94
    static public final boolean equals(Object o1, Object o2) {
95
        if (o1 == null && o2 == null)
96
            return true;
97
        if (o1 == null || o2 == null)
98
            return false;
99
        return o1.equals(o2);
100
    }
101
 
20 ilm 102
    /**
103
     * Compare 2 objets pouvant être <code>null</code> avec compareTo(). Useful since for some
104
     * classes equals() is more specific than compareTo()==0, e.g. {@link BigDecimal#equals(Object)}
105
     * doesn't compare the numeric value but instance variables (1E2 is not equal to 100 or 100.00).
106
     *
107
     * @param o1 the first object, can be <code>null</code>.
108
     * @param o2 the second object, can be <code>null</code>.
109
     * @return <code>true</code> if both are <code>null</code> or if o1.compareTo(o2) == 0.
110
     * @see Comparable#compareTo(Object)
111
     */
112
    static public final <T> boolean equalsWithCompareTo(Comparable<T> o1, T o2) {
113
        if (o1 == null && o2 == null)
114
            return true;
115
        if (o1 == null || o2 == null)
116
            return false;
117
        return o1.compareTo(o2) == 0;
118
    }
119
 
17 ilm 120
    static public interface Equalizer<T> {
121
        public boolean equals(T o1, T o2);
122
    }
123
 
124
    static public final <T> boolean equals(List<T> l1, List<T> l2, Equalizer<? super T> comp) {
125
        return compare(l1, l2, comp, null) == null;
126
    }
127
 
128
    /**
129
     * Compare two lists using the provided comparator.
130
     *
131
     * @param <T> type of items
132
     * @param l1 the first list.
133
     * @param l2 the second list.
134
     * @param comp how to compare each item.
135
     * @param toString how to dispay items, can be <code>null</code>.
136
     * @return <code>null</code> if the two lists are equal, otherwise a String explaining the
137
     *         difference.
138
     */
139
    static public final <T> String compare(List<T> l1, List<T> l2, Equalizer<? super T> comp, final ITransformer<? super T, String> toString) {
140
        final int size = l1.size();
141
        if (size != l2.size())
142
            return "unequal size";
143
        for (int i = 0; i < size; i++) {
144
            final T o1 = l1.get(i);
145
            final T o2 = l2.get(i);
146
            if (!comp.equals(o1, o2)) {
147
                final String s1 = toString == null ? String.valueOf(o1) : toString.transformChecked(o1);
148
                final String s2 = toString == null ? String.valueOf(o2) : toString.transformChecked(o2);
149
                return "unequal at " + i + ": " + s1 + " != " + s2;
150
            }
151
        }
152
        return null;
153
    }
154
}