TypeSafeMultiMap.java

  1. /*
  2.  * *************************************************************************************************************************************************************
  3.  *
  4.  * TheseFoolishThings: Miscellaneous utilities
  5.  * http://tidalwave.it/projects/thesefoolishthings
  6.  *
  7.  * Copyright (C) 2009 - 2025 by Tidalwave s.a.s. (http://tidalwave.it)
  8.  *
  9.  * *************************************************************************************************************************************************************
  10.  *
  11.  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
  12.  * You may obtain a copy of the License at
  13.  *
  14.  *     http://www.apache.org/licenses/LICENSE-2.0
  15.  *
  16.  * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  17.  * CONDITIONS OF ANY KIND, either express or implied.  See the License for the specific language governing permissions and limitations under the License.
  18.  *
  19.  * *************************************************************************************************************************************************************
  20.  *
  21.  * git clone https://bitbucket.org/tidalwave/thesefoolishthings-src
  22.  * git clone https://github.com/tidalwave-it/thesefoolishthings-src
  23.  *
  24.  * *************************************************************************************************************************************************************
  25.  */
  26. package it.tidalwave.util;

  27. // import javax.annotation.Nonnegative;
  28. import jakarta.annotation.Nonnull;
  29. import java.util.Collection;
  30. import java.util.Collections;
  31. import java.util.Map;
  32. import java.util.Set;
  33. import java.util.function.BiConsumer;
  34. import it.tidalwave.util.impl.TypeSafeHashMultiMap;

  35. /***************************************************************************************************************************************************************
  36.  *
  37.  * A map that is type safe, i.e. the pairs (key, value) are type-checked, and can contain multiple values, i.e. it's
  38.  * associated to collections (key, collection of values). It's immutable.
  39.  *
  40.  * @author  Fabrizio Giudici
  41.  *
  42.  **************************************************************************************************************************************************************/
  43. public interface TypeSafeMultiMap extends Iterable<Map.Entry<Key<?>, Collection<?>>>
  44.   {
  45.     /***********************************************************************************************************************************************************
  46.      * Returns a value given its key.
  47.      *
  48.      * @param   <T>   the type
  49.      * @param   key   the key
  50.      * @return        the value as a {@link Collection}
  51.      **********************************************************************************************************************************************************/
  52.     @Nonnull
  53.     public <T> Collection<T> get (@Nonnull Key<T> key);

  54.     /***********************************************************************************************************************************************************
  55.      * Checks whether a pair has been stored.
  56.      *
  57.      * @param   key   the key
  58.      * @return        {@code true} if the pair is present
  59.      **********************************************************************************************************************************************************/
  60.     public boolean containsKey (@Nonnull Key<?> key);

  61.     /***********************************************************************************************************************************************************
  62.      * Returns a set of all the contained keys.
  63.      *
  64.      * @return        the keys as a mutable set
  65.      **********************************************************************************************************************************************************/
  66.     @Nonnull
  67.     public Set<Key<?>> keySet();

  68.     /***********************************************************************************************************************************************************
  69.      * Returns a set of all the contained values.
  70.      *
  71.      * @return        the values as a mutable collection
  72.      * @since         3.2-ALPHA-6
  73.      **********************************************************************************************************************************************************/
  74.     @Nonnull
  75.     public Collection<Collection<?>> values();

  76.     /***********************************************************************************************************************************************************
  77.      * Returns a set of all the contained (key, value) pairs.
  78.      *
  79.      * @return        the pairs as a mutable collection
  80.      * @since         3.2-ALPHA-6
  81.      **********************************************************************************************************************************************************/
  82.      @Nonnull
  83.      public Set<Map.Entry<Key<?>, Collection<?>>> entrySet();

  84.     /***********************************************************************************************************************************************************
  85.      * Returns the size of this map.
  86.      *
  87.      * @return        the size
  88.      **********************************************************************************************************************************************************/
  89.     /* @Nonnegative */
  90.     public int size();

  91.     /***********************************************************************************************************************************************************
  92.      * Performs the given action on all the pairs (key, value) contained in this map.
  93.      *
  94.      * @param action  the action
  95.      * @param <T>     the value type
  96.      * @since         3.2-ALPHA-10
  97.      **********************************************************************************************************************************************************/
  98.     public <T> void forEach (@Nonnull BiConsumer<? super Key<T>, ? super Collection<T>> action);

  99.     /***********************************************************************************************************************************************************
  100.      * Returns the contents as a plain {@link Map}.
  101.      *
  102.      * @return        the contents as a mutable map
  103.      **********************************************************************************************************************************************************/
  104.     @Nonnull
  105.     public Map<Key<?>, Collection<?>> asMap();

  106.     /***********************************************************************************************************************************************************
  107.      * Creates a new instance with an additional pair (key, value).
  108.      *
  109.      * @param   <T>   the type
  110.      * @param   key   the key
  111.      * @param   value the value
  112.      * @return        the new instance
  113.      * @since         3.2-ALPHA-2
  114.      **********************************************************************************************************************************************************/
  115.     @Nonnull
  116.     public <T> TypeSafeMultiMap with (@Nonnull final Key<T> key, @Nonnull final T value);

  117.     /***********************************************************************************************************************************************************
  118.      * Creates an instance cloning the given map.
  119.      *
  120.      * @param   map   the map to clone
  121.      * @return        the new instance
  122.      * @since         3.2-ALPHA-2
  123.      **********************************************************************************************************************************************************/
  124.     @Nonnull
  125.     public static TypeSafeMultiMap ofCloned (@Nonnull final Map<? extends Key<?>, ? extends Collection<?>> map)
  126.       {
  127.         return new TypeSafeHashMultiMap(map);
  128.       }

  129.     /***********************************************************************************************************************************************************
  130.      * Creates a new empty instance.
  131.      *
  132.      * @return        the new instance
  133.      * @since         3.2-ALPHA-2
  134.      **********************************************************************************************************************************************************/
  135.     @Nonnull
  136.     public static TypeSafeMultiMap newInstance()
  137.       {
  138.         return new TypeSafeHashMultiMap(Collections.emptyMap());
  139.       }
  140.   }