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

com.enterprisemath.math.fa.ConstantFunction Maven / Gradle / Ivy

The newest version!
package com.enterprisemath.math.fa;

import org.apache.commons.lang3.builder.ToStringBuilder;

/**
 * Constant function. This is the function which returns same value across all domain.
 *
 * @author radek.hecl
 *
 * @param  type for the function domain
 * @param  type for the function range
 */
public class ConstantFunction implements Function {

    /**
     * Builder object.
     *
     * @param  type for the function domain
     * @param  type for the function range
     */
    public static class Builder {

        /**
         * Value of this function.
         */
        private R value;

        /**
         * Sets the value of this function.
         *
         * @param value value object
         * @return this instance
         */
        public Builder setValue(R value) {
            this.value = value;
            return this;
        }

        /**
         * Builds the result object.
         *
         * @return created object
         */
        public ConstantFunction build() {
            return new ConstantFunction(this);
        }
    }

    /**
     * Value for this function.
     */
    private R value;

    /**
     * Creates new instance.
     *
     * @param builder builder object
     */
    public ConstantFunction(Builder builder) {
        value = builder.value;
        guardInvariants();
    }

    /**
     * Guards this object to be consistent. Throws exception if this is not the case.
     */
    private void guardInvariants() {
    }

    @Override
    public R getValue(D x) {
        return value;
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }

    /**
     * Creates new instance of constant function.
     *
     * @param  type for the function domain
     * @param  type for the function range
     * @param value constant value
     * @return created function
     */
    public static  ConstantFunction create(R value) {
        return new ConstantFunction.Builder().
                setValue(value).
                build();
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy