blob: d05d90edfb3aac4f12964fc96353ab63b21ca833 [file] [log] [blame]
lishuai2ddc4692015-07-31 15:15:16 +08001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.ovsdb.rfc.utils;
17
18/**
19 * Version utility class.
20 */
21public final class VersionUtil {
22
23 /**
24 * Constructs a VersionUtil object. Utility classes should not have a public
25 * or default constructor, otherwise IDE will compile unsuccessfully. This
26 * class should not be instantiated.
27 */
28 private VersionUtil() {
29 }
30
31 public static final String DEFAULT_VERSION_STRING = "0.0.0";
32 private static final String FORMAT = "(\\d+)\\.(\\d+)\\.(\\d+)";
33
34 /**
35 * Match version by the format.
36 * @param version the version String
37 * @throws IllegalArgumentException this is an illegal argument exception
38 */
39 public static void versionMatch(String version) {
40 if (!version.matches(FORMAT)) {
41 throw new IllegalArgumentException("<" + version
42 + "> does not match format " + FORMAT);
43 }
44 }
45
46 /**
47 * Compare fromVersion and toVersion.
48 * @param fromVersion the initial version
49 * @param toVersion the end of the version
lishuai30bc7ad2015-08-05 14:24:07 +080050 * @return an int number
lishuai2ddc4692015-07-31 15:15:16 +080051 */
lishuai30bc7ad2015-08-05 14:24:07 +080052 public static int versionCompare(String fromVersion, String toVersion) {
53 String[] fromArr = fromVersion.split("\\.");
54 String[] toArr = toVersion.split("\\.");
55 int fromFirst = Integer.parseInt(fromArr[0]);
56 int fromMiddle = Integer.parseInt(fromArr[1]);
57 int fromEnd = Integer.parseInt(fromArr[2]);
58 int toFirst = Integer.parseInt(toArr[0]);
59 int toMiddle = Integer.parseInt(toArr[1]);
60 int toEnd = Integer.parseInt(toArr[2]);
61 if (fromFirst - toFirst != 0) {
62 return fromFirst - toFirst;
63 } else if (fromMiddle - toMiddle != 0) {
64 return fromMiddle - toMiddle;
65 } else {
66 return fromEnd - toEnd;
67 }
lishuai2ddc4692015-07-31 15:15:16 +080068 }
69}