comparison flys-backend/src/main/java/org/dive4elements/river/model/Range.java @ 5828:dfb26b03b179

Moved directories to org.dive4elements.river
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 25 Apr 2013 11:53:11 +0200
parents flys-backend/src/main/java/de/intevation/flys/model/Range.java@80b9218ac007
children 18619c1e7c2a
comparison
equal deleted inserted replaced
5827:e308d4ecd35a 5828:dfb26b03b179
1 package de.intevation.flys.model;
2
3 import java.io.Serializable;
4
5 import java.math.BigDecimal;
6
7 import javax.persistence.Entity;
8 import javax.persistence.Id;
9 import javax.persistence.Table;
10 import javax.persistence.GeneratedValue;
11 import javax.persistence.Column;
12 import javax.persistence.SequenceGenerator;
13 import javax.persistence.GenerationType;
14 import javax.persistence.JoinColumn;
15 import javax.persistence.OneToOne;
16
17 @Entity
18 @Table(name = "ranges")
19 public class Range
20 implements Serializable
21 {
22 public static final double EPSILON = 1e-5;
23 private Integer id;
24 private BigDecimal a;
25 private BigDecimal b;
26
27 private River river;
28
29 public Range() {
30 }
31
32 public Range(double a, double b, River river) {
33 this(new BigDecimal(a), new BigDecimal(b), river);
34 }
35
36 public Range(BigDecimal a, BigDecimal b, River river) {
37 this.a = a;
38 this.b = b;
39 this.river = river;
40 }
41
42 @Id
43 @SequenceGenerator(
44 name = "SEQUENCE_RANGES_ID_SEQ",
45 sequenceName = "RANGES_ID_SEQ",
46 allocationSize = 1)
47 @GeneratedValue(
48 strategy = GenerationType.SEQUENCE,
49 generator = "SEQUENCE_RANGES_ID_SEQ")
50 @Column(name = "id")
51 public Integer getId() {
52 return id;
53 }
54
55 public void setId(Integer id) {
56 this.id = id;
57 }
58
59 @Column(name = "a") // FIXME: type mapping needed?
60 public BigDecimal getA() {
61 return a;
62 }
63
64 public void setA(BigDecimal a) {
65 this.a = a;
66 }
67
68 @Column(name = "b") // FIXME: type mapping needed?
69 public BigDecimal getB() {
70 return b;
71 }
72
73 public void setB(BigDecimal b) {
74 this.b = b;
75 }
76
77 public boolean containsTolerant(double x) {
78 return containsTolerant(x, EPSILON);
79 }
80
81 public boolean containsTolerant(double x, double tolerance) {
82 BigDecimal b = this.b != null ? this.b : a;
83 double av = a.doubleValue();
84 double bv = b.doubleValue();
85 if (av > bv) {
86 double t = av;
87 av = bv;
88 bv = t;
89 }
90 return x+tolerance >= av && x-tolerance <= bv;
91 }
92
93 public boolean contains(double x) {
94 BigDecimal b = this.b != null ? this.b : a;
95 double av = a.doubleValue();
96 double bv = b.doubleValue();
97 if (av > bv) {
98 double t = av;
99 av = bv;
100 bv = t;
101 }
102 return x >= av && x <= bv;
103 }
104
105 @OneToOne
106 @JoinColumn(name = "river_id")
107 public River getRiver() {
108 return river;
109 }
110
111 public void setRiver(River river) {
112 this.river = river;
113 }
114
115 public int code() {
116 int code = 0;
117 if (a != null) code = 1;
118 if (b != null) code |= 2;
119 return code;
120 }
121
122 public boolean intersects(BigDecimal c) {
123 return !(a.compareTo(c) > 0 || b.compareTo(c) < 0);
124 }
125
126 public boolean intersects(Range other) {
127
128 int code = code();
129 int ocode = other.code();
130
131 if (code == 0 || ocode == 0) {
132 return false;
133 }
134
135 switch (code) {
136 case 1: // has a
137 switch (ocode) {
138 case 1: // has a
139 return a.compareTo(other.a) == 0;
140 case 2: // has b
141 return a.compareTo(other.b) == 0;
142 case 3: // has range
143 return other.intersects(a);
144 }
145 break;
146 case 2: // has b
147 switch (ocode) {
148 case 1: // has a
149 return b.compareTo(other.a) == 0;
150 case 2: // has b
151 return b.compareTo(other.b) == 0;
152 case 3: // has range
153 return other.intersects(b);
154 }
155 break;
156 case 3: // has range
157 switch (ocode) {
158 case 1: // has a
159 return intersects(other.a);
160 case 2: // has b
161 return intersects(other.b);
162 case 3: // has range
163 return !(other.b.compareTo(a) < 0
164 ||other.a.compareTo(b) > 0);
165 }
166 break;
167
168 }
169
170 return false;
171 }
172 }
173 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org