Skip to content

Commit a07471d

Browse files
csvirishawkins
andcommitted
Update operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/reconciler/PrimaryUpdateAndCacheUtils.java
Co-authored-by: Steven Hawkins <[email protected]>
1 parent 0e7749d commit a07471d

File tree

1 file changed

+44
-20
lines changed

1 file changed

+44
-20
lines changed

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

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -453,30 +453,54 @@ 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();
457-
if (v1Length > v2Length) {
458-
return 1;
456+
if (v1Length == 0) {
457+
throw new IllegalStateException("Resource version (1) is empty");
459458
}
460-
if (v2Length > v1Length) {
461-
return -1;
459+
var v2Length = v2.length();
460+
if (v2Length == 0) {
461+
throw new IllegalStateException("Resource version (2) is empty");
462462
}
463-
for (int i = 0; i < v1Length; i++) {
464-
var char1 = v1.charAt(i);
465-
var char2 = v2.charAt(i);
466-
if (!Character.isDigit(char1)) {
467-
throw new IllegalStateException("Non numeric characters in resource version (1): " + char1);
468-
}
469-
if (!Character.isDigit(char2)) {
470-
throw new IllegalStateException("Non numeric characters in resource version (2): " + char2);
471-
}
472-
473-
if (char1 > char2) {
474-
return 1;
463+
var maxLength = Math.max(v1Length, v2Length);
464+
boolean v1LeadingZero = true;
465+
boolean v2LeadingZero = true;
466+
int lengthComparison = 0;
467+
int comparison = 0;
468+
for (int i = 0; i < maxLength; i++) {
469+
char char1 = 0;
470+
if (i < v1Length) {
471+
char1 = v1.charAt(i);
472+
if (v1LeadingZero) {
473+
if (char1 == '0') {
474+
throw new IllegalStateException("Resource version (1) cannot begin with 0");
475+
}
476+
v1LeadingZero = false;
477+
}
478+
if (!Character.isDigit(char1)) {
479+
throw new IllegalStateException(
480+
"Non numeric characters in resource version (1): " + char1);
481+
}
475482
}
476-
if (char1 < char2) {
477-
return -1;
483+
if (i < v2Length) {
484+
var char2 = v2.charAt(i);
485+
if (v2LeadingZero) {
486+
if (char2 == '0') {
487+
throw new IllegalStateException("Resource version (1) cannot begin with 0");
488+
}
489+
v2LeadingZero = false;
490+
}
491+
if (!Character.isDigit(char2)) {
492+
throw new IllegalStateException(
493+
"Non numeric characters in resource version (2): " + char2);
494+
}
495+
if (char1 == 0) {
496+
lengthComparison = -1;
497+
} else if (comparison == 0) {
498+
comparison = Character.compare(char1, char2);
499+
}
500+
} else {
501+
lengthComparison = 1;
478502
}
479503
}
480-
return 0;
504+
return lengthComparison != 0 ? lengthComparison : comparison;
481505
}
482506
}

0 commit comments

Comments
 (0)