Returns the minimal range that {@linkplain Range#contains(Comparable) contains} all of the given values. The returned range is {@linkplain BoundType#CLOSED closed} on both ends. @throws ClassCastException if the values are not mutually comparable @throws NoSuchElementException if {@code values} is
(Iterable<C> values)
| 302 | * @since 14.0 |
| 303 | */ |
| 304 | public static <C extends Comparable<?>> Range<C> encloseAll(Iterable<C> values) { |
| 305 | checkNotNull(values); |
| 306 | if (values instanceof SortedSet) { |
| 307 | SortedSet<C> set = (SortedSet<C>) values; |
| 308 | Comparator<?> comparator = set.comparator(); |
| 309 | if (Ordering.<C>natural().equals(comparator) || comparator == null) { |
| 310 | return closed(set.first(), set.last()); |
| 311 | } |
| 312 | } |
| 313 | Iterator<C> valueIterator = values.iterator(); |
| 314 | C min = checkNotNull(valueIterator.next()); |
| 315 | C max = min; |
| 316 | while (valueIterator.hasNext()) { |
| 317 | C value = checkNotNull(valueIterator.next()); |
| 318 | min = Ordering.<C>natural().min(min, value); |
| 319 | max = Ordering.<C>natural().max(max, value); |
| 320 | } |
| 321 | return closed(min, max); |
| 322 | } |
| 323 | |
| 324 | final Cut<C> lowerBound; |
| 325 | final Cut<C> upperBound; |