/** * Obtains a {@link Deferred} representation of the last call to a * dynamic proxy created with either {@link #invoking(Object)} or * {@link #invoking(Deferred)}. If there was no call to create a * dynamic proxy, the provided deferred is ensured and returned. * * @param t the deferred value (usually returned from 'invoking') * * @return a {@link Deferred} representation of a previous call * {@link #invoking(Object)} */ @SuppressWarnings("unchecked") public static <T> Deferred<T> eventually(Deferred<T> t) { // get the last deferred value from invoking Deferred<T> deferred = (Deferred<T>) m_deferred.get(); if (deferred == null) { deferred = t; } else { // clear the last invoking call m_deferred.set(null); } return ensured(deferred); }
/** * Asserts that a value will eventually satisfy the specified {@link Matcher}. * <p> * Should the value be the result of a call to {@link DeferredHelper#invoking(Deferred)} * the result is unwrapped into a {@link Deferred} that is then used for the assert. * * @param <T> the type of value produced by the {@link Deferred} * * @param message the message for the AssertionError (<code>null</code> ok) * @param value the value * @param matcher the {@link Matcher} * * @throws AssertionError if the assertion fails */ public static <T> void assertThat(String message, T value, Matcher<?> matcher) throws AssertionError { assertThat(message, eventually(value), matcher); }
/** * Obtains an ensured of the specified {@link Deferred} * (configured using default {@link Ensured} timeouts) * * @param deferred the {@link Deferred} to ensure * * @return an {@link Ensured} of the {@link Deferred} */ public static <T> Deferred<T> ensured(Deferred<T> deferred) { return deferred instanceof Ensured ? deferred : new Ensured<T>(deferred, getDefaultEnsuredRetryDurationsMS(), getDefaultEnsuredTimeoutMS()); }
ensure(deferredMatch, totalRetryDuration, totalRetryDurationUnits);
/** * Obtains an ensured of the specified {@link Deferred}. * * @param deferred the {@link Deferred} to ensure * @param totalRetryDuration the maximum duration for retrying * @param totalRetryDurationUnits the {@link TimeUnit}s for the duration * * @return an {@link Ensured} of the {@link Deferred} */ public static <T> Deferred<T> ensured(Deferred<T> deferred, long totalRetryDuration, TimeUnit totalRetryDurationUnits) { return deferred instanceof Ensured ? deferred : new Ensured<T>(deferred, getDefaultEnsuredRetryDurationsMS(), totalRetryDurationUnits .toMillis(totalRetryDuration)); }
/** * Creates a dynamic proxy of an {@link Object}. The returned proxy will * record interactions (method calls) against the proxy for the * purposes of representing the calls as {@link Deferred}s. * <p> * The results of interactions on the returned proxy are always non-sense * and/or other dynamic proxies. To determine the actual result (as * a {@link Deferred}), one must call {@link #eventually(Object)}. * * @param <T> the type of {@link Object} * @param object the {@link Object} to proxy * * @return a recording dynamic proxy of the {@link Object} */ public static <T> T invoking(T object) { return invoking(new Existing<T>(object)); }
ensure(deferredMatch, totalRetryDuration, totalRetryDurationUnits);
/** * Obtains an {@link Ensured} of the specified {@link Deferred}. * * @param deferred the {@link Deferred} to ensure * @param totalRetryDurationMS the maximum duration (in milliseconds) to retry * * @return an {@link Ensured} of the {@link Deferred} */ public static <T> Deferred<T> ensured(Deferred<T> deferred, long totalRetryDurationMS) { return deferred instanceof Ensured ? (Ensured<T>) deferred : new Ensured<T>(deferred, getDefaultEnsuredRetryDurationsMS(), totalRetryDurationMS); }
/** * Obtains the value of a {@link Deferred}. * <p> * This is functionally equivalent to calling {@link Deferred#get()} on * {@link #ensured(Deferred)}. * * @param deferred the {@link Deferred} to ensure * * @return the value of the {@link Deferred} */ public static <T> T ensure(Deferred<T> deferred) { return ensured(deferred).get(); }
DeferredHelper.ensure(new Deferred<Boolean>()
/** * Asserts that a value will eventually satisfy the specified {@link Matcher}. * <p> * Should the value be the result of a call to {@link DeferredHelper#invoking(Deferred)} * the result is unwrapped into a {@link Deferred} that is then used for the assert. * * @param <T> the type of value produced by the {@link Deferred} * * @param value the value * @param matcher the {@link Matcher} * * @throws AssertionError if the assertion fails */ public static <T> void assertThat(T value, Matcher<?> matcher) throws AssertionError { assertThat(null, eventually(value), matcher); }
return ensured(deferred, totalRetryDuration, totalRetryDurationUnits);
/** * Obtains the value of a {@link Deferred}. * <p> * This is functionally equivalent to calling {@link Deferred#get()} on * {@link #ensure(Deferred, long)}. * * @param deferred the {@link Deferred} to ensure * @param totalRetryDurationMS the maximum duration (in milliseconds) to retry * * @return an {@link Ensured} of the {@link Deferred} */ public static <T> T ensure(Deferred<T> deferred, long totalRetryDurationMS) { return ensured(deferred, totalRetryDurationMS).get(); }
/** * Obtains the value of a {@link Deferred}. * <p> * This is functionally equivalent to calling {@link Deferred#get()} on * {@link #ensure(Deferred, long, TimeUnit)}. * * @param deferred the {@link Deferred} to ensure * @param totalRetryDuration the maximum duration for retrying * @param totalRetryDurationUnits the {@link TimeUnit}s for the duration * * @return the value of the {@link Deferred} */ public static <T> T ensure(Deferred<T> deferred, long totalRetryDuration, TimeUnit totalRetryDurationUnits) { return ensured(deferred, totalRetryDuration, totalRetryDurationUnits).get(); }
/** * Obtains a {@link Deferred} representation of the last call to a * dynamic proxy created with either {@link #invoking(Object)} or * {@link #invoking(Deferred)}. * * @param t the value returned from an call to 'invoking' * * @return a {@link Deferred} representation of a previous call * {@link #invoking(Object)} */ @SuppressWarnings("unchecked") public static <T> Deferred<T> eventually(T t) { // get the last deferred value from invoking Deferred<T> deferred = (Deferred<T>) m_deferred.get(); if (deferred == null) { deferred = t instanceof Deferred ? (Deferred<T>) t : new Existing<T>(t); } else { // clear the last invoking call m_deferred.set(null); } return ensured(deferred); }
/** * Obtains the value of a {@link Deferred}. * <p> * This is functionally equivalent to calling {@link Deferred#get()} on * {@link #ensure(Deferred, long, TimeUnit)}. * * @param deferred the {@link Deferred} to ensure * @param retryDelayDuration the time to wait between retrying * @param retryDelayDurationUnits the {@link TimeUnit}s for the retry delay duration * @param totalRetryDuration the maximum duration for retrying * @param totalRetryDurationUnits the {@link TimeUnit}s for the duration * * @return the value of the {@link Deferred} */ public static <T> T ensure(Deferred<T> deferred, long retryDelayDuration, TimeUnit retryDelayDurationUnits, long totalRetryDuration, TimeUnit totalRetryDurationUnits) { return ensured(deferred, retryDelayDuration, retryDelayDurationUnits, totalRetryDuration, totalRetryDurationUnits).get(); }
/** * Obtains a {@link Deferred} representation of the last call to a * dynamic proxy created with either {@link #invoking(Object)} or * {@link #invoking(Deferred)}. * * @param t the value returned from an call to 'invoking' * @param totalRetryDuration the maximum duration for retrying * @param totalRetryDurationUnits the {@link TimeUnit}s for the duration * * @return a {@link Deferred} representation of a previous call * {@link #invoking(Object)} */ @SuppressWarnings("unchecked") public static <T> Deferred<T> eventually(T t, long totalRetryDuration, TimeUnit totalRetryDurationUnits) { // get the last deferred value from invoking Deferred<T> deferred = (Deferred<T>) m_deferred.get(); if (deferred == null) { deferred = t instanceof Deferred ? (Deferred<T>) t : new Existing<T>(t); } else { // clear the last invoking call m_deferred.set(null); } return ensured(deferred, totalRetryDuration, totalRetryDurationUnits); }
/** * {@inheritDoc} */ @Override public <T> T getMBeanProxy(ObjectName objectName, Class<T> proxyClass) { return ensured(getDeferredMBeanProxy(objectName, proxyClass), getDefaultTimeout(), getDefaultTimeoutUnits()).get(); }