kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 65d9c6f825384c9195e0cc1633a92a75bc1d30b24853fd1895fc1209069a7685 |
|---|---|
| Author | olim7t <omichallat+github@gmail.com> · 2015-03-05 14:05 -08:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2015-03-05 15:49 -08:00 |
| Parents | 4f318d34b511 |
2 files changed
modified driver-core/src/main/java/com/datastax/driver/core/TokenRange.java +20 −0
| @@ -163,6 +163,26 @@ | ||
| 163 | 163 | || that.contains(this.end, false); |
| 164 | 164 | } |
| 165 | 165 | |
| 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 | + | |
| 166 | 186 | // isStart handles the case where the token is the start of another range, for example: |
| 167 | 187 | // * ]1,2] contains 2, but it does not contain the start of ]2,3] |
| 168 | 188 | // * ]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 @@ | ||
| 1 | 1 | package com.datastax.driver.core; |
| 2 | 2 | |
| 3 | -import java.math.BigInteger; | |
| 4 | 3 | import java.util.List; |
| 5 | 4 | |
| 6 | 5 | import org.testng.annotations.Test; |
| 7 | 6 | |
| 8 | 7 | import static org.testng.Assert.fail; |
| 9 | 8 | |
| 10 | -import com.datastax.driver.core.Token.M3PToken; | |
| 11 | - | |
| 12 | 9 | import static com.datastax.driver.core.Assertions.assertThat; |
| 13 | 10 | |
| 14 | 11 | public class TokenRangeTest { |
| @@ -76,6 +73,25 @@ | ||
| 76 | 73 | ; |
| 77 | 74 | } |
| 78 | 75 | |
| 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 | + | |
| 79 | 95 | @Test(groups = "unit") |
| 80 | 96 | public void should_merge_with_other_range() { |
| 81 | 97 | assertThat(tokenRange(3, 9).mergeWith(tokenRange(2, 3))).isEqualTo(tokenRange(2, 9)); |
| @@ -128,12 +144,12 @@ | ||
| 128 | 144 | |
| 129 | 145 | @Test(groups = "unit", expectedExceptions = IllegalArgumentException.class) |
| 130 | 146 | 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)); | |
| 132 | 148 | } |
| 133 | 149 | |
| 134 | 150 | @Test(groups = "unit") |
| 135 | 151 | 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)); | |
| 137 | 153 | } |
| 138 | 154 | |
| 139 | 155 | @Test(groups = "unit") |
| @@ -155,14 +171,13 @@ | ||
| 155 | 171 | assertThat(splits).containsExactly(tokenRange(3, 5), tokenRange(5, 7), tokenRange(7, 9)); |
| 156 | 172 | } |
| 157 | 173 | |
| 158 | - | |
| 159 | 174 | @Test(groups = "unit") |
| 160 | 175 | 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++) { | |
| 162 | 177 | try { |
| 163 | 178 | tokenRange(0, 1).splitEvenly(i); |
| 164 | 179 | fail("Expected error when providing " + i + " splits."); |
| 165 | - } catch(IllegalArgumentException e) { | |
| 180 | + } catch (IllegalArgumentException e) { | |
| 166 | 181 | // expected. |
| 167 | 182 | } |
| 168 | 183 | } |
| @@ -175,15 +190,15 @@ | ||
| 175 | 190 | |
| 176 | 191 | @Test(groups = "unit") |
| 177 | 192 | public void should_create_empty_token_ranges_if_too_many_splits() { |
| 178 | - TokenRange range = tokenRange(0,10); | |
| 193 | + TokenRange range = tokenRange(0, 10); | |
| 179 | 194 | |
| 180 | 195 | List<TokenRange> ranges = range.splitEvenly(255); |
| 181 | 196 | assertThat(ranges).hasSize(255); |
| 182 | 197 | |
| 183 | - for(int i = 0; i < ranges.size(); i++) { | |
| 198 | + for (int i = 0; i < ranges.size(); i++) { | |
| 184 | 199 | 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)); | |
| 187 | 202 | } else { |
| 188 | 203 | assertThat(tr.isEmpty()); |
| 189 | 204 | } |