KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > meshcms > util > ReverseComparator


1 /*
2  * MeshCMS - A simple CMS based on SiteMesh
3  * Copyright (C) 2004-2007 Luciano Vernaschi
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * You can contact the author at http://www.cromoteca.com
20  * and at info@cromoteca.com
21  */

22
23 package org.meshcms.util;
24
25 import java.io.*;
26 import java.util.*;
27
28 /**
29  * Allows to sort in reversed ordering. If a Comparator is not supplied, the
30  * order will be the reversed natural ordering.
31  */

32 public class ReverseComparator implements Comparator, Serializable {
33   private Comparator comparator;
34
35   /**
36    * Creates a new instance of ReverseComparator without specifying a base
37    * Comparator
38    */

39   public ReverseComparator() {
40   }
41
42   /**
43    * Creates a new instance of ReverseComparator to reverse the specified
44    * Comparator
45    */

46   public ReverseComparator(Comparator comparator) {
47     this.comparator = comparator;
48   }
49
50   public int compare(Object JavaDoc o1, Object JavaDoc o2) {
51     if (comparator == null) {
52       return ((Comparable JavaDoc) o2).compareTo(o1);
53     } else {
54       return comparator.compare(o2, o1);
55     }
56   }
57 }
58
Popular Tags