You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@knox.apache.org by lm...@apache.org on 2022/09/02 21:25:27 UTC

[knox] branch master updated: KNOX-2798 - Add a trim method to KnoxShellTable to trim all values in a Column (#628)

This is an automated email from the ASF dual-hosted git repository.

lmccay pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/knox.git


The following commit(s) were added to refs/heads/master by this push:
     new 428804e51 KNOX-2798 - Add a trim method to KnoxShellTable to trim all values in a Column (#628)
428804e51 is described below

commit 428804e518d3b0078b32dd34df39d43a98602b7e
Author: lmccay <lm...@apache.org>
AuthorDate: Fri Sep 2 17:25:22 2022 -0400

    KNOX-2798 - Add a trim method to KnoxShellTable to trim all values in a Column (#628)
---
 .../knox/gateway/shell/table/KnoxShellTable.java   | 26 ++++++++++++++++++++++
 .../gateway/shell/table/KnoxShellTableTest.java    | 18 +++++++++++++++
 2 files changed, 44 insertions(+)

diff --git a/gateway-shell/src/main/java/org/apache/knox/gateway/shell/table/KnoxShellTable.java b/gateway-shell/src/main/java/org/apache/knox/gateway/shell/table/KnoxShellTable.java
index 06acd164d..179fbd132 100644
--- a/gateway-shell/src/main/java/org/apache/knox/gateway/shell/table/KnoxShellTable.java
+++ b/gateway-shell/src/main/java/org/apache/knox/gateway/shell/table/KnoxShellTable.java
@@ -289,6 +289,32 @@ public class KnoxShellTable {
     return this;
   }
 
+  /**
+   * Trims the String value of whitespace for each of the values in a column
+   * given the column name.
+   * @param colIndex
+   * @return table
+   */
+  public KnoxShellTable trim(String colName) {
+    int colIndex = headers.indexOf(colName);
+    return trim(colIndex);
+  }
+
+  /**
+   * Trims the String value of whitespace for each of the values in a column
+   * given the column index.
+   * @param colIndex
+   * @return table
+   */
+  public KnoxShellTable trim(int colIndex) {
+    List<Comparable<? extends Object>> col = values(colIndex);
+    for (int i = 0; i < col.size(); i++) {
+      String v = (String) col.get(i);
+      rows.get(i).set(colIndex, v.trim());
+    }
+    return this;
+  }
+
   public List<String> getHeaders() {
     return headers == null || headers.isEmpty() ? null : headers;
   }
diff --git a/gateway-shell/src/test/java/org/apache/knox/gateway/shell/table/KnoxShellTableTest.java b/gateway-shell/src/test/java/org/apache/knox/gateway/shell/table/KnoxShellTableTest.java
index b46bc9f1f..0d32f86f5 100644
--- a/gateway-shell/src/test/java/org/apache/knox/gateway/shell/table/KnoxShellTableTest.java
+++ b/gateway-shell/src/test/java/org/apache/knox/gateway/shell/table/KnoxShellTableTest.java
@@ -295,6 +295,24 @@ public class KnoxShellTableTest {
     assertEquals(table2.getRows().get(1).get(1), "012");
   }
 
+  @Test
+  public void testTrim() throws IOException {
+    KnoxShellTable table = new KnoxShellTable();
+
+    table.header("Column A").header("Column B").header("Column C");
+
+    table.row().value(" 789").value("012").value("844444444");
+    table.row().value(" 123").value("456").value("344444444");
+
+    KnoxShellTable table2 = table.trim("Column A");
+    assertEquals(table2.getRows().get(0).get(0), "789");
+    assertEquals(table2.getRows().get(1).get(0), "123");
+
+    KnoxShellTable table3 = table.trim(0);
+    assertEquals(table3.getRows().get(0).get(0), "789");
+    assertEquals(table3.getRows().get(1).get(0), "123");
+  }
+
   @Test
   public void testSortStringValuesNumerically() throws IOException {
     KnoxShellTable table = new KnoxShellTable();