Mercurial > dive4elements > river
comparison flys-backend/src/main/java/de/intevation/flys/model/Range.java @ 2877:f0a67bc0e777 2.7
merged flys-backend/2.7
author | Thomas Arendsen Hein <thomas@intevation.de> |
---|---|
date | Fri, 28 Sep 2012 12:14:31 +0200 |
parents | 697d1faa8217 |
children | 7b1c5fe4ebf3 |
comparison
equal
deleted
inserted
replaced
2793:6310b1582f2d | 2877:f0a67bc0e777 |
---|---|
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 private Integer id; | |
23 private BigDecimal a; | |
24 private BigDecimal b; | |
25 | |
26 private River river; | |
27 | |
28 public Range() { | |
29 } | |
30 | |
31 public Range(double a, double b, River river) { | |
32 this(new BigDecimal(a), new BigDecimal(b), river); | |
33 } | |
34 | |
35 public Range(BigDecimal a, BigDecimal b, River river) { | |
36 this.a = a; | |
37 this.b = b; | |
38 this.river = river; | |
39 } | |
40 | |
41 @Id | |
42 @SequenceGenerator( | |
43 name = "SEQUENCE_RANGES_ID_SEQ", | |
44 sequenceName = "RANGES_ID_SEQ", | |
45 allocationSize = 1) | |
46 @GeneratedValue( | |
47 strategy = GenerationType.SEQUENCE, | |
48 generator = "SEQUENCE_RANGES_ID_SEQ") | |
49 @Column(name = "id") | |
50 public Integer getId() { | |
51 return id; | |
52 } | |
53 | |
54 public void setId(Integer id) { | |
55 this.id = id; | |
56 } | |
57 | |
58 @Column(name = "a") // FIXME: type mapping needed? | |
59 public BigDecimal getA() { | |
60 return a; | |
61 } | |
62 | |
63 public void setA(BigDecimal a) { | |
64 this.a = a; | |
65 } | |
66 | |
67 @Column(name = "b") // FIXME: type mapping needed? | |
68 public BigDecimal getB() { | |
69 return b; | |
70 } | |
71 | |
72 public void setB(BigDecimal b) { | |
73 this.b = b; | |
74 } | |
75 | |
76 @OneToOne | |
77 @JoinColumn(name = "river_id") | |
78 public River getRiver() { | |
79 return river; | |
80 } | |
81 | |
82 public void setRiver(River river) { | |
83 this.river = river; | |
84 } | |
85 | |
86 public int code() { | |
87 int code = 0; | |
88 if (a != null) code = 1; | |
89 if (b != null) code |= 2; | |
90 return code; | |
91 } | |
92 | |
93 public boolean intersects(BigDecimal c) { | |
94 return !(a.compareTo(c) > 0 || b.compareTo(c) < 0); | |
95 } | |
96 | |
97 public boolean intersects(Range other) { | |
98 | |
99 int code = code(); | |
100 int ocode = other.code(); | |
101 | |
102 if (code == 0 || ocode == 0) { | |
103 return false; | |
104 } | |
105 | |
106 switch (code) { | |
107 case 1: // has a | |
108 switch (ocode) { | |
109 case 1: // has a | |
110 return a.compareTo(other.a) == 0; | |
111 case 2: // has b | |
112 return a.compareTo(other.b) == 0; | |
113 case 3: // has range | |
114 return other.intersects(a); | |
115 } | |
116 break; | |
117 case 2: // has b | |
118 switch (ocode) { | |
119 case 1: // has a | |
120 return b.compareTo(other.a) == 0; | |
121 case 2: // has b | |
122 return b.compareTo(other.b) == 0; | |
123 case 3: // has range | |
124 return other.intersects(b); | |
125 } | |
126 break; | |
127 case 3: // has range | |
128 switch (ocode) { | |
129 case 1: // has a | |
130 return intersects(other.a); | |
131 case 2: // has b | |
132 return intersects(other.b); | |
133 case 3: // has range | |
134 return !(other.b.compareTo(a) < 0 | |
135 ||other.a.compareTo(b) > 0); | |
136 } | |
137 break; | |
138 | |
139 } | |
140 | |
141 return false; | |
142 } | |
143 } | |
144 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |