Skip to content

Commit 3075f11

Browse files
committed
refining resource version comparison
Signed-off-by: Steve Hawkins <[email protected]>
1 parent 27f050c commit 3075f11

File tree

1 file changed

+27
-45
lines changed

1 file changed

+27
-45
lines changed

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

Lines changed: 27 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -452,56 +452,38 @@ public static <P extends HasMetadata> P addFinalizerWithSSA(
452452
}
453453

454454
public static int compareResourceVersions(String v1, String v2) {
455-
var v1Length = v1.length();
456-
if (v1Length == 0) {
457-
throw new NonComparableResourceVersionException("Resource version (1) is empty");
458-
}
459-
var v2Length = v2.length();
460-
if (v2Length == 0) {
461-
throw new NonComparableResourceVersionException("Resource version (2) is empty");
455+
int v1Length = validateResourceVersion(v1);
456+
int v2Length = validateResourceVersion(v2);
457+
int comparison = v1Length - v2Length;
458+
if (comparison != 0) {
459+
return comparison;
462460
}
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 NonComparableResourceVersionException(
474-
"Resource version (1) cannot begin with 0");
475-
}
476-
v1LeadingZero = false;
477-
}
478-
if (!Character.isDigit(char1)) {
479-
throw new NonComparableResourceVersionException(
480-
"Non numeric characters in resource version (1): " + char1);
481-
}
461+
for (int i = 0; i < v2Length; i++) {
462+
int comp = v1.charAt(i) - v2.charAt(i);
463+
if (comp != 0) {
464+
return comp;
482465
}
483-
if (i < v2Length) {
484-
var char2 = v2.charAt(i);
485-
if (v2LeadingZero) {
486-
if (char2 == '0') {
487-
throw new NonComparableResourceVersionException(
488-
"Resource version (2) cannot begin with 0");
489-
}
490-
v2LeadingZero = false;
491-
}
492-
if (!Character.isDigit(char2)) {
466+
}
467+
return 0;
468+
}
469+
470+
private static final int validateResourceVersion(String v1) {
471+
int v1Length = v1.length();
472+
if (v1Length == 0) {
473+
throw new NonComparableResourceVersionException("Resource version is empty");
474+
}
475+
for (int i = 0; i < v1Length; i++) {
476+
char char1 = v1.charAt(i);
477+
if (char1 == '0') {
478+
if (i == 0) {
493479
throw new NonComparableResourceVersionException(
494-
"Non numeric characters in resource version (2): " + char2);
495-
}
496-
if (char1 == 0) {
497-
comparison = -1;
498-
} else if (comparison == 0) {
499-
comparison = Character.compare(char1, char2);
480+
"Resource version cannot begin with 0: " + v1);
500481
}
501-
} else {
502-
comparison = 1;
482+
} else if (char1 < '0' || char1 > '9') {
483+
throw new NonComparableResourceVersionException(
484+
"Non numeric characters in resource version: " + v1);
503485
}
504486
}
505-
return comparison;
487+
return v1Length;
506488
}
507489
}

0 commit comments

Comments
 (0)