/** 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)); }
/** creates an instance wrapping a {@link SoftReference}, using the second item given * if the first argument is dereferenced. * however if the first argument is null, this is a permanent present null, * as {@link #of(Object)} with null. */ public static <T> Maybe<T> softThen(T value, Maybe<T> ifEmpty) { if (value==null) return of((T)null); return new SoftlyPresent<T>(value).usingAfterExpiry(ifEmpty); }
@Override public T get() { throw getException(); } @Override
@Test public void testNotEqual() { checkTF(IfFunctions.ifNotEquals(false).value("T").defaultValue("F").build(), "T"); }
public Builder<T,V> onTimeoutThrow() { onTimeout = Maybe.<V>absent(); return this; } public Builder<T,V> onUnmanagedReturn(V val) {
private long sizeOfActiveReferences(List<Maybe<byte[]>> references) { long size = 0; for (Maybe<byte[]> ref: references) { byte[] deref = ref.orNull(); if (deref!=null) size += deref.length; } return size; }
ExpectedFlagConfigKeyAndValueRecord(ConfigKey<?> key, String flagName, Object val) { this.configKey = key; this.flagName = flagName; this.configKeyValue = Maybe.of(val); } @Override
@Test public void testFunction() { checkTF(IfFunctions.ifNotEquals((Boolean)null).apply(new Function<Boolean, String>() { @Override public String apply(Boolean input) { return input.toString().toUpperCase().substring(0, 1); } }).defaultValue("?"), "?"); }
@Test public void testWithoutCast() { Function<Boolean, String> f = IfFunctions.newInstance(Boolean.class, String.class).ifEquals(false).value("F").ifEquals(true).value("T").defaultValue("?").build(); checkTF(f, "?"); }
@Test public void testIfPredicateAndSupplier() { IfFunctionsTest.checkTF(Functionals.ifPredicate(Predicates.equalTo(false)).get(Suppliers.ofInstance("F")) .ifEquals(true).value("T").defaultGet(Suppliers.ofInstance("?")).build(), "?"); }
/** Returns an absent indicator. No message is available and access does not include any reference to this creation. * Therefore it is fast and simple, but hard to work with if someone might {@link #get()} it and want a useful exception. * See also {@link #absentNoTrace(String)} to include a message with very low overhead, * or {@link #absentWithTrace(String)} or {@link #absent(Throwable)} for more control over the exception thrown. */ public static <T> Maybe<T> absent() { return new Maybe.Absent<T>(); }
@Override public boolean isNull() { // should always be false as per Optional contract return get()==null; } };
/** 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)); }
/** As {@link #absent(Throwable)} but using the given message as the message on the {@link IllegalStateException} * thrown if a user does a {@link #get()}. */ public static <T> Maybe<T> absent(final String message, final Throwable cause) { return absent(new IllegalStateExceptionSupplier(message, cause)); }