[
https://issues.apache.org/jira/browse/METRON-830?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15960047#comment-15960047
]
ASF GitHub Bot commented on METRON-830:
---------------------------------------
Github user mattf-horton commented on a diff in the pull request:
https://github.com/apache/incubator-metron/pull/516#discussion_r110292164
--- Diff:
metron-platform/metron-common/src/main/java/org/apache/metron/common/dsl/functions/StringFunctions.java
---
@@ -343,4 +343,89 @@ public Object apply(List<Object> args) {
return String.format(format, formatArgs);
}
}
+
+ @Stellar( name="CHOP"
+ , description = "Remove the last character from a String"
+ , params = { "the String to chop last character from, may be
null"}
+ , returns = "String without last character, null if null String
input"
+ )
+ public static class chop extends BaseStellarFunction {
+
+ @Override
+ public Object apply(List<Object> strings) {
+
+ if(strings.size() == 0) {
+ throw new IllegalArgumentException("[CHOP] missing argument:
string to be chopped");
+ }
+
+ String chop = StringUtils.chop((String) strings.get(0));
+ return chop;
+ }
+ }
+
+ @Stellar( name = "PREPENDIFMISSING"
+ , description = "Prepends the prefix to the start of the string
if the string does not already start with any of the prefixes"
+ , params = { "str - The string.", "prefix - The prefix to
prepend to the start of the string", "prefixes - Additional prefixes that are
valid" }
+ , returns = "A new String if prefix was prepended, the same
string otherwise."
+ )
+ public static class prependifmissing extends BaseStellarFunction {
+
+ @Override
+ public Object apply(List<Object> strings) {
+
+ String prefixed;
+ switch (strings.size()) {
+ case 2: prefixed =
org.apache.commons.lang3.StringUtils.prependIfMissing((String) strings.get(0),
(String) strings.get(1));
+ break;
+ case 3: prefixed =
org.apache.commons.lang3.StringUtils.prependIfMissing((String) strings.get(0),
(String) strings.get(1), (String) strings.get(2));
+ break;
+ default: throw new IllegalArgumentException("[PREPENDIFMISSING]
incorrect arguments. Usage: PREPENDIFMISSING <String> <prefix> [<prefix>...]");
+ }
+ return prefixed;
+ }
+ }
+
+ @Stellar( name = "APPENDIFMISSING"
+ , description = "Appends the suffix to the end of the string if
the string does not already end with any of the suffixes"
+ , params = { "str - The string.", "suffix - The suffix to append
to the end of the string", "suffixes - Additional suffixes that are valid
terminators" }
+ , returns = "A new String if suffix was appended, the same
string otherwise."
+ )
+ public static class appendifmissing extends BaseStellarFunction {
+
+ @Override
+ public Object apply(List<Object> strings) {
+
+ String suffixed;
+ switch (strings.size()) {
+ case 2:
+ suffixed =
org.apache.commons.lang3.StringUtils.appendIfMissing((String) strings.get(0),
(String) strings.get(1));
+ break;
+ case 3:
+ suffixed =
org.apache.commons.lang3.StringUtils.appendIfMissing((String) strings.get(0),
(String) strings.get(1), (String) strings.get(2));
+ break;
+ default:
+ throw new IllegalArgumentException("[APPENDIFMISSING] incorrect
arguments. Usage: APPENDIFMISSING <String> <prefix> [<prefix>...]");
+ }
+ return suffixed;
+ }
+ }
+
+ @Stellar( name = "COUNTMATCHES"
+ , description = "Counts how many times the substring appears in
the larger string"
+ , params = { "str - the CharSequence to check, may be null",
"sub - the substring to count, may be null"}
+ , returns = "the number of occurrences, 0 if either CharSequence
is null"
--- End diff --
"the number of non-overlapping occurrences..."
> Adding StringFunctions to Stellar - chop, prependifmissing, appendifmissing
> and countmatches
> --------------------------------------------------------------------------------------------
>
> Key: METRON-830
> URL: https://issues.apache.org/jira/browse/METRON-830
> Project: Metron
> Issue Type: Bug
> Affects Versions: 0.3.1
> Reporter: Anand Subramanian
> Assignee: Anand Subramanian
> Fix For: Next + 1
>
>
> Enhance Stellar by adding the following apache common string functions.
> - chop -> Remove the last character from a String.
> - prependifmissing -> Prepends the prefix to the start of the string if the
> string does not already start with any of the prefixes.
> - appendifmissing -> Appends the suffix to the end of the string if the
> string does not already end with any of the suffixes.
> - countmatches -> Counts how many times the substring appears in the larger
> string.
--
This message was sent by Atlassian JIRA
(v6.3.15#6346)