From c3c49bc444db3b831e6da030d680ba6c246cb676 Mon Sep 17 00:00:00 2001 From: dolfinus Date: Sat, 12 Oct 2019 21:28:53 +0300 Subject: [PATCH] Allow to pass String to isExistsWhere and deleteWhere methods Add isExistsWithIndex as alias for isExists for index(es) input --- src/org/camunda/latera/bss/utils/CSV.groovy | 102 +++++++++++++------- 1 file changed, 67 insertions(+), 35 deletions(-) diff --git a/src/org/camunda/latera/bss/utils/CSV.groovy b/src/org/camunda/latera/bss/utils/CSV.groovy index 012138b1..999f2567 100644 --- a/src/org/camunda/latera/bss/utils/CSV.groovy +++ b/src/org/camunda/latera/bss/utils/CSV.groovy @@ -1059,21 +1059,16 @@ class CSV { } /** - Checks if line with index exist in CSV. Alias -

- Example: -

-    {@code
-    String data =
-    """
-    a;b;c
-    1;2;3
-    """
-    def csv = new CSV(data)
-    assert csv.isExist(0) // by index
-    assert !csv.isExist(1)
-    }
+ Checks if lines with indexes from list exist in CSV. + Alias for #isExists(List) + @see #isExists(List) + */ + Boolean isExistsWithIndex(List indexes) { + return isExists(indexes) + } + /** + Checks if line with index exist in CSV. @param input Integer of line index to search @returns True if there is any line in data which corresponds to condition, false otherwise. @see #isExists(Map) @@ -1082,6 +1077,15 @@ class CSV { return isExists([index]) } + /** + Checks if line with index exist in CSV. + Alias for #isExists(Integer) + @see #isExists(Integer) + */ + Boolean isExistsWithIndex(Integer index) { + return isExists(index) + } + /** Checks if some line or lines exist in CSV.

@@ -1131,29 +1135,30 @@ class CSV { } /** - Delete line or lines correspords to condition. -

- Example: -

-    {@code
-    String data =
-    """
-    a;b;c
-    1;2;3
-    """
-    def csv = new CSV(data)
-    csv.deleteLines(where: [a: 1]) // by column name and value
-    assert csv.dataMap == []
-
-    def csv = new CSV(data)
-    csv.deleteLines(where: [1]) // by line or its part
-    assert csv.dataMap == []
+    Checks if some line or lines exist in CSV.
+    @param input String line or its part
+    @returns True if there is any line in data which corresponds to condition, false otherwise.
+  */
+  Boolean isExistsWhere(CharSequence where) {
+    return isExistsWhere(parseLine(where))
+  }
 
-    def csv = new CSV(data)
-    csv.deleteLines(indexes: [0]) // by index
-    assert csv.dataMap == []
-    }
+ /** + in operator overload. Checks if some line or lines exist in CSV. + @param input Map[String,Object] or List[Object] query format Integer line intex to search. + @returns True if there is any line with this value of column, false otherwise. + @see #isExists(Map) + @see #isExistsWhere(Map) + */ + Boolean isCase(def item) { + if (isMap(item) || isString(item)) { + return isExistsWhere(item) + } + return isExistsWithIndex(item) + } + /** + Delete line or lines correspords to condition. @param input Map with 'where' query in Map[String,Object] or List[Object] format or 'indexes' in List[Integer] format to search. @returns List[List] of data. */ @@ -1299,6 +1304,33 @@ class CSV { return deleteLines(where: where) } + /** + Delete line by full content or lines by some part. + + @param input Map[String,Object] to search and delete. + @returns List[List] of data. + */ + List deleteLinesWhere(CharSequence where) { + return deleteLinesWhere(parseLine(where)) + } + + /** + - operator overload, Delete line or lines correspords to condition. + @param input Integer, List[Integer] or Map to search. + @see #deleteLines + */ + CSV minus(def item) { + if (isInteger(item)) { + deleteLinesByIndex(item) + } else if (isList(item)) { + deleteLinesByIndex(item) + } else if (isMap(item) || isString(item)) { + deleteLinesWhere(item) + } + deleteLinesByIndex([item]) + return this + } + /** Get String CSV interpretation.