5570 |
16 Feb 11 |
nicklas |
1 |
/** |
5570 |
16 Feb 11 |
nicklas |
$Id$ |
5570 |
16 Feb 11 |
nicklas |
3 |
|
5570 |
16 Feb 11 |
nicklas |
Copyright (C) 2011 Nicklas Nordborg |
5570 |
16 Feb 11 |
nicklas |
5 |
|
5570 |
16 Feb 11 |
nicklas |
This file is part of BASE - BioArray Software Environment. |
5570 |
16 Feb 11 |
nicklas |
Available at http://base.thep.lu.se/ |
5570 |
16 Feb 11 |
nicklas |
8 |
|
5570 |
16 Feb 11 |
nicklas |
BASE is free software; you can redistribute it and/or |
5570 |
16 Feb 11 |
nicklas |
modify it under the terms of the GNU General Public License |
5570 |
16 Feb 11 |
nicklas |
as published by the Free Software Foundation; either version 3 |
5570 |
16 Feb 11 |
nicklas |
of the License, or (at your option) any later version. |
5570 |
16 Feb 11 |
nicklas |
13 |
|
5570 |
16 Feb 11 |
nicklas |
BASE is distributed in the hope that it will be useful, |
5570 |
16 Feb 11 |
nicklas |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
5570 |
16 Feb 11 |
nicklas |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
5570 |
16 Feb 11 |
nicklas |
GNU General Public License for more details. |
5570 |
16 Feb 11 |
nicklas |
18 |
|
5570 |
16 Feb 11 |
nicklas |
You should have received a copy of the GNU General Public License |
5570 |
16 Feb 11 |
nicklas |
along with BASE. If not, see <http://www.gnu.org/licenses/>. |
5570 |
16 Feb 11 |
nicklas |
21 |
*/ |
5570 |
16 Feb 11 |
nicklas |
22 |
package net.sf.basedb.util; |
5570 |
16 Feb 11 |
nicklas |
23 |
|
5570 |
16 Feb 11 |
nicklas |
24 |
import java.util.Collection; |
5570 |
16 Feb 11 |
nicklas |
25 |
import java.util.Comparator; |
5570 |
16 Feb 11 |
nicklas |
26 |
import java.util.SortedSet; |
5570 |
16 Feb 11 |
nicklas |
27 |
|
5570 |
16 Feb 11 |
nicklas |
28 |
/** |
5570 |
16 Feb 11 |
nicklas |
29 |
|
5570 |
16 Feb 11 |
nicklas |
A comparator implemenation that sorts a given set of |
5570 |
16 Feb 11 |
nicklas |
objects before all other object. |
5570 |
16 Feb 11 |
nicklas |
32 |
|
5570 |
16 Feb 11 |
nicklas |
@author Nicklas |
5570 |
16 Feb 11 |
nicklas |
@since 2.17 |
5570 |
16 Feb 11 |
nicklas |
@base.modified $Date$ |
5570 |
16 Feb 11 |
nicklas |
36 |
*/ |
5570 |
16 Feb 11 |
nicklas |
37 |
public class PriorityComparator<T> |
5570 |
16 Feb 11 |
nicklas |
38 |
implements Comparator<T> |
5570 |
16 Feb 11 |
nicklas |
39 |
{ |
5570 |
16 Feb 11 |
nicklas |
40 |
|
5570 |
16 Feb 11 |
nicklas |
41 |
private final Collection<T> priority; |
5570 |
16 Feb 11 |
nicklas |
42 |
private final Comparator<? super T> parent; |
5570 |
16 Feb 11 |
nicklas |
43 |
|
5570 |
16 Feb 11 |
nicklas |
44 |
/** |
5570 |
16 Feb 11 |
nicklas |
Create a new comparator. |
5570 |
16 Feb 11 |
nicklas |
46 |
|
5570 |
16 Feb 11 |
nicklas |
@param priority A collection containing the objects that should be |
5570 |
16 Feb 11 |
nicklas |
sorted first |
5570 |
16 Feb 11 |
nicklas |
@param parent The parent comparator used to sort the objects |
5570 |
16 Feb 11 |
nicklas |
50 |
*/ |
5570 |
16 Feb 11 |
nicklas |
51 |
public PriorityComparator(Collection<T> priority, Comparator<? super T> parent) |
5570 |
16 Feb 11 |
nicklas |
52 |
{ |
5570 |
16 Feb 11 |
nicklas |
53 |
this.priority = priority; |
5570 |
16 Feb 11 |
nicklas |
54 |
this.parent = parent; |
5570 |
16 Feb 11 |
nicklas |
55 |
} |
5570 |
16 Feb 11 |
nicklas |
56 |
|
5570 |
16 Feb 11 |
nicklas |
57 |
/** |
5570 |
16 Feb 11 |
nicklas |
Create a new comparator using the same sort order as the given |
5570 |
16 Feb 11 |
nicklas |
sorted set. |
5570 |
16 Feb 11 |
nicklas |
60 |
|
5570 |
16 Feb 11 |
nicklas |
@param priority A collection containing the objects that should be |
5570 |
16 Feb 11 |
nicklas |
sorted first |
5570 |
16 Feb 11 |
nicklas |
63 |
*/ |
5570 |
16 Feb 11 |
nicklas |
64 |
public PriorityComparator(SortedSet<T> priority) |
5570 |
16 Feb 11 |
nicklas |
65 |
{ |
5570 |
16 Feb 11 |
nicklas |
66 |
this.priority = priority; |
5570 |
16 Feb 11 |
nicklas |
67 |
this.parent = priority.comparator(); |
5570 |
16 Feb 11 |
nicklas |
68 |
} |
5570 |
16 Feb 11 |
nicklas |
69 |
|
5570 |
16 Feb 11 |
nicklas |
70 |
/* |
5570 |
16 Feb 11 |
nicklas |
From the Comparator interface |
5570 |
16 Feb 11 |
nicklas |
72 |
----------------------------- |
5570 |
16 Feb 11 |
nicklas |
73 |
*/ |
5570 |
16 Feb 11 |
nicklas |
74 |
@Override |
5570 |
16 Feb 11 |
nicklas |
75 |
public int compare(T o1, T o2) |
5570 |
16 Feb 11 |
nicklas |
76 |
{ |
5570 |
16 Feb 11 |
nicklas |
77 |
boolean p1 = priority.contains(o1); |
5570 |
16 Feb 11 |
nicklas |
78 |
boolean p2 = priority.contains(o2); |
5570 |
16 Feb 11 |
nicklas |
79 |
if (p1 == p2) |
5570 |
16 Feb 11 |
nicklas |
80 |
{ |
5570 |
16 Feb 11 |
nicklas |
81 |
return parent.compare(o1, o2); |
5570 |
16 Feb 11 |
nicklas |
82 |
} |
5570 |
16 Feb 11 |
nicklas |
83 |
return p1 ? -1 : 1; |
5570 |
16 Feb 11 |
nicklas |
84 |
} |
5570 |
16 Feb 11 |
nicklas |
85 |
// ------------------------------ |
5570 |
16 Feb 11 |
nicklas |
86 |
|
5570 |
16 Feb 11 |
nicklas |
87 |
} |