diff options
author | Jon Bratseth <bratseth@gmail.com> | 2023-01-22 16:49:25 +0100 |
---|---|---|
committer | Jon Bratseth <bratseth@gmail.com> | 2023-01-22 16:49:25 +0100 |
commit | b349bcc5c27cf1c70a4838e279483328fc6c4a59 (patch) | |
tree | 3f15d00efdbe60792a5d22f258f9010a15919e5c /config-provisioning | |
parent | 45577c7d7d2c70637d63f89702c3229df312edc3 (diff) |
Move IntRange out of vespajlib
vespajlib classes in the interface between config model and config server
causes LinkageErrors.
Diffstat (limited to 'config-provisioning')
4 files changed, 170 insertions, 3 deletions
diff --git a/config-provisioning/src/main/java/com/yahoo/config/provision/Capacity.java b/config-provisioning/src/main/java/com/yahoo/config/provision/Capacity.java index 993ab686675..2477d19d46c 100644 --- a/config-provisioning/src/main/java/com/yahoo/config/provision/Capacity.java +++ b/config-provisioning/src/main/java/com/yahoo/config/provision/Capacity.java @@ -1,8 +1,6 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.config.provision; -import com.yahoo.collections.IntRange; - import java.util.Objects; import java.util.Optional; import java.util.stream.Stream; diff --git a/config-provisioning/src/main/java/com/yahoo/config/provision/IntRange.java b/config-provisioning/src/main/java/com/yahoo/config/provision/IntRange.java new file mode 100644 index 00000000000..4a2a472a1ad --- /dev/null +++ b/config-provisioning/src/main/java/com/yahoo/config/provision/IntRange.java @@ -0,0 +1,132 @@ +package com.yahoo.config.provision; + +import java.util.Objects; +import java.util.OptionalInt; + +/** + * An integer range. + * + * @author bratseth + */ +public class IntRange { + + private static final IntRange empty = new IntRange(OptionalInt.empty(), OptionalInt.empty()); + + private final OptionalInt from, to; + + public IntRange(OptionalInt from, OptionalInt to) { + if (from.isPresent() && to.isPresent() && from.getAsInt() > to.getAsInt()) + throw new IllegalArgumentException("from " + from.getAsInt() + " is greater than to " + to.getAsInt()); + this.from = from; + this.to = to; + } + + /** Returns the minimum value which is in this range, or empty if it is open downwards. */ + public OptionalInt from() { return from; } + + /** Returns the maximum value which is in this range, or empty if it is open upwards. */ + public OptionalInt to() { return to; } + + public boolean isEmpty() { + return from.isEmpty() && to.isEmpty(); + } + + /** Returns whether the given value is in this range. */ + public boolean includes(int value) { + if (from.isPresent() && value < from.getAsInt()) return false; + if (to.isPresent() && value > to.getAsInt()) return false; + return true; + } + + /** Returns the given value adjusted minimally to fit within this range. */ + public int fit(int value) { + if (from.isPresent() && value < from.getAsInt()) return from.getAsInt(); + if (to.isPresent() && value > to.getAsInt()) return to.getAsInt(); + return value; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if ( ! (o instanceof IntRange other)) return false; + if ( ! this.from.equals(other.from)) return false; + if ( ! this.to.equals(other.to)) return false; + return true; + } + + @Override + public int hashCode() { + return Objects.hash(from, to); + } + + @Override + public String toString() { + if (isEmpty()) return "[]"; + if (from.equals(to)) return String.valueOf(from.getAsInt()); + return "[" + (from.isPresent() ? from.getAsInt() : "") + ", " + (to.isPresent() ? to.getAsInt() : "") + "]"; + } + + public static IntRange empty() { return empty; } + + public static IntRange from(int from) { + return new IntRange(OptionalInt.of(from), OptionalInt.empty()); + } + + public static IntRange to(int to) { + return new IntRange(OptionalInt.empty(), OptionalInt.of(to)); + } + + public static IntRange of(int fromTo) { + return new IntRange(OptionalInt.of(fromTo), OptionalInt.of(fromTo)); + } + + public static IntRange of(int from, int to) { + return new IntRange(OptionalInt.of(from), OptionalInt.of(to)); + } + + /** Returns this with a 'from' limit which is at most the given value */ + public IntRange fromAtMost(int minLimit) { + if (from.isEmpty()) return this; + if (from.getAsInt() <= minLimit) return this; + return new IntRange(OptionalInt.of(minLimit), to); + } + + /** Returns this with a 'to' limit which is at least the given value */ + public IntRange toAtLeast(int maxLimit) { + if (to.isEmpty()) return this; + if (to.getAsInt() >= maxLimit) return this; + return new IntRange(from, OptionalInt.of(maxLimit)); + } + + /** Parses a value ("value"), value range ("[min-value?, max-value?]"), or empty. */ + public static IntRange from(String s) { + try { + s = s.trim(); + if (s.startsWith("[") && s.endsWith("]")) { + String innards = s.substring(1, s.length() - 1).trim(); + if (innards.isEmpty()) return empty(); + String[] numbers = (" " + innards + " ").split(","); // pad to make sure we get 2 elements + if (numbers.length != 2) throw new IllegalArgumentException("Expected two numbers"); + return new IntRange(parseOptionalInt(numbers[0]), parseOptionalInt(numbers[1])); + } else { + var fromTo = parseOptionalInt(s); + return new IntRange(fromTo, fromTo); + } + } + catch (IllegalArgumentException e) { + throw new IllegalArgumentException("Expected a number or range on the form [min, max], but got '" + s + "'", e); + } + } + + private static OptionalInt parseOptionalInt(String s) { + try { + s = s.trim(); + if (s.isEmpty()) return OptionalInt.empty(); + return OptionalInt.of(Integer.parseInt(s)); + } + catch (IllegalArgumentException e) { + throw new IllegalArgumentException("'" + s + "' is not an integer"); + } + } + +} diff --git a/config-provisioning/src/test/java/com/yahoo/config/provision/CapacityTest.java b/config-provisioning/src/test/java/com/yahoo/config/provision/CapacityTest.java index ca552003f7a..89c0e98b076 100644 --- a/config-provisioning/src/test/java/com/yahoo/config/provision/CapacityTest.java +++ b/config-provisioning/src/test/java/com/yahoo/config/provision/CapacityTest.java @@ -1,7 +1,6 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.config.provision; -import com.yahoo.collections.IntRange; import org.junit.jupiter.api.Test; import java.util.Optional; diff --git a/config-provisioning/src/test/java/com/yahoo/config/provision/IntRangeTestCase.java b/config-provisioning/src/test/java/com/yahoo/config/provision/IntRangeTestCase.java new file mode 100644 index 00000000000..bdec9fe33a5 --- /dev/null +++ b/config-provisioning/src/test/java/com/yahoo/config/provision/IntRangeTestCase.java @@ -0,0 +1,38 @@ +package com.yahoo.config.provision; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * @author bratseth + */ +public class IntRangeTestCase { + + @Test + public void testStringAndEquals() { + assertEquals(IntRange.empty(), IntRange.from(IntRange.from("[]").toString())); + assertEquals(IntRange.from(1), IntRange.from(IntRange.from("[1,]").toString())); + assertEquals(IntRange.to(3), IntRange.from(IntRange.from("[,3]").toString())); + assertEquals(IntRange.of(1, 3), IntRange.from(IntRange.from("[1,3]").toString())); + assertEquals(IntRange.of(1, 3), IntRange.from(IntRange.from("[1, 3]").toString())); + } + + @Test + public void testInclusion() { + assertFalse(IntRange.of(3, 5).includes(2)); + assertTrue(IntRange.of(3, 5).includes(3)); + assertTrue(IntRange.of(3, 5).includes(4)); + assertTrue(IntRange.of(3, 5).includes(5)); + assertFalse(IntRange.of(3, 5).includes(6)); + + assertTrue(IntRange.from(3).includes(1000)); + assertFalse(IntRange.from(3).includes(2)); + + assertTrue(IntRange.to(5).includes(-1000)); + assertFalse(IntRange.to(3).includes(4)); + } + +} |