Returns the intersection of the two ranges, or an empty range if their intersection is empty.
(GeneralRange<T> other)
| 174 | */ |
| 175 | |
| 176 | GeneralRange<T> intersect(GeneralRange<T> other) { |
| 177 | checkNotNull(other); |
| 178 | checkArgument(comparator.equals(other.comparator)); |
| 179 | boolean hasLowBound = this.hasLowerBound; |
| 180 | @Nullable T lowEnd = getLowerEndpoint(); |
| 181 | BoundType lowType = getLowerBoundType(); |
| 182 | if (!hasLowerBound()) { |
| 183 | hasLowBound = other.hasLowerBound; |
| 184 | lowEnd = other.getLowerEndpoint(); |
| 185 | lowType = other.getLowerBoundType(); |
| 186 | } else if (other.hasLowerBound()) { |
| 187 | int cmp = comparator.compare(getLowerEndpoint(), other.getLowerEndpoint()); |
| 188 | if (cmp < 0 || (cmp == 0 && other.getLowerBoundType() == OPEN)) { |
| 189 | lowEnd = other.getLowerEndpoint(); |
| 190 | lowType = other.getLowerBoundType(); |
| 191 | } |
| 192 | } |
| 193 | boolean hasUpBound = this.hasUpperBound; |
| 194 | @Nullable T upEnd = getUpperEndpoint(); |
| 195 | BoundType upType = getUpperBoundType(); |
| 196 | if (!hasUpperBound()) { |
| 197 | hasUpBound = other.hasUpperBound; |
| 198 | upEnd = other.getUpperEndpoint(); |
| 199 | upType = other.getUpperBoundType(); |
| 200 | } else if (other.hasUpperBound()) { |
| 201 | int cmp = comparator.compare(getUpperEndpoint(), other.getUpperEndpoint()); |
| 202 | if (cmp > 0 || (cmp == 0 && other.getUpperBoundType() == OPEN)) { |
| 203 | upEnd = other.getUpperEndpoint(); |
| 204 | upType = other.getUpperBoundType(); |
| 205 | } |
| 206 | } |
| 207 | if (hasLowBound && hasUpBound) { |
| 208 | int cmp = comparator.compare(lowEnd, upEnd); |
| 209 | if (cmp > 0 || (cmp == 0 && lowType == OPEN && upType == OPEN)) { |
| 210 | // force allowed empty range |
| 211 | lowEnd = upEnd; |
| 212 | lowType = OPEN; |
| 213 | upType = CLOSED; |
| 214 | } |
| 215 | } |
| 216 | return new GeneralRange<T>(comparator, hasLowBound, lowEnd, lowType, hasUpBound, upEnd, upType); |
| 217 | } |
| 218 | |
| 219 | @Override |
| 220 | public boolean equals(@Nullable Object obj) { |
no test coverage detected