You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

VersionComparator.java 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. package org.apache.archiva.common.utils;
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. import org.apache.commons.lang.ArrayUtils;
  21. import org.apache.commons.lang.StringUtils;
  22. import org.apache.commons.lang.math.NumberUtils;
  23. import java.util.ArrayList;
  24. import java.util.Comparator;
  25. import java.util.List;
  26. /**
  27. * VersionComparator - compare the parts of two version strings.
  28. * <p>
  29. * Technique.
  30. * </p>
  31. * <p>
  32. * * Split the version strings into parts by splitting on <code>"-._"</code> first, then breaking apart words from numbers.
  33. * </p>
  34. * <code>
  35. * "1.0" = "1", "0"
  36. * "1.0-alpha-1" = "1", "0", "alpha", "1"
  37. * "2.0-rc2" = "2", "0", "rc", "2"
  38. * "1.3-m2" = "1", "3", "m", "3"
  39. * </code>
  40. * <p>
  41. * compare each part individually, and when they do not match, perform the following test.
  42. * </p>
  43. * <p>
  44. * Numbers are calculated per normal comparison rules.
  45. * Words that are part of the "special word list" will be treated as their index within that heirarchy.
  46. * Words that cannot be identified as special, are treated using normal case-insensitive comparison rules.
  47. * </p>
  48. *
  49. */
  50. public class VersionComparator
  51. implements Comparator<String>
  52. {
  53. private static final Comparator<String> INSTANCE = new VersionComparator();
  54. private final List<String> specialWords;
  55. public VersionComparator()
  56. {
  57. specialWords = new ArrayList<>( 23 );
  58. // ids that refer to LATEST
  59. specialWords.add( "final" );
  60. specialWords.add( "release" );
  61. specialWords.add( "current" );
  62. specialWords.add( "latest" );
  63. specialWords.add( "g" );
  64. specialWords.add( "gold" );
  65. specialWords.add( "fcs" );
  66. // ids that are for a release cycle.
  67. specialWords.add( "a" );
  68. specialWords.add( "alpha" );
  69. specialWords.add( "b" );
  70. specialWords.add( "beta" );
  71. specialWords.add( "pre" );
  72. specialWords.add( "rc" );
  73. specialWords.add( "m" );
  74. specialWords.add( "milestone" );
  75. // ids that are for dev / debug cycles.
  76. specialWords.add( "dev" );
  77. specialWords.add( "test" );
  78. specialWords.add( "debug" );
  79. specialWords.add( "unofficial" );
  80. specialWords.add( "nightly" );
  81. specialWords.add( "incubating" );
  82. specialWords.add( "incubator" );
  83. specialWords.add( "snapshot" );
  84. }
  85. public static Comparator<String> getInstance()
  86. {
  87. return INSTANCE;
  88. }
  89. @Override
  90. public int compare( String o1, String o2 )
  91. {
  92. if ( o1 == null && o2 == null )
  93. {
  94. return 0;
  95. }
  96. if ( o1 == null )
  97. {
  98. return 1;
  99. }
  100. if ( o2 == null )
  101. {
  102. return -1;
  103. }
  104. String[] parts1 = toParts( o1 );
  105. String[] parts2 = toParts( o2 );
  106. int diff;
  107. int partLen = Math.max( parts1.length, parts2.length );
  108. for ( int i = 0; i < partLen; i++ )
  109. {
  110. diff = comparePart( safePart( parts1, i ), safePart( parts2, i ) );
  111. if ( diff != 0 )
  112. {
  113. return diff;
  114. }
  115. }
  116. diff = parts2.length - parts1.length;
  117. if ( diff != 0 )
  118. {
  119. return diff;
  120. }
  121. return o1.compareToIgnoreCase( o2 );
  122. }
  123. private String safePart( String[] parts, int idx )
  124. {
  125. if ( idx < parts.length )
  126. {
  127. return parts[idx];
  128. }
  129. return "0";
  130. }
  131. private int comparePart( String s1, String s2 )
  132. {
  133. boolean is1Num = NumberUtils.isNumber( s1 );
  134. boolean is2Num = NumberUtils.isNumber( s2 );
  135. // (Special Case) Test for numbers both first.
  136. if ( is1Num && is2Num )
  137. {
  138. int i1 = NumberUtils.toInt( s1 );
  139. int i2 = NumberUtils.toInt( s2 );
  140. return i1 - i2;
  141. }
  142. // Test for text both next.
  143. if ( !is1Num && !is2Num )
  144. {
  145. int idx1 = specialWords.indexOf( s1.toLowerCase() );
  146. int idx2 = specialWords.indexOf( s2.toLowerCase() );
  147. // Only operate perform index based operation, if both strings
  148. // are found in the specialWords index.
  149. if ( idx1 >= 0 && idx2 >= 0 )
  150. {
  151. return idx1 - idx2;
  152. }
  153. }
  154. // Comparing text to num
  155. if ( !is1Num && is2Num )
  156. {
  157. return -1;
  158. }
  159. // Comparing num to text
  160. if ( is1Num && !is2Num )
  161. {
  162. return 1;
  163. }
  164. // Return comparison of strings themselves.
  165. return s1.compareToIgnoreCase( s2 );
  166. }
  167. public static String[] toParts( String version )
  168. {
  169. if ( StringUtils.isBlank( version ) )
  170. {
  171. return ArrayUtils.EMPTY_STRING_ARRAY;
  172. }
  173. int modeOther = 0;
  174. int modeDigit = 1;
  175. int modeText = 2;
  176. List<String> parts = new ArrayList<>();
  177. int len = version.length();
  178. int i = 0;
  179. int start = 0;
  180. int mode = modeOther;
  181. while ( i < len )
  182. {
  183. char c = version.charAt( i );
  184. if ( Character.isDigit( c ) )
  185. {
  186. if ( mode != modeDigit )
  187. {
  188. if ( mode != modeOther )
  189. {
  190. parts.add( version.substring( start, i ) );
  191. }
  192. mode = modeDigit;
  193. start = i;
  194. }
  195. }
  196. else if ( Character.isLetter( c ) )
  197. {
  198. if ( mode != modeText )
  199. {
  200. if ( mode != modeOther )
  201. {
  202. parts.add( version.substring( start, i ) );
  203. }
  204. mode = modeText;
  205. start = i;
  206. }
  207. }
  208. else
  209. {
  210. // Other.
  211. if ( mode != modeOther )
  212. {
  213. parts.add( version.substring( start, i ) );
  214. mode = modeOther;
  215. }
  216. }
  217. i++;
  218. }
  219. // Add remainder
  220. if ( mode != modeOther )
  221. {
  222. parts.add( version.substring( start, i ) );
  223. }
  224. return parts.toArray( new String[parts.size()] );
  225. }
  226. }