
com.github.ljtfreitas.restify.util.Try Maven / Gradle / Ivy
The newest version!
/*******************************************************************************
*
* MIT License
*
* Copyright (c) 2016 Tiago de Freitas Lima
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*******************************************************************************/
package com.github.ljtfreitas.restify.util;
import java.util.function.Function;
import java.util.function.Supplier;
public interface Try {
public static Try of(TryableSupplier extends T> supplier) {
try {
return success(supplier.get());
} catch (Exception e) {
return failure(e);
}
}
public static Try run(TryableExpression expression) {
try {
expression.run();
return new Success<>(null);
} catch (Exception e) {
return new Failure<>(e);
}
}
public static TryWithResources withResources(TryableSupplier extends T> supplier) {
return new TryWithResources<>(supplier);
}
public static Try success(T value) {
return new Success<>(value);
}
public static Try failure(Throwable throwable) {
return new Failure<>(throwable);
}
public static void silently(TryableExpression... expressions) {
for (TryableExpression expression : expressions) {
try {
expression.run();
} catch (Exception e) {
}
}
}
public Try map(TryableFunction mapper);
public Try flatMap(Function> mapper);
public Try apply(TryableConsumer consumes);
public Try error(Function super Throwable, ? extends Throwable> mapper);
public Try recover(Function super Throwable, Try extends T>> mapper);
public Try recover(Class type, Function> mapper);
public T or(Supplier mapper);
public T get();
public void apply();
public class Success implements Try {
private final T value;
private Success(T value) {
this.value = value;
}
@Override
public Try map(TryableFunction mapper) {
try {
return new Success<>(mapper.apply(value));
} catch (Exception e) {
return new Failure<>(e);
}
}
@SuppressWarnings("unchecked")
@Override
public Try flatMap(Function> mapper) {
return (Try) mapper.apply(value);
}
@Override
public Try apply(TryableConsumer consumes) {
try {
consumes.accept(value);
return this;
} catch (Exception e) {
return new Failure<>(e);
}
}
@Override
public Try error(Function super Throwable, ? extends Throwable> mapper) {
return this;
}
@Override
public Try recover(Function super Throwable, Try extends T>> mapper) {
return this;
}
@Override
public Try recover(Class type, Function> mapper) {
return this;
}
@Override
public T or(Supplier mapper) {
return value;
}
@Override
public T get() {
return value;
}
@Override
public void apply() {
}
}
public class Failure implements Try {
private final Throwable cause;
private Failure(Throwable cause) {
this.cause = cause;
}
@SuppressWarnings("unchecked")
@Override
public Try map(TryableFunction mapper) {
return (Try) this;
}
@SuppressWarnings("unchecked")
@Override
public Try flatMap(Function> mapper) {
return (Try) this;
}
@Override
public Try apply(TryableConsumer consumes) {
return this;
}
@Override
public Try error(Function super Throwable, ? extends Throwable> mapper) {
return new Failure<>(mapper.apply(cause));
}
@SuppressWarnings("unchecked")
@Override
public Try recover(Function super Throwable, Try extends T>> mapper) {
return (Try) mapper.apply(cause);
}
@SuppressWarnings("unchecked")
@Override
public Try recover(Class type, Function> mapper) {
if (type.isAssignableFrom(cause.getClass())) {
return (Try) mapper.apply((E) cause);
} else {
return this;
}
}
@Override
public T or(Supplier mapper) {
return mapper.get();
}
@Override
public T get() {
throw (cause instanceof RuntimeException) ? (RuntimeException) cause : new RuntimeException(cause);
}
@Override
public void apply() {
throw (cause instanceof RuntimeException) ? (RuntimeException) cause : new RuntimeException(cause);
}
}
public class TryWithResources {
private final TryableSupplier extends T> supplier;
public TryWithResources(TryableSupplier extends T> supplier) {
this.supplier = supplier;
}
public Try apply(TryableConsumer super T> consumer) {
return Try.of(() -> {
try (T closeable = supplier.get()) {
consumer.accept(closeable);
return closeable;
}
});
}
public Try map(TryableFunction mapper) {
return Try.of(() -> {
try (T closeable = supplier.get()) {
return mapper.apply(closeable);
}
});
}
}
public interface TryableSupplier {
T get() throws Exception;
}
public interface TrowableSupplier {
T get() throws Throwable;
}
public interface TryableConsumer {
void accept(T t) throws Exception;
}
public interface TryableFunction {
R apply(T t) throws Exception;
}
public interface TryableExpression {
void run() throws Exception;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy