File tree Expand file tree Collapse file tree 2 files changed +40
-1
lines changed
main/java/no/nav/k9/søknad/felles
test/java/no/nav/k9/søknad/felles Expand file tree Collapse file tree 2 files changed +40
-1
lines changed Original file line number Diff line number Diff line change 1
1
package no .nav .k9 .søknad .felles ;
2
2
3
+ import java .util .Arrays ;
4
+ import java .util .List ;
3
5
import java .util .Objects ;
4
6
5
7
import jakarta .validation .constraints .Pattern ;
9
11
import com .fasterxml .jackson .annotation .JsonIgnore ;
10
12
import com .fasterxml .jackson .annotation .JsonValue ;
11
13
12
- public class Versjon {
14
+ public class Versjon implements Comparable < Versjon > {
13
15
14
16
@ JsonIgnore
15
17
private static final String SEMVER_REGEX = "(\\ d+)\\ .(\\ d+)\\ .(\\ d+)" ;
@@ -44,6 +46,27 @@ public boolean erGyldig() {
44
46
return verdi .matches (SEMVER_REGEX );
45
47
}
46
48
49
+ @ Override
50
+ public int compareTo (Versjon that ) {
51
+ if (!this .erGyldig () || !that .erGyldig ()) {
52
+ throw new IllegalArgumentException ("Ugyldig format på Versjon" );
53
+ }
54
+
55
+ List <Integer > thisComponents = Arrays .stream (this .verdi .split ("\\ ." ))
56
+ .map (Integer ::parseInt ).toList ();
57
+ List <Integer > thatComponents = Arrays .stream (that .verdi .split ("\\ ." ))
58
+ .map (Integer ::parseInt ).toList ();
59
+
60
+ for (int i =0 ; i <3 ; i ++) {
61
+ Integer thisVersjonstall = thisComponents .get (i );
62
+ Integer thatVersjonstall = thatComponents .get (i );
63
+ if (!Objects .equals (thisVersjonstall , thatVersjonstall )) {
64
+ return thisVersjonstall .compareTo (thatVersjonstall );
65
+ }
66
+ }
67
+ return 0 ;
68
+ }
69
+
47
70
@ Override
48
71
public boolean equals (Object obj ) {
49
72
if (obj == this )
Original file line number Diff line number Diff line change
1
+ package no .nav .k9 .søknad .felles ;
2
+
3
+ import static org .assertj .core .api .Assertions .assertThat ;
4
+
5
+ import org .junit .jupiter .api .Test ;
6
+
7
+ class VersjonTest {
8
+
9
+ @ Test
10
+ void sammenligneVersjoner () {
11
+ assertThat (Versjon .of ("2.0.0" )).isGreaterThan (Versjon .of ("1.9.9" ));
12
+ assertThat (Versjon .of ("1.2.0" )).isGreaterThan (Versjon .of ("1.1.9" ));
13
+ assertThat (Versjon .of ("1.1.2" )).isGreaterThan (Versjon .of ("1.1.1" ));
14
+ assertThat (Versjon .of ("1.1.1" )).isEqualTo (Versjon .of ("1.1.1" ));
15
+ }
16
+ }
You can’t perform that action at this time.
0 commit comments