sascha@167: package de.intevation.flys.model; sascha@167: sascha@167: import java.io.Serializable; sascha@167: sascha@167: import java.math.BigDecimal; sascha@167: sascha@171: import javax.persistence.Entity; sascha@171: import javax.persistence.Id; sascha@171: import javax.persistence.Table; sascha@171: import javax.persistence.GeneratedValue; sascha@171: import javax.persistence.Column; sascha@171: import javax.persistence.SequenceGenerator; sascha@171: import javax.persistence.GenerationType; sascha@174: import javax.persistence.JoinColumn; sascha@174: import javax.persistence.OneToOne; sascha@171: sascha@171: @Entity sascha@171: @Table(name = "ranges") sascha@167: public class Range sascha@167: implements Serializable sascha@167: { sascha@171: private Integer id; sascha@167: private BigDecimal a; sascha@167: private BigDecimal b; sascha@167: sascha@167: private River river; sascha@167: sascha@167: public Range() { sascha@167: } sascha@167: sascha@204: public Range(double a, double b, River river) { sascha@204: this(new BigDecimal(a), new BigDecimal(b), river); sascha@204: } sascha@204: sascha@201: public Range(BigDecimal a, BigDecimal b, River river) { sascha@201: this.a = a; sascha@201: this.b = b; sascha@201: this.river = river; sascha@201: } sascha@201: sascha@171: @Id sascha@171: @SequenceGenerator( sascha@171: name = "SEQUENCE_RANGES_ID_SEQ", sascha@171: sequenceName = "RANGES_ID_SEQ", sascha@171: allocationSize = 1) sascha@171: @GeneratedValue( sascha@171: strategy = GenerationType.SEQUENCE, sascha@171: generator = "SEQUENCE_RANGES_ID_SEQ") sascha@171: @Column(name = "id") sascha@171: public Integer getId() { sascha@171: return id; sascha@171: } sascha@171: sascha@168: public void setId(Integer id) { sascha@167: this.id = id; sascha@167: } sascha@167: sascha@172: @Column(name = "a") // FIXME: type mapping needed? sascha@172: public BigDecimal getA() { sascha@172: return a; sascha@172: } sascha@172: sascha@167: public void setA(BigDecimal a) { sascha@167: this.a = a; sascha@167: } sascha@167: sascha@172: @Column(name = "b") // FIXME: type mapping needed? sascha@172: public BigDecimal getB() { sascha@172: return b; sascha@167: } sascha@167: sascha@167: public void setB(BigDecimal b) { sascha@167: this.b = b; sascha@167: } sascha@167: sascha@188: @OneToOne sascha@188: @JoinColumn(name = "river_id") sascha@188: public River getRiver() { sascha@188: return river; sascha@188: } sascha@188: sascha@167: public void setRiver(River river) { sascha@167: this.river = river; sascha@167: } sascha@204: sascha@204: public int code() { sascha@204: int code = 0; sascha@204: if (a != null) code = 1; sascha@204: if (b != null) code |= 2; sascha@204: return code; sascha@204: } sascha@204: sascha@204: public boolean intersects(BigDecimal c) { sascha@204: return !(a.compareTo(c) > 0 || b.compareTo(c) < 0); sascha@204: } sascha@204: sascha@204: public boolean intersects(Range other) { sascha@204: sascha@204: int code = code(); sascha@204: int ocode = other.code(); sascha@204: sascha@204: if (code == 0 || ocode == 0) { sascha@204: return false; sascha@204: } sascha@204: sascha@204: switch (code) { sascha@204: case 1: // has a sascha@204: switch (ocode) { sascha@204: case 1: // has a sascha@204: return a.compareTo(other.a) == 0; sascha@204: case 2: // has b sascha@204: return a.compareTo(other.b) == 0; sascha@204: case 3: // has range sascha@204: return other.intersects(a); sascha@204: } sascha@204: break; sascha@204: case 2: // has b sascha@204: switch (ocode) { sascha@204: case 1: // has a sascha@204: return b.compareTo(other.a) == 0; sascha@204: case 2: // has b sascha@205: return b.compareTo(other.b) == 0; sascha@204: case 3: // has range sascha@204: return other.intersects(b); sascha@204: } sascha@204: break; sascha@204: case 3: // has range sascha@204: switch (ocode) { sascha@204: case 1: // has a sascha@204: return intersects(other.a); sascha@204: case 2: // has b sascha@204: return intersects(other.b); sascha@204: case 3: // has range sascha@204: return !(other.b.compareTo(a) < 0 sascha@204: ||other.a.compareTo(b) > 0); sascha@204: } sascha@204: break; sascha@204: sascha@204: } sascha@204: sascha@204: return false; sascha@204: } sascha@167: } sascha@167: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :