OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 20 | Go to most recent revision | Details | 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
 
21
import java.util.Comparator;
22
import java.util.List;
23
 
24
/**
25
 * @author Sylvain CUAZ
26
 */
27
public class CompareUtils {
28
 
29
    /**
30
     * Compare 2 nombres entier avec longValue().
31
     *
32
     * @param n1 le premier nombre.
33
     * @param n2 le deuxième nombre.
34
     * @return 0 si ==, >0 si n1>2.
35
     */
36
    public static final int compareIntNumbers(Number n1, Number n2) {
37
        return compareLong(n1.longValue(), n2.longValue());
38
    }
39
 
40
    static public final int compareInt(int int1, int int2) {
41
        if (int1 < int2)
42
            return -1;
43
        else if (int1 == int2)
44
            return 0;
45
        else
46
            return +1;
47
    }
48
 
49
    static public final int compareLong(long int1, long int2) {
50
        if (int1 < int2)
51
            return -1;
52
        else if (int1 == int2)
53
            return 0;
54
        else
55
            return +1;
56
    }
57
 
58
    /**
59
     * Renvoie un comparateur qui utilise successivement la liste passée tant que les objets sont
60
     * égaux.
61
     *
62
     * @param comparators une liste de Comparator.
63
     * @return le Comparator demandé.
64
     * @param <T> type of comparator
65
     */
66
    static public final <T> Comparator<T> createComparator(final List<? extends Comparator<T>> comparators) {
67
        return new Comparator<T>() {
68
            public String toString() {
69
                return "CompareUtils comparator with " + comparators;
70
            }
71
 
72
            public int compare(T o1, T o2) {
73
                int result = 0;
74
                int i = 0;
75
                while (i < comparators.size() && result == 0) {
76
                    final Comparator<T> transf = comparators.get(i);
77
                    result = transf.compare(o1, o2);
78
                    i++;
79
                }
80
                return result;
81
            }
82
        };
83
    }
84
 
85
    /**
86
     * Compare 2 objets pouvant être <code>null</code>.
87
     *
88
     * @param o1 the first object, can be <code>null</code>.
89
     * @param o2 the second object, can be <code>null</code>.
90
     * @return <code>true</code> if both are <code>null</code> or if o1.equals(o2).
91
     */
92
    static public final boolean equals(Object o1, Object o2) {
93
        if (o1 == null && o2 == null)
94
            return true;
95
        if (o1 == null || o2 == null)
96
            return false;
97
        return o1.equals(o2);
98
    }
99
 
100
    static public interface Equalizer<T> {
101
        public boolean equals(T o1, T o2);
102
    }
103
 
104
    static public final <T> boolean equals(List<T> l1, List<T> l2, Equalizer<? super T> comp) {
105
        return compare(l1, l2, comp, null) == null;
106
    }
107
 
108
    /**
109
     * Compare two lists using the provided comparator.
110
     *
111
     * @param <T> type of items
112
     * @param l1 the first list.
113
     * @param l2 the second list.
114
     * @param comp how to compare each item.
115
     * @param toString how to dispay items, can be <code>null</code>.
116
     * @return <code>null</code> if the two lists are equal, otherwise a String explaining the
117
     *         difference.
118
     */
119
    static public final <T> String compare(List<T> l1, List<T> l2, Equalizer<? super T> comp, final ITransformer<? super T, String> toString) {
120
        final int size = l1.size();
121
        if (size != l2.size())
122
            return "unequal size";
123
        for (int i = 0; i < size; i++) {
124
            final T o1 = l1.get(i);
125
            final T o2 = l2.get(i);
126
            if (!comp.equals(o1, o2)) {
127
                final String s1 = toString == null ? String.valueOf(o1) : toString.transformChecked(o1);
128
                final String s2 = toString == null ? String.valueOf(o2) : toString.transformChecked(o2);
129
                return "unequal at " + i + ": " + s1 + " != " + s2;
130
            }
131
        }
132
        return null;
133
    }
134
}