brooklyn-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From alasdairhodge <...@git.apache.org>
Subject [GitHub] brooklyn-server pull request #301: Test+fix DeferredSuppliers in $brooklyn:o...
Date Mon, 15 Aug 2016 12:31:28 GMT
Github user alasdairhodge commented on a diff in the pull request:

    https://github.com/apache/brooklyn-server/pull/301#discussion_r74754498
  
    --- Diff: camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/methods/BrooklynDslCommon.java
---
    @@ -428,28 +430,44 @@ public DslObject(
                 return DependentConfiguration.transformMultiple(flags, new Function<List<Object>,
Object>() {
                             @Override
                             public Object apply(List<Object> input) {
    -                            Iterator<Object> values = input.iterator();
    +                            Map<String, Object> resolvedFields = Maps.newLinkedHashMap();
    +                            Map<String, Object> resolvedConfig = Maps.newLinkedHashMap();
    +                            List<Object> resolvedConstructorArgs = Lists.newArrayList();
    +                            List<Object> resolvedFactoryMethodArgs = Lists.newArrayList();
    +                            
    +                            Iterator<Object> taskValues = input.iterator();
                                 for (String name : fields.keySet()) {
                                     Object value = fields.get(name);
    -                                if (value instanceof TaskAdaptable || value instanceof
TaskFactory) {
    -                                    fields.put(name, values.next());
    -                                } else if (value instanceof DeferredSupplier) {
    -                                    fields.put(name, ((DeferredSupplier<?>) value).get());
    -                                }
    +                                Object resolvedValue = requiresTask(value) ? taskValues.next()
: resolveValue(value);
    +                                resolvedFields.put(name, resolvedValue);
                                 }
                                 for (String name : config.keySet()) {
                                     Object value = config.get(name);
    -                                if (value instanceof TaskAdaptable || value instanceof
TaskFactory) {
    -                                    config.put(name, values.next());
    -                                } else if (value instanceof DeferredSupplier) {
    -                                    config.put(name, ((DeferredSupplier<?>) value).get());
    -                                }
    +                                Object resolvedValue = requiresTask(value) ? taskValues.next()
: resolveValue(value);
    +                                resolvedConfig.put(name, resolvedValue);
    +                            }
    +                            for (Object value : constructorArgs) {
    +                                Object resolvedValue = requiresTask(value) ? taskValues.next()
: resolveValue(value);
    +                                resolvedConstructorArgs.add(resolvedValue);
    +                            }
    +                            for (Object value : factoryMethodArgs) {
    +                                Object resolvedValue = requiresTask(value) ? taskValues.next()
: resolveValue(value);
    +                                resolvedFactoryMethodArgs.add(resolvedValue);
                                 }
                                 if (factoryMethodName == null) {
    -                                return create(clazz, constructorArgs, fields, config);
    +                                return create(clazz, resolvedConstructorArgs, resolvedFields,
resolvedConfig);
                                 } else {
    -                                return create(clazz, factoryMethodName, factoryMethodArgs,
fields, config);
    +                                return create(clazz, factoryMethodName, resolvedFactoryMethodArgs,
resolvedFields, resolvedConfig);
    +                            }
    +                        }
    +                        protected boolean requiresTask(Object value) {
    +                            return (value instanceof TaskAdaptable || value instanceof
TaskFactory);
    +                        }
    +                        protected Object resolveValue(Object value) {
    +                            if (value instanceof DeferredSupplier) {
    +                                return ((DeferredSupplier<?>) value).get();
                                 }
    +                            return value;
                             }
    --- End diff --
    
    Good catch. Lots of repetition here, however. Maybe:
    ```
    final Iterator<Object> taskValues = input.iterator();
    Function<Object, Object> resolver = new Function<Object, Object>() {
        @Override public Object apply(Object value) {
            Object resolvedValue = requiresTask(value) ? taskValues.next() : resolveValue(value);
            return resolvedValue;
        }
    };
    Map<String, Object> resolvedConfig = Maps.transformValues(config, resolver);
    Map<String, Object> resolvedFields = Maps.transformValues(fields, resolver);
    List<Object> resolvedConstructorArgs = Lists.transform(constructorArgs, resolver);
    List<Object> resolvedFactoryMethodArgs = Lists.transform(factoryMethodArgs, resolver);
    ```


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