private static Object getImmediateValue(Object value, @Nullable Entity context, @Nullable Boolean immediately, @Nullable Duration timeout) { return Tasks.resolving(value) .as(Object.class) .defaultValue(UNRESOLVED) .timeout(timeout) .immediately(immediately == null ? false : immediately.booleanValue()) .context(context) .swallowExceptions() .get(); }
<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; }
public void testDefaultBeforeDelayAndError() { ValueResolver<String> result = Tasks.resolving(newThrowTask(Duration.TEN_SECONDS)).as(String.class).context(app).timeout(Duration.ZERO).defaultValue("foo"); assertMaybeIsAbsent(result); Assert.assertEquals(result.get(), "foo"); }
public void testDefaultWhenSwallowError() { ValueResolver<String> result = Tasks.resolving(newThrowTask(Duration.ZERO)).as(String.class).context(app).swallowExceptions().defaultValue("foo"); assertMaybeIsAbsent(result); Assert.assertEquals(result.get(), "foo"); }
.defaultValue("UNRESOLVED") .timeout(Duration.millis(100)) .context(entity)
@Test public void testErrorsResolvingPropagatesOrSwallowedAllCorrectly() throws Exception { app.config().set(TestEntity.CONF_OBJECT, ValueResolverTest.newThrowTask(Duration.ZERO)); Task<Object> t = Tasks.builder().body(Functionals.callable(EntityFunctions.config(TestEntity.CONF_OBJECT), app)).build(); ValueResolver<Object> v = Tasks.resolving(t).as(Object.class).context(app); ValueResolverTest.assertThrowsOnGetMaybe(v); ValueResolverTest.assertThrowsOnGet(v); v.swallowExceptions(); ValueResolverTest.assertMaybeIsAbsent(v); ValueResolverTest.assertThrowsOnGet(v); v.defaultValue("foo"); ValueResolverTest.assertMaybeIsAbsent(v); assertEquals(v.clone().get(), "foo"); assertResolvesValue(v, Object.class, "foo"); }