garydgregory commented on code in PR #1270:
URL: https://github.com/apache/commons-lang/pull/1270#discussion_r1755500193


##########
src/main/java/org/apache/commons/lang3/ArrayUtils.java:
##########
@@ -1432,6 +1432,89 @@ public static <T> T arraycopy(final T source, final int 
sourcePos, final T dest,
         return dest;
     }
 
+    /**
+     * Finds element in sorted array.
+     *
+     * @param array
+     *      array sorted by key field
+     * @param key
+     *      key to search for
+     * @param keyExtractor
+     *      function to extract key from element
+     * @param comparator
+     *      comparator for keys
+     *
+     * @return
+     *      index of the search key, if it is contained in the array within 
specified range; otherwise,
+     *      (-first_greater - 1).  The first_greater is the index of lowest 
greater element in the list - if all elements
+     *      are lower, the first_greater is defined as toIndex.
+     *
+     * @param <T>
+     *     type of array element
+     * @param <K>
+     *     type of key
+     */
+    public static <K, T> int binarySearch(
+            T[] array,
+            K key,
+            Function<T, K> keyExtractor, Comparator<? super K> comparator
+    ) {
+        return binarySearch(array, 0, array.length, key, keyExtractor, 
comparator);
+    }
+
+    /**
+     * Finds element in sorted array, within range fromIndex - toIndex 
(inclusive - exclusive).

Review Comment:
   "FInds" -> "Searches" since the API is called "search".
   For compatibility with `java.util.Arrays.binarySearch(T[], [int, int,] T, 
Comparator<? super T>)`, you'll want to add range checks. From a call site's 
POV, I think the functionality should be the same as 
   - `java.util.Arrays.binarySearch(T[], int, int, T, Comparator<? super T>)` 
and
   - `java.util.Arrays.binarySearch(T[], T, Comparator<? super T>)`
   but with the addition of the key extractor feature.
   
   Like `Arrays`, you might want to refactor into a `binarySearch0`.
   



##########
src/main/java/org/apache/commons/lang3/ArrayUtils.java:
##########
@@ -1432,6 +1432,89 @@ public static <T> T arraycopy(final T source, final int 
sourcePos, final T dest,
         return dest;
     }
 
+    /**
+     * Finds element in sorted array.

Review Comment:
   "FInds" -> "Searches" since the API is called "search".



##########
src/test/java/org/apache/commons/lang3/ArrayUtilsBinarySearchTest.java:
##########
@@ -0,0 +1,82 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.lang3;
+
+import org.junit.jupiter.api.Test;
+
+import java.util.stream.IntStream;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+/**
+ * Unit tests {@link ArrayUtils} binarySearch functions.
+ */
+public class ArrayUtilsBinarySearchTest extends AbstractLangTest {
+
+    @Test
+    public void binarySearch_whenEmpty_returnM1() {
+        Data[] list = createList();
+        int found = ArrayUtils.binarySearch(list, 0, Data::getValue, 
Integer::compare);
+        assertEquals(-1, found);
+    }
+
+    @Test
+    public void binarySearch_whenExists_returnIndex() {
+        Data[] list = createList(0, 1, 2, 4, 7, 9, 12, 15, 17, 19, 25);
+        int found = ArrayUtils.binarySearch(list, 9, Data::getValue, 
Integer::compare);
+        assertEquals(5, found);
+    }
+
+    @Test
+    public void binarySearch_whenNotExists_returnMinusInsertion() {
+        Data[] list = createList(0, 1, 2, 4, 7, 9, 12, 15, 17, 19, 25);
+        int found = ArrayUtils.binarySearch(list, 8, Data::getValue, 
Integer::compare);
+        assertEquals(-6, found);
+    }
+
+    @Test
+    public void binarySearch_whenNotExistsBeginning_returnMinus1() {
+        Data[] list = createList(0, 1, 2, 4, 7, 9, 12, 15, 17, 19, 25);
+        int found = ArrayUtils.binarySearch(list, -3, Data::getValue, 
Integer::compare);
+        assertEquals(-1, found);
+    }
+
+    @Test
+    public void binarySearch_whenNotExistsEnd_returnMinusLength() {
+        Data[] list = createList(0, 1, 2, 4, 7, 9, 12, 15, 17, 19, 25);
+        int found = ArrayUtils.binarySearch(list, 29, Data::getValue, 
Integer::compare);
+        assertEquals(-(list.length + 1), found);
+    }
+
+    private Data[] createList(int... values) {
+        return IntStream.of(values).mapToObj(Data::new)
+                .toArray(Data[]::new);
+    }
+
+    public class Data

Review Comment:
   The test fixture should be static and package-private.



##########
src/test/java/org/apache/commons/lang3/ArrayUtilsBinarySearchTest.java:
##########
@@ -0,0 +1,82 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.lang3;
+
+import org.junit.jupiter.api.Test;
+
+import java.util.stream.IntStream;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+/**
+ * Unit tests {@link ArrayUtils} binarySearch functions.
+ */
+public class ArrayUtilsBinarySearchTest extends AbstractLangTest {
+
+    @Test
+    public void binarySearch_whenEmpty_returnM1() {
+        Data[] list = createList();
+        int found = ArrayUtils.binarySearch(list, 0, Data::getValue, 
Integer::compare);
+        assertEquals(-1, found);
+    }
+
+    @Test
+    public void binarySearch_whenExists_returnIndex() {
+        Data[] list = createList(0, 1, 2, 4, 7, 9, 12, 15, 17, 19, 25);
+        int found = ArrayUtils.binarySearch(list, 9, Data::getValue, 
Integer::compare);
+        assertEquals(5, found);
+    }
+
+    @Test
+    public void binarySearch_whenNotExists_returnMinusInsertion() {
+        Data[] list = createList(0, 1, 2, 4, 7, 9, 12, 15, 17, 19, 25);
+        int found = ArrayUtils.binarySearch(list, 8, Data::getValue, 
Integer::compare);
+        assertEquals(-6, found);
+    }
+
+    @Test
+    public void binarySearch_whenNotExistsBeginning_returnMinus1() {
+        Data[] list = createList(0, 1, 2, 4, 7, 9, 12, 15, 17, 19, 25);
+        int found = ArrayUtils.binarySearch(list, -3, Data::getValue, 
Integer::compare);
+        assertEquals(-1, found);
+    }
+
+    @Test
+    public void binarySearch_whenNotExistsEnd_returnMinusLength() {
+        Data[] list = createList(0, 1, 2, 4, 7, 9, 12, 15, 17, 19, 25);

Review Comment:
   Use `final` where you can.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to