All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.aol.cyclops.trycatch.Failure Maven / Gradle / Ivy

There is a newer version: 7.2.4
Show newest version
package com.aol.cyclops.trycatch;

import java.util.Arrays;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Stream;

import lombok.EqualsAndHashCode;
import lombok.RequiredArgsConstructor;
import lombok.ToString;

import com.aol.cyclops.invokedynamic.ExceptionSoftener;
import com.aol.cyclops.monad.AnyM;

/**
 * Class that represents the Failure of a Try
 * 
 * @author johnmcclean
 *
 * @param  Value type
 * @param  Error type
 */
@RequiredArgsConstructor @ToString @EqualsAndHashCode
public class Failure implements Try {

	@Override
	public > T unapply() {
		return (T)Arrays.asList(error);
	}
	private final X error;
	
	/**
	 * @return This monad, wrapped as AnyM of Success
	 */
	public AnyM anyM(){
		return this.anyMSuccess();
	}
	/**
	 * @return This monad, wrapped as AnyM of Failure
	 */
	public AnyM anyMFailure(){
		return AnyM.ofMonad(this);
	}
	/**
	 * @return This monad, wrapped as AnyM of Success
	 */
	public AnyM anyMSuccess(){
		return AnyM.fromOptional(Optional.empty());
	}
	/**
	 * Construct a Failure instance from a throwable
	 * 
	 * @param error for Failure
	 * @return new Failure with error
	 */
	public static  Failure of(X error){
		return new Failure<>(error);
	}
	/**
	 * Construct a Failure instance from a throwable
	 * 
	 * @param error for Failure
	 * @return new Failure with error
	 */
	public static  AnyM anyMOf(X error){
		return new Failure<>(error).anyMFailure();
	}
	/* 
	 *	@return throws an Exception
	 * @see com.aol.cyclops.trycatch.Try#get()
	 */
	public T get(){
		ExceptionSoftener.throwSoftenedException((Throwable)error);
		return null;
	}

	/* 
	 *	@return this
	 * @see com.aol.cyclops.trycatch.Try#map(java.util.function.Function)
	 */
	@Override
	public  Try map(Function fn) {
		return (Failure)this;
	}

	/* 
	 *	@return this
	 * @see com.aol.cyclops.trycatch.Try#flatMap(java.util.function.Function)
	 */
	@Override
	public  Try flatMap(Function> fn) {
		return (Try)this;
	}
	
	/* 
	 *	@return Empty optional
	 * @see com.aol.cyclops.trycatch.Try#filter(java.util.function.Predicate)
	 */
	@Override
	public Optional filter(Predicate p) {
		return Optional.empty();
	}
	
	/* 
	 * FlatMap recovery function if exception is of specified type
	 * 
	 * @param t Type of exception to match against
	 * @param fn Recovery FlatMap function. Map from a failure to a Success
	 * @return Success from recovery function
	 * @see com.aol.cyclops.trycatch.Try#recoverWithFor(java.lang.Class, java.util.function.Function)
	 */
	@Override
	public Try recoverWithFor(Class t,Function> fn){
		if(t.isAssignableFrom(error.getClass()))
			return recoverWith(fn);
		return this;
	}
	
	
	/* 
	 * Recover if exception is of specified type
	 * @param t Type of exception to match against
	 * @param fn Recovery function
	 * @return New Success
	 * @see com.aol.cyclops.trycatch.Try#recoverFor(java.lang.Class, java.util.function.Function)
	 */
	@Override
	public Try recoverFor(Class t,Function fn){
		if(t.isAssignableFrom(error.getClass()))
			return recover(fn);
		return this;
	}
	
	/* 
	 * @param fn Recovery function - map from a failure to a Success.
	 * @return new Success
	 * @see com.aol.cyclops.trycatch.Try#recover(java.util.function.Function)
	 */
	@Override
	public Success recover(Function fn) {
		return Success.of(fn.apply(error));
	}
	
	/* 
	 * flatMap recovery
	 * 
	 * @param fn Recovery FlatMap function. Map from a failure to a Success
	 * @return Success from recovery function
	 * @see com.aol.cyclops.trycatch.Try#recoverWith(java.util.function.Function)
	 */
	@Override
	public  Success recoverWith(Function> fn){
		return fn.apply(error);
	}
	/* 
	 * Flatten a nested Try Structure
	 * @return Lowest nested Try
	 * @see com.aol.cyclops.trycatch.Try#flatten()
	 */
	@Override
	public Try flatten() {
		return this;
	}
	/* 
	 *  @param value Return value supplied 
	 * @return  supplied value
	 * @see com.aol.cyclops.trycatch.Try#orElse(java.lang.Object)
	 */
	@Override
	public T orElse(T value) {
		return value;
	}
	/* 
	 * @param value from supplied Supplier 
	 * @return value from supplier
	 * @see com.aol.cyclops.trycatch.Try#orElseGet(java.util.function.Supplier)
	 */
	@Override
	public T orElseGet(Supplier value) {
		return value.get();
	}
	/* 
	 *	@return Optional.empty()
	 * @see com.aol.cyclops.trycatch.Try#toOptional()
	 */
	@Override
	public Optional toOptional() {
		return Optional.empty();
	}
	/* 
	 *	@return empty Stream
	 * @see com.aol.cyclops.trycatch.Try#toStream()
	 */
	@Override
	public Stream stream() {
		return Stream.of();
	}
	/* 
	 *	@return false
	 * @see com.aol.cyclops.trycatch.Try#isSuccess()
	 */
	@Override
	public boolean isSuccess() {
		return false;
	}
	/*  
	 *	@return true
	 * @see com.aol.cyclops.trycatch.Try#isFailure()
	 */
	@Override
	public boolean isFailure() {
		return true;
	}
	/* 
	 *	does nothing
	 * @see com.aol.cyclops.trycatch.Try#foreach(java.util.function.Consumer)
	 */
	@Override
	public void foreach(Consumer consumer) {
		
		
	}
	/* 
	 *	@param consumer is passed error
	 *	@return this
	 * @see com.aol.cyclops.trycatch.Try#onFail(java.util.function.Consumer)
	 */
	@Override
	public Try onFail(Consumer consumer) {
		consumer.accept(error);
		return this;
	}
	/* 
	 * @param t Class type of match Exception against
	 * @param consumer Accept Exception if present
	 * @return this
	 * @see com.aol.cyclops.trycatch.Try#onFail(java.lang.Class, java.util.function.Consumer)
	 */
	@Override
	public Try onFail(Class t, Consumer consumer) {
		if(t.isAssignableFrom(error.getClass()))
			consumer.accept(error);
		return this;
	}
	/* 
	 *	
	 * @see com.aol.cyclops.trycatch.Try#throwException()
	 */
	@Override
	public void throwException() {
		ExceptionSoftener.throwSoftenedException(error);
		
	}
	/* 
	 * @return Optional containing error
	 * @see com.aol.cyclops.trycatch.Try#toFailedOptional()
	 */
	@Override
	public Optional toFailedOptional() {
		
		return Optional.of(error);
	}
	/* 
	 *	@return Stream containing error
	 * @see com.aol.cyclops.trycatch.Try#toFailedStream()
	 */
	@Override
	public Stream toFailedStream() {
		return Stream.of(error);
	}
	/* 
	 * @param consumer that will accept error
	 * @see com.aol.cyclops.trycatch.Try#foreachFailed(java.util.function.Consumer)
	 */
	@Override
	public void foreachFailed(Consumer consumer) {
		consumer.accept(error);
		
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy