|
| 1 | +/* |
| 2 | + * Copyright 2016. |
| 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 | + */ |
| 16 | +package es.tid.keyserver.core.lib; |
| 17 | + |
| 18 | +/** |
| 19 | + * Version class controller. |
| 20 | + * This class provide an easy way for manipulate version strings or compare |
| 21 | + * between them. |
| 22 | + * @see <a href="http://semver.org/">Semantic Version 2.0.0</a> |
| 23 | + * @author <a href="mailto:jgm1986@hotmail.com">Javier Gusano Martinez</a> |
| 24 | + * @since v0.4.1 |
| 25 | + */ |
| 26 | +public class Version { |
| 27 | + /** |
| 28 | + * Major version field vXX.xx.xx |
| 29 | + */ |
| 30 | + private int major; |
| 31 | + /** |
| 32 | + * Minor version field vxx.XX.xx |
| 33 | + */ |
| 34 | + private int minor; |
| 35 | + /** |
| 36 | + * Patch version field vxx.xx.XX |
| 37 | + */ |
| 38 | + private int patch; |
| 39 | + |
| 40 | + /** |
| 41 | + * Class constructor. |
| 42 | + * @param strVer String with the version numbers. Example: v0.1.0 |
| 43 | + * @since v0.4.1 |
| 44 | + */ |
| 45 | + public Version(String strVer){ |
| 46 | + String tmp=strVer; |
| 47 | + if(tmp.startsWith("v")){ |
| 48 | + tmp = tmp.substring(1); |
| 49 | + } |
| 50 | + /* The period / dot is a special character in regex, you have to escape |
| 51 | + it either with a double backlash \\. */ |
| 52 | + String [] values = tmp.split("\\."); |
| 53 | + major = Integer.valueOf(values[0]); |
| 54 | + minor = Integer.valueOf(values[1]); |
| 55 | + patch = Integer.valueOf(values[2]); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * This method returns the major version field. |
| 60 | + * @return Major version field as integer. |
| 61 | + * @since v0.4.1 |
| 62 | + */ |
| 63 | + public int getMajor(){ |
| 64 | + return this.major; |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * This method returns the minor version field. |
| 69 | + * @return Minor version field as integer. |
| 70 | + * @since v0.4.1 |
| 71 | + */ |
| 72 | + public int getMinor(){ |
| 73 | + return this.minor; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * This method returns the patch version field. |
| 78 | + * @return Patch version field. |
| 79 | + * @since v0.4.1 |
| 80 | + */ |
| 81 | + public int getPatch(){ |
| 82 | + return this.patch; |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * This method is used to compare if two Version objects are equals |
| 87 | + * (contains the same version fields). |
| 88 | + * @param extVer External Version object to be compared with the current |
| 89 | + * version object. |
| 90 | + * @return True if are equals. |
| 91 | + * @since v0.4.1 |
| 92 | + */ |
| 93 | + public boolean equalsTo(Version extVer){ |
| 94 | + return ((extVer.getMajor() == this.major) && |
| 95 | + (extVer.getMinor() == this.minor) && |
| 96 | + (extVer.getPatch() == this.patch)); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Method used to compare two Version objects. |
| 101 | + * This method compare if the current object is major than the external |
| 102 | + * object. |
| 103 | + * @param extVer External version object used to be compared with the |
| 104 | + * current object. |
| 105 | + * @return True if the current object is greater than |
| 106 | + * @since v0.4.1 |
| 107 | + */ |
| 108 | + public boolean greaterThan(Version extVer){ |
| 109 | + if(this.major > extVer.getMajor()){ |
| 110 | + return true; |
| 111 | + } else if(this.major == extVer.getMajor()){ |
| 112 | + if(this.minor > extVer.getMinor()){ |
| 113 | + return true; |
| 114 | + } else if(this.minor == extVer.getMinor()){ |
| 115 | + return this.patch > extVer.getPatch(); |
| 116 | + } else{ |
| 117 | + return false; |
| 118 | + } |
| 119 | + } else { |
| 120 | + return false; |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Method used to compare two Version objects. |
| 126 | + * This method compare if the current object is lower than the external |
| 127 | + * object. |
| 128 | + * @param extVer External version object used to be compared with the |
| 129 | + * current object. |
| 130 | + * @return True if the current object is lower than |
| 131 | + * @since v0.4.1 |
| 132 | + */ |
| 133 | + public boolean lowerThan(Version extVer){ |
| 134 | + if(this.major < extVer.getMajor()){ |
| 135 | + return true; |
| 136 | + } else if(this.major == extVer.getMajor()){ |
| 137 | + if(this.minor < extVer.getMinor()){ |
| 138 | + return true; |
| 139 | + } else if(this.minor == extVer.getMinor()){ |
| 140 | + return this.patch < extVer.getPatch(); |
| 141 | + } else{ |
| 142 | + return false; |
| 143 | + } |
| 144 | + } else { |
| 145 | + return false; |
| 146 | + } |
| 147 | + } |
| 148 | +} |
0 commit comments