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

net.yetamine.lang.creational.Factory Maven / Gradle / Ivy

There is a newer version: 1.3.0
Show newest version
package net.yetamine.lang.creational;

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

/**
 * An interface for factories and builders.
 *
 * 

* For convenience this interface inherits from {@link Supplier} and the * inherited {@link #get()} method should be an alias to {@link #build()}. * * @param * the type of the product */ @FunctionalInterface public interface Factory extends Supplier { /** * Provides a new instance of the product. * *

* Technically, the result does not have to be a brand new instance if the * result is immutable and may be shared. However, the result must be such * an instance that it can be used by multiple clients without their mutual * interference. This is an additional requirement that the {@link #get()} * method does not impose. * * @return a new instance of the product */ T build(); /** * @see java.util.function.Supplier#get() */ default T get() { return build(); } /** * Makes a factory which uses a template object as the prototype for making * more instances. * * @param * the type of the product * @param template * the template for the builder. It must not be {@code null}. * @param builder * the builder which takes a template object and returns an * independent copy of it. It must not be {@code null}. * * @return a factory using a template object */ static Factory prototype(T template, Function builder) { Objects.requireNonNull(template); Objects.requireNonNull(builder); return () -> builder.apply(template); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy