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 static javascalautils.TryCompanion.Try;

import java.io.Serializable;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * Represents the 'successful' implementation of {@link Try}. 
* 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 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 */ public Success(T value) { this.value = value; } /** * Always returns true. */ @Override public boolean isSuccess() { return true; } /** * Returns the value provided in the constructor.
* I.e. the provided supplier is never use. */ @Override public T getOrElse(Supplier supplier) { return value; } /** * Always returns this.
* I.e. the provided supplier is never use. */ @Override public Try orElse(Supplier> supplier) { return this; } /** * Returns the value provided in the constructor. */ @Override public T get() { return value; } /** * Returns a {@link Failure} with an {@link UnsupportedOperationException}.
* Since this is a 'success' it cannot be mapped to a 'failure' */ @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. */ @Override public Try map(Function function) { return Try(() -> function.apply(value)); } /** * Applies the value to the function and returns the {@link Try} generated by the function.. */ @Override public Try flatMap(Function> function) { return function.apply(value); } /** * Returns a String representation of the instance. */ @Override public String toString() { return "Success:" + (value != null ? value.toString() : "NULL VALUE"); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy