/** * @deprecated since 0.11.0; explicit groovy utilities/support will be deleted. */ @Deprecated public Builder<T,V> readiness(Closure<Boolean> val) { this.readiness = GroovyJavaMethods.predicateFromClosure(checkNotNull(val, "val")); return this; } public Builder<T,V> readiness(Predicate<? super T> val) {
@SuppressWarnings("unchecked") @Deprecated /** @deprecated since 0.7.0 use Function */ public NamedActionWithUrl(String actionName, Closure<String> postProcessing) { this.actionName = actionName; this.postProcessing = (Function<T, String>) ((postProcessing == null) ? null : GroovyJavaMethods.functionFromClosure(postProcessing)); }
@SuppressWarnings("unchecked") public static <T> Predicate<T> castToPredicate(Object o) { try { if (safeGroovyIsCase(o, Closure.class)) { return predicateFromClosure((Closure<Boolean>)o); } else { return (Predicate<T>) o; } } catch (Throwable e) { throw Exceptions.propagate(e); } }
public static <T> T elvis(Object... preferences) { try { if (preferences.length == 0) throw new IllegalArgumentException("preferences must not be empty for elvis"); for (Object contender : preferences) { if (truth(contender)) return fix(contender); } return fix(preferences[preferences.length-1]); } catch (Throwable e) { throw Exceptions.propagate(e); } }
/** * @deprecated since 0.11.0; explicit groovy utilities/support will be deleted. */ @Deprecated public static <T,V> Task<V> attributePostProcessedWhenReady(Entity source, AttributeSensor<T> sensor, Closure<Boolean> ready, Closure<V> postProcess) { Predicate<? super T> readyPredicate = (ready != null) ? GroovyJavaMethods.predicateFromClosure(ready) : JavaGroovyEquivalents.groovyTruthPredicate(); Function<? super T, V> postProcessFunction = GroovyJavaMethods.<T,V>functionFromClosure(postProcess); return attributePostProcessedWhenReady(source, sensor, readyPredicate, postProcessFunction); }
@SuppressWarnings("unchecked") public static <T> Closure<T> castToClosure(Object o) { try { if (ScriptBytecodeAdapter.compareEqual(o, null)) { return (Closure<T>)ScriptBytecodeAdapter.castToType(o, Closure.class); } else if (safeGroovyIsCase(o, Closure.class)) { return (Closure<T>)ScriptBytecodeAdapter.castToType(o, Closure.class); } else if (o instanceof Runnable) { return closureFromRunnable((Runnable)ScriptBytecodeAdapter.createPojoWrapper(ScriptBytecodeAdapter.castToType(o, Runnable.class), Runnable.class)); } else if (o instanceof Callable) { return closureFromCallable((Callable<T>)ScriptBytecodeAdapter.createPojoWrapper(ScriptBytecodeAdapter.castToType(o, Callable.class), Callable.class)); } else if (o instanceof Function) { return closureFromFunction((Function<Object, T>)ScriptBytecodeAdapter.createPojoWrapper(ScriptBytecodeAdapter.castToType(o, Function.class), Function.class)); } else { throw new IllegalArgumentException("Cannot convert to closure: o="+o+"; type="+(o != null ? o.getClass() : null)); } } catch (Throwable e) { throw Exceptions.propagate(e); } }
@SuppressWarnings("unchecked") public static <T> T fix(Object o) { try { if (safeGroovyIsCase(o, GString.class)) { return (T)ScriptBytecodeAdapter.asType(o, String.class); } else { return (T)o; } } catch (Throwable e) { throw Exceptions.propagate(e); } }
@Override public boolean apply(Object input) { return truth(input); } };
/** * @deprecated since 0.11.0; explicit groovy utilities/support will be deleted. */ @Deprecated public BasicTask(Closure<T> job) { this(GroovyJavaMethods.callableFromClosure(job)); }
@Test public void testElvis() { final List<?> emptyList = ImmutableList.of(); final List<?> singletonList = ImmutableList.of("myVal"); final List<?> differentList = ImmutableList.of("differentVal"); assertEquals(elvis("", "string2"), "string2"); assertEquals(elvis("string1", "string2"), "string1"); assertEquals(elvis(null, "string2"), "string2"); assertEquals(elvis("", "string2"), "string2"); assertEquals((int)elvis(1, 2), 1); assertEquals((int)elvis(0, 2), 2); assertEquals(elvis(singletonList, differentList), singletonList); assertEquals(elvis(emptyList, differentList), differentList); assertEquals(elvis(gstring, "other"), gstringVal); assertEquals(elvis(emptyGstring, "other"), "other"); assertEquals(elvis(emptyGstring, gstring), gstringVal); }
@SuppressWarnings("unchecked") public static <T> Callable<T> callableFromRunnable(final Runnable job) { try { if (safeGroovyIsCase(job, Callable.class)) { return (Callable<T>)ScriptBytecodeAdapter.asType(job, Callable.class); } else { return CallableFromRunnable.newInstance(job, null); } } catch (Throwable e) { throw Exceptions.propagate(e); } }
private <T> boolean groovyTruthInvocation(T value) throws Throwable { // We expect this to be equivalent to Groovy-Truth boolean result = org.apache.brooklyn.util.JavaGroovyEquivalents.groovyTruth(value); boolean groovyTruth = org.apache.brooklyn.util.groovy.GroovyJavaMethods.truth(value); assertEquals(result, groovyTruth, "value="+value); return result; } }
/** * @deprecated since 0.11.0; explicit groovy utilities/support will be deleted. */ @Deprecated public BasicTask(Map<?,?> flags, Closure<T> job) { this(flags, GroovyJavaMethods.callableFromClosure(job)); }
/** * @deprecated since 0.11.0; explicit groovy utilities/support will be deleted. */ @Deprecated public static List<Class<?>> getAllAssignableTypes(Class<?> base, Closure<Boolean> filter) { return getAllAssignableTypes(base, GroovyJavaMethods.<Class<?>>predicateFromClosure(filter)); } public static List<Class<?>> getAllAssignableTypes(Class<?> base, Predicate<Class<?>> filter) {
/** * @deprecated since 0.11.0; explicit groovy utilities/support will be deleted; also use {@link EnricherSpec} */ @Deprecated public TimeWeightedDeltaEnricher(Entity producer, Sensor<T> source, Sensor<Double> target, int unitMillis, Closure<Double> postProcessor) { this(producer, source, target, unitMillis, GroovyJavaMethods.<Double,Double>functionFromClosure(postProcessor)); }
private boolean groovyIsCaseInvocation(Object switchValue, Class<?> caseExpression) throws Throwable { // We expect this to be equivalent to: // org.codehaus.groovy.runtime.ScriptBytecodeAdapter.isCase(switchValue, caseExpression); boolean result = org.apache.brooklyn.util.groovy.GroovyJavaMethods.safeGroovyIsCase(switchValue, caseExpression); boolean equiv = org.codehaus.groovy.runtime.ScriptBytecodeAdapter.isCase(switchValue, caseExpression); assertEquals(result, equiv, "switchValue="+switchValue+"; caseExpression="+caseExpression); return result; }
/** * @deprecated since 0.11.0; explicit groovy utilities/support will be deleted. */ @Deprecated public FunctionPollConfig<S, T> closure(Closure<?> val) { this.callable = GroovyJavaMethods.callableFromClosure(checkNotNull(val, "closure")); return this; }
/** * Takes a closure which accepts this ScriptHelper and returns true or false * as to whether the script needs to run (or can throw error if desired) * * @deprecated since 0.11.0; explicit groovy utilities/support will be deleted. */ @Deprecated @SuppressWarnings({ "rawtypes", "unchecked" }) public ScriptHelper executeIf(Closure c) { Predicate<ScriptHelper> predicate = GroovyJavaMethods.predicateFromClosure(c); return executeIf(predicate); }
/** * @deprecated since 0.11.0; explicit groovy utilities/support will be deleted. */ @Deprecated @SuppressWarnings({ "unchecked", "rawtypes" }) public <V2> Builder<T,V2> postProcess(Closure<V2> val) { this.postProcess = (Function) GroovyJavaMethods.<T,V2>functionFromClosure(checkNotNull(val, "postProcess")); return (Builder<T,V2>) this; } @SuppressWarnings({ "unchecked", "rawtypes" })