001/* 002 * Copyright (C) 2011 The Guava Authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 005 * in compliance with the License. You may obtain a copy of the License at 006 * 007 * http://www.apache.org/licenses/LICENSE-2.0 008 * 009 * Unless required by applicable law or agreed to in writing, software distributed under the 010 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 011 * express or implied. See the License for the specific language governing permissions and 012 * limitations under the License. 013 */ 014 015package com.google.common.collect; 016 017import com.google.common.annotations.Beta; 018 019import java.util.NoSuchElementException; 020import java.util.Set; 021 022import javax.annotation.Nullable; 023 024/** 025 * A set comprising zero or more {@linkplain Range#isEmpty nonempty}, 026 * {@linkplain Range#isConnected(Range) disconnected} ranges of type {@code C}. 027 * 028 * <p>Implementations that choose to support the {@link #add(Range)} operation are required to 029 * ignore empty ranges and coalesce connected ranges. For example: <pre> {@code 030 * 031 * RangeSet<Integer> rangeSet = TreeRangeSet.create(); 032 * rangeSet.add(Range.closed(1, 10)); // {[1, 10]} 033 * rangeSet.add(Range.closedOpen(11, 15)); // disconnected range; {[1, 10], [11, 15)} 034 * rangeSet.add(Range.closedOpen(15, 20)); // connected range; {[1, 10], [11, 20)} 035 * rangeSet.add(Range.openClosed(0, 0)); // empty range; {[1, 10], [11, 20)} 036 * rangeSet.remove(Range.open(5, 10)); // splits [1, 10]; {[1, 5], [10, 10], [11, 20)}}</pre> 037 * 038 * <p>Note that the behavior of {@link Range#isEmpty()} and {@link Range#isConnected(Range)} may 039 * not be as expected on discrete ranges. See the Javadoc of those methods for details. 040 * 041 * <p>For a {@link Set} whose contents are specified by a {@link Range}, see {@link ContiguousSet}. 042 * 043 * @author Kevin Bourrillion 044 * @author Louis Wasserman 045 * @since 14.0 046 */ 047@Beta 048public interface RangeSet<C extends Comparable> { 049 050 // Query methods 051 052 /** 053 * Determines whether any of this range set's member ranges contains {@code value}. 054 */ 055 boolean contains(C value); 056 057 /** 058 * Returns the unique range from this range set that {@linkplain Range#contains contains} 059 * {@code value}, or {@code null} if this range set does not contain {@code value}. 060 */ 061 Range<C> rangeContaining(C value); 062 063 /** 064 * Returns {@code true} if there exists a member range in this range set which 065 * {@linkplain Range#encloses encloses} the specified range. 066 */ 067 boolean encloses(Range<C> otherRange); 068 069 /** 070 * Returns {@code true} if for each member range in {@code other} there exists a member range in 071 * this range set which {@linkplain Range#encloses encloses} it. It follows that 072 * {@code this.contains(value)} whenever {@code other.contains(value)}. Returns {@code true} if 073 * {@code other} is empty. 074 * 075 * <p>This is equivalent to checking if this range set {@link #encloses} each of the ranges in 076 * {@code other}. 077 */ 078 boolean enclosesAll(RangeSet<C> other); 079 080 /** 081 * Returns {@code true} if this range set contains no ranges. 082 */ 083 boolean isEmpty(); 084 085 /** 086 * Returns the minimal range which {@linkplain Range#encloses(Range) encloses} all ranges 087 * in this range set. 088 * 089 * @throws NoSuchElementException if this range set is {@linkplain #isEmpty() empty} 090 */ 091 Range<C> span(); 092 093 // Views 094 095 /** 096 * Returns a view of the {@linkplain Range#isConnected disconnected} ranges that make up this 097 * range set. The returned set may be empty. The iterators returned by its 098 * {@link Iterable#iterator} method return the ranges in increasing order of lower bound 099 * (equivalently, of upper bound). 100 */ 101 Set<Range<C>> asRanges(); 102 103 /** 104 * Returns a descending view of the {@linkplain Range#isConnected disconnected} ranges that 105 * make up this range set. The returned set may be empty. The iterators returned by its 106 * {@link Iterable#iterator} method return the ranges in decreasing order of lower bound 107 * (equivalently, of upper bound). 108 * 109 * @since 19.0 110 */ 111 Set<Range<C>> asDescendingSetOfRanges(); 112 113 /** 114 * Returns a view of the complement of this {@code RangeSet}. 115 * 116 * <p>The returned view supports the {@link #add} operation if this {@code RangeSet} supports 117 * {@link #remove}, and vice versa. 118 */ 119 RangeSet<C> complement(); 120 121 /** 122 * Returns a view of the intersection of this {@code RangeSet} with the specified range. 123 * 124 * <p>The returned view supports all optional operations supported by this {@code RangeSet}, with 125 * the caveat that an {@link IllegalArgumentException} is thrown on an attempt to 126 * {@linkplain #add(Range) add} any range not {@linkplain Range#encloses(Range) enclosed} by 127 * {@code view}. 128 */ 129 RangeSet<C> subRangeSet(Range<C> view); 130 131 // Modification 132 133 /** 134 * Adds the specified range to this {@code RangeSet} (optional operation). That is, for equal 135 * range sets a and b, the result of {@code a.add(range)} is that {@code a} will be the minimal 136 * range set for which both {@code a.enclosesAll(b)} and {@code a.encloses(range)}. 137 * 138 * <p>Note that {@code range} will be {@linkplain Range#span(Range) coalesced} with any ranges in 139 * the range set that are {@linkplain Range#isConnected(Range) connected} with it. Moreover, 140 * if {@code range} is empty, this is a no-op. 141 * 142 * @throws UnsupportedOperationException if this range set does not support the {@code add} 143 * operation 144 */ 145 void add(Range<C> range); 146 147 /** 148 * Removes the specified range from this {@code RangeSet} (optional operation). After this 149 * operation, if {@code range.contains(c)}, {@code this.contains(c)} will return {@code false}. 150 * 151 * <p>If {@code range} is empty, this is a no-op. 152 * 153 * @throws UnsupportedOperationException if this range set does not support the {@code remove} 154 * operation 155 */ 156 void remove(Range<C> range); 157 158 /** 159 * Removes all ranges from this {@code RangeSet} (optional operation). After this operation, 160 * {@code this.contains(c)} will return false for all {@code c}. 161 * 162 * <p>This is equivalent to {@code remove(Range.all())}. 163 * 164 * @throws UnsupportedOperationException if this range set does not support the {@code clear} 165 * operation 166 */ 167 void clear(); 168 169 /** 170 * Adds all of the ranges from the specified range set to this range set (optional operation). 171 * After this operation, this range set is the minimal range set that 172 * {@linkplain #enclosesAll(RangeSet) encloses} both the original range set and {@code other}. 173 * 174 * <p>This is equivalent to calling {@link #add} on each of the ranges in {@code other} in turn. 175 * 176 * @throws UnsupportedOperationException if this range set does not support the {@code addAll} 177 * operation 178 */ 179 void addAll(RangeSet<C> other); 180 181 /** 182 * Removes all of the ranges from the specified range set from this range set (optional 183 * operation). After this operation, if {@code other.contains(c)}, {@code this.contains(c)} will 184 * return {@code false}. 185 * 186 * <p>This is equivalent to calling {@link #remove} on each of the ranges in {@code other} in 187 * turn. 188 * 189 * @throws UnsupportedOperationException if this range set does not support the {@code removeAll} 190 * operation 191 */ 192 void removeAll(RangeSet<C> other); 193 194 // Object methods 195 196 /** 197 * Returns {@code true} if {@code obj} is another {@code RangeSet} that contains the same ranges 198 * according to {@link Range#equals(Object)}. 199 */ 200 @Override 201 boolean equals(@Nullable Object obj); 202 203 /** 204 * Returns {@code asRanges().hashCode()}. 205 */ 206 @Override 207 int hashCode(); 208 209 /** 210 * Returns a readable string representation of this range set. For example, if this 211 * {@code RangeSet} consisted of {@code Range.closed(1, 3)} and {@code Range.greaterThan(4)}, 212 * this might return {@code " [1‥3](4‥+∞)}"}. 213 */ 214 @Override 215 String toString(); 216}