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

Code
Comments
Other
Rev Date Author Line
5570 16 Feb 11 nicklas 1 /**
5570 16 Feb 11 nicklas 2   $Id$
5570 16 Feb 11 nicklas 3
5570 16 Feb 11 nicklas 4   Copyright (C) 2011 Nicklas Nordborg
5570 16 Feb 11 nicklas 5
5570 16 Feb 11 nicklas 6   This file is part of BASE - BioArray Software Environment.
5570 16 Feb 11 nicklas 7   Available at http://base.thep.lu.se/
5570 16 Feb 11 nicklas 8
5570 16 Feb 11 nicklas 9   BASE is free software; you can redistribute it and/or
5570 16 Feb 11 nicklas 10   modify it under the terms of the GNU General Public License
5570 16 Feb 11 nicklas 11   as published by the Free Software Foundation; either version 3
5570 16 Feb 11 nicklas 12   of the License, or (at your option) any later version.
5570 16 Feb 11 nicklas 13
5570 16 Feb 11 nicklas 14   BASE is distributed in the hope that it will be useful,
5570 16 Feb 11 nicklas 15   but WITHOUT ANY WARRANTY; without even the implied warranty of
5570 16 Feb 11 nicklas 16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
5570 16 Feb 11 nicklas 17   GNU General Public License for more details.
5570 16 Feb 11 nicklas 18
5570 16 Feb 11 nicklas 19   You should have received a copy of the GNU General Public License
5570 16 Feb 11 nicklas 20   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 30   A comparator implemenation that sorts a given set of
5570 16 Feb 11 nicklas 31   objects before all other object. 
5570 16 Feb 11 nicklas 32
5570 16 Feb 11 nicklas 33   @author Nicklas
5570 16 Feb 11 nicklas 34   @since 2.17
5570 16 Feb 11 nicklas 35   @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 45     Create a new comparator.
5570 16 Feb 11 nicklas 46
5570 16 Feb 11 nicklas 47     @param priority A collection containing the objects that should be
5570 16 Feb 11 nicklas 48       sorted first
5570 16 Feb 11 nicklas 49     @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 58     Create a new comparator using the same sort order as the given
5570 16 Feb 11 nicklas 59     sorted set.
5570 16 Feb 11 nicklas 60   
5570 16 Feb 11 nicklas 61     @param priority A collection containing the objects that should be
5570 16 Feb 11 nicklas 62       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 71     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 }