1 /*******************************************************************************
\r
2 * Copyright (c) 2016 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * Semantum Oy - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.db.function;
\r
14 import java.util.Objects;
\r
16 import org.simantics.db.exception.DatabaseException;
\r
19 * Represents a function that accepts one argument and produces a result that
\r
20 * can throw database exceptions.
\r
22 * @param <T> the type of the first argument to the function
\r
23 * @param <U> the type of the second argument to the function
\r
24 * @param <R> the type of the result of the function
\r
26 * @since 1.22.1 & 1.24.0
\r
28 @FunctionalInterface
\r
29 public interface DbFunction<T, R> {
\r
32 * Applies this function to the given argument.
\r
34 * @param t the function argument
\r
35 * @return the function result
\r
37 R apply(T t) throws DatabaseException;
\r
40 * Returns a composed function that first applies the {@code before}
\r
41 * function to its input, and then applies this function to the result.
\r
42 * If evaluation of either function throws an exception, it is relayed to
\r
43 * the caller of the composed function.
\r
45 * @param <V> the type of input to the {@code before} function, and to the
\r
47 * @param before the function to apply before this function is applied
\r
48 * @return a composed function that first applies the {@code before}
\r
49 * function and then applies this function
\r
50 * @throws NullPointerException if before is null
\r
52 * @see #andThen(DbFunction)
\r
54 default <V> DbFunction<V, R> compose(DbFunction<? super V, ? extends T> before) {
\r
55 Objects.requireNonNull(before);
\r
56 return (V v) -> apply(before.apply(v));
\r
60 * Returns a composed function that first applies this function to
\r
61 * its input, and then applies the {@code after} function to the result.
\r
62 * If evaluation of either function throws an exception, it is relayed to
\r
63 * the caller of the composed function.
\r
65 * @param <V> the type of output of the {@code after} function, and of the
\r
67 * @param after the function to apply after this function is applied
\r
68 * @return a composed function that first applies this function and then
\r
69 * applies the {@code after} function
\r
70 * @throws NullPointerException if after is null
\r
72 * @see #compose(DbFunction)
\r
74 default <V> DbFunction<T, V> andThen(DbFunction<? super R, ? extends V> after) {
\r
75 Objects.requireNonNull(after);
\r
76 return (T t) -> after.apply(apply(t));
\r
80 * Returns a function that always returns its input argument.
\r
82 * @param <T> the type of the input and output objects to the function
\r
83 * @return a function that always returns its input argument
\r
85 static <T> DbFunction<T, T> identity() {
\r