2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
20 package org.openecomp.sdc.be.model.tosca.version;
22 * Licensed to the Apache Software Foundation (ASF) under one
23 * or more contributor license agreements. See the NOTICE file
24 * distributed with this work for additional information
25 * regarding copyright ownership. The ASF licenses this file
26 * to you under the Apache License, Version 2.0 (the
27 * "License"); you may not use this file except in compliance
28 * with the License. You may obtain a copy of the License at
30 * http://www.apache.org/licenses/LICENSE-2.0
32 * Unless required by applicable law or agreed to in writing,
33 * software distributed under the License is distributed on an
34 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
35 * KIND, either express or implied. See the License for the
36 * specific language governing permissions and limitations
40 import java.math.BigInteger;
41 import java.util.ArrayList;
42 import java.util.Arrays;
43 import java.util.Iterator;
44 import java.util.List;
45 import java.util.ListIterator;
46 import java.util.Locale;
47 import java.util.Properties;
48 import java.util.Stack;
51 * Generic implementation of version comparison.
56 * <li>mixing of '<code>-</code>' (dash) and '<code>.</code>' (dot)
58 * <li>transition between characters and digits also constitutes a separator:
59 * <code>1.0alpha1 => [1, 0, alpha, 1]</code></li>
60 * <li>unlimited number of version components,</li>
61 * <li>version components in the text can be digits or strings,</li>
62 * <li>strings are checked for well-known qualifiers and the qualifier ordering
63 * is used for version ordering. Well-known qualifiers (case insensitive) are:
65 * <li><code>alpha</code> or <code>a</code></li>
66 * <li><code>beta</code> or <code>b</code></li>
67 * <li><code>milestone</code> or <code>m</code></li>
68 * <li><code>rc</code> or <code>cr</code></li>
69 * <li><code>snapshot</code></li>
70 * <li><code>(the empty string)</code> or <code>ga</code> or
71 * <code>final</code></li>
72 * <li><code>sp</code></li>
74 * Unknown qualifiers are considered after known qualifiers, with lexical order
75 * (always case insensitive),</li>
76 * <li>a dash usually precedes a qualifier, and is always less important than
77 * something preceded with a dot.</li>
81 * @author <a href="mailto:kenney@apache.org">Kenney Westerhof</a>
82 * @author <a href="mailto:hboutemy@apache.org">Hervé Boutemy</a>
83 * @see <a href= "https://cwiki.apache.org/confluence/display/MAVENOLD/Versioning">"Versioning" on Maven Wiki</a>
85 public class ComparableVersion implements Comparable<ComparableVersion> {
88 private String canonical;
89 private ListItem items;
91 public ComparableVersion(String version) {
92 parseVersion(version);
95 private static Item parseItem(boolean isDigit, String buf) {
96 return isDigit ? new IntegerItem(buf) : new StringItem(buf, false);
99 public final void parseVersion(String version) {
100 this.value = version;
101 items = new ListItem();
102 version = version.toLowerCase(Locale.ENGLISH);
103 ListItem list = items;
104 Stack<Item> stack = new Stack<>();
106 boolean isDigit = false;
108 for (int i = 0; i < version.length(); i++) {
109 char c = version.charAt(i);
111 if (i == startIndex) {
112 list.add(IntegerItem.ZERO);
114 list.add(parseItem(isDigit, version.substring(startIndex, i)));
117 } else if (c == '-') {
118 if (i == startIndex) {
119 list.add(IntegerItem.ZERO);
121 list.add(parseItem(isDigit, version.substring(startIndex, i)));
125 list.normalize(); // 1.0-* = 1-*
126 if ((i + 1 < version.length()) && Character.isDigit(version.charAt(i + 1))) {
127 // new ListItem only if previous were digits and new
131 // ie need to differentiate only 1.1 from 1-1
132 list.add(list = new ListItem());
136 } else if (Character.isDigit(c)) {
137 if (!isDigit && i > startIndex) {
138 list.add(new StringItem(version.substring(startIndex, i), true));
143 if (isDigit && i > startIndex) {
144 list.add(parseItem(true, version.substring(startIndex, i)));
150 if (version.length() > startIndex) {
151 list.add(parseItem(isDigit, version.substring(startIndex)));
153 while (!stack.isEmpty()) {
154 list = (ListItem) stack.pop();
157 canonical = items.toString();
161 public int compareTo(ComparableVersion o) {
162 return items.compareTo(o.items);
166 public String toString() {
171 public boolean equals(Object o) {
172 return (o instanceof ComparableVersion) && canonical.equals(((ComparableVersion) o).canonical);
176 public int hashCode() {
177 return canonical.hashCode();
180 private interface Item {
182 int INTEGER_ITEM = 0;
186 int compareTo(Item item);
194 * Represents a numeric item in the version item list.
196 private static class IntegerItem implements Item {
198 public static final IntegerItem ZERO = new IntegerItem();
199 private static final String INVALID_ITEM = "invalid item: ";
200 private static final BigInteger BIG_INTEGER_ZERO = new BigInteger("0");
201 private final BigInteger value;
203 private IntegerItem() {
204 this.value = BIG_INTEGER_ZERO;
207 public IntegerItem(String str) {
208 this.value = new BigInteger(str);
212 public int getType() {
217 public boolean isNull() {
218 return BIG_INTEGER_ZERO.equals(value);
222 public int compareTo(Item item) {
224 return BIG_INTEGER_ZERO.equals(value) ? 0 : 1; // 1.0 == 1, 1.1
228 switch (item.getType()) {
230 return value.compareTo(((IntegerItem) item).value);
232 return 1; // 1.1 > 1-sp
234 return 1; // 1.1 > 1-1
236 throw new RuntimeException(INVALID_ITEM + item.getClass());
241 public String toString() {
242 return value.toString();
247 * Represents a string in the version item list, usually a qualifier.
249 private static class StringItem implements Item {
251 private static final String[] QUALIFIERS = {"alpha", "beta", "milestone", "rc", "snapshot", "", "sp"};
252 private static final List<String> _QUALIFIERS = Arrays.asList(QUALIFIERS);
253 private static final Properties ALIASES = new Properties();
255 * A comparable value for the empty-string qualifier. This one is used to determine if a given qualifier makes the version older than one
256 * without a qualifier, or more recent.
258 private static final String RELEASE_VERSION_INDEX = String.valueOf(_QUALIFIERS.indexOf(""));
261 ALIASES.put("ga", "");
262 ALIASES.put("final", "");
263 ALIASES.put("cr", "rc");
266 private String value;
268 public StringItem(String value, boolean followedByDigit) {
269 if (followedByDigit && value.length() == 1) {
270 // a1 = alpha-1, b1 = beta-1, m1 = milestone-1
271 switch (value.charAt(0)) {
282 throw new RuntimeException("Invalid item");
285 this.value = ALIASES.getProperty(value, value);
289 * Returns a comparable value for a qualifier.
291 * This method takes into account the ordering of known qualifiers then unknown qualifiers with lexical ordering.
293 * just returning an Integer with the index here is faster, but requires a lot of if/then/else to check for -1 or QUALIFIERS.size and then
294 * resort to lexical ordering. Most comparisons are decided by the first character, so this is still fast. If more characters are needed then
295 * it requires a lexical sort anyway.
298 * @return an equivalent value that can be used with lexical comparison
300 public static String comparableQualifier(String qualifier) {
301 int i = _QUALIFIERS.indexOf(qualifier);
302 return i == -1 ? (_QUALIFIERS.size() + "-" + qualifier) : String.valueOf(i);
306 public int getType() {
311 public boolean isNull() {
312 return (comparableQualifier(value).compareTo(RELEASE_VERSION_INDEX) == 0);
316 public int compareTo(Item item) {
318 // 1-rc < 1, 1-ga > 1
319 return comparableQualifier(value).compareTo(RELEASE_VERSION_INDEX);
321 switch (item.getType()) {
323 return -1; // 1.any < 1.1 ?
325 return comparableQualifier(value).compareTo(comparableQualifier(((StringItem) item).value));
327 return -1; // 1.any < 1-1
329 throw new RuntimeException("invalid item: " + item.getClass());
334 public String toString() {
340 * Represents a version list item. This class is used both for the global item list and for sub-lists (which start with '-(number)' in the version
343 private static class ListItem extends ArrayList<Item> implements Item {
346 public int getType() {
351 public boolean isNull() {
352 return (size() == 0);
356 for (ListIterator<Item> iterator = listIterator(size()); iterator.hasPrevious(); ) {
357 Item item = iterator.previous();
359 iterator.remove(); // remove null trailing items: 0, "",
369 public int compareTo(Item item) {
372 return 0; // 1-0 = 1- (normalize) = 1
375 return first.compareTo(null);
377 switch (item.getType()) {
379 return -1; // 1-1 < 1.0.x
381 return 1; // 1-1 > 1-sp
383 Iterator<Item> left = iterator();
384 Iterator<Item> right = ((ListItem) item).iterator();
385 while (left.hasNext() || right.hasNext()) {
386 Item l = left.hasNext() ? left.next() : null;
387 Item r = right.hasNext() ? right.next() : null;
389 if (r != null && l != null) {
390 result = l.compareTo(r);
391 } else if (r == null && l == null) {
393 } else if (l == null) {
398 // if this is shorter, then invert the compare and mul with
402 // int result = (l == null ? (r == null ? 0 : -1 *
404 // r.compareTo(l)) : l.compareTo(r));
411 throw new RuntimeException("invalid item: " + item.getClass());
416 public String toString() {
417 StringBuilder buffer = new StringBuilder("(");
418 for (Iterator<Item> iter = iterator(); iter.hasNext(); ) {
419 buffer.append(iter.next());
420 if (iter.hasNext()) {
425 return buffer.toString();