Github user aledsage commented on a diff in the pull request:

    https://github.com/apache/incubator-brooklyn/pull/910#discussion_r41485615
  
    --- Diff: 
core/src/main/java/org/apache/brooklyn/core/sensor/DependentConfiguration.java 
---
    @@ -483,6 +483,114 @@ 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>> {
    --- End diff --
    
    I agree it feels convoluted.
    
    When the function stuff was going to be added, I initially expected it to 
return a normal(ish) function. The pattern/replacement could come from 
attributeWhenReady etc, but the `apply(input)` would expect normal input. For 
example, this could be used in a `Transformer.TRANSFORMATION_FROM_VALUE`.
    
    This use of function seems to be by-passing the use of a transformer in 
order to get the input via another attributeWhenReady, rather than getting it 
from the enricher's subscription.


---
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 [email protected] or file a JIRA ticket
with INFRA.
---

Reply via email to