OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 180 | 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
 *
185 ilm 4
 * Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
17 ilm 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
 package org.openconcerto.utils;
15
 
16
import org.openconcerto.utils.cc.ITransformer;
17
 
18
import java.util.Comparator;
19
 
20
/**
21
 * A comparator that transforms before comparing.
22
 *
23
 * @author Sylvain
24
 *
25
 * @param <E> the type of the objects before being transformed.
26
 */
180 ilm 27
public class TransformedComparator<E> implements Comparator<E> {
17 ilm 28
 
180 ilm 29
    private final Comparator<E> comp;
17 ilm 30
 
180 ilm 31
    public <T extends Comparable<T>> TransformedComparator(final ITransformer<E, T> transf) {
32
        this(transf, Comparator.naturalOrder());
17 ilm 33
    }
34
 
180 ilm 35
    public <T> TransformedComparator(final ITransformer<E, T> transf, final Comparator<T> comp) {
17 ilm 36
        super();
180 ilm 37
        this.comp = (o1, o2) -> (comp.compare(transf.transformChecked(o1), transf.transformChecked(o2)));
17 ilm 38
    }
39
 
180 ilm 40
    @Override
17 ilm 41
    public int compare(E o1, E o2) {
180 ilm 42
        return this.comp.compare(o1, o2);
17 ilm 43
    }
44
}