5575 |
18 Feb 11 |
nicklas |
1 |
/** |
5575 |
18 Feb 11 |
nicklas |
$Id$ |
5575 |
18 Feb 11 |
nicklas |
3 |
|
5575 |
18 Feb 11 |
nicklas |
Copyright (C) 2010 Nicklas Nordborg |
5575 |
18 Feb 11 |
nicklas |
5 |
|
5575 |
18 Feb 11 |
nicklas |
This file is part of BASE - BioArray Software Environment. |
5575 |
18 Feb 11 |
nicklas |
Available at http://base.thep.lu.se/ |
5575 |
18 Feb 11 |
nicklas |
8 |
|
5575 |
18 Feb 11 |
nicklas |
BASE is free software; you can redistribute it and/or |
5575 |
18 Feb 11 |
nicklas |
modify it under the terms of the GNU General Public License |
5575 |
18 Feb 11 |
nicklas |
as published by the Free Software Foundation; either version 3 |
5575 |
18 Feb 11 |
nicklas |
of the License, or (at your option) any later version. |
5575 |
18 Feb 11 |
nicklas |
13 |
|
5575 |
18 Feb 11 |
nicklas |
BASE is distributed in the hope that it will be useful, |
5575 |
18 Feb 11 |
nicklas |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
5575 |
18 Feb 11 |
nicklas |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
5575 |
18 Feb 11 |
nicklas |
GNU General Public License for more details. |
5575 |
18 Feb 11 |
nicklas |
18 |
|
5575 |
18 Feb 11 |
nicklas |
You should have received a copy of the GNU General Public License |
5575 |
18 Feb 11 |
nicklas |
along with BASE. If not, see <http://www.gnu.org/licenses/>. |
5575 |
18 Feb 11 |
nicklas |
21 |
*/ |
5575 |
18 Feb 11 |
nicklas |
22 |
package net.sf.basedb.util; |
5575 |
18 Feb 11 |
nicklas |
23 |
|
5575 |
18 Feb 11 |
nicklas |
24 |
import java.util.Comparator; |
5575 |
18 Feb 11 |
nicklas |
25 |
|
5575 |
18 Feb 11 |
nicklas |
26 |
/** |
5575 |
18 Feb 11 |
nicklas |
A comparator implementation for strings that sorts them according |
5575 |
18 Feb 11 |
nicklas |
to their length. Shortest strings are sorted first. Strings of equal |
5575 |
18 Feb 11 |
nicklas |
length are sorted in their natural order. |
5575 |
18 Feb 11 |
nicklas |
30 |
|
5575 |
18 Feb 11 |
nicklas |
@author Nicklas |
5575 |
18 Feb 11 |
nicklas |
@since 2.16.2 |
5575 |
18 Feb 11 |
nicklas |
@base.modified $Date$ |
5575 |
18 Feb 11 |
nicklas |
34 |
*/ |
5575 |
18 Feb 11 |
nicklas |
35 |
public class StringLengthComparator |
5575 |
18 Feb 11 |
nicklas |
36 |
implements Comparator<String> |
5575 |
18 Feb 11 |
nicklas |
37 |
{ |
5575 |
18 Feb 11 |
nicklas |
38 |
|
5575 |
18 Feb 11 |
nicklas |
39 |
/** |
5575 |
18 Feb 11 |
nicklas |
Create a new string length comparator instance. |
5575 |
18 Feb 11 |
nicklas |
41 |
*/ |
5575 |
18 Feb 11 |
nicklas |
42 |
public StringLengthComparator() |
5575 |
18 Feb 11 |
nicklas |
43 |
{} |
5575 |
18 Feb 11 |
nicklas |
44 |
|
5575 |
18 Feb 11 |
nicklas |
45 |
/* |
5575 |
18 Feb 11 |
nicklas |
From the Comparator interface |
5575 |
18 Feb 11 |
nicklas |
47 |
------------------------------ |
5575 |
18 Feb 11 |
nicklas |
48 |
*/ |
5575 |
18 Feb 11 |
nicklas |
49 |
@Override |
5575 |
18 Feb 11 |
nicklas |
50 |
public int compare(String o1, String o2) |
5575 |
18 Feb 11 |
nicklas |
51 |
{ |
5575 |
18 Feb 11 |
nicklas |
52 |
if (o1 == o2) return 0; |
5575 |
18 Feb 11 |
nicklas |
53 |
int length1 = o1 == null ? 0 : o1.length(); |
5575 |
18 Feb 11 |
nicklas |
54 |
int length2 = o2 == null ? 0 : o2.length(); |
5575 |
18 Feb 11 |
nicklas |
55 |
if (length1 != length2) return length1 - length2; |
5575 |
18 Feb 11 |
nicklas |
56 |
if (length1 == 0) return 0; |
5575 |
18 Feb 11 |
nicklas |
57 |
return o1.compareTo(o2); |
5575 |
18 Feb 11 |
nicklas |
58 |
} |
5575 |
18 Feb 11 |
nicklas |
59 |
// ------------------------------- |
5575 |
18 Feb 11 |
nicklas |
60 |
} |