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

io.datarouter.httpclient.response.Conditional Maven / Gradle / Ivy

There is a newer version: 0.0.126
Show newest version
/**
 * Copyright © 2009 HotPads ([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 io.datarouter.httpclient.response;

import java.util.Optional;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;

/**
 * Response wrapper object that allows the caller to avoid try/catch blocks
 */
public class Conditional{

	private final boolean success;
	private final T response;
	private final Exception exception;

	private Conditional(boolean success, T response, Exception exception){
		this.success = success;
		this.response = response;
		this.exception = exception;
	}

	public static  Conditional success(T response){
		return new Conditional<>(true, response, null);
	}

	public static  Conditional failure(Exception exception){
		return new Conditional<>(false, null, exception);
	}

	public  Conditional map(Function mapper){
		if(success){
			return Conditional.success(mapper.apply(response));
		}
		return Conditional.failure(exception);
	}

	public T orElseThrow(){
		if(success){
			return response;
		}
		if(exception instanceof RuntimeException){
			throw (RuntimeException)exception;
		}
		throw new RuntimeException(exception);
	}

	public T orElseThrow(Function mapper){
		if(success){
			return response;
		}
		throw mapper.apply(exception);
	}

	public T orElse(T alternative){
		return success ? response : alternative;
	}

	public T orElseGet(Function alternative){
		if(success){
			return response;
		}
		return alternative.apply(exception);
	}

	public boolean isFailure(){
		return !success;
	}

	public void assertSuccess(){
		orElseThrow();
	}

	public void ifSuccess(Consumer consumer){
		if(success){
			consumer.accept(response);
		}
	}

	public Conditional peekFailure(Consumer consumer){
		if(!success){
			consumer.accept(exception);
		}
		return this;
	}

	public Exception getException(){
		return exception;
	}

	public  JoinedConditional join(Conditional other){
		return new JoinedConditional<>(this, other);
	}

	public static class JoinedConditional{

		private final Conditional first;
		private final Conditional second;

		private JoinedConditional(Conditional first, Conditional second){
			this.first = first;
			this.second = second;
		}

		public void ifSuccess(BiConsumer consumer){
			if(first.success && second.success){
				consumer.accept(first.response, second.response);
			}
		}

	}

	public Conditional validate(Function> validator){
		if(success){
			Optional invalidReason = validator.apply(response);
			if(invalidReason.isPresent()){
				return Conditional.failure(new Exception(invalidReason.get()));
			}
		}
		return this;
	}

}