001package org.opencv.core;
002
003//javadoc:Range
004public class Range {
005
006    public int start, end;
007
008    public Range(int s, int e) {
009        this.start = s;
010        this.end = e;
011    }
012
013    public Range() {
014        this(0, 0);
015    }
016
017    public Range(double[] vals) {
018        set(vals);
019    }
020
021    public void set(double[] vals) {
022        if (vals != null) {
023            start = vals.length > 0 ? (int) vals[0] : 0;
024            end = vals.length > 1 ? (int) vals[1] : 0;
025        } else {
026            start = 0;
027            end = 0;
028        }
029
030    }
031
032    public int size() {
033        return empty() ? 0 : end - start;
034    }
035
036    public boolean empty() {
037        return end <= start;
038    }
039
040    public static Range all() {
041        return new Range(Integer.MIN_VALUE, Integer.MAX_VALUE);
042    }
043
044    public Range intersection(Range r1) {
045        Range r = new Range(Math.max(r1.start, this.start), Math.min(r1.end, this.end));
046        r.end = Math.max(r.end, r.start);
047        return r;
048    }
049
050    public Range shift(int delta) {
051        return new Range(start + delta, end + delta);
052    }
053
054    public Range clone() {
055        return new Range(start, end);
056    }
057
058    @Override
059    public int hashCode() {
060        final int prime = 31;
061        int result = 1;
062        long temp;
063        temp = Double.doubleToLongBits(start);
064        result = prime * result + (int) (temp ^ (temp >>> 32));
065        temp = Double.doubleToLongBits(end);
066        result = prime * result + (int) (temp ^ (temp >>> 32));
067        return result;
068    }
069
070    @Override
071    public boolean equals(Object obj) {
072        if (this == obj) return true;
073        if (!(obj instanceof Range)) return false;
074        Range it = (Range) obj;
075        return start == it.start && end == it.end;
076    }
077
078    @Override
079    public String toString() {
080        return "[" + start + ", " + end + ")";
081    }
082}