001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software GmbH & Co. KG, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.file.collectors;
029
030import org.opencms.file.I_CmsResource;
031
032import java.util.Comparator;
033
034/**
035 * Wrapper around a comparator that inverts comparison results which may e.g. be
036 * used to invert sort orders. <p>
037 *
038 * This is used to create <code>{@link java.util.SortedSet}</code> instances that may
039 * sort in different order (ascending vs. descending).<p>
040 *
041 * <table border="1">
042 * <tr>
043 * <th>Internal comparator result</th>
044 * <th>Transformed result</th>
045 * </tr>
046 * <tr>
047 * <td>-1</td>
048 * <td>+1</td>
049 * </tr>
050 * <tr>
051 * <td>0</td>
052 * <td>0</td>
053 * </tr>
054 * <tr>
055 * <td>+1</td>
056 * <td>-1</td>
057 * </tr>
058 * </table>
059 * <p>
060 *
061 * @since 7.0.3
062 *
063 */
064public final class ComparatorInverter implements Comparator<I_CmsResource> {
065
066    /** The comparator to invert. */
067    private Comparator<I_CmsResource> m_delegate;
068
069    /**
070     * Creates a comparator that will invert the result of the given comparator.
071     * <p>
072     *
073     * @param toInvert the comparator to invert results of
074     */
075    public ComparatorInverter(Comparator<I_CmsResource> toInvert) {
076
077        m_delegate = toInvert;
078    }
079
080    /**
081     * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
082     */
083    public int compare(I_CmsResource o1, I_CmsResource o2) {
084
085        int result = m_delegate.compare(o1, o2);
086        return -result;
087    }
088}