Home | History | Annotate | Download | only in collect
      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 javax.annotation.Nullable;
     18 
     19 /**
     20  * A skeletal implementation of {@code RangeSet}.
     21  *
     22  * @author Louis Wasserman
     23  */
     24 abstract class AbstractRangeSet<C extends Comparable> implements RangeSet<C> {
     25   AbstractRangeSet() {}
     26 
     27   @Override
     28   public boolean contains(C value) {
     29     return rangeContaining(value) != null;
     30   }
     31 
     32   @Override
     33   public abstract Range<C> rangeContaining(C value);
     34 
     35   @Override
     36   public boolean isEmpty() {
     37     return asRanges().isEmpty();
     38   }
     39 
     40   @Override
     41   public void add(Range<C> range) {
     42     throw new UnsupportedOperationException();
     43   }
     44 
     45   @Override
     46   public void remove(Range<C> range) {
     47     throw new UnsupportedOperationException();
     48   }
     49 
     50   @Override
     51   public void clear() {
     52     remove(Range.<C>all());
     53   }
     54 
     55   @Override
     56   public boolean enclosesAll(RangeSet<C> other) {
     57     for (Range<C> range : other.asRanges()) {
     58       if (!encloses(range)) {
     59         return false;
     60       }
     61     }
     62     return true;
     63   }
     64 
     65   @Override
     66   public void addAll(RangeSet<C> other) {
     67     for (Range<C> range : other.asRanges()) {
     68       add(range);
     69     }
     70   }
     71 
     72   @Override
     73   public void removeAll(RangeSet<C> other) {
     74     for (Range<C> range : other.asRanges()) {
     75       remove(range);
     76     }
     77   }
     78 
     79   @Override
     80   public abstract boolean encloses(Range<C> otherRange);
     81 
     82   @Override
     83   public boolean equals(@Nullable 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
     94   public final int hashCode() {
     95     return asRanges().hashCode();
     96   }
     97 
     98   @Override
     99   public final String toString() {
    100     return asRanges().toString();
    101   }
    102 }
    103