brooklyn-dev mailing list archives

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

    https://github.com/apache/incubator-brooklyn/pull/910#discussion_r40001198
  
    --- 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>> {
    +
    +        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));
    +        }
    +
    +    }
    +
    +    /**
    +     * Resolves the argument as follows:
    +     *
    +     * If the argument is a DeferredSupplier, we will block and wait for it to resolve.
If the argument is TaskAdaptable or TaskFactory,
    +     * we will assume that the resolved task has been queued on the {@code taskArgsIterator},
otherwise the argument has already been resolved.
    +     */
    +    private static String resolveArgument(Object argument, Iterator<?> taskArgsIterator)
{
    +        Object resolvedArgument;
    +        if (argument instanceof TaskAdaptable) {
    +            resolvedArgument = taskArgsIterator.next();
    +        } else if (argument instanceof DeferredSupplier) {
    +            resolvedArgument = ((DeferredSupplier<?>) argument).get();
    +        } else {
    +            resolvedArgument = argument;
    +        }
    +        return String.valueOf(resolvedArgument);
    +    }
    +
    +    public static class RegexReplacer implements Function<String, String> {
    --- End diff --
    
    move to `StringFunctions` ?


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