com.softicar.platform.common.math.range.LongRange Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of platform-common Show documentation
Show all versions of platform-common Show documentation
The SoftiCAR Platform is a lightweight, Java-based library to create interactive business web applications.
package com.softicar.platform.common.math.range;
import com.softicar.platform.common.core.exceptions.SofticarDeveloperException;
/**
* Represents a range, based on the primitive type long.
*
* @author Oliver Richers
*/
public class LongRange {
private final long begin;
private final long end;
public LongRange(long begin, long end) {
this.begin = begin;
this.end = end;
}
public boolean isEmpty() {
return begin == end;
}
public boolean isValid() {
return begin <= end;
}
public LongRange getIntersection(LongRange other) {
long begin = Math.max(this.begin, other.begin);
long end = Math.min(this.end, other.end);
return new LongRange(begin, end);
}
public long size() {
if (!isValid()) {
throw new SofticarDeveloperException("Tried to compute size of an invalid range.");
}
return end - begin;
}
}