001 /*--------------------------------------------------------------------------+
002 $Id: StringLengthComparator.java 26283 2010-02-18 11:18:57Z juergens $
003 | |
004 | Copyright 2005-2010 Technische Universitaet Muenchen |
005 | |
006 | Licensed under the Apache License, Version 2.0 (the "License"); |
007 | you may not use this file except in compliance with the License. |
008 | You may obtain a copy of the License at |
009 | |
010 | http://www.apache.org/licenses/LICENSE-2.0 |
011 | |
012 | Unless required by applicable law or agreed to in writing, software |
013 | distributed under the License is distributed on an "AS IS" BASIS, |
014 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
015 | See the License for the specific language governing permissions and |
016 | limitations under the License. |
017 +--------------------------------------------------------------------------*/
018 package edu.tum.cs.commons.collections;
019
020 import java.util.Comparator;
021
022 /**
023 * Compares strings by their length. Using this comparator on a list of string
024 * makes shorter strings appear first.
025 *
026 *
027 * @author Florian Deissenboeck
028 * @author $Author: juergens $
029 * @version $Rev: 26283 $
030 * @levd.rating GREEN Hash: BBB78877A0FFB47B3C9FB847CA3B4929
031 */
032 public class StringLengthComparator implements Comparator<String> {
033
034 /**
035 * Compare strings by their length. Using this comparator on a list of
036 * string makes shorter strings appear first. {@inheritDoc}
037 */
038 public int compare(String string1, String string2) {
039 int length1;
040 int length2;
041
042 if (string1 == null) {
043 length1 = 0;
044 } else {
045 length1 = string1.length();
046 }
047
048 if (string2 == null) {
049 length2 = 0;
050 } else {
051 length2 = string2.length();
052 }
053
054 return length1 - length2;
055 }
056
057 }