Elffuss BraidHub

kikocis/java-driver public

Add TokenRange#intersectWith (JAVA-681).
Changeset65d9c6f825384c9195e0cc1633a92a75bc1d30b24853fd1895fc1209069a7685
Authorolim7t <omichallat+github@gmail.com> · 2015-03-05 14:05 -08:00
Committerolim7t <omichallat+github@gmail.com> · 2015-03-05 15:49 -08:00
Parents4f318d34b511

Browse the files at this changeset

2 files changed

modified driver-core/src/main/java/com/datastax/driver/core/TokenRange.java +20 −0
@@ -163,6 +163,26 @@
163163 || that.contains(this.end, false);
164164 }
165165
166+ /**
167+ * Computes the intersection of this range with another one.
168+ * <p>
169+ * This call will fail if the two ranges do not intersect, you must check by calling
170+ * {@link #intersects(TokenRange)} beforehand.
171+ *
172+ * @param that the other range.
173+ * @return the range resulting from the intersection.
174+ * @throws IllegalArgumentException if the ranges do not intersect.
175+ */
176+ public TokenRange intersectWith(TokenRange that) {
177+ if (!this.intersects(that))
178+ throw new IllegalArgumentException("The two ranges do not intersect, use intersects() before calling this method");
179+
180+ return new TokenRange(
181+ (this.contains(that.start, true)) ? that.start : this.start,
182+ (this.contains(that.end, false)) ? that.end : this.end,
183+ factory);
184+ }
185+
166186 // isStart handles the case where the token is the start of another range, for example:
167187 // * ]1,2] contains 2, but it does not contain the start of ]2,3]
168188 // * ]1,2] does not contain 1, but it contains the start of ]1,3]
modified driver-core/src/test/java/com/datastax/driver/core/TokenRangeTest.java +27 −12
@@ -1,14 +1,11 @@
11 package com.datastax.driver.core;
22
3-import java.math.BigInteger;
43 import java.util.List;
54
65 import org.testng.annotations.Test;
76
87 import static org.testng.Assert.fail;
98
10-import com.datastax.driver.core.Token.M3PToken;
11-
129 import static com.datastax.driver.core.Assertions.assertThat;
1310
1411 public class TokenRangeTest {
@@ -76,6 +73,25 @@
7673 ;
7774 }
7875
76+ @Test(groups = "unit")
77+ public void should_compute_intersection() {
78+ assertThat(tokenRange(3, 9).intersectWith(tokenRange(2, 4)))
79+ .isEqualTo(tokenRange(3, 4));
80+ assertThat(tokenRange(3, 9).intersectWith(tokenRange(3, 5)))
81+ .isEqualTo(tokenRange(3, 5));
82+ assertThat(tokenRange(3, 9).intersectWith(tokenRange(4, 6)))
83+ .isEqualTo(tokenRange(4, 6));
84+ assertThat(tokenRange(3, 9).intersectWith(tokenRange(7, 9)))
85+ .isEqualTo(tokenRange(7, 9));
86+ assertThat(tokenRange(3, 9).intersectWith(tokenRange(8, 10)))
87+ .isEqualTo(tokenRange(8, 9));
88+ }
89+
90+ @Test(groups = "unit", expectedExceptions = IllegalArgumentException.class)
91+ public void should_fail_to_compute_intersection_when_ranges_dont_intersect() {
92+ tokenRange(1, 2).intersectWith(tokenRange(2, 3));
93+ }
94+
7995 @Test(groups = "unit")
8096 public void should_merge_with_other_range() {
8197 assertThat(tokenRange(3, 9).mergeWith(tokenRange(2, 3))).isEqualTo(tokenRange(2, 9));
@@ -128,12 +144,12 @@
128144
129145 @Test(groups = "unit", expectedExceptions = IllegalArgumentException.class)
130146 public void should_not_merge_with_nonadjacent_and_disjoint_ranges() {
131- tokenRange(0,5).mergeWith(tokenRange(7,14));
147+ tokenRange(0, 5).mergeWith(tokenRange(7, 14));
132148 }
133149
134150 @Test(groups = "unit")
135151 public void should_return_non_empty_range_if_other_range_is_empty() {
136- assertThat(tokenRange(1,5).mergeWith(tokenRange(5,5))).isEqualTo(tokenRange(1,5));
152+ assertThat(tokenRange(1, 5).mergeWith(tokenRange(5, 5))).isEqualTo(tokenRange(1, 5));
137153 }
138154
139155 @Test(groups = "unit")
@@ -155,14 +171,13 @@
155171 assertThat(splits).containsExactly(tokenRange(3, 5), tokenRange(5, 7), tokenRange(7, 9));
156172 }
157173
158-
159174 @Test(groups = "unit")
160175 public void should_throw_error_with_less_than_1_splits() {
161- for(int i = -255; i < 1; i++) {
176+ for (int i = -255; i < 1; i++) {
162177 try {
163178 tokenRange(0, 1).splitEvenly(i);
164179 fail("Expected error when providing " + i + " splits.");
165- } catch(IllegalArgumentException e) {
180+ } catch (IllegalArgumentException e) {
166181 // expected.
167182 }
168183 }
@@ -175,15 +190,15 @@
175190
176191 @Test(groups = "unit")
177192 public void should_create_empty_token_ranges_if_too_many_splits() {
178- TokenRange range = tokenRange(0,10);
193+ TokenRange range = tokenRange(0, 10);
179194
180195 List<TokenRange> ranges = range.splitEvenly(255);
181196 assertThat(ranges).hasSize(255);
182197
183- for(int i = 0; i < ranges.size(); i++) {
198+ for (int i = 0; i < ranges.size(); i++) {
184199 TokenRange tr = ranges.get(i);
185- if(i < 10) {
186- assertThat(tr).isEqualTo(tokenRange(i, i+1));
200+ if (i < 10) {
201+ assertThat(tr).isEqualTo(tokenRange(i, i + 1));
187202 } else {
188203 assertThat(tr.isEmpty());
189204 }