com.enterprisemath.math.algebra.Interval Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of em-math Show documentation
Show all versions of em-math Show documentation
Advanced mathematical algorithms.
The newest version!
package com.enterprisemath.math.algebra;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;
import com.enterprisemath.utils.ValidationUtils;
/**
* This class represents interval. Interval is cube in R^1 space.
* Interval is closed, therefore boundaries are also part of the intervals.
*
* @author radek.hecl
*
*/
public final class Interval {
/**
*
* Builder object.
*
*/
public static class Builder {
/**
* Minimum value.
*/
private Double min = null;
/**
* Maximum value.
*/
private Double max = null;
/**
* Sets minimum.
*
* @param min minimum value
* @return this instance
*/
public Builder setMin(double min) {
this.min = min;
return this;
}
/**
* Sets maximum value.
*
* @param max maximum value
* @return this instance
*/
public Builder setMax(double max) {
this.max = max;
return this;
}
/**
* Adds point.
* Meaning is that boundaries are extended in the way
* the specified point will be inside.
*
* @param point point
* @return this instance
*/
public Builder addPoint(double point) {
if (min == null) {
min = point;
max = point;
}
else {
min = Math.min(min, point);
max = Math.max(max, point);
}
return this;
}
/**
* Adds sub interval.
* Meaning is that boundaries are extended in the way
* the specified sub interval will be inside.
*
* @param subInterval interval
* @return this instance
*/
public Builder addSubInterval(Interval subInterval) {
addPoint(subInterval.getMin());
addPoint(subInterval.getMax());
return this;
}
/**
* Builds the result object.
*
* @return created object
*/
public Interval build() {
return new Interval(this);
}
}
/**
* Vector with minimum values.
*/
private double min;
/**
* Vector with maximum values.
*/
private double max;
/**
* Creates new instance.
*/
private Interval() {
}
/**
* Creates new instance.
*
* @param builder builder
*/
public Interval(Builder builder) {
min = builder.min;
max = builder.max;
guardInvariants();
}
/**
* Guards this object to be consistent. Throws exception if this is not the case.
*/
private void guardInvariants() {
ValidationUtils.guardGreaterOrEqualDouble(max, min, "max must be greater or equal than min");
}
/**
* Returns minimum.
*
* @return minimum
*/
public double getMin() {
return min;
}
/**
* Returns maximum.
*
* @return maximum
*/
public double getMax() {
return max;
}
/**
* Returns the center of this interval.
*
* @return center of this interval
*/
public double getCenter() {
return (min + max) / 2;
}
/**
* Returns size of this interval.
*
* @return size
*/
public double getSize() {
return max - min;
}
/**
* Returns whether this interval contains the specified point or not.
* Border is considered as inside, because interval is closed.
*
* @param x tested point
* @return true if this interval contains specified point, false otherwise
*/
public boolean contains(double x) {
return min <= x && x <= max;
}
@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 interval.
*
* @param min minimum value
* @param max maximum value
* @return created interval
*/
public static Interval create(double min, double max) {
Interval res = new Interval();
res.min = min;
res.max = max;
res.guardInvariants();
return res;
}
/**
* Creates interval from a given points.
*
* @param points points
* @return created hypercube
*/
public static Interval createFromPoints(Double... points) {
Interval.Builder builder = new Interval.Builder();
for (int i = 0; i < points.length; ++i) {
builder.addPoint(points[i]);
}
return builder.build();
}
}