1993 |
15 Feb 06 |
nicklas |
1 |
/* |
1993 |
15 Feb 06 |
nicklas |
$Id$ |
1993 |
15 Feb 06 |
nicklas |
3 |
|
4889 |
06 Apr 09 |
nicklas |
Copyright (C) 2006 Jari Häkkinen, Nicklas Nordborg |
1993 |
15 Feb 06 |
nicklas |
5 |
|
2304 |
22 May 06 |
jari |
This file is part of BASE - BioArray Software Environment. |
2304 |
22 May 06 |
jari |
Available at http://base.thep.lu.se/ |
1993 |
15 Feb 06 |
nicklas |
8 |
|
1993 |
15 Feb 06 |
nicklas |
BASE is free software; you can redistribute it and/or |
1993 |
15 Feb 06 |
nicklas |
modify it under the terms of the GNU General Public License |
4479 |
05 Sep 08 |
jari |
as published by the Free Software Foundation; either version 3 |
1993 |
15 Feb 06 |
nicklas |
of the License, or (at your option) any later version. |
1993 |
15 Feb 06 |
nicklas |
13 |
|
1993 |
15 Feb 06 |
nicklas |
BASE is distributed in the hope that it will be useful, |
1993 |
15 Feb 06 |
nicklas |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
1993 |
15 Feb 06 |
nicklas |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
1993 |
15 Feb 06 |
nicklas |
GNU General Public License for more details. |
1993 |
15 Feb 06 |
nicklas |
18 |
|
1993 |
15 Feb 06 |
nicklas |
You should have received a copy of the GNU General Public License |
4515 |
11 Sep 08 |
jari |
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 |
An implementation of the <code>Comparator</code> interface which uses |
1993 |
15 Feb 06 |
nicklas |
the result of the <code>toString</code> method to compare the objects. |
1993 |
15 Feb 06 |
nicklas |
Ie. by using this comparator objects are sorted according to the |
1993 |
15 Feb 06 |
nicklas |
<code>toString()</code> |
1993 |
15 Feb 06 |
nicklas |
values. |
1993 |
15 Feb 06 |
nicklas |
<p> |
1993 |
15 Feb 06 |
nicklas |
Note: this comparator may impose orderings that are inconsistent with equals. |
1993 |
15 Feb 06 |
nicklas |
35 |
|
1993 |
15 Feb 06 |
nicklas |
@author Nicklas |
1993 |
15 Feb 06 |
nicklas |
@version 2.0 |
1993 |
15 Feb 06 |
nicklas |
@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 |
Create a new comparator. |
1993 |
15 Feb 06 |
nicklas |
@param descending TRUE to sort objects in descending order (z--a), |
1993 |
15 Feb 06 |
nicklas |
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 |
} |