metron-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nickwallen <...@git.apache.org>
Subject [GitHub] incubator-metron pull request #370: METRON-586 add FILL_LEFT and FILL_RIGHT ...
Date Sat, 03 Dec 2016 15:54:13 GMT
Github user nickwallen commented on a diff in the pull request:

    https://github.com/apache/incubator-metron/pull/370#discussion_r90759419
  
    --- 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;
    +    }
    --- End diff --
    
    If I accidentally pass in a String for the requiredLength, then it will give me back the
origin input, right?  Does that make sense?  Should it blow up instead and report the problem?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

Mime
View raw message