Home | History | Annotate | Download | only in collect
      1 /*
      2  * Copyright (C) 2012 The Guava Authors
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  * http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.google.common.collect;
     18 
     19 import com.google.common.annotations.Beta;
     20 
     21 import java.util.Map;
     22 
     23 import javax.annotation.Nullable;
     24 
     25 /**
     26  * A mapping from disjoint nonempty ranges to non-null values. Queries look up the value
     27  * associated with the range (if any) that contains a specified key.
     28  *
     29  * <p>In contrast to {@link RangeSet}, no "coalescing" is done of {@linkplain
     30  * Range#isConnected(Range) connected} ranges, even if they are mapped to the same value.
     31  *
     32  * @author Louis Wasserman
     33  * @since 14.0
     34  */
     35 @Beta
     36 public interface RangeMap<K extends Comparable, V> {
     37   /**
     38    * Returns the value associated with the specified key, or {@code null} if there is no
     39    * such value.
     40    *
     41    * <p>Specifically, if any range in this range map contains the specified key, the value
     42    * associated with that range is returned.
     43    */
     44   @Nullable
     45   V get(K key);
     46 
     47   /**
     48    * Returns the range containing this key and its associated value, if such a range is present
     49    * in the range map, or {@code null} otherwise.
     50    */
     51   @Nullable
     52   Map.Entry<Range<K>, V> getEntry(K key);
     53 
     54   /**
     55    * Returns the minimal range {@linkplain Range#encloses(Range) enclosing} the ranges
     56    * in this {@code RangeMap}.
     57    *
     58    * @throws NoSuchElementException if this range map is empty
     59    */
     60   Range<K> span();
     61 
     62   /**
     63    * Maps a range to a specified value (optional operation).
     64    *
     65    * <p>Specifically, after a call to {@code put(range, value)}, if
     66    * {@link Range#contains(Comparable) range.contains(k)}, then {@link #get(Comparable) get(k)}
     67    * will return {@code value}.
     68    *
     69    * <p>If {@code range} {@linkplain Range#isEmpty() is empty}, then this is a no-op.
     70    */
     71   void put(Range<K> range, V value);
     72 
     73   /**
     74    * Puts all the associations from {@code rangeMap} into this range map (optional operation).
     75    */
     76   void putAll(RangeMap<K, V> rangeMap);
     77 
     78   /**
     79    * Removes all associations from this range map (optional operation).
     80    */
     81   void clear();
     82 
     83   /**
     84    * Removes all associations from this range map in the specified range (optional operation).
     85    *
     86    * <p>If {@code !range.contains(k)}, {@link #get(Comparable) get(k)} will return the same result
     87    * before and after a call to {@code remove(range)}.  If {@code range.contains(k)}, then
     88    * after a call to {@code remove(range)}, {@code get(k)} will return {@code null}.
     89    */
     90   void remove(Range<K> range);
     91 
     92   /**
     93    * Returns a view of this range map as an unmodifiable {@code Map<Range<K>, V>}.
     94    * Modifications to this range map are guaranteed to read through to the returned {@code Map}.
     95    *
     96    * <p>It is guaranteed that no empty ranges will be in the returned {@code Map}.
     97    */
     98   Map<Range<K>, V> asMapOfRanges();
     99 
    100   /**
    101    * Returns a view of the part of this range map that intersects with {@code range}.
    102    *
    103    * <p>For example, if {@code rangeMap} had the entries
    104    * {@code [1, 5] => "foo", (6, 8) => "bar", (10, \u2025) => "baz"}
    105    * then {@code rangeMap.subRangeMap(Range.open(3, 12))} would return a range map
    106    * with the entries {@code (3, 5) => "foo", (6, 8) => "bar", (10, 12) => "baz"}.
    107    *
    108    * <p>The returned range map supports all optional operations that this range map supports,
    109    * except for {@code asMapOfRanges().iterator().remove()}.
    110    *
    111    * <p>The returned range map will throw an {@link IllegalArgumentException} on an attempt to
    112    * insert a range not {@linkplain Range#encloses(Range) enclosed} by {@code range}.
    113    */
    114   RangeMap<K, V> subRangeMap(Range<K> range);
    115 
    116   /**
    117    * Returns {@code true} if {@code obj} is another {@code RangeMap} that has an equivalent
    118    * {@link #asMapOfRanges()}.
    119    */
    120   @Override
    121   boolean equals(@Nullable Object o);
    122 
    123   /**
    124    * Returns {@code asMapOfRanges().hashCode()}.
    125    */
    126   @Override
    127   int hashCode();
    128 
    129   /**
    130    * Returns a readable string representation of this range map.
    131    */
    132   @Override
    133   String toString();
    134 }
    135