1
16 package net.sf.ehcache.store.disk.ods;
17
18
23 public class Region extends AATreeSet.AbstractTreeNode<Comparable> implements Comparable<Comparable> {
24 private long start;
25 private long end;
26
27 private long contiguous;
28
29
33 public Region(long value) {
34 this(value, value);
35 }
36
37
40 public Region(long start, long end) {
41 super();
42 this.start = start;
43 this.end = end;
44 updateContiguous();
45 }
46
47
52 public Region(Region r) {
53 this(r.start(), r.end());
54 }
55
56
59 public long contiguous() {
60 if (getLeft().getPayload() == null && getRight().getPayload() == null) {
61 return size();
62 } else {
63 return contiguous;
64 }
65 }
66
67 private void updateContiguous() {
68 Region left = (Region) getLeft().getPayload();
69 Region right = (Region) getRight().getPayload();
70 long leftContiguous = left == null ? 0 : left.contiguous();
71 long rightContiguous = right == null ? 0 : right.contiguous();
72 contiguous = Math.max(size(), Math.max(leftContiguous, rightContiguous));
73 }
74
75 @Override
76 public void setLeft(AATreeSet.Node<Comparable> l) {
77 super.setLeft(l);
78 updateContiguous();
79 }
80
81 @Override
82 public void setRight(AATreeSet.Node<Comparable> r) {
83 super.setRight(r);
84 updateContiguous();
85 }
86
87
90 @Override
91 public String toString() {
92 return "Range(" + this.start + "," + this.end + ")" + " contiguous:" + this.contiguous();
93 }
94
95
98 public long size() {
99
100 return (isNull() ? 0 : this.end - this.start + 1);
101 }
102
103
106 protected boolean isNull() {
107 return this.start > this.end;
108 }
109
110
117 protected Region remove(Region r) throws IllegalArgumentException {
118 if (r.start < this.start || r.end > this.end) {
119 throw new IllegalArgumentException("Ranges : Illegal value passed to remove : " + this + " remove called for : " + r);
120 }
121 if (this.start == r.start) {
122 this.start = r.end + 1;
123 updateContiguous();
124 return null;
125 } else if (this.end == r.end) {
126 this.end = r.start - 1;
127 updateContiguous();
128 return null;
129 } else {
130 Region newRegion = new Region(r.end + 1, this.end);
131 this.end = r.start - 1;
132 updateContiguous();
133 return newRegion;
134 }
135 }
136
137
143 protected void merge(Region r) throws IllegalArgumentException {
144 if (this.start == r.end + 1) {
145 this.start = r.start;
146 } else if (this.end == r.start - 1) {
147 this.end = r.end;
148 } else {
149 throw new IllegalArgumentException("Ranges : Merge called on non contiguous values : [this]:" + this + " and " + r);
150 }
151 updateContiguous();
152 }
153
154
157 public int compareTo(Comparable other) {
158 if (other instanceof Region) {
159 return compareTo((Region) other);
160 } else if (other instanceof Long) {
161 return compareTo((Long) other);
162 } else {
163 throw new AssertionError("Unusual Type " + other.getClass());
164 }
165 }
166
167 private int compareTo(Region r) {
168 if (this.start > r.start || this.end > r.end) {
169 return 1;
170 } else if (this.start < r.start || this.end < r.end) {
171 return -1;
172 } else {
173 return 0;
174 }
175 }
176
177 private int compareTo(Long l) {
178 if (l.longValue() > end) {
179 return -1;
180 } else if (l.longValue() < start) {
181 return 1;
182 } else {
183 return 0;
184 }
185 }
186
187
190 public void swapPayload(AATreeSet.Node<Comparable> other) {
191 if (other instanceof Region) {
192 Region r = (Region) other;
193 long temp = this.start;
194 this.start = r.start;
195 r.start = temp;
196 temp = this.end;
197 this.end = r.end;
198 r.end = temp;
199 updateContiguous();
200 } else {
201 throw new AssertionError();
202 }
203 }
204
205
208 public Region getPayload() {
209 return this;
210 }
211
212
215 public long start() {
216 return start;
217 }
218
219
222 public long end() {
223 return end;
224 }
225
226 }
227