re base code
[sdc.git] / catalog-model / src / main / java / org / openecomp / sdc / be / model / tosca / VersionUtil.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdc.be.model.tosca;
22
23 import org.openecomp.sdc.be.model.tosca.version.ApplicationVersionException;
24 import org.openecomp.sdc.be.model.tosca.version.Version;
25
26 import java.util.regex.Pattern;
27
28 public final class VersionUtil {
29
30     /** Utility class should not have public constructor. */
31     private VersionUtil() {
32     }
33
34     /**
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 '-'
38      */
39     public static final Pattern VERSION_PATTERN = Pattern.compile("\\d+(?:\\.\\d+)*(?:[\\.-]\\p{Alnum}+)*");
40     private static final String SNAPSHOT_IDENTIFIER = "SNAPSHOT";
41
42     /**
43      * Check if a version is a SNAPSHOT (development) version.
44      *
45      * @param version
46      *            The actual version string.
47      * @return True if the version is a SNAPSHOT version, false if not (RELEASE
48      *         version).
49      */
50     public static boolean isSnapshot(String version) {
51         return version.toUpperCase().contains(SNAPSHOT_IDENTIFIER);
52     }
53
54     /**
55      * Check if a version is valid
56      *
57      * @param version
58      *            version string to parse
59      * @return true if it's following the defined version pattern
60      */
61     public static boolean isValid(String version) {
62         return VERSION_PATTERN.matcher(version).matches();
63     }
64
65     /**
66      * Parse the version's text to produce a comparable version object
67      *
68      * @param version
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
73      *             pattern
74      */
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 + "]");
79         } else {
80             return new Version(version);
81         }
82     }
83
84     /**
85      * Compare 2 versions
86      *
87      * @param versionLeft
88      * @param versionRight
89      * @return
90      */
91     public static int compare(String versionLeft, String versionRight) {
92         return parseVersion(versionLeft).compareTo(parseVersion(versionRight));
93     }
94 }