1 /*
2 * Themis: Java Project Framework
3 * Copyright 2026. Tony Washer
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
6 * use this file except in compliance with the License. You may obtain a copy
7 * of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 * License for the specific language governing permissions and limitations under
15 * the License.
16 */
17
18 package io.github.tonywasher.joceanus.themis.parser.proj;
19
20 import java.util.Objects;
21
22 /**
23 * Comparable version.
24 */
25 public final class ThemisMavenVersion
26 implements Comparable<ThemisMavenVersion> {
27 /**
28 * Version.
29 */
30 private final String theVersion;
31
32 /**
33 * Components.
34 */
35 private final int[] theComponents;
36
37 /**
38 * Modifier.
39 */
40 private final String theModifier;
41
42 /**
43 * Validity.
44 */
45 private final boolean isValid;
46
47 /**
48 * Constructor.
49 *
50 * @param pVersion the version string
51 */
52 private ThemisMavenVersion(final String pVersion) {
53 /* Save the version */
54 theVersion = pVersion;
55
56 /* Split out the modifier */
57 String myVersion = pVersion;
58 boolean valid = true;
59 final String[] myMod = pVersion.split("-");
60 if (myMod.length == 2) {
61 myVersion = myMod[0];
62 theModifier = myMod[1];
63 } else {
64 theModifier = null;
65 valid = myMod.length == 1;
66 }
67
68 /* If we are valid */
69 if (valid) {
70 /* Split out the components */
71 final String[] myVers = myVersion.split("\\.");
72 theComponents = new int[myVers.length];
73
74 /* Protect against exceptions */
75 try {
76 for (int i = 0; i < myVers.length; i++) {
77 theComponents[i] = Integer.parseInt(myVers[i]);
78 }
79 } catch (final NumberFormatException e) {
80 valid = false;
81 }
82 } else {
83 theComponents = null;
84 }
85
86 /* Set validity flag */
87 isValid = valid;
88 }
89
90 /**
91 * Obtain the version.
92 *
93 * @return the version
94 */
95 public String getVersion() {
96 return theVersion;
97 }
98
99 /**
100 * Parse a version string.
101 *
102 * @param pVersion the version
103 * @return the parsed version
104 */
105 public static ThemisMavenVersion parseVersion(final String pVersion) {
106 final ThemisMavenVersion myVersion = new ThemisMavenVersion(pVersion);
107 return myVersion.isValid ? myVersion : null;
108 }
109
110 @Override
111 public String toString() {
112 return theVersion;
113 }
114
115 @Override
116 public int compareTo(final ThemisMavenVersion pThat) {
117 /* Note the lengths of the two components */
118 final int thisLen = theComponents.length;
119 final int thatLen = pThat.theComponents.length;
120
121 /* Loop through the components */
122 for (int i = 0; i <= thisLen; i++) {
123 /* If we have exhausted all our components */
124 if (i == thisLen) {
125 /* If the object has further versions, it is later */
126 if (thatLen > i) {
127 return -1;
128 }
129
130 /* else we are equal on components */
131 break;
132 }
133
134 /* If we have exhausted all objects versions, it is earlier */
135 if (thatLen == i) {
136 return 1;
137 }
138
139 /* Handle the case where we have a version beyond the other object */
140 if (theComponents[i] != pThat.theComponents[i]) {
141 return theComponents[i] - pThat.theComponents[i];
142 }
143 }
144
145 /* Versions are equal, so check the modifier */
146 if (theModifier == null) {
147 return pThat.theModifier == null ? 0 : -1;
148 }
149 return pThat.theModifier == null ? 1 : theModifier.compareTo(pThat.theModifier);
150 }
151
152 @Override
153 public boolean equals(final Object pThat) {
154 /* Handle the trivial cases */
155 if (this == pThat) {
156 return true;
157 }
158 if (pThat == null) {
159 return false;
160 }
161
162 /* Check components */
163 return pThat instanceof ThemisMavenVersion myVers
164 && Objects.equals(theVersion, myVers.getVersion());
165
166 }
167
168 @Override
169 public int hashCode() {
170 return theVersion.hashCode();
171 }
172 }