summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHåkon Hallingstad <hakon@oath.com>2018-05-06 16:30:37 +0200
committerHåkon Hallingstad <hakon@oath.com>2018-05-06 16:30:37 +0200
commit393581f67c338c426beafd464c2a9e6c8b01546c (patch)
treebb259ae949a1f752e68eb910c66c386a501c0588
parent3caf6f87d473c6ceebf168c9cfe035ae613ad027 (diff)
Move Comparables to vespajlib
-rw-r--r--node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/editor/Comparables.java24
-rw-r--r--node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/editor/CursorImpl.java4
-rw-r--r--vespajlib/src/main/java/com/yahoo/collections/Comparables.java26
-rw-r--r--vespajlib/src/test/java/com/yahoo/collections/ComparablesTest.java21
4 files changed, 49 insertions, 26 deletions
diff --git a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/editor/Comparables.java b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/editor/Comparables.java
deleted file mode 100644
index b6fea7ad972..00000000000
--- a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/editor/Comparables.java
+++ /dev/null
@@ -1,24 +0,0 @@
-// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
-
-package com.yahoo.vespa.hosted.node.admin.task.util.editor;
-
-/**
- * @author hakon
- */
-public class Comparables {
- public static <T extends Comparable<T>> T min(T first, T second) {
- if (first.compareTo(second) < 0) {
- return first;
- } else {
- return second;
- }
- }
-
- public static <T extends Comparable<T>> T max(T first, T second) {
- if (first.compareTo(second) < 0) {
- return second;
- } else {
- return first;
- }
- }
-}
diff --git a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/editor/CursorImpl.java b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/editor/CursorImpl.java
index c5f708d908c..47c710395f5 100644
--- a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/editor/CursorImpl.java
+++ b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/editor/CursorImpl.java
@@ -8,8 +8,8 @@ import java.util.function.Consumer;
import java.util.function.Function;
import java.util.regex.Pattern;
-import static com.yahoo.vespa.hosted.node.admin.task.util.editor.Comparables.max;
-import static com.yahoo.vespa.hosted.node.admin.task.util.editor.Comparables.min;
+import static com.yahoo.collections.Comparables.max;
+import static com.yahoo.collections.Comparables.min;
/**
* @author hakon
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