com.github.TKnudsen.ComplexDataObject.data.interval.NumberInterval Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of complex-data-object Show documentation
Show all versions of complex-data-object Show documentation
A library that models real-world objects in Java, referred to as ComplexDataObjects. Other features: IO and preprocessing of ComplexDataObjects.
The newest version!
package com.github.TKnudsen.ComplexDataObject.data.interval;
/**
*
* ComplexDataObject
*
*
*
* Stores a numerical interval defined by a start and an end value. The start
* value must not be larger than the end value.
*
*
*
* Copyright (c) 2016-2019
*
*
* @author Juergen Bernard
* @version 1.03
*/
public class NumberInterval {
protected Number start;
protected Number end;
protected String name;
protected NumberInterval() {
}
public NumberInterval(Number start, Number end) {
this(start, end, "");
}
public NumberInterval(Number start, Number end, String name) {
this.start = start;
this.end = end;
if (end.doubleValue() < start.doubleValue())
throw new IllegalArgumentException(getName() + ": start value must not be larger than end value");
this.name = name;
}
public boolean contains(Number value) {
if (value.doubleValue() >= start.doubleValue() && value.doubleValue() <= end.doubleValue())
return true;
else
return false;
}
public Number getDuration() {
return Math.abs(getEnd().doubleValue() - getStart().doubleValue());
}
public Number getStart() {
return start;
}
public void setStart(Number start) {
this.start = start;
}
public Number getEnd() {
return end;
}
public void setEnd(Number end) {
this.end = end;
}
@Override
public int hashCode() {
int hash = 17;
hash = 29 * hash + start.hashCode();
hash = 29 * hash + end.hashCode();
return hash;
}
@Override
public boolean equals(Object obj) {
if (obj == null)
return false;
if (!(obj instanceof NumberInterval))
return false;
NumberInterval that = (NumberInterval) obj;
return this.hashCode() == that.hashCode();
}
@Override
public String toString() {
return "Interval: [" + start + "-" + end + "]";
}
public String getName() {
return name;
}
}