Returns a {@code ContiguousSet} containing the same values in the given domain {@linkplain Range#contains contained} by the range. @throws IllegalArgumentException if neither range nor the domain has a lower bound, or if neither has an upper bound @since 13.0
(
Range<C> range, DiscreteDomain<C> domain)
| 60 | * @since 13.0 |
| 61 | */ |
| 62 | public static <C extends Comparable> ContiguousSet<C> create( |
| 63 | Range<C> range, DiscreteDomain<C> domain) { |
| 64 | checkNotNull(range); |
| 65 | checkNotNull(domain); |
| 66 | Range<C> effectiveRange = range; |
| 67 | try { |
| 68 | if (!range.hasLowerBound()) { |
| 69 | effectiveRange = effectiveRange.intersection(Range.atLeast(domain.minValue())); |
| 70 | } |
| 71 | if (!range.hasUpperBound()) { |
| 72 | effectiveRange = effectiveRange.intersection(Range.atMost(domain.maxValue())); |
| 73 | } |
| 74 | } catch (NoSuchElementException e) { |
| 75 | throw new IllegalArgumentException(e); |
| 76 | } |
| 77 | |
| 78 | boolean empty; |
| 79 | if (effectiveRange.isEmpty()) { |
| 80 | empty = true; |
| 81 | } else { |
| 82 | /* |
| 83 | * requireNonNull is safe because the effectiveRange operations above would have thrown or |
| 84 | * effectiveRange.isEmpty() would have returned true. |
| 85 | */ |
| 86 | C afterLower = requireNonNull(range.lowerBound.leastValueAbove(domain)); |
| 87 | C beforeUpper = requireNonNull(range.upperBound.greatestValueBelow(domain)); |
| 88 | // Per class spec, we are allowed to throw CCE if necessary |
| 89 | empty = Range.compareOrThrow(afterLower, beforeUpper) > 0; |
| 90 | } |
| 91 | |
| 92 | return empty |
| 93 | ? new EmptyContiguousSet<C>(domain) |
| 94 | : new RegularContiguousSet<C>(effectiveRange, domain); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Returns a nonempty contiguous set containing all {@code int} values from {@code lower} |