/** attempt to resolve the given value as the given type, waiting on futures, submitting if necessary, * and coercing as allowed by TypeCoercions; * contextMessage (optional) will be displayed in status reports while it waits (e.g. the name of the config key being looked up). * if no execution context supplied (null) this method will throw an exception if the object is an unsubmitted task */ public static <T> T resolveValue(Object v, Class<T> type, @Nullable ExecutionContext exec, String contextMessage) throws ExecutionException, InterruptedException { return new ValueResolver<T>(v, type).context(exec).description(contextMessage).get(); }
/** * Resolves the given object, blocking on futures and coercing it to the given type. If the object is a * map or iterable (or a list of map of maps, etc, etc) then walks these maps/iterables to convert all of * their values to the given type. For example, the following will return a list containing a map with "1"="true": * * {@code Object result = resolveDeepValue(ImmutableList.of(ImmutableMap.of(1, true)), String.class, exec)} * * To perform a deep conversion of futures contained within Iterables or Maps without coercion of each element, * the type should normally be Object, not the type of the collection. This differs from * {@link #resolveValue(Object, Class, ExecutionContext, String)} which will accept Map and Iterable * as the required type. */ public static <T> T resolveDeepValue(Object v, Class<T> type, ExecutionContext exec, String contextMessage) throws ExecutionException, InterruptedException { return new ValueResolver<T>(v, type).context(exec).deep(true).description(contextMessage).get(); }
private static ValueResolverIterator<Boolean> resolveLatchIterator(EntityInternal entity, Object val, ConfigKey<Boolean> key) { return Tasks.resolving(val, Boolean.class) .context(entity.getExecutionContext()) .description("config " + key.getName()) .iterator(); }
<S> ValueResolver<S> cloneReplacingValueAndType(Object newValue, Class<S> superType) { // superType expected to be either type or Object.class if (!superType.isAssignableFrom(type)) { throw new IllegalStateException("superType must be assignable from " + type); } ValueResolver<S> result = new ValueResolver<S>(newValue, superType) .context(exec).description(description) .embedResolutionInTask(embedResolutionInTask) .deep(forceDeep) .timeout(timeout) .immediately(immediately) .recursive(recursive); if (returnDefaultOnGet) { if (!superType.isInstance(defaultValue)) { throw new IllegalStateException("Existing default value " + defaultValue + " not compatible with new type " + superType); } @SuppressWarnings("unchecked") S typedDefaultValue = (S)defaultValue; result.defaultValue(typedDefaultValue); } if (swallowExceptions) result.swallowExceptions(); return result; }
@SuppressWarnings("unchecked") private U resolveImmediately(Object rawVal, Sensor<U> targetSensor) { if (rawVal == Entities.UNCHANGED || rawVal == Entities.REMOVE) { // If it's a special marker-object, then don't try to transform it return (U) rawVal; } // evaluate immediately, or return null. // For vals that implement ImmediateSupplier, we'll use that to get the value // (or Maybe.absent) without blocking. // Otherwise, the Tasks.resolving will give it its best shot at resolving without // blocking on external events (such as waiting for another entity's sensor). return (U) Tasks.resolving(rawVal).as(targetSensor.getType()) .context(entity) .description("Computing sensor "+targetSensor+" from "+rawVal) .immediately(true) .getMaybe().orNull(); } }
@Override public Maybe<?> call() throws Exception { return Tasks.resolving(dsl).as(type) .context(context) .description("Computing "+dsl) .immediately(true) .getMaybe(); } };
static Maybe<?> execDslEventually(BrooklynDslDeferredSupplier<?> dsl, Class<?> type, Entity context, Duration timeout) { return Tasks.resolving(dsl).as(type) .context(context) .description("Computing "+dsl) .timeout(timeout) .getMaybe(); } }
for (Map.Entry<?,?> entry : ((Map<?,?>)v).entrySet()) { Maybe<?> kk = new ValueResolver(entry.getKey(), type, this) .description( (description!=null ? description+", " : "") + "map key "+entry.getKey() ) .getMaybe(); if (kk.isAbsent()) return (Maybe<T>)kk; Maybe<?> vv = new ValueResolver(entry.getValue(), type, this) .description( (description!=null ? description+", " : "") + "map value for key "+kk.get() ) .getMaybe(); if (vv.isAbsent()) return (Maybe<T>)vv; for (Object it : (Set)v) { Maybe<?> vv = new ValueResolver(it, type, this) .description( (description!=null ? description+", " : "") + "entry "+count ) .getMaybe(); if (vv.isAbsent()) return (Maybe<T>)vv; for (Object it : (Iterable)v) { Maybe<?> vv = new ValueResolver(it, type, this) .description( (description!=null ? description+", " : "") + "entry "+count ) .getMaybe(); if (vv.isAbsent()) return (Maybe<T>)vv;