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

Code
Comments
Other
Rev Date Author Line
5575 18 Feb 11 nicklas 1 /**
5575 18 Feb 11 nicklas 2   $Id$
5575 18 Feb 11 nicklas 3
5575 18 Feb 11 nicklas 4   Copyright (C) 2010 Nicklas Nordborg
5575 18 Feb 11 nicklas 5
5575 18 Feb 11 nicklas 6   This file is part of BASE - BioArray Software Environment.
5575 18 Feb 11 nicklas 7   Available at http://base.thep.lu.se/
5575 18 Feb 11 nicklas 8
5575 18 Feb 11 nicklas 9   BASE is free software; you can redistribute it and/or
5575 18 Feb 11 nicklas 10   modify it under the terms of the GNU General Public License
5575 18 Feb 11 nicklas 11   as published by the Free Software Foundation; either version 3
5575 18 Feb 11 nicklas 12   of the License, or (at your option) any later version.
5575 18 Feb 11 nicklas 13
5575 18 Feb 11 nicklas 14   BASE is distributed in the hope that it will be useful,
5575 18 Feb 11 nicklas 15   but WITHOUT ANY WARRANTY; without even the implied warranty of
5575 18 Feb 11 nicklas 16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
5575 18 Feb 11 nicklas 17   GNU General Public License for more details.
5575 18 Feb 11 nicklas 18
5575 18 Feb 11 nicklas 19   You should have received a copy of the GNU General Public License
5575 18 Feb 11 nicklas 20   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 27   A comparator implementation for strings that sorts them according
5575 18 Feb 11 nicklas 28   to their length. Shortest strings are sorted first. Strings of equal
5575 18 Feb 11 nicklas 29   length are sorted in their natural order.
5575 18 Feb 11 nicklas 30
5575 18 Feb 11 nicklas 31   @author Nicklas
5575 18 Feb 11 nicklas 32   @since 2.16.2
5575 18 Feb 11 nicklas 33   @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 40     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 46     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 }