src/core/net/sf/basedb/util/ToStringComparator.java

Code
Comments
Other
Rev Date Author Line
1993 15 Feb 06 nicklas 1 /*
1993 15 Feb 06 nicklas 2   $Id$
1993 15 Feb 06 nicklas 3
4889 06 Apr 09 nicklas 4   Copyright (C) 2006 Jari Häkkinen, Nicklas Nordborg
1993 15 Feb 06 nicklas 5  
2304 22 May 06 jari 6   This file is part of BASE - BioArray Software Environment.
2304 22 May 06 jari 7   Available at http://base.thep.lu.se/
1993 15 Feb 06 nicklas 8
1993 15 Feb 06 nicklas 9   BASE is free software; you can redistribute it and/or
1993 15 Feb 06 nicklas 10   modify it under the terms of the GNU General Public License
4479 05 Sep 08 jari 11   as published by the Free Software Foundation; either version 3
1993 15 Feb 06 nicklas 12   of the License, or (at your option) any later version.
1993 15 Feb 06 nicklas 13
1993 15 Feb 06 nicklas 14   BASE is distributed in the hope that it will be useful,
1993 15 Feb 06 nicklas 15   but WITHOUT ANY WARRANTY; without even the implied warranty of
1993 15 Feb 06 nicklas 16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1993 15 Feb 06 nicklas 17   GNU General Public License for more details.
1993 15 Feb 06 nicklas 18
1993 15 Feb 06 nicklas 19    You should have received a copy of the GNU General Public License
4515 11 Sep 08 jari 20    along with BASE. If not, see <http://www.gnu.org/licenses/>.
1993 15 Feb 06 nicklas 21 */
1993 15 Feb 06 nicklas 22 package net.sf.basedb.util;
1993 15 Feb 06 nicklas 23
5384 13 Aug 10 nicklas 24 import java.io.Serializable;
1993 15 Feb 06 nicklas 25 import java.util.Comparator;
1993 15 Feb 06 nicklas 26
1993 15 Feb 06 nicklas 27 /**
1993 15 Feb 06 nicklas 28   An implementation of the <code>Comparator</code> interface which uses
1993 15 Feb 06 nicklas 29   the result of the <code>toString</code> method to compare the objects.
1993 15 Feb 06 nicklas 30   Ie. by using this comparator objects are sorted according to the 
1993 15 Feb 06 nicklas 31   <code>toString()</code>
1993 15 Feb 06 nicklas 32   values.
1993 15 Feb 06 nicklas 33   <p>
1993 15 Feb 06 nicklas 34   Note: this comparator may impose orderings that are inconsistent with equals.
1993 15 Feb 06 nicklas 35   
1993 15 Feb 06 nicklas 36   @author Nicklas
1993 15 Feb 06 nicklas 37   @version 2.0
1993 15 Feb 06 nicklas 38   @base.modified $Date$
1993 15 Feb 06 nicklas 39
1993 15 Feb 06 nicklas 40 */
1993 15 Feb 06 nicklas 41 public class ToStringComparator<T>
5384 13 Aug 10 nicklas 42   implements Comparator<T>, Serializable
1993 15 Feb 06 nicklas 43 {
1993 15 Feb 06 nicklas 44
5384 13 Aug 10 nicklas 45   private static final long serialVersionUID = -4125738438373368082L;
1993 15 Feb 06 nicklas 46   private final boolean descending;
1993 15 Feb 06 nicklas 47   
1993 15 Feb 06 nicklas 48   /**
1993 15 Feb 06 nicklas 49     Create a new comparator.
1993 15 Feb 06 nicklas 50     @param descending TRUE to sort objects in descending order (z--a), 
1993 15 Feb 06 nicklas 51       FALSE to sort the objects in ascending order (a--z)
1993 15 Feb 06 nicklas 52   */
1993 15 Feb 06 nicklas 53   public ToStringComparator(boolean descending)
1993 15 Feb 06 nicklas 54   {
1993 15 Feb 06 nicklas 55     this.descending = descending;
1993 15 Feb 06 nicklas 56   }
1993 15 Feb 06 nicklas 57
5384 13 Aug 10 nicklas 58   @Override
1993 15 Feb 06 nicklas 59   public int compare(T o1, T o2)
1993 15 Feb 06 nicklas 60   {
1993 15 Feb 06 nicklas 61     return descending ? o2.toString().compareTo(o1.toString()) : o1.toString().compareTo(o2.toString());
1993 15 Feb 06 nicklas 62   }
1993 15 Feb 06 nicklas 63
1993 15 Feb 06 nicklas 64 }