1. Project Clover database Mon Dec 22 2025 14:59:38 CST
  2. Package org.europa.together.domain

File Version.java

 

Coverage histogram

../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

34
59
10
1
203
123
34
0.58
5.9
10
3.4

Classes

Class Line # Actions
Version 36 59 0% 34 0
1.0100%
 

Contributing tests

This file is covered by 21 tests. .

Source view

1    package org.europa.together.domain;
2   
3    import java.util.Objects;
4    import org.europa.together.application.LogbackLogger;
5    import org.europa.together.business.Logger;
6    import org.europa.together.exceptions.MisconfigurationException;
7    import org.europa.together.utils.StringUtils;
8    import org.europa.together.utils.Validator;
9   
10    /**
11    * Data class for program version numbers, based on semantic versioning. This
12    * implementation allwos to compare and sort version numbers.<br>
13    * Pattern: Major.Minor.Patch-Label (Patch and Label are optional)<br>
14    * <code>
15    * true: equals(1.2.3-SNAPSHOT : 1.2.3-SNAPSHOT);
16    * true: equals(1.2.3-LABLE : 1.2.3-SNAPSHOT);
17    * true: equals(1.0 : 1.0.0);
18    * false: equals(1.0 : 1.0.1);
19    * false: equals(1.0-LABEL : 1.0);
20    * A greater B: compareTo(2.0 : 1.0);
21    * A greater B: compareTo(1.1 : 1.0);
22    * A greater B: compareTo(1.0.1 : 1.0.0);
23    * A greater B: compareTo(2.1 : 1.1);
24    * A equal B: compareTo(2.1.1 : 2.1.1-SNAPSHOT);
25    * </code> The only recomennded label is SNAPSHOT, because labels are not part
26    * of comparing.
27    *
28    * <b>Definition:</b><br>
29    * A version of a software artifact is always uniqe. Artifacts can have
30    * different versions (1:n relation). That means when an artifact a exist two
31    * times and both are not identical, the first artifact has a different version
32    * than the second artifact. Both artifacts can not have the same version. If
33    * both artifacts with the same name have the same version it is impossible to
34    * distinguish them.
35    */
 
36    public final class Version implements Comparable<Version> {
37   
38    private static final Logger LOGGER = new LogbackLogger(Version.class);
39   
40    private int major = -1;
41    private int minor = -1;
42    private int patch = -1;
43    private String label = "";
44   
45    /**
46    * Constructor.
47    *
48    * @param version as String
49    * @throws org.europa.together.exceptions.MisconfigurationException
50    */
 
51  58 toggle public Version(final String version)
52    throws MisconfigurationException {
53   
54  57 if (!Validator.validate(version, Validator.SEMANTIC_VERSION_NUMBER)) {
55  5 String msg = "The version number " + version
56    + " do not match the Pattern: [1].[2].[3]-[LABEL].";
57  5 throw new MisconfigurationException(msg);
58    } else {
59  52 String[] fragments = version.split("\\.");
60  52 LOGGER.log("Fragments: " + fragments.length, LogLevel.DEBUG);
61   
62  52 major = Integer.parseInt(fragments[0]);
63  51 if (fragments.length > 1) {
64  50 if (!fragments[1].contains("-")) {
65  49 minor = Integer.parseInt(fragments[1]);
66    } else {
67  1 LOGGER.log("Lable after minor.", LogLevel.DEBUG);
68  1 String[] optional = fragments[1].split("-");
69  1 minor = Integer.parseInt(optional[0]);
70  1 label = optional[1];
71    }
72    }
73  51 if (fragments.length > 2) {
74  30 String[] optional = fragments[2].split("-");
75   
76  30 patch = Integer.parseInt(optional[0]);
77  30 if (optional.length == 2) { //prevent index out of bound exception
78  5 label = optional[1];
79    }
80    }
81  51 LOGGER.log("Version: " + toString(), LogLevel.DEBUG);
82    }
83    }
84   
85    //<editor-fold defaultstate="collapsed" desc="Getter / Setter">
86    /**
87    * Return the Major section of a version number as int. MANDANTORY.
88    *
89    * @return Major as int
90    */
 
91  5 toggle public int getMajor() {
92  5 return major;
93    }
94   
95    /**
96    * Return the Minor section of a version number as int. MANDANTORY.
97    *
98    * @return Minor as int
99    */
 
100  5 toggle public int getMinor() {
101  5 return minor;
102    }
103   
104    /**
105    * Return the Patch level section of a version number as int. OPTIONAL. If
106    * the patch level not exist the method return -1.
107    *
108    * @return Patch as int
109    */
 
110  5 toggle public int getPatch() {
111  5 return patch;
112    }
113   
114    /**
115    * Return the Label section of a version number as String. OPTIONAL. If the
116    * label not exist the method return an empty String.
117    *
118    * @return Label as String
119    */
 
120  4 toggle public String getLabel() {
121  4 return label;
122    }
123   
124    /**
125    * Return the whole version number as String.
126    *
127    * @return version as String
128    */
 
129  68 toggle public String getVersion() {
130  68 String version = Integer.toString(major);
131  68 if (minor != -1) {
132  66 version = version + "." + minor;
133    }
134  68 if (patch != -1) {
135  42 version = version + "." + patch;
136    }
137  68 if (!StringUtils.isEmpty(label)) {
138  8 version = version + "-" + label;
139    }
140  68 return version;
141    }
142    //</editor-fold>
143   
 
144  20 toggle @Override
145    public int compareTo(final Version o) {
146    // -1:smaller | 0:equal | 1:greater
147  20 int compare;
148  20 if (this.major > o.major) {
149  2 compare = 1;
150  18 } else if (this.major < o.major) {
151  6 compare = -1;
152    } else {
153  12 if (this.minor > o.minor) {
154  2 compare = 1;
155  10 } else if (this.minor < o.minor) {
156  2 compare = -1;
157    } else {
158   
159  8 if (this.patch > o.patch) {
160  3 compare = 1;
161  5 } else if (this.patch < o.patch) {
162  1 compare = -1;
163    } else {
164  4 compare = 0;
165    }
166    }
167    }
168  20 return compare;
169    }
170   
 
171  15 toggle @Override
172    public boolean equals(final Object object) {
173  15 boolean success = false;
174  15 if (object != null && object instanceof Version) {
175  13 if (this == object) {
176  1 success = true;
177    } else {
178  12 final Version other = (Version) object;
179  12 if (Objects.equals(this.major, other.major)
180    && Objects.equals(this.minor, other.minor)
181    && ((this.patch == -1 || this.patch == 0)
182    && (other.patch == -1 || other.patch == 0)
183    || Objects.equals(this.patch, other.patch))) {
184  5 success = true;
185    }
186    }
187    }
188  15 return success;
189    }
190   
 
191  2 toggle @Override
192    public int hashCode() {
193  2 int hash = Objects.hashCode(this.major);
194  2 hash += Objects.hashCode(this.minor);
195  2 hash += Objects.hashCode(this.patch);
196  2 return hash;
197    }
198   
 
199  63 toggle @Override
200    public String toString() {
201  63 return "Version{" + getVersion() + "}";
202    }
203    }