Skip to content

Commit 609012d

Browse files
committed
wip
Signed-off-by: Attila Mészáros <[email protected]>
1 parent e068261 commit 609012d

File tree

2 files changed

+24
-38
lines changed

2 files changed

+24
-38
lines changed

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/reconciler/PrimaryUpdateAndCacheUtils.java

Lines changed: 21 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -453,53 +453,39 @@ public static <P extends HasMetadata> P addFinalizerWithSSA(
453453

454454
public static int compareResourceVersions(String v1, String v2) {
455455
var v1Length = v1.length();
456+
var v2Length = v2.length();
456457
if (v1Length == 0) {
457-
throw new IllegalStateException("Resource version (1) is empty");
458+
throw new IllegalArgumentException("resource version must not be empty (1)");
458459
}
459-
var v2Length = v2.length();
460460
if (v2Length == 0) {
461-
throw new IllegalStateException("Resource version (2) is empty");
461+
throw new IllegalArgumentException("resource version must not be empty (2)");
462462
}
463-
var maxLength = Math.max(v1Length, v2Length);
464-
boolean v1LeadingZero = true;
465-
boolean v2LeadingZero = true;
466-
int comparison = 0;
467-
for (int i = 0; i < maxLength; i++) {
468-
char char1 = 0;
469-
if (i < v1Length) {
470-
char1 = v1.charAt(i);
471-
if (v1LeadingZero) {
472-
if (char1 == '0') {
473-
throw new IllegalStateException("Resource version (1) cannot begin with 0");
474-
}
475-
v1LeadingZero = false;
476-
}
463+
if (v1Length > v2Length) {
464+
return 1;
465+
}
466+
if (v2Length > v1Length) {
467+
return -1;
468+
}
469+
for (int i = 0; i < v1Length; i++) {
470+
if (v1.charAt(i) > v2.charAt(i)) {
471+
var char1 = v1.charAt(i);
472+
var char2 = v2.charAt(i);
477473
if (!Character.isDigit(char1)) {
478-
throw new IllegalStateException(
474+
throw new IllegalArgumentException(
479475
"Non numeric characters in resource version (1): " + char1);
480476
}
481-
}
482-
if (i < v2Length) {
483-
var char2 = v2.charAt(i);
484-
if (v2LeadingZero) {
485-
if (char2 == '0') {
486-
throw new IllegalStateException("Resource version (2) cannot begin with 0");
487-
}
488-
v2LeadingZero = false;
489-
}
490477
if (!Character.isDigit(char2)) {
491-
throw new IllegalStateException(
478+
throw new IllegalArgumentException(
492479
"Non numeric characters in resource version (2): " + char2);
493480
}
494-
if (char1 == 0) {
495-
comparison = -1;
496-
} else if (comparison == 0) {
497-
comparison = Character.compare(char1, char2);
481+
if (char1 > char2) {
482+
return 1;
483+
}
484+
if (char1 < char2) {
485+
return -1;
498486
}
499-
} else {
500-
comparison = 1;
501487
}
502488
}
503-
return comparison;
489+
return 0;
504490
}
505491
}

operator-framework-core/src/test/java/io/javaoperatorsdk/operator/api/reconciler/PrimaryUpdateAndCacheUtilsTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,14 +191,14 @@ public void compareResourceVersionsTest() {
191191
assertThat(compareResourceVersions("123", "2")).isPositive();
192192
assertThat(compareResourceVersions("3", "211")).isNegative();
193193

194-
assertThrows(IllegalStateException.class, () -> compareResourceVersions("aa", "22"));
195-
assertThrows(IllegalStateException.class, () -> compareResourceVersions("11", "ba"));
194+
assertThrows(IllegalArgumentException.class, () -> compareResourceVersions("aa", "22"));
195+
assertThrows(IllegalArgumentException.class, () -> compareResourceVersions("11", "ba"));
196196
}
197197

198198
// naive performance that compares the works case scenario for non parsing variant
199199
@Test
200200
public void compareResourcePerformanceTest() {
201-
var execNum = 10000000;
201+
var execNum = 20000000;
202202
var startTime = System.currentTimeMillis();
203203
for (int i = 0; i < execNum; i++) {
204204
var res = compareResourceVersions("123456788", "123456789");

0 commit comments

Comments
 (0)