1
1
package org.helioviewer.base.math;
4
* An immutable Interval class, used for integer ranges. Both end points are
4
* An immutable Interval class, used for integer ranges. Both end points are
5
5
* considered to be included in the interval (open interval).
9
10
public class Interval {
11
/** The left side of the interval. Guaranteed that leftSide <= rightSide. */
12
public final int leftSide;
14
/** The right side of the interval. Guaranteed that leftSide <= rightSide. */
15
public final int rightSide;
18
public final int width;
21
* Simple constructor that takes two points on the number line.
25
public Interval(int _a, int _b){
26
leftSide = Math.min(_a, _b);
27
rightSide = Math.max(_a, _b);
28
width = rightSide - leftSide;
31
/** Tests to see if the value is contained on the interval. */
32
public boolean containsValue(Number _val) {return (leftSide<=_val.doubleValue() && _val.doubleValue()<=rightSide);}
34
/** Returns the number of integers contained in the interval */
35
public int getNumValues() {return rightSide-leftSide+1;}
37
/** Attempts to map the value to the interval, 'squeezing' it if necessary. */
38
public int squeeze(int _val) {
41
else if(_val < leftSide)
47
/** Overridden equals method */
48
public boolean equals(Object _obj){
49
if(_obj == null) return false;
50
else if(!(_obj instanceof Interval)) return false;
52
Interval i = Interval.class.cast(_obj);
53
return (i.leftSide == leftSide && i.rightSide == rightSide);
57
/** Overridden toString method */
58
public String toString(){
59
String ret = "[Interval=";
60
ret += "[leftSize="+leftSide+"]";
61
ret += "[rightSize="+rightSide+"]";
62
ret += "[width="+width+"]";
12
/** The left side of the interval. Guaranteed that leftSide <= rightSide. */
13
public final int leftSide;
15
/** The right side of the interval. Guaranteed that leftSide <= rightSide. */
16
public final int rightSide;
18
public final int width;
21
* Simple constructor that takes two points on the number line.
26
public Interval(int _a, int _b) {
27
leftSide = Math.min(_a, _b);
28
rightSide = Math.max(_a, _b);
29
width = rightSide - leftSide;
32
/** Tests to see if the value is contained on the interval. */
33
public boolean containsValue(Number _val) {
34
return (leftSide <= _val.doubleValue() && _val.doubleValue() <= rightSide);
37
/** Returns the number of integers contained in the interval */
38
public int getNumValues() {
39
return rightSide - leftSide + 1;
42
/** Attempts to map the value to the interval, 'squeezing' it if necessary. */
43
public int squeeze(int _val) {
46
else if (_val < leftSide)
52
/** Overridden equals method */
53
public boolean equals(Object _obj) {
56
else if (!(_obj instanceof Interval))
59
Interval i = Interval.class.cast(_obj);
60
return (i.leftSide == leftSide && i.rightSide == rightSide);
64
/** Overridden toString method */
65
public String toString() {
66
String ret = "[Interval=";
67
ret += "[leftSize=" + leftSide + "]";
68
ret += "[rightSize=" + rightSide + "]";
69
ret += "[width=" + width + "]";
b'\\ No newline at end of file'