1. Project Clover database Wed Jan 17 2024 23:40:18 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
201
122
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 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  57 if (!Validator.validate(version, Validator.SEMANTIC_VERSION_NUMBER)) {
54  5 String msg = "The version number " + version
55    + " do not match the Pattern: [1].[2].[3]-[LABEL].";
56  5 throw new MisconfigurationException(msg);
57    }
58  52 String[] fragments = version.split("\\.");
59  52 LOGGER.log("Fragments: " + fragments.length, LogLevel.DEBUG);
60   
61  52 major = Integer.parseInt(fragments[0]);
62  51 if (fragments.length > 1) {
63  50 if (!fragments[1].contains("-")) {
64  49 minor = Integer.parseInt(fragments[1]);
65    } else {
66  1 LOGGER.log("Lable after minor.", LogLevel.DEBUG);
67  1 String[] optional = fragments[1].split("-");
68  1 minor = Integer.parseInt(optional[0]);
69  1 label = optional[1];
70    }
71    }
72  51 if (fragments.length > 2) {
73  30 String[] optional = fragments[2].split("-");
74   
75  30 patch = Integer.parseInt(optional[0]);
76  30 if (optional.length == 2) { //prevent index out of bound exception
77  5 label = optional[1];
78    }
79    }
80  51 LOGGER.log("Version: " + toString(), LogLevel.DEBUG);
81    }
82   
83    //<editor-fold defaultstate="collapsed" desc="Getter / Setter">
84    /**
85    * Return the Major section of a version number as int. MANDANTORY.
86    *
87    * @return Major as int
88    */
 
89  5 toggle public int getMajor() {
90  5 return major;
91    }
92   
93    /**
94    * Return the Minor section of a version number as int. MANDANTORY.
95    *
96    * @return Minor as int
97    */
 
98  5 toggle public int getMinor() {
99  5 return minor;
100    }
101   
102    /**
103    * Return the Patch level section of a version number as int. OPTIONAL. If
104    * the patch level not exist the method return -1.
105    *
106    * @return Patch as int
107    */
 
108  5 toggle public int getPatch() {
109  5 return patch;
110    }
111   
112    /**
113    * Return the Label section of a version number as String. OPTIONAL. If the
114    * label not exist the method return an empty String.
115    *
116    * @return Label as String
117    */
 
118  4 toggle public String getLabel() {
119  4 return label;
120    }
121   
122    /**
123    * Return the whole version number as String.
124    *
125    * @return version as String
126    */
 
127  68 toggle public String getVersion() {
128  68 String version = Integer.toString(major);
129  68 if (minor != -1) {
130  66 version = version + "." + minor;
131    }
132  68 if (patch != -1) {
133  42 version = version + "." + patch;
134    }
135  68 if (!StringUtils.isEmpty(label)) {
136  8 version = version + "-" + label;
137    }
138  68 return version;
139    }
140    //</editor-fold>
141   
 
142  20 toggle @Override
143    public int compareTo(final Version o) {
144    // -1:smaller | 0:equal | 1:greater
145  20 int compare;
146  20 if (this.major > o.major) {
147  2 compare = 1;
148  18 } else if (this.major < o.major) {
149  6 compare = -1;
150    } else {
151  12 if (this.minor > o.minor) {
152  2 compare = 1;
153  10 } else if (this.minor < o.minor) {
154  2 compare = -1;
155    } else {
156   
157  8 if (this.patch > o.patch) {
158  3 compare = 1;
159  5 } else if (this.patch < o.patch) {
160  1 compare = -1;
161    } else {
162  4 compare = 0;
163    }
164    }
165    }
166  20 return compare;
167    }
168   
 
169  15 toggle @Override
170    public boolean equals(final Object object) {
171  15 boolean success = false;
172  15 if (object != null && object instanceof Version) {
173  13 if (this == object) {
174  1 success = true;
175    } else {
176  12 final Version other = (Version) object;
177  12 if (Objects.equals(this.major, other.major)
178    && Objects.equals(this.minor, other.minor)
179    && ((this.patch == -1 || this.patch == 0)
180    && (other.patch == -1 || other.patch == 0)
181    || Objects.equals(this.patch, other.patch))) {
182  5 success = true;
183    }
184    }
185    }
186  15 return success;
187    }
188   
 
189  2 toggle @Override
190    public int hashCode() {
191  2 int hash = Objects.hashCode(this.major);
192  2 hash += Objects.hashCode(this.minor);
193  2 hash += Objects.hashCode(this.patch);
194  2 return hash;
195    }
196   
 
197  63 toggle @Override
198    public String toString() {
199  63 return "Version{" + getVersion() + "}";
200    }
201    }