-/*\r
- * The Alphanum Algorithm is an improved sorting algorithm for strings\r
- * containing numbers. Instead of sorting numbers in ASCII order like\r
- * a standard sort, this algorithm sorts numbers in numeric order.\r
- *\r
- * The Alphanum Algorithm is discussed at http://www.DaveKoelle.com\r
- *\r
- *\r
- * This library is free software; you can redistribute it and/or\r
- * modify it under the terms of the GNU Lesser General Public\r
- * License as published by the Free Software Foundation; either\r
- * version 2.1 of the License, or any later version.\r
- *\r
- * This library is distributed in the hope that it will be useful,\r
- * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\r
- * Lesser General Public License for more details.\r
- *\r
- * You should have received a copy of the GNU Lesser General Public\r
- * License along with this library; if not, write to the Free Software\r
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\r
- *\r
- */\r
-package org.simantics.utils.strings;\r
-\r
-import java.nio.CharBuffer;\r
-import java.util.Comparator;\r
-\r
-/**\r
- * This is an updated version with enhancements made by Daniel Migowski, Andre\r
- * Bogus, and David Koelle\r
- * \r
- * To convert to use Templates (Java 1.5+): - Change "implements Comparator" to\r
- * "implements Comparator<String>" - Change "compare(Object o1, Object o2)" to\r
- * "compare(String s1, String s2)" - Remove the type checking and casting in\r
- * compare().\r
- * \r
- * To use this class: Use the static "sort" method from the\r
- * java.util.Collections class: Collections.sort(your list, new\r
- * AlphanumComparator());\r
- * \r
- * Modified by Tuukka Lehtonen to re-use two CharBuffer instances compared to\r
- * 2*chunk-count StringBuilders per single comparison for some added efficiency.\r
- */\r
-public class AlphanumComparator implements Comparator<Object>\r
-{\r
- private static final CaseInsensitiveComparator CASE_INSENSITIVE_ORDER = new CaseInsensitiveComparator();\r
-\r
- private static class CaseInsensitiveComparator implements Comparator<CharBuffer>, java.io.Serializable {\r
-\r
- private static final long serialVersionUID = 5247677019801470582L;\r
-\r
- @Override\r
- public int compare(CharBuffer s1, CharBuffer s2) {\r
- int n1 = s1.limit(), n2 = s2.limit();\r
- for (int i1 = 0, i2 = 0; i1 < n1 && i2 < n2; i1++, i2++) {\r
- char c1 = s1.charAt(i1);\r
- char c2 = s2.charAt(i2);\r
- if (c1 != c2) {\r
- c1 = Character.toUpperCase(c1);\r
- c2 = Character.toUpperCase(c2);\r
- if (c1 != c2) {\r
- c1 = Character.toLowerCase(c1);\r
- c2 = Character.toLowerCase(c2);\r
- if (c1 != c2) {\r
- return c1 - c2;\r
- }\r
- }\r
- }\r
- }\r
- return n1 - n2;\r
- }\r
- }\r
-\r
- public static final AlphanumComparator COMPARATOR = new AlphanumComparator(null);\r
- public static final AlphanumComparator CASE_INSENSITIVE_COMPARATOR = new AlphanumComparator(CASE_INSENSITIVE_ORDER);\r
-\r
- private final Comparator<CharBuffer> comparator;\r
-\r
- private static class Buffers {\r
- CharBuffer b1 = CharBuffer.allocate(0);\r
- CharBuffer b2 = b1;\r
- }\r
-\r
- private static final ThreadLocal<Buffers> buffers = new ThreadLocal<Buffers>() {\r
- protected Buffers initialValue() {\r
- return new Buffers();\r
- }\r
- };\r
-\r
- private AlphanumComparator(Comparator<CharBuffer> comparator) {\r
- this.comparator = comparator;\r
- }\r
-\r
- private final boolean isDigit(char ch)\r
- {\r
- return ch >= 48 && ch <= 57;\r
- }\r
-\r
- /** Length of string is passed in for improved efficiency (only need to calculate it once) **/\r
- private final CharBuffer getChunk(String s, int slength, int marker, CharBuffer chunk)\r
- {\r
- if (chunk == null || chunk.capacity() < slength) {\r
- chunk = CharBuffer.allocate(slength);\r
- } else {\r
- chunk.rewind();\r
- chunk.limit(chunk.capacity());\r
- }\r
-\r
- char c = s.charAt(marker);\r
- chunk.append(c);\r
- marker++;\r
- if (isDigit(c))\r
- {\r
- while (marker < slength)\r
- {\r
- c = s.charAt(marker);\r
- if (!isDigit(c))\r
- break;\r
- chunk.append(c);\r
- marker++;\r
- }\r
- } else\r
- {\r
- while (marker < slength)\r
- {\r
- c = s.charAt(marker);\r
- if (isDigit(c))\r
- break;\r
- chunk.append(c);\r
- marker++;\r
- }\r
- }\r
- chunk.limit(chunk.position());\r
- chunk.rewind();\r
- return chunk;\r
- }\r
-\r
- @Override\r
- public int compare(Object o1, Object o2)\r
- {\r
- if (o1 == o2)\r
- return 0;\r
-\r
- String s1 = o1.toString();\r
- String s2 = o2.toString();\r
-\r
- int thisMarker = 0;\r
- int thatMarker = 0;\r
- int s1Length = s1.length();\r
- int s2Length = s2.length();\r
-\r
- Buffers bufs = buffers.get();\r
- CharBuffer thisChunk = bufs.b1;\r
- CharBuffer thatChunk = bufs.b2;\r
-\r
- try {\r
- while (thisMarker < s1Length && thatMarker < s2Length)\r
- {\r
- thisChunk = getChunk(s1, s1Length, thisMarker, thisChunk);\r
- thisMarker += thisChunk.limit();\r
-\r
- thatChunk = getChunk(s2, s2Length, thatMarker, thatChunk);\r
- thatMarker += thatChunk.limit();\r
-\r
- // If both chunks contain numeric characters, sort them numerically\r
- int result = 0;\r
- if (isDigit(thisChunk.charAt(0)) && isDigit(thatChunk.charAt(0)))\r
- {\r
- // Simple chunk comparison by length.\r
- int thisChunkLength = thisChunk.limit();\r
- result = thisChunkLength - thatChunk.limit();\r
- // If equal, the first different number counts\r
- if (result == 0)\r
- {\r
- for (int i = 0; i < thisChunkLength; i++)\r
- {\r
- result = thisChunk.charAt(i) - thatChunk.charAt(i);\r
- if (result != 0)\r
- {\r
- return result;\r
- }\r
- }\r
- }\r
- } else\r
- {\r
- result = comparator != null ? comparator.compare(thisChunk, thatChunk) : thisChunk.compareTo(thatChunk);\r
- }\r
-\r
- if (result != 0)\r
- return result;\r
- }\r
-\r
- return s1Length - s2Length;\r
- } finally {\r
- bufs.b1 = reset(thisChunk);\r
- bufs.b2 = reset(thatChunk);\r
- }\r
- }\r
-\r
- private static CharBuffer reset(CharBuffer buffer) {\r
- buffer.rewind();\r
- buffer.limit(buffer.capacity());\r
- return buffer;\r
- }\r
-\r
-}\r
+/*
+ * The Alphanum Algorithm is an improved sorting algorithm for strings
+ * containing numbers. Instead of sorting numbers in ASCII order like
+ * a standard sort, this algorithm sorts numbers in numeric order.
+ *
+ * The Alphanum Algorithm is discussed at http://www.DaveKoelle.com
+ *
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ */
+package org.simantics.utils.strings;
+
+import java.nio.CharBuffer;
+import java.util.Comparator;
+
+/**
+ * This is an updated version with enhancements made by Daniel Migowski, Andre
+ * Bogus, and David Koelle
+ *
+ * To convert to use Templates (Java 1.5+): - Change "implements Comparator" to
+ * "implements Comparator<String>" - Change "compare(Object o1, Object o2)" to
+ * "compare(String s1, String s2)" - Remove the type checking and casting in
+ * compare().
+ *
+ * To use this class: Use the static "sort" method from the
+ * java.util.Collections class: Collections.sort(your list, new
+ * AlphanumComparator());
+ *
+ * Modified by Tuukka Lehtonen to re-use two CharBuffer instances compared to
+ * 2*chunk-count StringBuilders per single comparison for some added efficiency.
+ */
+public class AlphanumComparator implements Comparator<Object>
+{
+ private static final CaseInsensitiveComparator CASE_INSENSITIVE_ORDER = new CaseInsensitiveComparator();
+
+ private static class CaseInsensitiveComparator implements Comparator<CharBuffer>, java.io.Serializable {
+
+ private static final long serialVersionUID = 5247677019801470582L;
+
+ @Override
+ public int compare(CharBuffer s1, CharBuffer s2) {
+ int n1 = s1.limit(), n2 = s2.limit();
+ for (int i1 = 0, i2 = 0; i1 < n1 && i2 < n2; i1++, i2++) {
+ char c1 = s1.charAt(i1);
+ char c2 = s2.charAt(i2);
+ if (c1 != c2) {
+ c1 = Character.toUpperCase(c1);
+ c2 = Character.toUpperCase(c2);
+ if (c1 != c2) {
+ c1 = Character.toLowerCase(c1);
+ c2 = Character.toLowerCase(c2);
+ if (c1 != c2) {
+ return c1 - c2;
+ }
+ }
+ }
+ }
+ return n1 - n2;
+ }
+ }
+
+ public static final AlphanumComparator COMPARATOR = new AlphanumComparator(null);
+ public static final AlphanumComparator CASE_INSENSITIVE_COMPARATOR = new AlphanumComparator(CASE_INSENSITIVE_ORDER);
+
+ private final Comparator<CharBuffer> comparator;
+
+ private static class Buffers {
+ CharBuffer b1 = CharBuffer.allocate(0);
+ CharBuffer b2 = b1;
+ }
+
+ private static final ThreadLocal<Buffers> buffers = new ThreadLocal<Buffers>() {
+ protected Buffers initialValue() {
+ return new Buffers();
+ }
+ };
+
+ private AlphanumComparator(Comparator<CharBuffer> comparator) {
+ this.comparator = comparator;
+ }
+
+ private final boolean isDigit(char ch)
+ {
+ return ch >= 48 && ch <= 57;
+ }
+
+ /** Length of string is passed in for improved efficiency (only need to calculate it once) **/
+ private final CharBuffer getChunk(String s, int slength, int marker, CharBuffer chunk)
+ {
+ if (chunk == null || chunk.capacity() < slength) {
+ chunk = CharBuffer.allocate(slength);
+ } else {
+ chunk.rewind();
+ chunk.limit(chunk.capacity());
+ }
+
+ char c = s.charAt(marker);
+ chunk.append(c);
+ marker++;
+ if (isDigit(c))
+ {
+ while (marker < slength)
+ {
+ c = s.charAt(marker);
+ if (!isDigit(c))
+ break;
+ chunk.append(c);
+ marker++;
+ }
+ } else
+ {
+ while (marker < slength)
+ {
+ c = s.charAt(marker);
+ if (isDigit(c))
+ break;
+ chunk.append(c);
+ marker++;
+ }
+ }
+ chunk.limit(chunk.position());
+ chunk.rewind();
+ return chunk;
+ }
+
+ @Override
+ public int compare(Object o1, Object o2)
+ {
+ if (o1 == o2)
+ return 0;
+
+ String s1 = o1.toString();
+ String s2 = o2.toString();
+
+ int thisMarker = 0;
+ int thatMarker = 0;
+ int s1Length = s1.length();
+ int s2Length = s2.length();
+
+ Buffers bufs = buffers.get();
+ CharBuffer thisChunk = bufs.b1;
+ CharBuffer thatChunk = bufs.b2;
+
+ try {
+ while (thisMarker < s1Length && thatMarker < s2Length)
+ {
+ thisChunk = getChunk(s1, s1Length, thisMarker, thisChunk);
+ thisMarker += thisChunk.limit();
+
+ thatChunk = getChunk(s2, s2Length, thatMarker, thatChunk);
+ thatMarker += thatChunk.limit();
+
+ // If both chunks contain numeric characters, sort them numerically
+ int result = 0;
+ if (isDigit(thisChunk.charAt(0)) && isDigit(thatChunk.charAt(0)))
+ {
+ // Simple chunk comparison by length.
+ int thisChunkLength = thisChunk.limit();
+ result = thisChunkLength - thatChunk.limit();
+ // If equal, the first different number counts
+ if (result == 0)
+ {
+ for (int i = 0; i < thisChunkLength; i++)
+ {
+ result = thisChunk.charAt(i) - thatChunk.charAt(i);
+ if (result != 0)
+ {
+ return result;
+ }
+ }
+ }
+ } else
+ {
+ result = comparator != null ? comparator.compare(thisChunk, thatChunk) : thisChunk.compareTo(thatChunk);
+ }
+
+ if (result != 0)
+ return result;
+ }
+
+ return s1Length - s2Length;
+ } finally {
+ bufs.b1 = reset(thisChunk);
+ bufs.b2 = reset(thatChunk);
+ }
+ }
+
+ private static CharBuffer reset(CharBuffer buffer) {
+ buffer.rewind();
+ buffer.limit(buffer.capacity());
+ return buffer;
+ }
+
+}