org.eu.zajc.ef.EHandle Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of extended-functions Show documentation
Show all versions of extended-functions Show documentation
Specializations and support utilities for functional interfaces that are missing from the standard
library
The newest version!
// SPDX-License-Identifier: Apache-2.0
/*
* extended-functions
* Copyright 2021-2024 Marko Zajc
*
* 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.eu.zajc.ef;
import static org.eu.zajc.ef.Utilities.asUnchecked;
import java.util.concurrent.Callable;
import java.util.function.*;
import javax.annotation.Nonnull;
import org.eu.zajc.ef.biconsumer.*;
import org.eu.zajc.ef.biconsumer.except.*;
import org.eu.zajc.ef.bifunction.*;
import org.eu.zajc.ef.bifunction.except.*;
import org.eu.zajc.ef.bipredicate.*;
import org.eu.zajc.ef.bipredicate.except.*;
import org.eu.zajc.ef.consumer.*;
import org.eu.zajc.ef.consumer.execpt.*;
import org.eu.zajc.ef.function.*;
import org.eu.zajc.ef.function.except.*;
import org.eu.zajc.ef.predicate.*;
import org.eu.zajc.ef.predicate.except.*;
import org.eu.zajc.ef.runnable.except.ERunnable;
import org.eu.zajc.ef.supplier.*;
import org.eu.zajc.ef.supplier.except.*;
import org.eu.zajc.ef.triconsumer.*;
import org.eu.zajc.ef.trifunction.*;
import org.eu.zajc.ef.tripredicate.*;
import org.eu.zajc.ef.unary.*;
import org.eu.zajc.ef.unary.except.*;
/**
* A class containing various helper utilities for exceptionable (E*) functions and
* {@link Callable} ({@link ESupplier}).
*
* @author Marko Zajc
*/
public class EHandle {
/*
* ================== Consumers ==================
*/
/**
* Handles a generic exception type that may occur in the handled consumer with a
* provided handler.
*
* @param
* The handled function's input type
* @param
* The handled function's exception type
* @param handled
* The function that may throw an exception
* @param handler
* The function handling exceptions
*
* @return An exception-proofed consumer
*/
@Nonnull
public static Consumer handle(@Nonnull EConsumer handled,
@Nonnull BiConsumer handler) {
return t -> {
try {
handled.acceptChecked(t);
} catch (Throwable e) { // NOSONAR can't catch generic exceptions
handler.accept(e, t);
}
};
}
/**
* Handles a generic exception type that may occur in the handled consumer by
* throwing a different exception.
*
* @param
* The handled function's input type
* @param
* The handled function's exception type
* @param
* The exception type to throw
* @param handled
* The function that may throw an exception
* @param handler
* The function handling exceptions
*
* @return An exception-proofed consumer
*
* @throws X
* The exception returned by the handler
*/
@Nonnull
@SuppressWarnings({ "null", "unused" })
public static Consumer handleThrowing(@Nonnull EConsumer handled,
@Nonnull Function handler) throws X {
return t -> {
try {
handled.acceptChecked(t);
} catch (Throwable e) { // NOSONAR can't catch generic exceptions
throw asUnchecked(handler.apply(e));
}
};
}
/**
* Handles a generic exception type that may occur in the handled consumer by
* throwing it explicitly.
*
* @param
* The handled function's input type
* @param
* The handled function's exception type
* @param handled
* The function that may throw an exception
*
* @return An exception-proofed consumer
*
* @throws E
* The checked exception
*/
@Nonnull
@SuppressWarnings("unused")
public static Consumer handleThrowing(@Nonnull EConsumer handled) throws E {
return t -> {
try {
handled.acceptChecked(t);
} catch (Throwable e) { // NOSONAR can't catch generic exceptions
throw asUnchecked(e);
}
};
}
/**
* Handles a generic exception type that may occur in the handled consumer by
* throwing a different exception.
*
* @param
* The handled function's exception type
* @param handled
* The function that may throw an exception
* @param handler
* The function handling exceptions
*
* @return An exception-proofed consumer
*/
@Nonnull
public static BooleanConsumer handle(@Nonnull EBooleanConsumer handled,
@Nonnull ObjBooleanConsumer handler) {
return p -> {
try {
handled.acceptChecked(p);
} catch (Throwable e) { // NOSONAR can't catch generic exceptions
handler.accept(e, p);
}
};
}
/**
* Handles a generic exception type that may occur in the handled consumer by
* throwing a different exception.
*
* @param
* The handled function's exception type
* @param
* The exception type to throw
* @param handled
* The function that may throw an exception
* @param handler
* The function handling exceptions
*
* @return An exception-proofed consumer
*
* @throws X
* The exception returned by the handler
*/
@Nonnull
@SuppressWarnings({ "null", "unused" })
public static BooleanConsumer handleThrowing(@Nonnull EBooleanConsumer handled,
@Nonnull Function handler) throws X {
return p -> {
try {
handled.acceptChecked(p);
} catch (Throwable e) { // NOSONAR can't catch generic exceptions
throw asUnchecked(handler.apply(e));
}
};
}
/**
* Handles a generic exception type that may occur in the handled consumer by
* throwing it explicitly.
*
* @param
* The handled function's exception type
* @param handled
* The function that may throw an exception
*
* @return An exception-proofed consumer
*
* @throws E
* The checked exception
*/
@Nonnull
@SuppressWarnings("unused")
public static BooleanConsumer handleThrowing(@Nonnull EBooleanConsumer handled) throws E {
return p -> {
try {
handled.acceptChecked(p);
} catch (Throwable e) { // NOSONAR can't catch generic exceptions
throw asUnchecked(e);
}
};
}
/**
* Handles a generic exception type that may occur in the handled consumer with a
* provided handler.
*
* @param
* The handled function's exception type
* @param handled
* The function that may throw an exception
* @param handler
* The function handling exceptions
*
* @return An exception-proofed consumer
*/
@Nonnull
public static ByteConsumer handle(@Nonnull EByteConsumer handled,
@Nonnull ObjByteConsumer handler) {
return p -> {
try {
handled.acceptChecked(p);
} catch (Throwable e) { // NOSONAR can't catch generic exceptions
handler.accept(e, p);
}
};
}
/**
* Handles a generic exception type that may occur in the handled consumer by
* throwing a different exception.
*
* @param
* The handled function's exception type
* @param
* The exception type to throw
* @param handled
* The function that may throw an exception
* @param handler
* The function handling exceptions
*
* @return An exception-proofed consumer
*
* @throws X
* The exception returned by the handler
*/
@Nonnull
@SuppressWarnings({ "null", "unused" })
public static ByteConsumer handleThrowing(@Nonnull EByteConsumer handled,
@Nonnull Function handler) throws X {
return p -> {
try {
handled.acceptChecked(p);
} catch (Throwable e) { // NOSONAR can't catch generic exceptions
throw asUnchecked(handler.apply(e));
}
};
}
/**
* Handles a generic exception type that may occur in the handled consumer by
* throwing it explicitly.
*
* @param
* The handled function's exception type
* @param handled
* The function that may throw an exception
*
* @return An exception-proofed consumer
*
* @throws E
* The checked exception
*/
@Nonnull
@SuppressWarnings("unused")
public static ByteConsumer handleThrowing(@Nonnull EByteConsumer handled) throws E {
return p -> {
try {
handled.acceptChecked(p);
} catch (Throwable e) { // NOSONAR can't catch generic exceptions
throw asUnchecked(e);
}
};
}
/**
* Handles a generic exception type that may occur in the handled consumer with a
* provided handler.
*
* @param
* The handled function's exception type
* @param handled
* The function that may throw an exception
* @param handler
* The function handling exceptions
*
* @return An exception-proofed consumer
*/
@Nonnull
public static CharConsumer handle(@Nonnull ECharConsumer handled,
@Nonnull ObjCharConsumer handler) {
return p -> {
try {
handled.acceptChecked(p);
} catch (Throwable e) { // NOSONAR can't catch generic exceptions
handler.accept(e, p);
}
};
}
/**
* Handles a generic exception type that may occur in the handled consumer by
* throwing a different exception.
*
* @param
* The handled function's exception type
* @param
* The exception type to throw
* @param handled
* The function that may throw an exception
* @param handler
* The function handling exceptions
*
* @return An exception-proofed consumer
*
* @throws X
* The exception returned by the handler
*/
@Nonnull
@SuppressWarnings({ "null", "unused" })
public static CharConsumer handleThrowing(@Nonnull ECharConsumer handled,
@Nonnull Function handler) throws X {
return p -> {
try {
handled.acceptChecked(p);
} catch (Throwable e) { // NOSONAR can't catch generic exceptions
throw asUnchecked(handler.apply(e));
}
};
}
/**
* Handles a generic exception type that may occur in the handled consumer by
* throwing it explicitly.
*
* @param
* The handled function's exception type
* @param handled
* The function that may throw an exception
*
* @return An exception-proofed consumer
*
* @throws E
* The checked exception
*/
@Nonnull
@SuppressWarnings("unused")
public static CharConsumer handleThrowing(@Nonnull ECharConsumer handled) throws E {
return p -> {
try {
handled.acceptChecked(p);
} catch (Throwable e) { // NOSONAR can't catch generic exceptions
throw asUnchecked(e);
}
};
}
/**
* Handles a generic exception type that may occur in the handled consumer with a
* provided handler.
*
* @param
* The handled function's exception type
* @param handled
* The function that may throw an exception
* @param handler
* The function handling exceptions
*
* @return An exception-proofed consumer
*/
@Nonnull
public static IntConsumer handle(@Nonnull EIntConsumer handled,
@Nonnull ObjIntConsumer handler) {
return p -> {
try {
handled.acceptChecked(p);
} catch (Throwable e) { // NOSONAR can't catch generic exceptions
handler.accept(e, p);
}
};
}
/**
* Handles a generic exception type that may occur in the handled consumer by
* throwing a different exception.
*
* @param
* The handled function's exception type
* @param
* The exception type to throw
* @param handled
* The function that may throw an exception
* @param handler
* The function handling exceptions
*
* @return An exception-proofed consumer
*
* @throws X
* The exception returned by the handler
*/
@Nonnull
@SuppressWarnings({ "null", "unused" })
public static IntConsumer handleThrowing(@Nonnull EIntConsumer handled,
@Nonnull Function handler) throws X {
return p -> {
try {
handled.acceptChecked(p);
} catch (Throwable e) { // NOSONAR can't catch generic exceptions
throw asUnchecked(handler.apply(e));
}
};
}
/**
* Handles a generic exception type that may occur in the handled consumer by
* throwing it explicitly.
*
* @param
* The handled function's exception type
* @param handled
* The function that may throw an exception
*
* @return An exception-proofed consumer
*
* @throws E
* The checked exception
*/
@Nonnull
@SuppressWarnings("unused")
public static IntConsumer handleThrowing(@Nonnull EIntConsumer handled) throws E {
return p -> {
try {
handled.acceptChecked(p);
} catch (Throwable e) { // NOSONAR can't catch generic exceptions
throw asUnchecked(e);
}
};
}
/**
* Handles a generic exception type that may occur in the handled consumer with a
* provided handler.
*
* @param
* The handled function's exception type
* @param handled
* The function that may throw an exception
* @param handler
* The function handling exceptions
*
* @return An exception-proofed consumer
*/
@Nonnull
public static LongConsumer handle(@Nonnull ELongConsumer handled,
@Nonnull ObjLongConsumer handler) {
return p -> {
try {
handled.acceptChecked(p);
} catch (Throwable e) { // NOSONAR can't catch generic exceptions
handler.accept(e, p);
}
};
}
/**
* Handles a generic exception type that may occur in the handled consumer by
* throwing a different exception.
*
* @param
* The handled function's exception type
* @param
* The exception type to throw
* @param handled
* The function that may throw an exception
* @param handler
* The function handling exceptions
*
* @return An exception-proofed consumer
*
* @throws X
* The exception returned by the handler
*/
@Nonnull
@SuppressWarnings({ "null", "unused" })
public static LongConsumer handleThrowing(@Nonnull ELongConsumer handled,
@Nonnull Function handler) throws X {
return p -> {
try {
handled.acceptChecked(p);
} catch (Throwable e) { // NOSONAR can't catch generic exceptions
throw asUnchecked(handler.apply(e));
}
};
}
/**
* Handles a generic exception type that may occur in the handled consumer by
* throwing it explicitly.
*
* @param
* The handled function's exception type
* @param handled
* The function that may throw an exception
*
* @return An exception-proofed consumer
*
* @throws E
* The checked exception
*/
@Nonnull
@SuppressWarnings("unused")
public static LongConsumer handleThrowing(@Nonnull ELongConsumer handled) throws E {
return p -> {
try {
handled.acceptChecked(p);
} catch (Throwable e) { // NOSONAR can't catch generic exceptions
throw asUnchecked(e);
}
};
}
/**
* Handles a generic exception type that may occur in the handled consumer with a
* provided handler.
*
* @param
* The handled function's exception type
* @param handled
* The function that may throw an exception
* @param handler
* The function handling exceptions
*
* @return An exception-proofed consumer
*/
@Nonnull
public static ShortConsumer handle(@Nonnull EShortConsumer handled,
@Nonnull ObjShortConsumer handler) {
return p -> {
try {
handled.acceptChecked(p);
} catch (Throwable e) { // NOSONAR can't catch generic exceptions
handler.accept(e, p);
}
};
}
/**
* Handles a generic exception type that may occur in the handled consumer by
* throwing a different exception.
*
* @param
* The handled function's exception type
* @param
* The exception type to throw
* @param handled
* The function that may throw an exception
* @param handler
* The function handling exceptions
*
* @return An exception-proofed consumer
*
* @throws X
* The exception returned by the handler
*/
@Nonnull
@SuppressWarnings({ "null", "unused" })
public static ShortConsumer handleThrowing(@Nonnull EShortConsumer handled,
@Nonnull Function handler) throws X {
return p -> {
try {
handled.acceptChecked(p);
} catch (Throwable e) { // NOSONAR can't catch generic exceptions
throw asUnchecked(handler.apply(e));
}
};
}
/**
* Handles a generic exception type that may occur in the handled consumer by
* throwing it explicitly.
*
* @param
* The handled function's exception type
* @param handled
* The function that may throw an exception
*
* @return An exception-proofed consumer
*
* @throws E
* The checked exception
*/
@Nonnull
@SuppressWarnings("unused")
public static ShortConsumer handleThrowing(@Nonnull EShortConsumer handled) throws E {
return p -> {
try {
handled.acceptChecked(p);
} catch (Throwable e) { // NOSONAR can't catch generic exceptions
throw asUnchecked(e);
}
};
}
/*
* ================== BiConsumers ==================
*/
/**
* Handles a generic exception type that may occur in the handled consumer with a
* provided handler.
*
* @param
* The handled function's input type
* @param
* The handled function's input type
* @param
* The handled function's exception type
* @param handled
* The function that may throw an exception
* @param handler
* The function handling exceptions
*
* @return An exception-proofed consumer
*/
@Nonnull
public static BiConsumer handle(@Nonnull EBiConsumer handled,
@Nonnull TriConsumer handler) {
return (t, u) -> {
try {
handled.acceptChecked(t, u);
} catch (Throwable e) { // NOSONAR can't catch generic exceptions
handler.accept(e, t, u);
}
};
}
/**
* Handles a generic exception type that may occur in the handled consumer by
* throwing a different exception.
*
* @param
* The handled function's input type
* @param
* The handled function's input type
* @param
* The handled function's exception type
* @param
* The exception type to throw
* @param handled
* The function that may throw an exception
* @param handler
* The function handling exceptions
*
* @return An exception-proofed consumer
*
* @throws X
* The exception returned by the handler
*/
@Nonnull
@SuppressWarnings({ "null", "unused" })
public static BiConsumer handleThrowing(@Nonnull EBiConsumer handled,
@Nonnull Function handler) throws X {
return (t, u) -> {
try {
handled.acceptChecked(t, u);
} catch (Throwable e) { // NOSONAR can't catch generic exceptions
throw asUnchecked(handler.apply(e));
}
};
}
/**
* Handles a generic exception type that may occur in the handled consumer by
* throwing it explicitly.
*
* @param
* The handled function's input type
* @param
* The handled function's input type
* @param
* The handled function's exception type
* @param handled
* The function that may throw an exception
*
* @return An exception-proofed consumer
*
* @throws E
* The checked exception
*/
@Nonnull
@SuppressWarnings("unused")
public static BiConsumer handleThrowing(@Nonnull EBiConsumer handled) throws E {
return (t, u) -> {
try {
handled.acceptChecked(t, u);
} catch (Throwable e) { // NOSONAR can't catch generic exceptions
throw asUnchecked(e);
}
};
}
/**
* Handles a generic exception type that may occur in the handled consumer with a
* provided handler.
*
* @param
* The handled function's input type
* @param
* The handled function's exception type
* @param handled
* The function that may throw an exception
* @param handler
* The function handling exceptions
*
* @return An exception-proofed consumer
*/
@Nonnull
public static ObjBooleanConsumer handle(@Nonnull EObjBooleanConsumer handled,
@Nonnull ObjObjBooleanConsumer handler) {
return (t, p) -> {
try {
handled.acceptChecked(t, p);
} catch (Throwable e) { // NOSONAR can't catch generic exceptions
handler.accept(e, t, p);
}
};
}
/**
* Handles a generic exception type that may occur in the handled consumer by
* throwing a different exception.
*
* @param
* The handled function's input type
* @param
* The handled function's exception type
* @param
* The exception type to throw
* @param handled
* The function that may throw an exception
* @param handler
* The function handling exceptions
*
* @return An exception-proofed consumer
*
* @throws X
* The exception returned by the handler
*/
@Nonnull
@SuppressWarnings({ "null", "unused" })
public static ObjBooleanConsumer handleThrowing(@Nonnull EObjBooleanConsumer handled,
@Nonnull Function handler) throws X {
return (t, p) -> {
try {
handled.acceptChecked(t, p);
} catch (Throwable e) { // NOSONAR can't catch generic exceptions
throw asUnchecked(handler.apply(e));
}
};
}
/**
* Handles a generic exception type that may occur in the handled consumer by
* throwing it explicitly.
*
* @param
* The handled function's input type
* @param
* The handled function's exception type
* @param handled
* The function that may throw an exception
*
* @return An exception-proofed consumer
*
* @throws E
* The checked exception
*/
@Nonnull
@SuppressWarnings("unused")
public static ObjBooleanConsumer handleThrowing(@Nonnull EObjBooleanConsumer handled) throws E {
return (t, p) -> {
try {
handled.acceptChecked(t, p);
} catch (Throwable e) { // NOSONAR can't catch generic exceptions
throw asUnchecked(e);
}
};
}
/**
* Handles a generic exception type that may occur in the handled consumer with a
* provided handler.
*
* @param
* The handled function's input type
* @param
* The handled function's exception type
* @param handled
* The function that may throw an exception
* @param handler
* The function handling exceptions
*
* @return An exception-proofed consumer
*/
@Nonnull
public static ObjByteConsumer handle(@Nonnull EObjByteConsumer handled,
@Nonnull ObjObjByteConsumer handler) {
return (t, p) -> {
try {
handled.acceptChecked(t, p);
} catch (Throwable e) { // NOSONAR can't catch generic exceptions
handler.accept(e, t, p);
}
};
}
/**
* Handles a generic exception type that may occur in the handled consumer by
* throwing a different exception.
*
* @param
* The handled function's input type
* @param
* The handled function's exception type
* @param
* The exception type to throw
* @param handled
* The function that may throw an exception
* @param handler
* The function handling exceptions
*
* @return An exception-proofed consumer
*
* @throws X
* The exception returned by the handler
*/
@Nonnull
@SuppressWarnings({ "null", "unused" })
public static ObjByteConsumer handleThrowing(@Nonnull EObjByteConsumer handled,
@Nonnull Function handler) throws X {
return (t, p) -> {
try {
handled.acceptChecked(t, p);
} catch (Throwable e) { // NOSONAR can't catch generic exceptions
throw asUnchecked(handler.apply(e));
}
};
}
/**
* Handles a generic exception type that may occur in the handled consumer by
* throwing it explicitly.
*
* @param
* The handled function's input type
* @param
* The handled function's exception type
* @param handled
* The function that may throw an exception
*
* @return An exception-proofed consumer
*
* @throws E
* The checked exception
*/
@Nonnull
@SuppressWarnings("unused")
public static ObjByteConsumer handleThrowing(@Nonnull EObjByteConsumer handled) throws E {
return (t, p) -> {
try {
handled.acceptChecked(t, p);
} catch (Throwable e) { // NOSONAR can't catch generic exceptions
throw asUnchecked(e);
}
};
}
/**
* Handles a generic exception type that may occur in the handled consumer with a
* provided handler.
*
* @param
* The handled function's input type
* @param
* The handled function's exception type
* @param handled
* The function that may throw an exception
* @param handler
* The function handling exceptions
*
* @return An exception-proofed consumer
*/
@Nonnull
public static ObjCharConsumer handle(@Nonnull EObjCharConsumer handled,
@Nonnull ObjObjCharConsumer handler) {
return (t, p) -> {
try {
handled.acceptChecked(t, p);
} catch (Throwable e) { // NOSONAR can't catch generic exceptions
handler.accept(e, t, p);
}
};
}
/**
* Handles a generic exception type that may occur in the handled consumer by
* throwing a different exception.
*
* @param
* The handled function's input type
* @param
* The handled function's exception type
* @param
* The exception type to throw
* @param handled
* The function that may throw an exception
* @param handler
* The function handling exceptions
*
* @return An exception-proofed consumer
*
* @throws X
* The exception returned by the handler
*/
@Nonnull
@SuppressWarnings({ "null", "unused" })
public static ObjCharConsumer handleThrowing(@Nonnull EObjCharConsumer handled,
@Nonnull Function handler) throws X {
return (t, p) -> {
try {
handled.acceptChecked(t, p);
} catch (Throwable e) { // NOSONAR can't catch generic exceptions
throw asUnchecked(handler.apply(e));
}
};
}
/**
* Handles a generic exception type that may occur in the handled consumer by
* throwing it explicitly.
*
* @param
* The handled function's input type
* @param
* The handled function's exception type
* @param handled
* The function that may throw an exception
*
* @return An exception-proofed consumer
*
* @throws E
* The checked exception
*/
@Nonnull
@SuppressWarnings("unused")
public static ObjCharConsumer handleThrowing(@Nonnull EObjCharConsumer handled) throws E {
return (t, p) -> {
try {
handled.acceptChecked(t, p);
} catch (Throwable e) { // NOSONAR can't catch generic exceptions
throw asUnchecked(e);
}
};
}
/**
* Handles a generic exception type that may occur in the handled consumer with a
* provided handler.
*
* @param
* The handled function's input type
* @param
* The handled function's exception type
* @param handled
* The function that may throw an exception
* @param handler
* The function handling exceptions
*
* @return An exception-proofed consumer
*/
@Nonnull
public static ObjIntConsumer handle(@Nonnull EObjIntConsumer handled,
@Nonnull ObjObjIntConsumer handler) {
return (t, p) -> {
try {
handled.acceptChecked(t, p);
} catch (Throwable e) { // NOSONAR can't catch generic exceptions
handler.accept(e, t, p);
}
};
}
/**
* Handles a generic exception type that may occur in the handled consumer by
* throwing a different exception.
*
* @param
* The handled function's input type
* @param
* The handled function's exception type
* @param
* The exception type to throw
* @param handled
* The function that may throw an exception
* @param handler
* The function handling exceptions
*
* @return An exception-proofed consumer
*
* @throws X
* The exception returned by the handler
*/
@Nonnull
@SuppressWarnings({ "null", "unused" })
public static ObjIntConsumer handleThrowing(@Nonnull EObjIntConsumer handled,
@Nonnull Function handler) throws X {
return (t, p) -> {
try {
handled.acceptChecked(t, p);
} catch (Throwable e) { // NOSONAR can't catch generic exceptions
throw asUnchecked(handler.apply(e));
}
};
}
/**
* Handles a generic exception type that may occur in the handled consumer by
* throwing it explicitly.
*
* @param
* The handled function's input type
* @param
* The handled function's exception type
* @param handled
* The function that may throw an exception
*
* @return An exception-proofed consumer
*
* @throws E
* The checked exception
*/
@Nonnull
@SuppressWarnings("unused")
public static ObjIntConsumer handleThrowing(@Nonnull EObjIntConsumer handled) throws E {
return (t, p) -> {
try {
handled.acceptChecked(t, p);
} catch (Throwable e) { // NOSONAR can't catch generic exceptions
throw asUnchecked(e);
}
};
}
/**
* Handles a generic exception type that may occur in the handled consumer with a
* provided handler.
*
* @param
* The handled function's input type
* @param
* The handled function's exception type
* @param handled
* The function that may throw an exception
* @param handler
* The function handling exceptions
*
* @return An exception-proofed consumer
*/
@Nonnull
public static ObjLongConsumer handle(@Nonnull EObjLongConsumer handled,
@Nonnull ObjObjLongConsumer handler) {
return (t, p) -> {
try {
handled.acceptChecked(t, p);
} catch (Throwable e) { // NOSONAR can't catch generic exceptions
handler.accept(e, t, p);
}
};
}
/**
* Handles a generic exception type that may occur in the handled consumer by
* throwing a different exception.
*
* @param
* The handled function's input type
* @param
* The handled function's exception type
* @param
* The exception type to throw
* @param handled
* The function that may throw an exception
* @param handler
* The function handling exceptions
*
* @return An exception-proofed consumer
*
* @throws X
* The exception returned by the handler
*/
@Nonnull
@SuppressWarnings({ "null", "unused" })
public static ObjLongConsumer handleThrowing(@Nonnull EObjLongConsumer handled,
@Nonnull Function handler) throws X {
return (t, p) -> {
try {
handled.acceptChecked(t, p);
} catch (Throwable e) { // NOSONAR can't catch generic exceptions
throw asUnchecked(handler.apply(e));
}
};
}
/**
* Handles a generic exception type that may occur in the handled consumer by
* throwing it explicitly.
*
* @param
* The handled function's input type
* @param
* The handled function's exception type
* @param handled
* The function that may throw an exception
*
* @return An exception-proofed consumer
*
* @throws E
* The checked exception
*/
@Nonnull
@SuppressWarnings("unused")
public static ObjLongConsumer handleThrowing(@Nonnull EObjLongConsumer handled) throws E {
return (t, p) -> {
try {
handled.acceptChecked(t, p);
} catch (Throwable e) { // NOSONAR can't catch generic exceptions
throw asUnchecked(e);
}
};
}
/**
* Handles a generic exception type that may occur in the handled consumer with a
* provided handler.
*
* @param
* The handled function's input type
* @param
* The handled function's exception type
* @param handled
* The function that may throw an exception
* @param handler
* The function handling exceptions
*
* @return An exception-proofed consumer
*/
@Nonnull
public static ObjShortConsumer handle(@Nonnull EObjShortConsumer handled,
@Nonnull ObjObjShortConsumer handler) {
return (t, p) -> {
try {
handled.acceptChecked(t, p);
} catch (Throwable e) { // NOSONAR can't catch generic exceptions
handler.accept(e, t, p);
}
};
}
/**
* Handles a generic exception type that may occur in the handled consumer by
* throwing a different exception.
*
* @param
* The handled function's input type
* @param
* The handled function's exception type
* @param
* The exception type to throw
* @param handled
* The function that may throw an exception
* @param handler
* The function handling exceptions
*
* @return An exception-proofed consumer
*
* @throws X
* The exception returned by the handler
*/
@Nonnull
@SuppressWarnings({ "null", "unused" })
public static ObjShortConsumer handleThrowing(@Nonnull EObjShortConsumer handled,
@Nonnull Function handler) throws X {
return (t, p) -> {
try {
handled.acceptChecked(t, p);
} catch (Throwable e) { // NOSONAR can't catch generic exceptions
throw asUnchecked(handler.apply(e));
}
};
}
/**
* Handles a generic exception type that may occur in the handled consumer by
* throwing it explicitly.
*
* @param
* The handled function's input type
* @param
* The handled function's exception type
* @param handled
* The function that may throw an exception
*
* @return An exception-proofed consumer
*
* @throws E
* The checked exception
*/
@Nonnull
@SuppressWarnings("unused")
public static ObjShortConsumer handleThrowing(@Nonnull EObjShortConsumer handled) throws E {
return (t, p) -> {
try {
handled.acceptChecked(t, p);
} catch (Throwable e) { // NOSONAR can't catch generic exceptions
throw asUnchecked(e);
}
};
}
/*
* ================== Functions ==================
*/
/**
* Handles a generic exception type that may occur in the handled function with a
* provided handler.
*
* @param
* The handled function's input type
* @param
* The handled function's return type
* @param
* The handled function's exception type
* @param handled
* The function that may throw an exception
* @param handler
* The function handling exceptions
*
* @return An exception-proofed function
*/
@Nonnull
public static Function handle(@Nonnull EFunction handled,
@Nonnull BiFunction handler) {
return t -> {
try {
return handled.applyChecked(t);
} catch (Throwable e) { // NOSONAR can't catch generic exceptions
return handler.apply(e, t);
}
};
}
/**
* Handles a generic exception type that may occur in the handled function by
* throwing a different exception.
*
* @param
* The handled function's input type
* @param
* The handled function's return type
* @param
* The handled function's exception type
* @param
* The exception type to throw
* @param handled
* The function that may throw an exception
* @param handler
* The function handling exceptions
*
* @return An exception-proofed function
*
* @throws X
* The exception returned by the handler
*/
@Nonnull
@SuppressWarnings({ "null", "unused" })
public static Function handleThrowing(@Nonnull EFunction handled,
@Nonnull Function handler) throws X {
return t -> {
try {
return handled.applyChecked(t);
} catch (Throwable e) { // NOSONAR can't catch generic exceptions
throw asUnchecked(handler.apply(e));
}
};
}
/**
* Handles a generic exception type that may occur in the handled function by
* throwing it explicitly.
*
* @param
* The handled function's input type
* @param
* The handled function's return type
* @param
* The handled function's exception type
* @param handled
* The function that may throw an exception
*
* @return An exception-proofed function
*
* @throws E
* The checked exception
*/
@Nonnull
@SuppressWarnings("unused")
public static Function handleThrowing(@Nonnull EFunction handled) throws E {
return t -> {
try {
return handled.applyChecked(t);
} catch (Throwable e) { // NOSONAR can't catch generic exceptions
throw asUnchecked(e);
}
};
}
/**
* Handles a generic exception type that may occur in the handled function with a
* provided handler.
*
* @param
* The handled function's return type
* @param
* The handled function's exception type
* @param handled
* The function that may throw an exception
* @param handler
* The function handling exceptions
*
* @return An exception-proofed function
*/
@Nonnull
public static BooleanFunction handle(@Nonnull EBooleanFunction handled,
@Nonnull ObjBooleanFunction handler) {
return p -> {
try {
return handled.applyChecked(p);
} catch (Throwable e) { // NOSONAR can't catch generic exceptions
return handler.apply(e, p);
}
};
}
/**
* Handles a generic exception type that may occur in the handled function by
* throwing a different exception.
*
* @param
* The handled function's return type
* @param
* The handled function's exception type
* @param
* The exception type to throw
* @param handled
* The function that may throw an exception
* @param handler
* The function handling exceptions
*
* @return An exception-proofed function
*
* @throws X
* The exception returned by the handler
*/
@Nonnull
@SuppressWarnings({ "null", "unused" })
public static BooleanFunction handleThrowing(@Nonnull EBooleanFunction