public Builder<T,V> onTimeoutThrow() { onTimeout = Maybe.<V>absent(); return this; } public Builder<T,V> onUnmanagedReturn(V val) {
public Builder<T,V> onUnmanagedThrow() { onUnmanaged = Maybe.<V>absent(); return this; } /** @since 0.7.0 included in case old behaviour of not checking whether the entity is managed is required
/** Creates an absent whose {@link #get()} throws an {@link IllegalStateException} with the indicated message. * Both stack traces (the cause and the callers) are provided, which can be quite handy, * but comparatively expensive as the cause stack trace has to generated when this method is invoked, * even if it is never accessed. See also {@link #absentNoTrace(String)} and {@link #absent(Throwable)}. */ public static <T> Maybe<T> absentWithTrace(final String message) { return absent(new IllegalStateException(message)); }
@Override public Maybe<T> apply(T oldValue) { if (Objects.equal(oldValue, newValue)) { return lastValue = Maybe.absent("Skipping update, values equal"); } else { return lastValue = Maybe.of(newValue); } }
/** Creates an absent whose get throws an {@link IllegalStateException} with the indicated message, * but not a stack trace for the calling location. As stack traces can be comparatively expensive * this is useful for efficiency, but it can make debugging harder as the origin of the absence is not kept, * in contrast to {@link #absentWithTrace(String)} and {@link #absent(Throwable)}. */ public static <T> Maybe<T> absentNoTrace(final String message) { return absent(new IllegalStateExceptionSupplier(message)); }
/** As {@link #absentWithTrace(String)} but using the provided exception instead of this location * as the cause, and a string based on this cause as the message on the {@link IllegalStateException} * thrown if a user does a {@link #get()}. * Useful if an {@link Exception} has already been generated (and no overhead) * or if you want to supply a specific cause as in <code>absent(new MyException(...))</code> * (but there is the Exception creation overhead there). */ public static <T> Maybe<T> absent(final Throwable cause) { return absent(new IllegalStateExceptionSupplier(cause)); }
private synchronized Maybe<Object> getRawStringKeyMaybe(String key, boolean markUsed) { if (config.containsKey(key)) { if (markUsed) markUsed(key); return Maybe.of(config.get(key)); } return Maybe.absent(); }
private static <T> Maybe<T> lookupViaServiceLoader(Class<T> clazz, ClassLoader loader) { LOG.trace("Looking up " + clazz.getSimpleName() + " via ServiceLoader"); Maybe<T> result = Maybe.absent("No class " + clazz.getSimpleName() + " found with ServiceLoader"); ServiceLoader<T> LOADER = lookupAllViaServiceLoader(clazz, loader); for (T item : LOADER) { return Maybe.of(item); } return result; }
@Override public Maybe<Effector<?>> getEffectorByName(String name) { if (name != null) { for (Effector<?> contender : effectors) { if (name.equals(contender.getName())) return Maybe.<Effector<?>>of(contender); } } return Maybe.<Effector<?>>absent("No effector matching '" + name + "'"); }
/** @return a {@link Maybe} object which is absent if the argument {@link #isBlank(CharSequence)} */ public static <T extends CharSequence> Maybe<T> maybeNonBlank(T s) { if (isNonBlank(s)) return Maybe.of(s); return Maybe.absent(); }
@Override public Maybe<OsgiManager> getOsgiManager() { switch (mode) { case PRE_MANAGEMENT: case MANAGEMENT_STARTING: case MANAGEMENT_STARTED: checkInitialManagementContextReal(); return initialManagementContext.getOsgiManager(); default: return Maybe.absent("Entity " + entity + " is no longer managed; OSGi context no longer available"); } }
@Override public Maybe<TValue> getDefaultValue() { if (key==null || !key.hasDefaultValue()) return Maybe.absent(); return conversionFunction.apply(Maybe.of((Object)key.getDefaultValue())); }
/** @deprecated since 0.11.0, use {@link org.apache.brooklyn.util.core.ClassLoaderUtils} in a combination with {@link #invokeConstructorFromArgs(Class, Object...)} instead */ @SuppressWarnings("unchecked") public static <T> Maybe<T> invokeConstructorFromArgs(ClassLoader classLoader, Class<T> optionalSupertype, String className, Object...argsArray) { Reflections reflections = new Reflections(classLoader); Class<?> clazz = reflections.loadClass(className); if (optionalSupertype!=null && !optionalSupertype.isAssignableFrom(clazz)) { return Maybe.absent("The type requested '"+className+"' is not assignable to "+optionalSupertype); } return invokeConstructorFromArgs(reflections, (Class<T>)clazz, argsArray, false); }
@Test(expectedExceptions=IllegalStateException.class) public void testCastMWhenAbsent() { // Maybe<JsonElement> m = JsonFunctions.walkM("europe", "spain", "barcelona").apply( Maybe.of( europeMap()) ); Maybe<JsonElement> m = Maybe.absent(); JsonFunctions.castM(String.class).apply(m); }
public static Maybe<CatalogBundle> resolve(ManagementContext mgmt, CatalogBundle b) { if (b.isNameResolved()) return Maybe.of(b); OsgiManager osgi = ((ManagementContextInternal)mgmt).getOsgiManager().orNull(); if (osgi==null) return Maybe.absent("No OSGi manager"); Maybe<Bundle> b2 = osgi.findBundle(b); if (b2.isAbsent()) return Maybe.absent("Nothing installed for "+b); return Maybe.of(new CatalogBundleDto(b2.get().getSymbolicName(), b2.get().getVersion().toString(), b.getUrl())); }
public static Maybe<String> findSubnetIp(Iterable<? extends Location> ll) { // TODO Or if can't find MachineLocation, should we throw new IllegalStateException("Cannot find hostname for among "+ll); Maybe<MachineLocation> l = findUniqueMachineLocation(ll); return (l.isPresent()) ? Machines.getSubnetIp(l.get()) : Maybe.<String>absent(); }
public static Maybe<ReleaseableLatch> maxConcurrencyImmediately(Object maxThreads) { Maybe<?> resolvedMaxThreads = resolveImmediately(maxThreads); if (resolvedMaxThreads.isAbsent()) return Maybe.absent(); Integer resolvedMaxThreadsInt = TypeCoercions.coerce(resolvedMaxThreads, Integer.class); ReleaseableLatch result = ReleaseableLatch.Factory.newMaxConcurrencyLatch(resolvedMaxThreadsInt); return Maybe.<ReleaseableLatch>of(result); }
@Override public Maybe<Object> getConfigRaw(ConfigKey<?> key) { if (containsKey(key.getName())) return Maybe.of(get(key.getName())); return Maybe.absent(); }
/** as {@link #invokeMethodFromArgs(Object, String, List)} but giving control over whether to set it accessible */ public static Maybe<Object> invokeMethodFromArgs(Object clazzOrInstance, String method, List<?> args, boolean setAccessible, Optional<? extends TypeCoercer> coercer) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException { Maybe<Method> maybeMethod = getMethodFromArgs(clazzOrInstance, method, args); if (coercer.isPresent() && maybeMethod.isAbsent()) { maybeMethod = getMethodFromArgs(clazzOrInstance, method, args, coercer); } if (maybeMethod.isAbsent()) { return Maybe.absent(Maybe.getException(maybeMethod)); } Method m = maybeMethod.get(); return Maybe.of(invokeMethodFromArgs(clazzOrInstance, m, args, setAccessible, coercer)); }
@Test public void testModifyAttributeReturningAbsentDoesNotEmit() throws Exception { AttributeSensor<Integer> sensor = Sensors.newIntegerSensor("a", ""); AttributeSensor<Integer> childSensor = Sensors.newIntegerSensor("a.b", ""); final RecordingSensorEventListener<Object> listener = new RecordingSensorEventListener<>(); entityImpl.subscriptions().subscribe(entityImpl, sensor, listener); map.modify(childSensor, Functions.constant(Maybe.<Integer>absent())); Asserts.succeedsContinually(new Runnable() { @Override public void run() { assertTrue(Iterables.isEmpty(listener.getEvents()), "events="+listener.getEvents()); }}); }