source: josm/trunk/src/org/openstreetmap/josm/tools/AlphanumComparator.java@ 8308

Last change on this file since 8308 was 8308, checked in by Don-vip, 9 years ago

fix potential NPEs and Sonar issues related to serialization

  • Property svn:eol-style set to native
File size: 4.8 KB
Line 
1package org.openstreetmap.josm.tools;
2
3/*
4 * The Alphanum Algorithm is an improved sorting algorithm for strings
5 * containing numbers. Instead of sorting numbers in ASCII order like a standard
6 * sort, this algorithm sorts numbers in numeric order.
7 *
8 * The Alphanum Algorithm is discussed at http://www.DaveKoelle.com
9 *
10 *
11 * This library is free software; you can redistribute it and/or modify it under
12 * the terms of the GNU Lesser General Public License as published by the Free
13 * Software Foundation; either version 2.1 of the License, or any later version.
14 *
15 * This library is distributed in the hope that it will be useful, but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
18 * details.
19 *
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this library; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 *
24 */
25import java.io.Serializable;
26import java.text.Collator;
27import java.util.Comparator;
28import java.util.Locale;
29
30/**
31 * The Alphanum Algorithm is an improved sorting algorithm for strings
32 * containing numbers: Instead of sorting numbers in ASCII order like a standard
33 * sort, this algorithm sorts numbers in numeric order.
34 *
35 * The Alphanum Algorithm is discussed at http://www.DaveKoelle.com
36 *
37 * This is an updated version with enhancements made by Daniel Migowski, Andre
38 * Bogus, and David Koelle and others.
39 *
40 */
41public class AlphanumComparator implements Comparator<String>, Serializable {
42
43 private static final long serialVersionUID = 1L;
44
45 private static final AlphanumComparator INSTANCE = new AlphanumComparator();
46
47 /**
48 * Replies the unique instance.
49 * @return the unique instance
50 */
51 public static AlphanumComparator getInstance() {
52 return INSTANCE;
53 }
54
55 /**
56 * Constructs a new Alphanum Comparator.
57 */
58 private AlphanumComparator() {
59 }
60
61 /**
62 * Length of string is passed in for improved efficiency (only need to
63 * calculate it once) *
64 */
65 private String getChunk(String s, int slength, int marker) {
66 StringBuilder chunk = new StringBuilder();
67 char c = s.charAt(marker);
68 chunk.append(c);
69 marker++;
70 if (Character.isDigit(c)) {
71 while (marker < slength) {
72 c = s.charAt(marker);
73 if (!Character.isDigit(c)) {
74 break;
75 }
76 chunk.append(c);
77 marker++;
78 }
79 } else {
80 while (marker < slength) {
81 c = s.charAt(marker);
82 if (Character.isDigit(c)) {
83 break;
84 }
85 chunk.append(c);
86 marker++;
87 }
88 }
89 return chunk.toString();
90 }
91
92 @Override
93 public int compare(String s1, String s2) {
94 if (s1 == null && s2 == null) {
95 return 0;
96 } else if (s1 == null) {
97 return -1;
98 } else if (s2 == null) {
99 return 1;
100 }
101
102 int thisMarker = 0;
103 int thatMarker = 0;
104 int s1Length = s1.length();
105 int s2Length = s2.length();
106
107 while (thisMarker < s1Length && thatMarker < s2Length) {
108 String thisChunk = getChunk(s1, s1Length, thisMarker);
109 thisMarker += thisChunk.length();
110
111 String thatChunk = getChunk(s2, s2Length, thatMarker);
112 thatMarker += thatChunk.length();
113
114 // If both chunks contain numeric characters, sort them numerically
115 int result;
116 if (Character.isDigit(thisChunk.charAt(0)) && Character.isDigit(thatChunk.charAt(0))) {
117 // Simple chunk comparison by length.
118 int thisChunkLength = thisChunk.length();
119 result = thisChunkLength - thatChunk.length();
120 // If equal, the first different number counts
121 if (result == 0) {
122 for (int i = 0; i < thisChunkLength; i++) {
123 result = thisChunk.charAt(i) - thatChunk.charAt(i);
124 if (result != 0) {
125 return result;
126 }
127 }
128 }
129 } else {
130 // Instantiate the collator
131 Collator compareOperator = Collator.getInstance(Locale.getDefault());
132 // Compare regardless of accented letters
133 compareOperator.setStrength(Collator.SECONDARY);
134 result = compareOperator.compare(thisChunk, thatChunk);
135 }
136
137 if (result != 0) {
138 return result;
139 }
140 }
141
142 return s1Length - s2Length;
143 }
144}
Note: See TracBrowser for help on using the repository browser.