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

aQute.bnd.result.Err Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2017 René Perschon 
 *
 * 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 aQute.bnd.result;

import static java.util.Objects.requireNonNull;

import java.util.Optional;

import aQute.bnd.exceptions.ConsumerWithException;
import aQute.bnd.exceptions.Exceptions;
import aQute.bnd.exceptions.FunctionWithException;
import aQute.bnd.exceptions.SupplierWithException;

/**
 * This class represents the Err side of @{link Result}.
 *
 * @param  The value type
 */
final class Err implements Result {
	private final String error;

	Err(CharSequence error) {
		this.error = requireNonNull(error.toString());
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public boolean isOk() {
		return false;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public boolean isErr() {
		return true;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public Optional value() {
		return Optional.empty();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public Optional error() {
		return Optional.of(error);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public V unwrap() {
		throw new ResultException(error);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public V unwrap(CharSequence message) throws ResultException {
		throw new ResultException(message.toString());
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public V orElse(V orElse) {
		return orElse;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public V orElseGet(SupplierWithException orElseSupplier) {
		try {
			return orElseSupplier.get();
		} catch (Exception e) {
			throw Exceptions.duck(e);
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public  V orElseThrow(FunctionWithException throwableSupplier)
		throws R {
		requireNonNull(throwableSupplier);
		R r;
		try {
			r = requireNonNull(throwableSupplier.apply(error));
		} catch (Exception e) {
			throw Exceptions.duck(e);
		}
		throw r;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public  Result map(FunctionWithException mapper) {
		requireNonNull(mapper);
		return asError();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public Result mapErr(FunctionWithException mapper) {
		try {
			return Result.err(mapper.apply(error));
		} catch (Exception e) {
			throw Exceptions.duck(e);
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public  Result flatMap(FunctionWithException> mapper) {
		requireNonNull(mapper);
		return asError();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public Result recover(FunctionWithException recover) {
		try {
			V v = recover.apply(error);
			return (v != null) ? Result.ok(v) : this;
		} catch (Exception e) {
			throw Exceptions.duck(e);
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public Result recoverWith(FunctionWithException> recover) {
		try {
			@SuppressWarnings("unchecked")
			Result result = (Result) requireNonNull(recover.apply(error));
			return result;
		} catch (Exception e) {
			throw Exceptions.duck(e);
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void accept(ConsumerWithException ok, ConsumerWithException err) {
		requireNonNull(ok);
		try {
			err.accept(error);
		} catch (Exception e) {
			throw Exceptions.duck(e);
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public  Result asError() {
		@SuppressWarnings("unchecked")
		Result coerced = (Result) this;
		return coerced;
	}

	@Override
	public String toString() {
		return String.format("Err(%s)", error);
	}
}