@Override public B apply(ListMonadic<A> m) { return f.apply(); } };
public A value() { if (a == null) { a = f.apply(); } return a; } }
@Override protected A calc() { if (a == null) { a = f.apply(); } return a; } };
@Override protected void run() { f.apply(); } };
@Override public A getOrElse(Function0<A> none) { return none.apply(); }
/** * Get a value from a map, creating and adding a new one, if the value is missing, i.e. it is null. */ public static <K, V> V getOrCreate(Map<K, V> map, K key, Function0<V> f) { V v = map.get(key); if (v == null) { v = f.apply(); map.put(key, v); } return v; }
/** Throw exception returned by <code>none</code> if none. */ public <T extends Throwable> Option<A> orError(Function0<T> none) throws T { if (isSome()) return this; else throw none.apply(); }
@Override public B apply() { return g.apply(f.apply()); } };
@Override public B apply() { return f.apply(g.apply()); } };
/** Lazy version of {@link #orElse(Option)}. */ public Option<A> orElse(Function0<Option<A>> none) { return isSome() ? this : none.apply(); }
/** * Apply function <code>f</code> only if no other thread currently applies a function using this needle eye. Please * note that <code>f</code> must <em>not</em> return null, so please do not use * {@link org.opencastproject.util.data.Effect0}. * * @return the result of <code>f</code> or none if another function is currently being applied. */ public <A> Option<A> apply(Function0<A> f) { if (running.compareAndSet(false, true)) { try { return some(f.apply()); } finally { running.set(false); } } else { return none(); } } }
/** Lazy version of {@link #andThen(Option)}. */ public <B> Option<B> andThen(Function0<Option<B>> some) { return isSome() ? some.apply() : Option.<B> none(); }
/** Lazy version of {@link #andThenV(Object)}. */ public <B> Option<B> andThenV(Function0<B> some) { return isSome() ? some(some.apply()) : Option.<B> none(); }
/** Safe decomposition of the option type using functions. */ public <B> B fold(Function<A, B> some, Function0<B> none) { return isSome() ? some.apply(get()) : none.apply(); }
/** Typesafe replacement for {@link #execute(org.quartz.JobExecutionContext)}. */ protected abstract void execute(A parameters, JobExecutionContext ctx);
/** * Handle a stream inside <code>f</code> and ensure that <code>s</code> gets closed properly. * * @param s * the stream creation function * @param toErr * error handler transforming an exception into something else * @param f * stream handler * @deprecated use * {@link #withResource(org.opencastproject.util.data.Function0, org.opencastproject.util.data.Function, org.opencastproject.util.data.Function)} * instead */ @Deprecated public static <A, Err> Either<Err, A> withStream(Function0<InputStream> s, Function<Exception, Err> toErr, Function<InputStream, A> f) { InputStream in = null; try { in = s.apply(); return right(f.apply(in)); } catch (Exception e) { return left(toErr.apply(e)); } finally { IoSupport.closeQuietly(in); } }
/** * Handle a closeable resource inside <code>f</code> and ensure that <code>r</code> gets closed properly. * * @param r * resource creation function * @param toErr * error handler transforming an exception into something else * @param f * resource handler */ public static <A, Err, B extends Closeable> Either<Err, A> withResource(Function0<B> r, Function<Exception, Err> toErr, Function<B, A> f) { B b = null; try { b = r.apply(); return right(f.apply(b)); } catch (Exception e) { return left(toErr.apply(e)); } finally { IoSupport.closeQuietly(b); } }