Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## Unreleased

### Fixed

- array functions should use `List`, not `[]`

## [0.7.3] - 2022-03-29

### Fixed

- Implementation mappings in `grel_java_mapping.ttl` used class `fnoi:Mapping` instead of `fno:Mapping` (see [GitLab issue #2](https://gitlab.ilabt.imec.be/fno/lib/grel-functions-java/-/issues/2))

## [0.7.2] - 2022-03-24
Expand Down
6 changes: 6 additions & 0 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,11 @@ So `Integer` instead of `int`, etc.

> The Function Handler for the moment only handles Classes, not primitives.

### Use JAVA Lists, not Arrays

So `List<Object>` instead of `Object[]`, etc.

> The Function Handler for the moment only handles Lists, not Arrays.

[FnO]: https://fno.io/spec/
[GREL]: https://docs.openrefine.org/manual/grelfunctions
42 changes: 21 additions & 21 deletions src/main/java/io/fno/grel/ArrayFunctions.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package io.fno.grel;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;

import java.util.*;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class ArrayFunctions {

Expand Down Expand Up @@ -72,39 +74,37 @@ public static String join(List<String> a, String sep) {
return StringUtils.join(a, sep);
}

// TO-DO these functions are untested and need docstrings
// (brought over from commit 98360fe7f7c13dcbd51c14db12218b605bd86c16)

public static Integer length(Object[] a) {
return a.length;
// TODO these functions need docstrings
public static Integer length(List<Object> a) {
return a.size();
}

public static Object[] slice(Object[] a, Integer from, Integer to) {
return Arrays.copyOfRange(a, from, to + 1);
public static List<Object> slice(List<Object> a, Integer from, Integer to) {
return a.subList(from, to + 1);
}

public static Object[] slice(Object[] a, Integer from) {
return slice(a, from, a.length);
public static List<Object> slice(List<Object> a, Integer from) {
return slice(a, from, length(a));
}

// see get of strings
public static Object[] reverse(Object[] a) {
ArrayUtils.reverse(a);
public static List<Object> reverse(List<Object> a) {
Collections.reverse(a);
return a;
}

public static Object[] sort(Object[] a) {
Arrays.sort(a);
return a;
public static Integer sum(List<Integer> a) {
return a.stream().reduce(0, Integer::sum);
}

public static Integer sum(Integer[] a) {
return Arrays.stream(a).mapToInt(Integer::intValue).sum();
// TODO how to do this sort?
public static List<Object> sort(List<Object> a) {
// a.sort();
return a;
}

public static Object[] uniques(Object[] a) {
SortedSet<Object> set = new TreeSet<>(Arrays.asList(a));
return set.toArray(new Object[0]);
public static List<Object> uniques(List<Object> a) {
return a.stream().distinct().collect(Collectors.toList());

}

Expand Down
36 changes: 18 additions & 18 deletions src/main/java/io/fno/grel/StringFunctions.java
Original file line number Diff line number Diff line change
Expand Up @@ -305,14 +305,14 @@ public static String replaceChars(String s, String f, String r) throws Exception
* @param p regex pattern
* @return Array of pattern matches
*/
public static String[] match(String s, String p) {
public static List<String> match(String s, String p) {
List<String> allMatches = new ArrayList<String>();
Matcher m = Pattern.compile(p)
.matcher(s);
while (m.find()) {
allMatches.add(m.group());
}
return (String[]) allMatches.toArray();
return allMatches;
}

// NOTE: this was implemented in commit 5161c959985daabc90a53520b00752cc9c69b94d,
Expand Down Expand Up @@ -359,14 +359,14 @@ public static List<String> split(String s, String sep) {
* @param numbers lengths of subsequent substrings to be extracted
* @return Array of strings after splitting
*/
public static String[] splitByLengths(String s, int... numbers) {
public static List<String> splitByLengths(String s, int... numbers) {
List<String> output = new ArrayList<>();
int i = 0;
for (int n : numbers) {
output.add(s.substring(i, i + n));
i += n;
}
return output.toArray(new String[0]);
return output;
}

// TODO https://github.com/OpenRefine/OpenRefine/wiki/GREL-String-Functions#splitbylengthsstring-s-number-n1-number-n2-
Expand All @@ -377,7 +377,7 @@ public static String[] splitByLengths(String s, int... numbers) {
* Guesses tab or comma separator if sep is not given.
* Also, value.escape('javascript') is useful for previewing unprintable chars prior to using smartSplit.
*/
public static String[] smartSplit(String s) {
public static List<String> smartSplit(String s) {
String sep;
if (StringUtils.countMatches(s, "\t") < StringUtils.countMatches(s, ",")) {
sep = ",";
Expand All @@ -387,8 +387,8 @@ public static String[] smartSplit(String s) {
return smartSplit(s, sep);
}

public static String[] smartSplit(String s, String sep) {
return s.split(sep);
public static List<String> smartSplit(String s, String sep) {
return Arrays.asList(s.split(sep));
}

/**
Expand All @@ -398,11 +398,11 @@ public static String[] smartSplit(String s, String sep) {
* will result in an array of [ "H", "enry", "CT", "aylor" ]. It is useful for separating letters
* and numbers: "BE1A3E".splitByCharType() will result in [ "BE", "1", "A", "3", "E" ].
*/
public static String[] splitByCharType(String value) {
return StringUtils.splitByCharacterType(value);
public static List<String> splitByCharType(String value) {
return Arrays.asList(StringUtils.splitByCharacterType(value));
}

public static String[] _partition(String s, String frag, Boolean omitFragment, Boolean last) {
public static List<String> _partition(String s, String frag, Boolean omitFragment, Boolean last) {
List<String> output = new ArrayList<>();
int offset = 0;
int index;
Expand All @@ -412,15 +412,15 @@ public static String[] _partition(String s, String frag, Boolean omitFragment, B
index = s.lastIndexOf(frag);
}
if (index == -1) {
return new String[]{s, "", ""};
return Arrays.asList(s, "", "");
}
output.add(s.substring(0, index));
if (! omitFragment) {
output.add(frag);
offset += frag.length();
}
output.add(s.substring(index + offset));
return output.toArray(new String[0]);
return output;
}

/**
Expand All @@ -433,7 +433,7 @@ public static String[] _partition(String s, String frag, Boolean omitFragment, B
* [ "inter", "nation", "alization" ]. If s does not contain fragment, it returns an array of
* [ s, "", "" ] (the original unpartitioned string, and two empty strings).
*/
public static String[] partition(String s, String frag) {
public static List<String> partition(String s, String frag) {
return partition(s, frag, false);
}

Expand All @@ -450,7 +450,7 @@ public static String[] partition(String s, String frag) {
* If the omitFragment boolean is true, for example with "internationalization".partition("nation", true),
* the fragment is not returned. The output is [ "inter", "alization" ].
*/
public static String[] partition(String s, String frag, Boolean omitFragment) {
public static List<String> partition(String s, String frag, Boolean omitFragment) {
return _partition(s, frag, omitFragment, false);
}

Expand All @@ -463,7 +463,7 @@ public static String[] partition(String s, String frag, Boolean omitFragment) {
* For example, "parallel".rpartition("a") returns 3 strings:
* [ "par", "a", "llel" ]. Otherwise works identically to partition().
*/
public static String[] rpartition(String s, String frag) {
public static List<String> rpartition(String s, String frag) {
return rpartition(s, frag, false);
}

Expand All @@ -476,7 +476,7 @@ public static String[] rpartition(String s, String frag) {
* For example, "parallel".rpartition("a") returns 3 strings:
* [ "par", "a", "llel" ]. Otherwise works identically to partition().
*/
public static String[] rpartition(String s, String frag, Boolean omitFragment) {
public static List<String> rpartition(String s, String frag, Boolean omitFragment) {
return _partition(s, frag, omitFragment, true);
}

Expand Down Expand Up @@ -606,11 +606,11 @@ public static String reinterpret(String s, String encoder) {

// https://docs.openrefine.org/manual/grelfunctions#unicodes
// TODO add docstring and write unit test
public static String[] unicode(String s) {
public static List<String> unicode(String s) {
return s.chars()
.mapToObj(c -> (char) c)
.map(c -> encodeURIComponent(String.valueOf(c)))
.toArray(String[]::new);
.toList();
}

// TODO https://github.com/OpenRefine/OpenRefine/wiki/GREL-String-Functions#unicodetypestring-s
Expand Down
15 changes: 13 additions & 2 deletions src/test/java/io/fno/grel/ArrayFunctions_Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@


import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -39,9 +40,19 @@ public void join() {

@Test
public void unique() {
List<String> input = Arrays.asList("1","2","2","3");
assertArrayEquals(
new String[]{"1", "2", "3"},
ArrayFunctions.uniques(new String[]{"1", "2", "2", "3"})
new String[]{"1","2","3"},
ArrayFunctions.uniques(new ArrayList<Object>(input)).toArray()
);
}

@Test
public void sum() {
List<Integer> input = new ArrayList<>();
input.add(2);
input.add(3);
Integer output = ArrayFunctions.sum(input);
assertEquals(Integer.valueOf(5), output);
}
}
Loading