View Javadoc

1   /*
2    * Copyright (C) 2003-2006 Christian Siefkes <christian@siefkes.net>.
3    * Development of this software is supported by the German Research Society,
4    * Berlin-Brandenburg Graduate School in Distributed Information Systems
5    * (DFG grant no. GRK 316).
6    *
7    * This program is free software; you can redistribute it and/or modify
8    * it under the terms of the GNU General Public License as published by
9    * the Free Software Foundation; either version 2 of the License, or
10   * (at your option) any later version.
11   *
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Public License for more details.
16   *
17   * You should have received a copy of the GNU General Public License
18   * along with this program; if not, visit
19   * http://www.gnu.org/licenses/gpl.html or write to the Free Software
20   * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21   */
22  package de.fu_berlin.ties.util;
23  
24  import java.util.Comparator;
25  
26  import org.apache.commons.lang.builder.ToStringBuilder;
27  
28  /***
29   * Compares objects, inverting the order of a wrapped comparator. This means
30   * that numeric objects will be sorted in decreasing order etc.
31   *
32   * @author Christian Siefkes
33   * @version $Revision: 1.9 $, $Date: 2006/10/21 16:04:27 $, $Author: siefkes $
34   * @param <T> the type to compare
35   */
36  public class InvertedComparator<T> implements Comparator<T> {
37  
38      /***
39       * The comparator wrapped and inverted by this instance.
40       */
41      private final Comparator<T> wrappedComparator;
42  
43      /***
44       * Creates a new instance, inverting the order returned by the provided
45       * comparator.
46       * @param wrappedComp the comparator to wrap and invert; must not be
47       * <code>null</code>
48       */
49      public InvertedComparator(final Comparator<T> wrappedComp) {
50          super();
51          wrappedComparator = wrappedComp;
52      }
53  
54      /***
55       * Compares its two arguments for order. Inverts the natural ordering
56       * of the two arguments resp. if order of the {@link #getWrappedComparator()
57       * wrapped comparator}, if any. So this method returns a negative integer,
58       * zero, or a positive integer as the first argument is greater than,
59       * equal to, or less than the second.
60       *
61       * @param o1 the first object to be compared
62       * @param o2 the second object to be compared
63       * @return the inverted ordering of the arguments
64       */
65      public int compare(final T o1, final T o2) {
66          return -wrappedComparator.compare(o1, o2);
67      }
68  
69      /***
70       * Returns the comparator wrapped and inverted by this instance.
71       *
72       * @return the value of the attribute
73       */
74      public Comparator getWrappedComparator() {
75          return wrappedComparator;
76      }
77  
78      /***
79       * Returns a string representation of this object.
80       *
81       * @return a textual representation
82       */
83      public String toString() {
84          return new ToStringBuilder(this)
85          .append("wrapped comparator", wrappedComparator)
86          .toString();
87      }
88  
89  }