]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.utils/src/org/simantics/utils/strings/AlphanumComparator.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.utils / src / org / simantics / utils / strings / AlphanumComparator.java
diff --git a/bundles/org.simantics.utils/src/org/simantics/utils/strings/AlphanumComparator.java b/bundles/org.simantics.utils/src/org/simantics/utils/strings/AlphanumComparator.java
new file mode 100644 (file)
index 0000000..74a687d
--- /dev/null
@@ -0,0 +1,207 @@
+/*\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