Reformat catalog-model
[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 package org.openecomp.sdc.be.model.tosca;
21
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;
25
26 public final class VersionUtil {
27
28     /**
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 '-'
31      */
32     public static final Pattern VERSION_PATTERN = Pattern.compile("\\d+(?:\\.\\d+)*(?:[\\.-]\\p{Alnum}+)*");
33     private static final String SNAPSHOT_IDENTIFIER = "SNAPSHOT";
34
35     /**
36      * Utility class should not have public constructor.
37      */
38     private VersionUtil() {
39     }
40
41     /**
42      * Check if a version is a SNAPSHOT (development) version.
43      *
44      * @param version The actual version string.
45      * @return True if the version is a SNAPSHOT version, false if not (RELEASE version).
46      */
47     public static boolean isSnapshot(String version) {
48         return version.toUpperCase().contains(SNAPSHOT_IDENTIFIER);
49     }
50
51     /**
52      * Check if a version is valid
53      *
54      * @param version version string to parse
55      * @return true if it's following the defined version pattern
56      */
57     public static boolean isValid(String version) {
58         return VERSION_PATTERN.matcher(version).matches();
59     }
60
61     /**
62      * Parse the version's text to produce a comparable version object
63      *
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
67      */
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 + "]");
71         } else {
72             return new Version(version);
73         }
74     }
75
76     /**
77      * Compare 2 versions
78      *
79      * @param versionLeft
80      * @param versionRight
81      * @return
82      */
83     public static int compare(String versionLeft, String versionRight) {
84         return parseVersion(versionLeft).compareTo(parseVersion(versionRight));
85     }
86 }