[8378] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
[5083] | 2 | package org.openstreetmap.josm.tools;
|
---|
| 3 |
|
---|
| 4 | /*
|
---|
| 5 | * The Alphanum Algorithm is an improved sorting algorithm for strings
|
---|
| 6 | * containing numbers. Instead of sorting numbers in ASCII order like a standard
|
---|
| 7 | * sort, this algorithm sorts numbers in numeric order.
|
---|
| 8 | *
|
---|
| 9 | * The Alphanum Algorithm is discussed at http://www.DaveKoelle.com
|
---|
| 10 | *
|
---|
[12909] | 11 | * Released under the MIT License - https://opensource.org/licenses/MIT
|
---|
[5083] | 12 | *
|
---|
[12909] | 13 | * Copyright 2007-2017 David Koelle
|
---|
[5083] | 14 | *
|
---|
[12909] | 15 | * Permission is hereby granted, free of charge, to any person obtaining
|
---|
| 16 | * a copy of this software and associated documentation files (the "Software"),
|
---|
| 17 | * to deal in the Software without restriction, including without limitation
|
---|
| 18 | * the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
---|
| 19 | * and/or sell copies of the Software, and to permit persons to whom the
|
---|
| 20 | * Software is furnished to do so, subject to the following conditions:
|
---|
[5083] | 21 | *
|
---|
[12909] | 22 | * The above copyright notice and this permission notice shall be included
|
---|
| 23 | * in all copies or substantial portions of the Software.
|
---|
[5083] | 24 | *
|
---|
[12909] | 25 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
---|
| 26 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
---|
| 27 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
---|
| 28 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
---|
| 29 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
---|
| 30 | * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
---|
| 31 | * USE OR OTHER DEALINGS IN THE SOFTWARE.
|
---|
[5083] | 32 | */
|
---|
[8308] | 33 | import java.io.Serializable;
|
---|
[6126] | 34 | import java.text.Collator;
|
---|
[5083] | 35 | import java.util.Comparator;
|
---|
[6126] | 36 | import java.util.Locale;
|
---|
[5083] | 37 |
|
---|
| 38 | /**
|
---|
| 39 | * The Alphanum Algorithm is an improved sorting algorithm for strings
|
---|
| 40 | * containing numbers: Instead of sorting numbers in ASCII order like a standard
|
---|
| 41 | * sort, this algorithm sorts numbers in numeric order.
|
---|
| 42 | *
|
---|
| 43 | * The Alphanum Algorithm is discussed at http://www.DaveKoelle.com
|
---|
[6070] | 44 | *
|
---|
[5083] | 45 | * This is an updated version with enhancements made by Daniel Migowski, Andre
|
---|
[12909] | 46 | * Bogus, David Koelle and others.
|
---|
[5083] | 47 | *
|
---|
| 48 | */
|
---|
[8419] | 49 | public final class AlphanumComparator implements Comparator<String>, Serializable {
|
---|
[5083] | 50 |
|
---|
[8308] | 51 | private static final long serialVersionUID = 1L;
|
---|
| 52 |
|
---|
[6804] | 53 | private static final AlphanumComparator INSTANCE = new AlphanumComparator();
|
---|
| 54 |
|
---|
[6985] | 55 | /**
|
---|
| 56 | * Replies the unique instance.
|
---|
| 57 | * @return the unique instance
|
---|
| 58 | */
|
---|
[6804] | 59 | public static AlphanumComparator getInstance() {
|
---|
| 60 | return INSTANCE;
|
---|
| 61 | }
|
---|
| 62 |
|
---|
[5083] | 63 | /**
|
---|
[6804] | 64 | * Constructs a new Alphanum Comparator.
|
---|
| 65 | */
|
---|
[6985] | 66 | private AlphanumComparator() {
|
---|
[6804] | 67 | }
|
---|
| 68 |
|
---|
| 69 | /**
|
---|
[8928] | 70 | * Returns an alphanum chunk.
|
---|
| 71 | * Length of string is passed in for improved efficiency (only need to calculate it once).
|
---|
| 72 | * @param s string
|
---|
| 73 | * @param slength string length
|
---|
| 74 | * @param marker position
|
---|
| 75 | * @return alphanum chunk found at given position
|
---|
[5083] | 76 | */
|
---|
[8870] | 77 | private static String getChunk(String s, int slength, int marker) {
|
---|
[5083] | 78 | StringBuilder chunk = new StringBuilder();
|
---|
| 79 | char c = s.charAt(marker);
|
---|
| 80 | chunk.append(c);
|
---|
| 81 | marker++;
|
---|
| 82 | if (Character.isDigit(c)) {
|
---|
| 83 | while (marker < slength) {
|
---|
| 84 | c = s.charAt(marker);
|
---|
| 85 | if (!Character.isDigit(c)) {
|
---|
| 86 | break;
|
---|
| 87 | }
|
---|
| 88 | chunk.append(c);
|
---|
| 89 | marker++;
|
---|
| 90 | }
|
---|
| 91 | } else {
|
---|
| 92 | while (marker < slength) {
|
---|
| 93 | c = s.charAt(marker);
|
---|
| 94 | if (Character.isDigit(c)) {
|
---|
| 95 | break;
|
---|
| 96 | }
|
---|
| 97 | chunk.append(c);
|
---|
| 98 | marker++;
|
---|
| 99 | }
|
---|
| 100 | }
|
---|
| 101 | return chunk.toString();
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | @Override
|
---|
| 105 | public int compare(String s1, String s2) {
|
---|
[5087] | 106 | if (s1 == null && s2 == null) {
|
---|
| 107 | return 0;
|
---|
| 108 | } else if (s1 == null) {
|
---|
| 109 | return -1;
|
---|
| 110 | } else if (s2 == null) {
|
---|
| 111 | return 1;
|
---|
| 112 | }
|
---|
| 113 |
|
---|
[5083] | 114 | int thisMarker = 0;
|
---|
| 115 | int thatMarker = 0;
|
---|
| 116 | int s1Length = s1.length();
|
---|
| 117 | int s2Length = s2.length();
|
---|
| 118 |
|
---|
| 119 | while (thisMarker < s1Length && thatMarker < s2Length) {
|
---|
| 120 | String thisChunk = getChunk(s1, s1Length, thisMarker);
|
---|
| 121 | thisMarker += thisChunk.length();
|
---|
| 122 |
|
---|
| 123 | String thatChunk = getChunk(s2, s2Length, thatMarker);
|
---|
| 124 | thatMarker += thatChunk.length();
|
---|
| 125 |
|
---|
| 126 | // If both chunks contain numeric characters, sort them numerically
|
---|
| 127 | int result;
|
---|
| 128 | if (Character.isDigit(thisChunk.charAt(0)) && Character.isDigit(thatChunk.charAt(0))) {
|
---|
| 129 | // Simple chunk comparison by length.
|
---|
| 130 | int thisChunkLength = thisChunk.length();
|
---|
| 131 | result = thisChunkLength - thatChunk.length();
|
---|
| 132 | // If equal, the first different number counts
|
---|
| 133 | if (result == 0) {
|
---|
| 134 | for (int i = 0; i < thisChunkLength; i++) {
|
---|
| 135 | result = thisChunk.charAt(i) - thatChunk.charAt(i);
|
---|
| 136 | if (result != 0) {
|
---|
| 137 | return result;
|
---|
| 138 | }
|
---|
| 139 | }
|
---|
| 140 | }
|
---|
| 141 | } else {
|
---|
[6126] | 142 | // Instantiate the collator
|
---|
| 143 | Collator compareOperator = Collator.getInstance(Locale.getDefault());
|
---|
| 144 | // Compare regardless of accented letters
|
---|
| 145 | compareOperator.setStrength(Collator.SECONDARY);
|
---|
| 146 | result = compareOperator.compare(thisChunk, thatChunk);
|
---|
[5083] | 147 | }
|
---|
| 148 |
|
---|
| 149 | if (result != 0) {
|
---|
| 150 | return result;
|
---|
| 151 | }
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | return s1Length - s2Length;
|
---|
| 155 | }
|
---|
| 156 | }
|
---|