org.adeptnet.sql.SQLConsumer Maven / Gradle / Ivy
Show all versions of sql Show documentation
/*
* Copyright 2015 Francois Steyn - Adept Internet (PTY) LTD ([email protected]).
*
* Licensed under the Apache 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://www.apache.org/licenses/LICENSE-2.0
*
* 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.adeptnet.sql;
import java.sql.SQLException;
import java.util.Objects;
import java.util.function.Consumer;
/**
* Represents an operation that accepts a single input argument and returns no
* result. Unlike most other functional interfaces, {@code Consumer} is expected
* to operate via side-effects.
*
*
* This is a functional interface
* whose functional method is {@link #accept(Object)}.
*
* Copied from java.util.function.Consumer
*
* @author Francois Steyn - Adept Internet (PTY) LTD ([email protected])
* @param the type of the input to the operation
*/
@FunctionalInterface
public interface SQLConsumer {
/**
* Performs this operation on the given argument.
*
* @param t the input argument
* @throws java.sql.SQLException if underlying operation throws SQLException
* @throws SQLDataAccessException if underlying operation throws
* SQLDataAccessException
*/
void accept(T t) throws java.sql.SQLException, SQLDataAccessException;
/**
* Returns a composed {@code Consumer} that performs, in sequence, this
* operation followed by the {@code after} operation. If performing either
* operation throws an exception, it is relayed to the caller of the
* composed operation. If performing this operation throws an exception, the
* {@code after} operation will not be performed.
*
* @param after the operation to perform after this operation
* @return a composed {@code Consumer} that performs in sequence this
* operation followed by the {@code after} operation
* @throws NullPointerException if {@code after} is null
*/
default SQLConsumer andThen(SQLConsumer super T> after) {
Objects.requireNonNull(after);
return (T t) -> {
accept(t);
after.accept(t);
};
}
/**
*
* @param the type of arguments to the consumer
* @param sqlConsumer SQLConsumer that will be wrapped
* @return Consumer with possible SQLDataAccessException when SQLException
* thrown by the SQLConsumer
*/
static Consumer checked(SQLConsumer sqlConsumer) {
return (T t) -> {
try {
sqlConsumer.accept(t);
} catch (SQLException ex) {
throw new SQLDataAccessException(ex.getMessage(), ex);
}
};
}
}