[
https://issues.apache.org/jira/browse/METRON-586?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15719265#comment-15719265
]
ASF GitHub Bot commented on METRON-586:
---------------------------------------
Github user ottobackwards commented on a diff in the pull request:
https://github.com/apache/incubator-metron/pull/370#discussion_r90770215
--- Diff:
metron-platform/metron-common/src/main/java/org/apache/metron/common/dsl/functions/StringFunctions.java
---
@@ -222,4 +222,71 @@ public Object apply(List<Object> args) {
return null;
}
}
+
+ private enum FillDirection{
+ LEFT,
+ RIGHT
+ }
+
+ @Stellar(name="FILL_LEFT"
+ , description="Fills or pads a given string with a given
character, to a given length on the left"
+ , params = { "input - string", "fill - the fill character", "len
- the required length"}
+ , returns = "Filled String"
+ )
+ public static class FillLeft extends BaseStellarFunction {
+ @Override
+ public Object apply(List<Object> args) {
+ if(args.size() < 3) {
+ throw new IllegalStateException("FILL_LEFT expects three args:
[string,char,length] where char is the fill character string and length is the
required length of the result");
+ }
+ return fill(FillDirection.LEFT,args.get(0),args.get(1),args.get(2));
+ }
+ }
+
+ @Stellar(name="FILL_RIGHT"
+ , description="Fills or pads a given string with a given
character, to a given length on the right"
+ , params = { "input - string", "fill - the fill character", "len
- the required length"}
+ , returns = "Filled String"
+ )
+ public static class FillRight extends BaseStellarFunction {
+ @Override
+ public Object apply(List<Object> args) {
+ if(args.size() < 3) {
+ throw new IllegalStateException("FILL_RIGHT expects three args:
[string,char,length] where char is the fill character string and length is the
required length of the result");
+ }
+ return fill(FillDirection.RIGHT,args.get(0),args.get(1),args.get(2));
+ }
+ }
+
+ private static Object fill(FillDirection direction, Object inputObject,
Object fillObject, Object requiredLengthObject){
+ if(inputObject == null) {
+ return null;
+ }
+ String input = inputObject.toString();
+
+ if(requiredLengthObject == null || fillObject == null) {
+ return input;
+ }
+
+ String fill = fillObject.toString().substring(0,1);
+ int requiredLength = 0;
+ if(requiredLengthObject instanceof Integer){
+ requiredLength = (int)requiredLengthObject;
+ }else{
+ return input;
+ }
+ int actualLength = input.length();
+ if(actualLength >= requiredLength){
+ return input;
+ }
+ int howMany = requiredLength - actualLength;
+ String fillString = fill;
+ for(int i = 0; i < (howMany - 1); i++){
+ fillString += fill;
+ }
+ if(direction == FillDirection.LEFT) {
+ return fillString += input;
+ }
+ return input += fillString;
--- End diff --
That sounds good. I don't want to lose my @apache.org but I have not used
commons enough to have it be the first thing I think of.
> STELLAR should have FILL_LEFT and FILL_RIGHT functions
> ------------------------------------------------------
>
> Key: METRON-586
> URL: https://issues.apache.org/jira/browse/METRON-586
> Project: Metron
> Issue Type: Improvement
> Affects Versions: 0.3.0
> Reporter: Otto Fowler
>
> Stellar should support functions for filling string fields, left or right
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)