You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Range.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. /*
  2. * Copyright 2000-2014 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.shared.ui.grid;
  17. import java.io.Serializable;
  18. /**
  19. * An immutable representation of a range, marked by start and end points.
  20. * <p>
  21. * The range is treated as inclusive at the start, and exclusive at the end.
  22. * I.e. the range [0..1[ has the length 1, and represents one integer: 0.
  23. * <p>
  24. * The range is considered {@link #isEmpty() empty} if the start is the same as
  25. * the end.
  26. *
  27. * @since
  28. * @author Vaadin Ltd
  29. */
  30. public final class Range implements Serializable {
  31. private final int start;
  32. private final int end;
  33. /**
  34. * Creates a range object representing a single integer.
  35. *
  36. * @param integer
  37. * the number to represent as a range
  38. * @return the range represented by <code>integer</code>
  39. */
  40. public static Range withOnly(final int integer) {
  41. return new Range(integer, integer + 1);
  42. }
  43. /**
  44. * Creates a range between two integers.
  45. * <p>
  46. * The range start is <em>inclusive</em> and the end is <em>exclusive</em>.
  47. * So, a range "between" 0 and 5 represents the numbers 0, 1, 2, 3 and 4,
  48. * but not 5.
  49. *
  50. * @param start
  51. * the start of the the range, inclusive
  52. * @param end
  53. * the end of the range, exclusive
  54. * @return a range representing <code>[start..end[</code>
  55. * @throws IllegalArgumentException
  56. * if <code>start &gt; end</code>
  57. */
  58. public static Range between(final int start, final int end)
  59. throws IllegalArgumentException {
  60. return new Range(start, end);
  61. }
  62. /**
  63. * Creates a range from a start point, with a given length.
  64. *
  65. * @param start
  66. * the first integer to include in the range
  67. * @param length
  68. * the length of the resulting range
  69. * @return a range starting from <code>start</code>, with
  70. * <code>length</code> number of integers following
  71. * @throws IllegalArgumentException
  72. * if length &lt; 0
  73. */
  74. public static Range withLength(final int start, final int length)
  75. throws IllegalArgumentException {
  76. if (length < 0) {
  77. /*
  78. * The constructor of Range will throw an exception if start >
  79. * start+length (i.e. if length is negative). We're throwing the
  80. * same exception type, just with a more descriptive message.
  81. */
  82. throw new IllegalArgumentException("length must not be negative");
  83. }
  84. return new Range(start, start + length);
  85. }
  86. /**
  87. * Creates a new range between two numbers: <code>[start..end[</code>.
  88. *
  89. * @param start
  90. * the start integer, inclusive
  91. * @param end
  92. * the end integer, exclusive
  93. * @throws IllegalArgumentException
  94. * if <code>start &gt; end</code>
  95. */
  96. private Range(final int start, final int end)
  97. throws IllegalArgumentException {
  98. if (start > end) {
  99. throw new IllegalArgumentException(
  100. "start must not be greater than end");
  101. }
  102. this.start = start;
  103. this.end = end;
  104. }
  105. /**
  106. * Returns the <em>inclusive</em> start point of this range.
  107. *
  108. * @return the start point of this range
  109. */
  110. public int getStart() {
  111. return start;
  112. }
  113. /**
  114. * Returns the <em>exclusive</em> end point of this range.
  115. *
  116. * @return the end point of this range
  117. */
  118. public int getEnd() {
  119. return end;
  120. }
  121. /**
  122. * The number of integers contained in the range.
  123. *
  124. * @return the number of integers contained in the range
  125. */
  126. public int length() {
  127. return getEnd() - getStart();
  128. }
  129. /**
  130. * Checks whether the range has no elements between the start and end.
  131. *
  132. * @return <code>true</code> iff the range contains no elements.
  133. */
  134. public boolean isEmpty() {
  135. return getStart() >= getEnd();
  136. }
  137. /**
  138. * Checks whether this range and another range are at least partially
  139. * covering the same values.
  140. *
  141. * @param other
  142. * the other range to check against
  143. * @return <code>true</code> if this and <code>other</code> intersect
  144. */
  145. public boolean intersects(final Range other) {
  146. return getStart() < other.getEnd() && other.getStart() < getEnd();
  147. }
  148. /**
  149. * Checks whether an integer is found within this range.
  150. *
  151. * @param integer
  152. * an integer to test for presence in this range
  153. * @return <code>true</code> iff <code>integer</code> is in this range
  154. */
  155. public boolean contains(final int integer) {
  156. return getStart() <= integer && integer < getEnd();
  157. }
  158. /**
  159. * Checks whether this range is a subset of another range.
  160. *
  161. * @return <code>true</code> iff <code>other</code> completely wraps this
  162. * range
  163. */
  164. public boolean isSubsetOf(final Range other) {
  165. if (isEmpty() && other.isEmpty()) {
  166. return true;
  167. }
  168. return other.getStart() <= getStart() && getEnd() <= other.getEnd();
  169. }
  170. /**
  171. * Overlay this range with another one, and partition the ranges according
  172. * to how they position relative to each other.
  173. * <p>
  174. * The three partitions are returned as a three-element Range array:
  175. * <ul>
  176. * <li>Elements in this range that occur before elements in
  177. * <code>other</code>.
  178. * <li>Elements that are shared between the two ranges.
  179. * <li>Elements in this range that occur after elements in
  180. * <code>other</code>.
  181. * </ul>
  182. *
  183. * @param other
  184. * the other range to act as delimiters.
  185. * @return a three-element Range array of partitions depicting the elements
  186. * before (index 0), shared/inside (index 1) and after (index 2).
  187. */
  188. public Range[] partitionWith(final Range other) {
  189. final Range[] splitBefore = splitAt(other.getStart());
  190. final Range rangeBefore = splitBefore[0];
  191. final Range[] splitAfter = splitBefore[1].splitAt(other.getEnd());
  192. final Range rangeInside = splitAfter[0];
  193. final Range rangeAfter = splitAfter[1];
  194. return new Range[] { rangeBefore, rangeInside, rangeAfter };
  195. }
  196. /**
  197. * Get a range that is based on this one, but offset by a number.
  198. *
  199. * @param offset
  200. * the number to offset by
  201. * @return a copy of this range, offset by <code>offset</code>
  202. */
  203. public Range offsetBy(final int offset) {
  204. if (offset == 0) {
  205. return this;
  206. } else {
  207. return new Range(start + offset, end + offset);
  208. }
  209. }
  210. @Override
  211. public String toString() {
  212. return getClass().getSimpleName() + " [" + getStart() + ".." + getEnd()
  213. + "[" + (isEmpty() ? " (empty)" : "");
  214. }
  215. @Override
  216. public int hashCode() {
  217. final int prime = 31;
  218. int result = 1;
  219. result = prime * result + end;
  220. result = prime * result + start;
  221. return result;
  222. }
  223. @Override
  224. public boolean equals(final Object obj) {
  225. if (this == obj) {
  226. return true;
  227. }
  228. if (obj == null) {
  229. return false;
  230. }
  231. if (getClass() != obj.getClass()) {
  232. return false;
  233. }
  234. final Range other = (Range) obj;
  235. if (end != other.end) {
  236. return false;
  237. }
  238. if (start != other.start) {
  239. return false;
  240. }
  241. return true;
  242. }
  243. /**
  244. * Checks whether this range starts before the start of another range.
  245. *
  246. * @param other
  247. * the other range to compare against
  248. * @return <code>true</code> iff this range starts before the
  249. * <code>other</code>
  250. */
  251. public boolean startsBefore(final Range other) {
  252. return getStart() < other.getStart();
  253. }
  254. /**
  255. * Checks whether this range ends before the start of another range.
  256. *
  257. * @param other
  258. * the other range to compare against
  259. * @return <code>true</code> iff this range ends before the
  260. * <code>other</code>
  261. */
  262. public boolean endsBefore(final Range other) {
  263. return getEnd() <= other.getStart();
  264. }
  265. /**
  266. * Checks whether this range ends after the end of another range.
  267. *
  268. * @param other
  269. * the other range to compare against
  270. * @return <code>true</code> iff this range ends after the
  271. * <code>other</code>
  272. */
  273. public boolean endsAfter(final Range other) {
  274. return getEnd() > other.getEnd();
  275. }
  276. /**
  277. * Checks whether this range starts after the end of another range.
  278. *
  279. * @param other
  280. * the other range to compare against
  281. * @return <code>true</code> iff this range starts after the
  282. * <code>other</code>
  283. */
  284. public boolean startsAfter(final Range other) {
  285. return getStart() >= other.getEnd();
  286. }
  287. /**
  288. * Split the range into two at a certain integer.
  289. * <p>
  290. * <em>Example:</em> <code>[5..10[.splitAt(7) == [5..7[, [7..10[</code>
  291. *
  292. * @param integer
  293. * the integer at which to split the range into two
  294. * @return an array of two ranges, with <code>[start..integer[</code> in the
  295. * first element, and <code>[integer..end[</code> in the second
  296. * element.
  297. * <p>
  298. * If {@code integer} is less than {@code start}, [empty,
  299. * {@code this} ] is returned. if <code>integer</code> is equal to
  300. * or greater than {@code end}, [{@code this}, empty] is returned
  301. * instead.
  302. */
  303. public Range[] splitAt(final int integer) {
  304. if (integer < start) {
  305. return new Range[] { Range.withLength(start, 0), this };
  306. } else if (integer >= end) {
  307. return new Range[] { this, Range.withLength(end, 0) };
  308. } else {
  309. return new Range[] { new Range(start, integer),
  310. new Range(integer, end) };
  311. }
  312. }
  313. /**
  314. * Split the range into two after a certain number of integers into the
  315. * range.
  316. * <p>
  317. * Calling this method is equivalent to calling
  318. * <code>{@link #splitAt(int) splitAt}({@link #getStart()}+length);</code>
  319. * <p>
  320. * <em>Example:</em>
  321. * <code>[5..10[.splitAtFromStart(2) == [5..7[, [7..10[</code>
  322. *
  323. * @param length
  324. * the length at which to split this range into two
  325. * @return an array of two ranges, having the <code>length</code>-first
  326. * elements of this range, and the second range having the rest. If
  327. * <code>length</code> &leq; 0, the first element will be empty, and
  328. * the second element will be this range. If <code>length</code>
  329. * &geq; {@link #length()}, the first element will be this range,
  330. * and the second element will be empty.
  331. */
  332. public Range[] splitAtFromStart(final int length) {
  333. return splitAt(getStart() + length);
  334. }
  335. /**
  336. * Combines two ranges to create a range containing all values in both
  337. * ranges, provided there are no gaps between the ranges.
  338. *
  339. * @param other
  340. * the range to combine with this range
  341. *
  342. * @return the combined range
  343. *
  344. * @throws IllegalArgumentException
  345. * if the two ranges aren't connected
  346. */
  347. public Range combineWith(Range other) throws IllegalArgumentException {
  348. if (getStart() > other.getEnd() || other.getStart() > getEnd()) {
  349. throw new IllegalArgumentException("There is a gap between " + this
  350. + " and " + other);
  351. }
  352. return Range.between(Math.min(getStart(), other.getStart()),
  353. Math.max(getEnd(), other.getEnd()));
  354. }
  355. /**
  356. * Creates a range that is expanded the given amounts in both ends.
  357. *
  358. * @param startDelta
  359. * the amount to expand by in the beginning of the range
  360. * @param endDelta
  361. * the amount to expand by in the end of the range
  362. *
  363. * @return an expanded range
  364. *
  365. * @throws IllegalArgumentException
  366. * if the new range would have <code>start &gt; end</code>
  367. */
  368. public Range expand(int startDelta, int endDelta)
  369. throws IllegalArgumentException {
  370. return Range.between(getStart() - startDelta, getEnd() + endDelta);
  371. }
  372. /**
  373. * Limits this range to be within the bounds of the provided range.
  374. * <p>
  375. * This is basically an optimized way of calculating
  376. * <code>{@link #partitionWith(Range)}[1]</code> without the overhead of
  377. * defining the parts that do not overlap.
  378. * <p>
  379. * If the two ranges do not intersect, an empty range is returned. There are
  380. * no guarantees about the position of that range.
  381. *
  382. * @param bounds
  383. * the bounds that the returned range should be limited to
  384. * @return a bounded range
  385. */
  386. public Range restrictTo(Range bounds) {
  387. boolean startWithin = bounds.contains(getStart());
  388. boolean endWithin = bounds.contains(getEnd());
  389. boolean boundsWithin = getStart() < bounds.getStart()
  390. && getEnd() >= bounds.getEnd();
  391. if (startWithin) {
  392. if (endWithin) {
  393. return this;
  394. } else {
  395. return Range.between(getStart(), bounds.getEnd());
  396. }
  397. } else {
  398. if (endWithin) {
  399. return Range.between(bounds.getStart(), getEnd());
  400. } else if (boundsWithin) {
  401. return bounds;
  402. } else {
  403. return Range.withLength(getStart(), 0);
  404. }
  405. }
  406. }
  407. }