net.markenwerk.commons.interfaces.Provider Maven / Gradle / Ivy
Show all versions of commons-interfaces Show documentation
/*
* Copyright (c) 2015, 2016 Torsten Krause, Markenwerk GmbH
*
* 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 net.markenwerk.commons.interfaces;
import java.util.Iterator;
import net.markenwerk.commons.exceptions.CreationException;
/**
* A {@link Provider} provides values of the corresponding product type.
*
*
* Implementers may provide a new instance of the product each time
* {@link Provider#create()} is called, but aren't required to do so. An
* instance of the product that has already been returned once, may be returned
* again for any or all following calls.
*
*
* It is therefore not recommended to use {@link Provider Provides} for products
* that are stateful unless the internal state of a values doesn't change the
* value. It is for example okay to use {@link String} as the product type,
* although instances are stateful ({@link String} instances store their hash
* value after it has been calculated for the first time and are therefore
* technically stateful), but it is not okay to use {@link Iterator} as the
* product type, because every call to {@link Iterator#next()} actually changes
* the {@link Iterator} instance.
*
*
* {@link Provider Providers} are intended to be used in situation, where a
* mechanism to retrieve a value is more desirable than having the value from
* the start. These are usually, but not necessarily, situations where the
* following two conditions are met.
*
*
* - It is not certain that the value will be used.
* - It is likely that it is a costly operation to create the value.
*
*
*
* The second condition may only be true for the first call to
* {@link Provider#create()} since {@link Provider Providers} are allowed to
* cache and reuse the value.
*
* @param
* The type of the values to be provided.
* @since 1.0.0
* @author Torsten Krause (tk at markenwerk dot net)
* @see Producer
*/
public interface Provider {
/**
* Provides a product, This may be a costly operation.
*
*
* Implementers may provide a new instance of the product each time this
* method is called, but aren't required to do so. An instance of the
* product that has already been returned once, may be returned again in any
* or all following calls.
*
*
* It lies in the responsibility of the caller, to handle unwanted
* {@literal null}-values by replacing them with a sensible default value or
* throwing a {@link NullPointerException}.
*
*
* Implementers should catch any exception and wrap them in a
* {@link CreationException}.
*
* @return The provided product.
* @throws CreationException
* If the provisioning of the product failed.
*/
public Product create() throws CreationException;
}