01: /*
02: * File : $Source: /usr/local/cvs/opencms/src/org/opencms/file/collectors/ComparatorInverter.java,v $
03: * Date : $Date: 2008-02-27 12:05:43 $
04: * Version: $Revision: 1.3 $
05: *
06: * This library is part of OpenCms -
07: * the Open Source Content Management System
08: *
09: * Copyright (c) 2002 - 2008 Alkacon Software GmbH (http://www.alkacon.com)
10: *
11: * This library is free software; you can redistribute it and/or
12: * modify it under the terms of the GNU Lesser General Public
13: * License as published by the Free Software Foundation; either
14: * version 2.1 of the License, or (at your option) any later version.
15: *
16: * This library is distributed in the hope that it will be useful,
17: * but WITHOUT ANY WARRANTY; without even the implied warranty of
18: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19: * Lesser General Public License for more details.
20: *
21: * For further information about Alkacon Software GmbH, please see the
22: * company website: http://www.alkacon.com
23: *
24: * For further information about OpenCms, please see the
25: * project website: http://www.opencms.org
26: *
27: * You should have received a copy of the GNU Lesser General Public
28: * License along with this library; if not, write to the Free Software
29: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30: */
31:
32: package org.opencms.file.collectors;
33:
34: import java.util.Comparator;
35:
36: /**
37: * Wrapper around a comparator that inverts comparison results which may e.g. be
38: * used to invert sort orders. <p>
39: *
40: * This is used to create <code>{@link java.util.SortedSet}</code> instances that may
41: * sort in different order (ascending vs. descending).<p>
42: *
43: * <table border="1">
44: * <tr>
45: * <th>Internal comparator result</th>
46: * <th>Transformed result</th>
47: * </tr>
48: * <tr>
49: * <td>-1</td>
50: * <td>+1</td>
51: * </tr>
52: * <tr>
53: * <td>0</td>
54: * <td>0</td>
55: * </tr>
56: * <tr>
57: * <td>+1</td>
58: * <td>-1</td>
59: * </tr>
60: * </table>
61: * <p>
62: *
63: * @author Achim Westermann
64: *
65: * @since 7.0.3
66: *
67: */
68: public final class ComparatorInverter implements Comparator {
69:
70: private Comparator m_delegate;
71:
72: /**
73: * Creates a comparator that will invert the result of the given comparator.
74: * <p>
75: *
76: * @param toInvert the comparator to invert results of
77: */
78: public ComparatorInverter(Comparator toInvert) {
79:
80: this .m_delegate = toInvert;
81: }
82:
83: /**
84: * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
85: */
86: public int compare(Object o1, Object o2) {
87:
88: int result = this.m_delegate.compare(o1, o2);
89: return -result;
90: }
91:
92: }
|