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;
22 import java.util.regex.Pattern;
23 import org.openecomp.sdc.be.model.tosca.version.ApplicationVersionException;
24 import org.openecomp.sdc.be.model.tosca.version.Version;
26 public final class VersionUtil {
29 * The version must begin with a bloc of numbers, and then it can have one or more bloc of numbers separated by '.' and then it can have alpha
30 * numeric bloc separated by '.' or '-'
32 public static final Pattern VERSION_PATTERN = Pattern.compile("\\d+(?:\\.\\d+)*(?:[\\.-]\\p{Alnum}+)*");
33 private static final String SNAPSHOT_IDENTIFIER = "SNAPSHOT";
36 * Utility class should not have public constructor.
38 private VersionUtil() {
42 * Check if a version is a SNAPSHOT (development) version.
44 * @param version The actual version string.
45 * @return True if the version is a SNAPSHOT version, false if not (RELEASE version).
47 public static boolean isSnapshot(String version) {
48 return version.toUpperCase().contains(SNAPSHOT_IDENTIFIER);
52 * Check if a version is valid
54 * @param version version string to parse
55 * @return true if it's following the defined version pattern
57 public static boolean isValid(String version) {
58 return VERSION_PATTERN.matcher(version).matches();
62 * Parse the version's text to produce a comparable version object
64 * @param version version text to parse
65 * @return a comparable version object
66 * @throws ApplicationVersionException if the version text is not following the defined version pattern
68 public static Version parseVersion(String version) {
69 if (!isValid(version)) {
70 throw new ApplicationVersionException("This version is not valid [" + version + "] as it does not match [" + VERSION_PATTERN + "]");
72 return new Version(version);
83 public static int compare(String versionLeft, String versionRight) {
84 return parseVersion(versionLeft).compareTo(parseVersion(versionRight));