1 /* 2 * Copyright (C) 2011 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the 10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 11 * express or implied. See the License for the specific language governing permissions and 12 * limitations under the License. 13 */ 14 15 package com.google.common.collect; 16 17 import com.google.common.annotations.GwtIncompatible; 18 import javax.annotation.CheckForNull; 19 20 /** 21 * A skeletal implementation of {@code RangeSet}. 22 * 23 * @author Louis Wasserman 24 */ 25 @SuppressWarnings("rawtypes") // https://github.com/google/guava/issues/989 26 @GwtIncompatible 27 @ElementTypesAreNonnullByDefault 28 abstract class AbstractRangeSet<C extends Comparable> implements RangeSet<C> { AbstractRangeSet()29 AbstractRangeSet() {} 30 31 @Override contains(C value)32 public boolean contains(C value) { 33 return rangeContaining(value) != null; 34 } 35 36 @Override 37 @CheckForNull rangeContaining(C value)38 public abstract Range<C> rangeContaining(C value); 39 40 @Override isEmpty()41 public boolean isEmpty() { 42 return asRanges().isEmpty(); 43 } 44 45 @Override add(Range<C> range)46 public void add(Range<C> range) { 47 throw new UnsupportedOperationException(); 48 } 49 50 @Override remove(Range<C> range)51 public void remove(Range<C> range) { 52 throw new UnsupportedOperationException(); 53 } 54 55 @Override clear()56 public void clear() { 57 remove(Range.<C>all()); 58 } 59 60 @Override enclosesAll(RangeSet<C> other)61 public boolean enclosesAll(RangeSet<C> other) { 62 return enclosesAll(other.asRanges()); 63 } 64 65 @Override addAll(RangeSet<C> other)66 public void addAll(RangeSet<C> other) { 67 addAll(other.asRanges()); 68 } 69 70 @Override removeAll(RangeSet<C> other)71 public void removeAll(RangeSet<C> other) { 72 removeAll(other.asRanges()); 73 } 74 75 @Override intersects(Range<C> otherRange)76 public boolean intersects(Range<C> otherRange) { 77 return !subRangeSet(otherRange).isEmpty(); 78 } 79 80 @Override encloses(Range<C> otherRange)81 public abstract boolean encloses(Range<C> otherRange); 82 83 @Override equals(@heckForNull Object obj)84 public boolean equals(@CheckForNull Object obj) { 85 if (obj == this) { 86 return true; 87 } else if (obj instanceof RangeSet) { 88 RangeSet<?> other = (RangeSet<?>) obj; 89 return this.asRanges().equals(other.asRanges()); 90 } 91 return false; 92 } 93 94 @Override hashCode()95 public final int hashCode() { 96 return asRanges().hashCode(); 97 } 98 99 @Override toString()100 public final String toString() { 101 return asRanges().toString(); 102 } 103 } 104