brooklyn-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aledsage <...@git.apache.org>
Subject [GitHub] incubator-brooklyn pull request: Regex replacement dsl
Date Mon, 21 Sep 2015 11:49:13 GMT
Github user aledsage commented on a diff in the pull request:

    https://github.com/apache/incubator-brooklyn/pull/910#discussion_r39962111
  
    --- Diff: core/src/main/java/org/apache/brooklyn/core/sensor/DependentConfiguration.java
---
    @@ -483,6 +483,108 @@ public T apply(@Nullable U input) {
                 taskArgs);
         }
     
    +    public static Task<String> regexReplacement(Object source, Object pattern,
Object replacement) {
    +        List<TaskAdaptable<Object>> taskArgs = getTaskAdaptable(source, pattern,
replacement);
    +        Function<List<Object>, String> transformer = new RegexTransformerString(source,
pattern, replacement);
    +        return transformMultiple(
    +                MutableMap.of("displayName", String.format("creating regex replacement
function (%s:%s)", pattern, replacement)),
    +                transformer,
    +                taskArgs
    +        );
    +    }
    +
    +    public static Task<Function<String, String>> regexReplacement(Object
pattern, Object replacement) {
    +        List<TaskAdaptable<Object>> taskArgs = getTaskAdaptable(pattern,
replacement);
    +        Function<List<Object>, Function<String, String>> transformer
= new RegexTransformerFunction(pattern, replacement);
    +        return transformMultiple(
    +                MutableMap.of("displayName", String.format("creating regex replacement
function (%s:%s)", pattern, replacement)),
    +                transformer,
    +                taskArgs
    +        );
    +    }
    +
    +    private static List<TaskAdaptable<Object>> getTaskAdaptable(Object...
args){
    +        List<TaskAdaptable<Object>> taskArgs = Lists.newArrayList();
    +        for (Object arg: args) {
    +            if (arg instanceof TaskAdaptable) {
    +                taskArgs.add((TaskAdaptable<Object>)arg);
    +            } else if (arg instanceof TaskFactory) {
    +                taskArgs.add(((TaskFactory<TaskAdaptable<Object>>)arg).newTask());
    +            }
    +        }
    +        return taskArgs;
    +    }
    +
    +    public static class RegexTransformerString implements Function<List<Object>,
String> {
    +
    +        private final Object source;
    +        private final Object pattern;
    +        private final Object replacement;
    +
    +        public RegexTransformerString(Object source, Object pattern, Object replacement){
    +            this.source = source;
    +            this.pattern = pattern;
    +            this.replacement = replacement;
    +        }
    +
    +        @Nullable
    +        @Override
    +        public String apply(@Nullable List<Object> input) {
    +            Iterator<?> taskArgsIterator = input.iterator();
    +            String resolvedSource = resolveArgument(source, taskArgsIterator);
    +            String resolvedPattern = resolveArgument(pattern, taskArgsIterator);
    +            String resolvedReplacement = resolveArgument(replacement, taskArgsIterator);
    +            return new RegexReplacer(resolvedPattern, resolvedReplacement).apply(resolvedSource);
    +        }
    +    }
    +
    +    public static class RegexTransformerFunction implements Function<List<Object>,
Function<String, String>> {
    +
    +        private final Object pattern;
    +        private final Object replacement;
    +
    +        public RegexTransformerFunction(Object pattern, Object replacement){
    +            this.pattern = pattern;
    +            this.replacement = replacement;
    +        }
    +
    +        @Override
    +        public Function<String, String> apply(List<Object> input) {
    +            Iterator<?> taskArgsIterator = input.iterator();
    +            return new RegexReplacer(resolveArgument(pattern, taskArgsIterator), resolveArgument(replacement,
taskArgsIterator));
    +        }
    +
    +    }
    +
    +    private static String resolveArgument(Object argument, Iterator<?> taskArgsIterator)
{
    --- End diff --
    
    Worth having some javadoc (even though the method is small, it's behaviour is non-obvious).
It wasn't clear until looking carefully why would increment the iterator sometimes (i.e. call
`next()`) and not other times.


---
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