NodeComparator.java

    1/*******************************************************************************
    2 * Copyright (c) 2009 Mountainminds GmbH & Co. KG and others
    3 * All rights reserved. This program and the accompanying materials
    4 * are made available under the terms of the Eclipse Public License v1.0
    5 * which accompanies this distribution, and is available at
    6 * http://www.eclipse.org/legal/epl-v10.html
    7 *
    8 * Contributors:
    9 *    Marc R. Hoffmann - initial API and implementation
   10 *    
   11 * $Id: $
   12 *******************************************************************************/
   13package org.jacoco.core.analysis;
   14
   15import java.util.ArrayList;
   16import java.util.Collection;
   17import java.util.Collections;
   18import java.util.Comparator;
   19import java.util.List;
   20
   21import org.jacoco.core.analysis.ICoverageNode.CounterEntity;
   22
   23/**
   24 * Collection of comparators to compare {@link ICoverageNode} objects by
   25 * different criteria.
   26 * 
   27 * @see CounterComparator#on(ICoverageNode.CounterEntity)
   28 * @author Marc R. Hoffmann
   29 * @version $Revision: $
   30 */
   31public class NodeComparator implements Comparator<ICoverageNode> {
   32
   33    private final Comparator<ICounter> counterComparator;
   34
   35    private final CounterEntity entity;
   36
   37    NodeComparator(final Comparator<ICounter> counterComparator,
   38            final CounterEntity entity) {
   39        this.counterComparator = counterComparator;
   40        this.entity = entity;
   41    }
   42
   43    /**
   44     * Creates a new composite comparator with a second search criterion.
   45     * 
   46     * @param second
   47     *            second criterion comparator
   48     * 
   49     * @return composite comparator
   50     */
   51    public NodeComparator second(final Comparator<ICoverageNode> second) {
   52        final Comparator<ICoverageNode> first = this;
   53        return new NodeComparator(null, null) {
   54            @Override
   55            public int compare(final ICoverageNode o1, final ICoverageNode o2) {
   56                final int result = first.compare(o1, o2);
   57                return result == 0 ? second.compare(o1, o2) : result;
   58            }
   59        };
   60    }
   61
   62    /**
   63     * Returns a sorted copy of the given collection of {@link ICoverageNode}
   64     * elements.
   65     * 
   66     * @param <T>
   67     *            actual type of the elements
   68     * @param summaries
   69     *            collection to create a copy of
   70     * @return sorted copy
   71     */
   72    public <T extends ICoverageNode> List<T> sort(final Collection<T> summaries) {
   73        final List<T> result = new ArrayList<T>(summaries);
   74        Collections.sort(result, this);
   75        return result;
   76    }
   77
   78    public int compare(final ICoverageNode n1, final ICoverageNode n2) {
   79        final ICounter c1 = n1.getCounter(entity);
   80        final ICounter c2 = n2.getCounter(entity);
   81        return counterComparator.compare(c1, c2);
   82    }
   83
   84}