diff options
author | Håkon Hallingstad <hakon@oath.com> | 2018-05-06 16:30:37 +0200 |
---|---|---|
committer | Håkon Hallingstad <hakon@oath.com> | 2018-05-06 16:30:37 +0200 |
commit | 393581f67c338c426beafd464c2a9e6c8b01546c (patch) | |
tree | bb259ae949a1f752e68eb910c66c386a501c0588 /vespajlib | |
parent | 3caf6f87d473c6ceebf168c9cfe035ae613ad027 (diff) |
Move Comparables to vespajlib
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 |