com.jongsoft.lang.control.OrElse Maven / Gradle / Ivy
/*
* The MIT License
*
* Copyright 2016-2018 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.lang.control;
import java.util.function.Supplier;
/**
* The OrElse interface is an extension to the {@link com.jongsoft.lang.control.Optional} interface. This interface can be used to cascade behaviour
* when an entity is or is not present in the wrapper.
*
* For example if the {@link com.jongsoft.lang.control.Optional} has no entity contained within then the OrElse can be used to throw an exception
* or run alternative logic.
*
*
* Example:
* {@code API.Option(null)
* .ifPresent(System.out::println)
* .elseThrow(() -> new NullPointerException("No value present"))
* }
*
* @since 0.0.1
*/
public interface OrElse {
/**
* This operation will run when the precondition of this OrElse is not met.
*
* @param runner the code to be run when the else is running
*/
default void elseRun(Runnable runner) {}
/**
* This operation will create an exception using the provided {@link Supplier} and throw it.
*
* @param exceptionSupplier the supplier to create the exception
* @param the type of exception
* @throws X the exception in case of an OrElse run
*/
default void elseThrow(Supplier exceptionSupplier) throws X {}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy