AlphanumComparator.java

  1. /*******************************************************************************
  2.  * Copyright 2013 André Rouél
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  *   http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  ******************************************************************************/
  16. package net.sf.uadetector.internal.util;

  17. /*
  18.  * The Alphanum Algorithm is an improved sorting algorithm for strings
  19.  * containing numbers.  Instead of sorting numbers in ASCII order like
  20.  * a standard sort, this algorithm sorts numbers in numeric order.
  21.  *
  22.  * The Alphanum Algorithm is discussed at http://www.DaveKoelle.com
  23.  *
  24.  *
  25.  * This library is free software; you can redistribute it and/or
  26.  * modify it under the terms of the GNU Lesser General Public
  27.  * License as published by the Free Software Foundation; either
  28.  * version 2.1 of the License, or any later version.
  29.  *
  30.  * This library is distributed in the hope that it will be useful,
  31.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  32.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  33.  * Lesser General Public License for more details.
  34.  *
  35.  * You should have received a copy of the GNU Lesser General Public
  36.  * License along with this library; if not, write to the Free Software
  37.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  38.  *
  39.  */

  40. import java.io.Serializable;
  41. import java.util.Comparator;

  42. /**
  43.  * This is an updated version with enhancements made by Daniel Migowski, Andre Bogus, and David Koelle
  44.  *
  45.  * To convert to use Templates (Java 1.5+): - Change "implements Comparator" to "implements Comparator<String>" - Change
  46.  * "compare(Object o1, Object o2)" to "compare(String s1, String s2)" - Remove the type checking and casting in
  47.  * compare().
  48.  *
  49.  * To use this class: Use the static "sort" method from the java.util.Collections class: Collections.sort(your list, new
  50.  * AlphanumComparator());
  51.  */
  52. public final class AlphanumComparator implements Comparator<String>, Serializable {

  53.     private static final long serialVersionUID = 5727692755534146935L;

  54.     protected static int compareDigits(final String s1, final String s2) {
  55.         int thisMarker = 0;
  56.         int thatMarker = 0;
  57.         final int s1Length = s1.length();
  58.         final int s2Length = s2.length();

  59.         while (thisMarker < s1Length && thatMarker < s2Length) {
  60.             final String thisChunk = getChunk(s1, s1Length, thisMarker);
  61.             thisMarker += thisChunk.length();

  62.             final String thatChunk = getChunk(s2, s2Length, thatMarker);
  63.             thatMarker += thatChunk.length();

  64.             // If both chunks contain numeric characters, sort them numerically
  65.             int result = 0;
  66.             if (isDigit(thisChunk.charAt(0)) && isDigit(thatChunk.charAt(0))) {
  67.                 // Simple chunk comparison by length.
  68.                 final int thisChunkLength = thisChunk.length();
  69.                 result = thisChunkLength - thatChunk.length();
  70.                 // If equal, the first different number counts
  71.                 if (result == 0) {
  72.                     for (int i = 0; i < thisChunkLength; i++) {
  73.                         result = thisChunk.charAt(i) - thatChunk.charAt(i);
  74.                         if (result != 0) {
  75.                             return result;
  76.                         }
  77.                     }
  78.                 }
  79.             } else {
  80.                 result = thisChunk.compareTo(thatChunk);
  81.             }

  82.             if (result != 0) {
  83.                 return result;
  84.             }
  85.         }

  86.         return s1Length - s2Length;
  87.     }

  88.     /**
  89.      * Length of string is passed in for improved efficiency (only need to calculate it once)
  90.      */
  91.     private static String getChunk(final String s, final int slength, int marker) {
  92.         final StringBuilder chunk = new StringBuilder();
  93.         char c = s.charAt(marker);
  94.         chunk.append(c);
  95.         marker++;
  96.         if (isDigit(c)) {
  97.             while (marker < slength) {
  98.                 c = s.charAt(marker);
  99.                 if (!isDigit(c)) {
  100.                     break;
  101.                 }
  102.                 chunk.append(c);
  103.                 marker++;
  104.             }
  105.         } else {
  106.             while (marker < slength) {
  107.                 c = s.charAt(marker);
  108.                 if (isDigit(c)) {
  109.                     break;
  110.                 }
  111.                 chunk.append(c);
  112.                 marker++;
  113.             }
  114.         }
  115.         return chunk.toString();
  116.     }

  117.     private static boolean isDigit(final char ch) {
  118.         return ch >= 48 && ch <= 57;
  119.     }

  120.     @Override
  121.     public int compare(final String a, final String b) {
  122.         int result = 0;
  123.         if (a == null) {
  124.             if (b != null) {
  125.                 result = -1;
  126.             }
  127.         } else {
  128.             if (b == null) {
  129.                 result = 1;
  130.             } else {
  131.                 result = compareDigits(a, b);
  132.             }
  133.         }
  134.         return result;
  135.     }

  136. }