org.opencastproject.util.data.Function Maven / Gradle / Ivy
/*
* Licensed to The Apereo Foundation under one or more contributor license
* agreements. See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
*
*
* The Apereo Foundation licenses this file to you under the Educational
* Community License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of the License
* at:
*
* http://opensource.org/licenses/ecl2.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*
*/
package org.opencastproject.util.data;
import static org.opencastproject.util.data.functions.Misc.chuck;
import org.opencastproject.util.data.functions.Functions;
import com.entwinemedia.fn.Fn;
/**
* Function of arity 1.
*
* A general note on function implementations: Each function has an associated X version with the only difference that a
* checked exception may be thrown on application. This behaviour could be nicely combined into the main function class
* but with the cost of having no abstract method. Some IDEs such as IntelliJ support nice code folding for SAM (single
* abstract method) classes.
*
* @see X
*/
public abstract class Function {
/** Apply function to a
. */
public abstract B apply(A a);
/** Currying. */
public Function0 curry(final A a) {
return Functions.curry(this, a);
}
/** Currying. */
public Function> curry() {
return Functions.curry(this);
}
/** Function composition. g.o(f).apply(x) == g(f(x))
*/
public Function o(final Function super C, ? extends A> f) {
return Functions.o(this, f);
}
/** Function composition. g.o(f).apply() == g(f())
*/
public Function0 o(final Function0 extends A> f) {
return Functions.o(this, f);
}
/** Left to right composition: f then g = g(f(x))
*/
public Function then(final Function super B, ? extends C> f) {
return Functions.then(this, f);
}
/** @see Functions#rethrow(Function, Function) */
public Function rethrow(final Function super Exception, ? extends Exception> transformer) {
return Functions.rethrow(this, transformer);
}
/** @see Functions#handle(Function, Function) */
public Function handle(final Function super Exception, B> handler) {
return Functions.handle(this, handler);
}
/** @see Functions#either(Function, Function) */
public Function> either(final Function handler) {
return Functions.either(this, handler);
}
/** Turn this function into an effect by discarding its result. */
public Effect toEffect() {
return Functions.toEffect(this);
}
public Fn toFn() {
return new Fn() {
@Override
public B apply(A a) {
return Function.this.apply(a);
}
};
}
/** Version of {@link Function} that allows for throwing a checked exception. */
public abstract static class X extends Function {
@Override
public final B apply(A a) {
try {
return xapply(a);
} catch (Exception e) {
return chuck(e);
}
}
/**
* Apply function to a
. Any thrown exception gets "chucked" so that you may catch them as is. See
* {@link Functions#chuck(Throwable)} for details.
*/
protected abstract B xapply(A a) throws Exception;
}
}