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

javascalautils.Success Maven / Gradle / Ivy

There is a newer version: 1.11.2
Show newest version
/**
 * Copyright 2015 Peter Nerg
 *
 * 

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 javascalautils; import java.io.Serializable; import java.util.NoSuchElementException; import java.util.function.Predicate; import java.util.function.Supplier; import static javascalautils.TryCompanion.Try; /** * Represents the successful implementation of {@link Try}.
* Acts as a carrier for the result of a successful computation.
* For examples on usage refer to the documentation for {@link Try}. * * @author Peter Nerg * @since 1.0 * @param The type of the value represented by this instance */ public final class Success extends SingleItemContainer implements Try, Serializable { private static final long serialVersionUID = -5588727176438040926L; private final T value; /** * Creates a 'successful' instance with the provided value.
* Null values are allowed. * * @param value The value represented by this instance * @since 1.0 */ public Success(T value) { this.value = value; } /** * Always returns true. * * @since 1.0 */ @Override public boolean isSuccess() { return true; } /** * Returns the value provided in the constructor.
* I.e. the provided supplier is never use. * * @since 1.0 */ @Override public T getOrElse(Supplier supplier) { return value; } /** * Always returns this.
* I.e. the provided supplier is never use. * * @since 1.0 */ @Override public Try orElse(Supplier> supplier) { return this; } /** * Returns the value held by this instance.
* Never throws an exception as this is a success * * @since 1.0 */ @Override public T get() { return value; } /** * Returns a {@link Failure} with an {@link UnsupportedOperationException}.
* Since this is a success there is no failure to map to a success i.e. this * is a illegal operation. * * @since 1.0 */ @Override public Try failed() { return new Failure<>(new UnsupportedOperationException("Cannot make a Failure of a Success")); } /** * Applies the value to the function and returns the {@link Try} representing the mapped value. * * @since 1.0 */ @Override public Try map(ThrowableFunction1 function) { return Try(() -> function.apply(value)); } /** * Applies the value to the function and returns the {@link Try} generated by the function. * * @since 1.2 */ @Override public Try flatMap(ThrowableFunction1> function) { try { return function.apply(value); } catch (Throwable t) { return new Failure<>(t); } } /** * Applies the predicate to the value of this instance, if it matches this is returned else * a {@link Failure}. * * @since 1.4 */ @Override public Try filter(Predicate predicate) { return predicate.test(value) ? this : new Failure<>( new NoSuchElementException("Predicate did not match value, now empty result")); } /** * Always returns this .
* As per definition this is a success and will not need to be recovered. * * @since 1.4 */ @Override public Try recover(ThrowableFunction1 function) { return this; } /** * Always returns this .
* As per definition this is a success and will not need to be recovered. * * @since 1.4 */ @Override public Try recoverWith(ThrowableFunction1> function) { return this; } /** * Returns a String representation of the instance. * * @since 1.0 */ @Override public String toString() { return String.format("Success(%s)", value != null ? value.toString() : "NULL VALUE"); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy