Repository: jtablesaw/tablesaw
Pull request #787 (2020-04-11 01:47:18)

Added implementation and tests for column's indexOf method.
Should solve #786

Thanks for contributing.

- [x] Tick to sign-off your agreement to the [Developer Certificate of Origin (DCO) 1.1](https://developercertificate.org)

## Description

Added an indexOf method to all column types.
Works the same way as Java's ``List::indexOf(Object)``.

## Testing

A simple test for basic behaviour was added to the ``ColumnTest`` class.

benmccann: There are still a number of formatting changes in this PR that should be reverted. I would expect the Google Code formatter pre-commit hook should do that for you automatically
islaterm: I did that, but it seems that it didn't fix all of the issues.
Ok, I will manually revert the formatting changes to make sure that the code stays like before.

Patch:
diff --git a/core/src/main/java/tech/tablesaw/columns/AbstractColumn.java b/core/src/main/java/tech/tablesaw/columns/AbstractColumn.java
index e9215855..9ea1b762 100644
--- a/core/src/main/java/tech/tablesaw/columns/AbstractColumn.java
+++ b/core/src/main/java/tech/tablesaw/columns/AbstractColumn.java
@@ -17,6 +17,7 @@ package tech.tablesaw.columns;
 import java.util.Comparator;
 import java.util.function.Function;
 import java.util.function.Predicate;
+import java.util.stream.IntStream;
 import tech.tablesaw.api.ColumnType;
 import tech.tablesaw.api.StringColumn;
 import tech.tablesaw.selection.Selection;
@@ -146,4 +147,9 @@ public abstract class AbstractColumn<C extends Column<T>, T> implements Column<T
     }
     return sc;
   }
+
+  @Override
+  public int indexOf(final Object o) {
+    return IntStream.range(0, size()).filter(i -> get(i).equals(o)).findFirst().orElse(-1);
+  }
 }
diff --git a/core/src/main/java/tech/tablesaw/columns/Column.java b/core/src/main/java/tech/tablesaw/columns/Column.java
index 509d7fd0..24f6e1ec 100644
--- a/core/src/main/java/tech/tablesaw/columns/Column.java
+++ b/core/src/main/java/tech/tablesaw/columns/Column.java
@@ -656,4 +656,10 @@ public interface Column<T> extends Iterable<T>, Comparator<T> {
    * @return a {@link StringColumn} built using the column {@link #getUnformattedString} method
    */
   StringColumn asStringColumn();
+
+  /**
+   * Returns the index of the first occurrence of {@code o} in the column or -1 if the element is
+   * not in the column.
+   */
+  int indexOf(Object o);
 }


Test patch:
diff --git a/core/src/test/java/tech/tablesaw/columns/ColumnTest.java b/core/src/test/java/tech/tablesaw/columns/ColumnTest.java
index d00a0756..195fe731 100644
--- a/core/src/test/java/tech/tablesaw/columns/ColumnTest.java
+++ b/core/src/test/java/tech/tablesaw/columns/ColumnTest.java
@@ -331,4 +331,13 @@ public class ColumnTest {
     assertContentEquals(
         DoubleColumn.create("t1", new double[] {1, -1, 0}).sorted(Double::compare), -1.0, 0.0, 1.0);
   }
+
+  @Test
+  public void indexOfTest() {
+    DoubleColumn testDoubleCol = DoubleColumn.create("t1", 0, 1, 1);
+    assertEquals(-1, testDoubleCol.indexOf(2));
+    assertEquals(-1, testDoubleCol.indexOf(0));
+    assertEquals(0, testDoubleCol.indexOf(0.0));
+    assertEquals(1, testDoubleCol.indexOf(1.0));
+  }
 }