OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 17 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 17 Rev 180
Line 12... Line 12...
12
 */
12
 */
13
 
13
 
14
 package org.openconcerto.utils;
14
 package org.openconcerto.utils;
15
 
15
 
16
import org.openconcerto.utils.cc.ITransformer;
16
import org.openconcerto.utils.cc.ITransformer;
17
import org.openconcerto.utils.cc.Transformer;
-
 
18
 
17
 
19
import java.util.Comparator;
18
import java.util.Comparator;
20
 
19
 
21
import org.apache.commons.collections.ComparatorUtils;
-
 
22
 
-
 
23
/**
20
/**
24
 * A comparator that transforms before comparing.
21
 * A comparator that transforms before comparing.
25
 * 
22
 * 
26
 * @author Sylvain
23
 * @author Sylvain
27
 * 
24
 * 
28
 * @param <E> the type of the objects before being transformed.
25
 * @param <E> the type of the objects before being transformed.
29
 * @param <T> the type of the objects after being transformed.
-
 
30
 */
26
 */
31
public class TransformedComparator<E, T> implements Comparator<E> {
27
public class TransformedComparator<E> implements Comparator<E> {
32
 
-
 
33
    public static final <T> TransformedComparator<T, T> from(final Comparator<T> comp) {
-
 
34
        return new TransformedComparator<T, T>(Transformer.<T> nopTransformer(), comp);
-
 
35
    }
-
 
36
 
28
 
37
    private final ITransformer<E, T> transf;
-
 
38
    private final Comparator<T> comp;
29
    private final Comparator<E> comp;
39
 
30
 
40
    @SuppressWarnings("unchecked")
-
 
41
    public TransformedComparator(final ITransformer<E, T> transf) {
31
    public <T extends Comparable<T>> TransformedComparator(final ITransformer<E, T> transf) {
42
        this(transf, ComparatorUtils.NATURAL_COMPARATOR);
32
        this(transf, Comparator.naturalOrder());
43
    }
33
    }
44
 
34
 
45
    public TransformedComparator(final ITransformer<E, T> transf, final Comparator<T> comp) {
35
    public <T> TransformedComparator(final ITransformer<E, T> transf, final Comparator<T> comp) {
46
        super();
36
        super();
47
        this.transf = transf;
37
        this.comp = (o1, o2) -> (comp.compare(transf.transformChecked(o1), transf.transformChecked(o2)));
48
        this.comp = comp;
-
 
49
    }
38
    }
50
 
39
 
-
 
40
    @Override
51
    public int compare(E o1, E o2) {
41
    public int compare(E o1, E o2) {
52
        return this.comp.compare(this.transf.transformChecked(o1), this.transf.transformChecked(o2));
42
        return this.comp.compare(o1, o2);
53
    }
43
    }
54
}
44
}