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

com.jongsoft.utils.control.Optional Maven / Gradle / Ivy

The newest version!
/*
 * The MIT License
 *
 * Copyright 2016 Jong Soft.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package com.jongsoft.utils.control;

import com.jongsoft.utils.core.Mappable;
import com.jongsoft.utils.core.OrElse;
import com.jongsoft.utils.core.Value;

import java.util.Objects;
import java.util.function.Function;
import java.util.function.Supplier;

public interface Optional extends Value, Mappable, OrElse {
    
    @Override
     Optional map(Function mapper);

    /**
     * This method will provide the entity contained within the {@link Optional}, in case no entity
     * is present the {@link Supplier} is called to create the else situation.
     *
     * @param supplier  the supplier to create an entity of none is contained in the {@link Optional}
     * @return          either the contained entity or the created one using the {@link Supplier}
     */
    T getOrSupply(Supplier supplier);

    /**
     * This method will return the contained entity within the {@link Optional}, in case no entity
     * is present an exception will be thrown using the provided exceptionSupplier.
     *
     * @param exceptionSupplier     the {@link Supplier} used to create the {@link Throwable}
     * @param                    the type of the Throwable that will be thrown
     * @return                      the entity contained within the {@link Optional}
     * @throws X                    in case of no entity
     * @throws NullPointerException in case the exceptionSupplier was null
     */
     T getOrThrow(Supplier exceptionSupplier) throws X;
    
    //----------------------------------------------------------------------------------------------
    //-- All static helper methods to instantiate the Optional class
    
    /**
     * Creates a new {@link Optional} representing the provided value.
     * 

* This method will throw an {@link NullPointerException} when the provided value is null. * * @param the type of the value * @param value the actual value * * @return an {@link Optional} representing the value */ static Optional of(T value) { Objects.requireNonNull(value, "Value cannot be null"); return new OptionalSome<>(value); } /** * Creates a new {@link Optional} representing the provided value. *

* This method will allow null values. * * @param the type of the value * @param value the actual value * * @return an {@link Optional} representing the value */ @SuppressWarnings("unchecked") static Optional ofNullable(T value) { return value != null ? new OptionalSome<>(value) : (Optional) OptionalNone.NONE; } /** * Creates a default empty {@link Optional}. * * @param the type of the value * @return a default optional with a null value */ @SuppressWarnings("unchecked") static Optional empty() { return (Optional) OptionalNone.NONE; } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy