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=========================================================
21 package org.openecomp.sdc.be.model.tosca;
23 import org.openecomp.sdc.be.model.tosca.version.ApplicationVersionException;
24 import org.openecomp.sdc.be.model.tosca.version.Version;
26 import java.util.regex.Pattern;
28 public final class VersionUtil {
30 /** Utility class should not have public constructor. */
31 private VersionUtil() {
35 * The version must begin with a bloc of numbers, and then it can have one
36 * or more bloc of numbers separated by '.' and then it can have alpha
37 * numeric bloc separated by '.' or '-'
39 public static final Pattern VERSION_PATTERN = Pattern.compile("\\d+(?:\\.\\d+)*(?:[\\.-]\\p{Alnum}+)*");
40 private static final String SNAPSHOT_IDENTIFIER = "SNAPSHOT";
43 * Check if a version is a SNAPSHOT (development) version.
46 * The actual version string.
47 * @return True if the version is a SNAPSHOT version, false if not (RELEASE
50 public static boolean isSnapshot(String version) {
51 return version.toUpperCase().contains(SNAPSHOT_IDENTIFIER);
55 * Check if a version is valid
58 * version string to parse
59 * @return true if it's following the defined version pattern
61 public static boolean isValid(String version) {
62 return VERSION_PATTERN.matcher(version).matches();
66 * Parse the version's text to produce a comparable version object
69 * version text to parse
70 * @return a comparable version object
71 * @throws ApplicationVersionException
72 * if the version text is not following the defined version
75 public static Version parseVersion(String version) {
76 if (!isValid(version)) {
77 throw new ApplicationVersionException(
78 "This version is not valid [" + version + "] as it does not match [" + VERSION_PATTERN + "]");
80 return new Version(version);
91 public static int compare(String versionLeft, String versionRight) {
92 return parseVersion(versionLeft).compareTo(parseVersion(versionRight));