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

com.enterprisemath.math.probability.ProbabilityDistrtibutionFunction Maven / Gradle / Ivy

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

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

import com.enterprisemath.math.fa.Function;
import com.enterprisemath.utils.ValidationUtils;

/**
 * This is adapter which converts any probability distribution to the function.
 *
 * @author radek.hecl
 *
 * @param  type of the domain
 */
public class ProbabilityDistrtibutionFunction implements Function {

    /**
     * Builder object.
     *
     * @param  type of the domain
     */
    public static class Builder {

        /**
         * Source probability distribution.
         */
        private ProbabilityDistribution source;

        /**
         * Sets source probability distribution.
         *
         * @param source source probability distribution
         * @return this instance
         */
        public Builder setSource(ProbabilityDistribution source) {
            this.source = source;
            return this;
        }

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

    /**
     * Source probability distribution.
     */
    private ProbabilityDistribution source;

    /**
     * Creates new instance.
     */
    private ProbabilityDistrtibutionFunction() {
    }

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

    /**
     * Guards this object to be consistent. Throws exception if this is not the case.
     */
    private void guardInvariants() {
        ValidationUtils.guardNotNull(source, "source cannot be null");
    }

    @Override
    public Double getValue(D x) {
        return source.getValue(x);
    }

    @Override
    public int hashCode() {
        return HashCodeBuilder.reflectionHashCode(this);
    }

    @Override
    public boolean equals(Object obj) {
        return EqualsBuilder.reflectionEquals(this, obj);
    }

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

    /**
     * Creates new instance.
     *
     * @param source source probability distribution
     * @return created function
     * @param  domain type
     */
    public static  ProbabilityDistrtibutionFunction create(ProbabilityDistribution source) {
        ProbabilityDistrtibutionFunction res = new ProbabilityDistrtibutionFunction();
        res.source = source;
        res.guardInvariants();
        return res;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy