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 @GwtIncompatible 26 @ElementTypesAreNonnullByDefault 27 abstract class AbstractRangeSet<C extends Comparable> implements RangeSet<C> { AbstractRangeSet()28 AbstractRangeSet() {} 29 30 @Override contains(C value)31 public boolean contains(C value) { 32 return rangeContaining(value) != null; 33 } 34 35 @Override 36 @CheckForNull rangeContaining(C value)37 public abstract Range<C> rangeContaining(C value); 38 39 @Override isEmpty()40 public boolean isEmpty() { 41 return asRanges().isEmpty(); 42 } 43 44 @Override add(Range<C> range)45 public void add(Range<C> range) { 46 throw new UnsupportedOperationException(); 47 } 48 49 @Override remove(Range<C> range)50 public void remove(Range<C> range) { 51 throw new UnsupportedOperationException(); 52 } 53 54 @Override clear()55 public void clear() { 56 remove(Range.<C>all()); 57 } 58 59 @Override enclosesAll(RangeSet<C> other)60 public boolean enclosesAll(RangeSet<C> other) { 61 return enclosesAll(other.asRanges()); 62 } 63 64 @Override addAll(RangeSet<C> other)65 public void addAll(RangeSet<C> other) { 66 addAll(other.asRanges()); 67 } 68 69 @Override removeAll(RangeSet<C> other)70 public void removeAll(RangeSet<C> other) { 71 removeAll(other.asRanges()); 72 } 73 74 @Override intersects(Range<C> otherRange)75 public boolean intersects(Range<C> otherRange) { 76 return !subRangeSet(otherRange).isEmpty(); 77 } 78 79 @Override encloses(Range<C> otherRange)80 public abstract boolean encloses(Range<C> otherRange); 81 82 @Override equals(@heckForNull Object obj)83 public boolean equals(@CheckForNull Object obj) { 84 if (obj == this) { 85 return true; 86 } else if (obj instanceof RangeSet) { 87 RangeSet<?> other = (RangeSet<?>) obj; 88 return this.asRanges().equals(other.asRanges()); 89 } 90 return false; 91 } 92 93 @Override hashCode()94 public final int hashCode() { 95 return asRanges().hashCode(); 96 } 97 98 @Override toString()99 public final String toString() { 100 return asRanges().toString(); 101 } 102 } 103