teichmann@5863: /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde teichmann@5863: * Software engineering by Intevation GmbH teichmann@5863: * teichmann@5994: * This file is Free Software under the GNU AGPL (>=v3) teichmann@5863: * and comes with ABSOLUTELY NO WARRANTY! Check out the teichmann@5994: * documentation coming with Dive4Elements River for details. teichmann@5863: */ teichmann@5863: teichmann@5831: package org.dive4elements.river.artifacts.model; sascha@3138: sascha@3138: import java.io.Serializable; sascha@3138: felix@4334: /** A range from ... to .*/ sascha@3138: public class Range implements Serializable { sascha@3138: felix@5636: public static final double EPSILON = 1e-5; sascha@3138: sascha@3138: protected double start; sascha@3138: protected double end; sascha@3138: sascha@3138: public Range() { sascha@3138: } sascha@3138: sascha@3401: public Range(Range other) { sascha@3401: start = other.start; sascha@3401: end = other.end; sascha@3401: } sascha@3401: sascha@3138: public Range(double start, double end) { sascha@3138: this.start = start; sascha@3138: this.end = end; sascha@3138: } sascha@3138: felix@6380: public void setStart(double start) { felix@6380: this.start = start; felix@6380: } felix@6380: sascha@3138: public double getStart() { sascha@3138: return start; sascha@3138: } sascha@3138: felix@6380: felix@6380: public void setEnd(double end) { felix@6380: this.end = end; felix@6380: } felix@6380: sascha@3138: public double getEnd() { sascha@3138: return end; sascha@3138: } sascha@3138: sascha@3743: public boolean disjoint(double ostart, double oend) { sascha@3743: return start > oend || ostart > end; sascha@3743: } sascha@3743: sascha@3138: public boolean disjoint(Range other) { sascha@3138: return start > other.end || other.start > end; sascha@3138: } sascha@3138: sascha@3138: public boolean intersects(Range other) { sascha@3138: return !disjoint(other); sascha@3138: } sascha@3138: sascha@3138: public void extend(Range other) { sascha@3138: if (other.start < start) start = other.start; sascha@3138: if (other.end > end ) end = other.end; sascha@3138: } sascha@3138: sascha@3138: public boolean clip(Range other) { sascha@3138: if (disjoint(other)) return false; sascha@3138: sascha@3138: if (other.start > start) start = other.start; sascha@3138: if (other.end < end ) end = other.end; sascha@3138: sascha@3138: return true; sascha@3138: } sascha@3138: felix@4334: /** True if start>x start-EPSILON && x < end+EPSILON; sascha@3138: } felix@6372: felix@6380: public boolean contains(double x) { felix@6380: return inside(x); felix@6380: } felix@6380: felix@6380: felix@6380: /** Hash Code. */ felix@6372: @Override felix@6372: public int hashCode() { felix@6372: return new Double(this.start).hashCode() ^ felix@6372: new Double(this.end).hashCode(); felix@6372: } felix@6372: felix@6380: felix@6380: /** felix@6380: * Compares start and end values with some epsilon. felix@6380: */ felix@6372: @Override felix@6372: public boolean equals(Object otherRange) { teichmann@6378: if (otherRange instanceof Range) { felix@6372: Range oRange = (Range) otherRange; felix@6372: return felix@6372: Math.abs(oRange.start - this.start) <= EPSILON felix@6372: && Math.abs(oRange.end - this.end) <= EPSILON; felix@6372: } felix@6372: return false; felix@6372: } felix@6380: felix@6380: /** Returns clone with same start and end values. */ felix@6380: @Override felix@6380: public Object clone() { felix@6380: return new Range(this.start, this.end); felix@6380: } teichmann@6494: teichmann@6494: public String toString() { teichmann@6494: return "[Range: start=" + start + " end=" + end + "]"; teichmann@6494: } sascha@3138: } sascha@3138: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :