diff options
author | HÃ¥kon Hallingstad <hakon@oath.com> | 2018-05-07 12:23:08 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-05-07 12:23:08 +0200 |
commit | c852de1e8352ff7323f2a4b2dac3cceb62561fb2 (patch) | |
tree | 5da82d20dc27652c6086af7fd30e9304f41d1f76 /vespajlib | |
parent | e7d292dbf1525ae004a40365e2327379b6474e2b (diff) | |
parent | b5181039ad233c4c28820ee9217334bcddb70e3f (diff) |
Merge pull request #5783 from vespa-engine/hakonhall/procedural-editor
Procedural editor
Diffstat (limited to 'vespajlib')
-rw-r--r-- | vespajlib/src/main/java/com/yahoo/collections/Comparables.java | 26 | ||||
-rw-r--r-- | vespajlib/src/test/java/com/yahoo/collections/ComparablesTest.java | 21 |
2 files changed, 47 insertions, 0 deletions
diff --git a/vespajlib/src/main/java/com/yahoo/collections/Comparables.java b/vespajlib/src/main/java/com/yahoo/collections/Comparables.java new file mode 100644 index 00000000000..e638b1fcd75 --- /dev/null +++ b/vespajlib/src/main/java/com/yahoo/collections/Comparables.java @@ -0,0 +1,26 @@ +// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. + +package com.yahoo.collections; + +/** + * Utilities for {@link Comparable} classes. + * + * @author hakon + */ +public class Comparables { + /** + * Returns the least element, or {@code first} if they are equal according to + * {@link Comparable#compareTo(Object) compareTo}. + */ + public static <T extends Comparable<? super T>> T min(T first, T second) { + return first.compareTo(second) <= 0 ? first : second; + } + + /** + * Returns the least element, or {@code second} if they are equal according to + * {@link Comparable#compareTo(Object) compareTo}. + */ + public static <T extends Comparable<? super T>> T max(T first, T second) { + return first.compareTo(second) <= 0 ? second : first; + } +} diff --git a/vespajlib/src/test/java/com/yahoo/collections/ComparablesTest.java b/vespajlib/src/test/java/com/yahoo/collections/ComparablesTest.java new file mode 100644 index 00000000000..0ec9cd2d8f7 --- /dev/null +++ b/vespajlib/src/test/java/com/yahoo/collections/ComparablesTest.java @@ -0,0 +1,21 @@ +// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. + +package com.yahoo.collections; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class ComparablesTest { + @Test + public void testMinAndMax() { + Integer i1 = 1; + Integer i2 = 2; + + assertEquals(i1, Comparables.min(i1, i2)); + assertEquals(i1, Comparables.min(i2, i1)); + + assertEquals(i2, Comparables.max(i1, i2)); + assertEquals(i2, Comparables.max(i2, i1)); + } +}
\ No newline at end of file |