TypeSafeMap.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 javax.annotation.concurrent.Immutable;
  29. import jakarta.annotation.Nonnull;
  30. import java.util.Collection;
  31. import java.util.Collections;
  32. import java.util.Map;
  33. import java.util.Optional;
  34. import java.util.Set;
  35. import java.util.function.BiConsumer;
  36. import it.tidalwave.util.impl.TypeSafeHashMap;

  37. /***************************************************************************************************************************************************************
  38.  *
  39.  * A map that is type safe, i.e. the pairs (key, value) are type-checked. It's immutable.
  40.  *
  41.  * @author  Fabrizio Giudici
  42.  *
  43.  **************************************************************************************************************************************************************/
  44. @Immutable
  45. public interface TypeSafeMap extends Iterable<Map.Entry<Key<?>, Object>>
  46.   {
  47.     /***********************************************************************************************************************************************************
  48.      * Returns a value given its key.
  49.      *
  50.      * @param   <T>   the type
  51.      * @param   key   the key
  52.      * @return        the value
  53.      * @throws        NotFoundException if the key is not found
  54.      * @deprecated    Use {@link #getOptional(Key)} instead
  55.      **********************************************************************************************************************************************************/
  56.     @Nonnull @Deprecated
  57.     public <T> T get (@Nonnull Key<T> key)
  58.       throws NotFoundException;

  59.     /***********************************************************************************************************************************************************
  60.      * Returns an optional value given its key.
  61.      *
  62.      * @param   <T>   the type
  63.      * @param   key   the key
  64.      * @return        the value
  65.      *
  66.      * @since 3.2-ALPHA-1
  67.      **********************************************************************************************************************************************************/
  68.     @Nonnull
  69.     public default <T> Optional<T> getOptional (@Nonnull final Key<? extends T> key)
  70.       {
  71.         try
  72.           {
  73.             return Optional.of(get(key));
  74.           }
  75.         catch (NotFoundException e)
  76.           {
  77.             return Optional.empty();
  78.           }
  79.       }

  80.     /***********************************************************************************************************************************************************
  81.      * Checks whether a pair has been stored.
  82.      *
  83.      * @param   key   the key
  84.      * @return        {@code true} if the pair is present
  85.      **********************************************************************************************************************************************************/
  86.     public boolean containsKey (@Nonnull Key<?> key);

  87.     /***********************************************************************************************************************************************************
  88.      * Returns a set of all the contained keys.
  89.      *
  90.      * @return  the keys as a mutable set
  91.      **********************************************************************************************************************************************************/
  92.     @Nonnull
  93.     public Set<Key<?>> keySet();

  94.     /***********************************************************************************************************************************************************
  95.      * Returns a set of all the contained values.
  96.      *
  97.      * @return  the values as a mutable collection
  98.      * @since   3.2-ALPHA-6
  99.      **********************************************************************************************************************************************************/
  100.     @Nonnull
  101.     public Collection<Object> values();

  102.     /***********************************************************************************************************************************************************
  103.      * Returns a set of all the contained (key, value) pairs.
  104.      *
  105.      * @return  the pairs as a mutable collection
  106.      * @since   3.2-ALPHA-6
  107.      **********************************************************************************************************************************************************/
  108.     @Nonnull
  109.     public Set<Map.Entry<Key<?>, Object>> entrySet();

  110.     /***********************************************************************************************************************************************************
  111.      * Returns the size of this map.
  112.      *
  113.      * @return    the size
  114.      **********************************************************************************************************************************************************/
  115.     @Nonnegative
  116.     public int size();

  117.     /***********************************************************************************************************************************************************
  118.      * Returns the contents as a plain {@link Map}.
  119.      *
  120.      * @return        the contents as a mutable map
  121.      **********************************************************************************************************************************************************/
  122.     @Nonnull
  123.     public Map<Key<?>, Object> asMap();

  124.     /***********************************************************************************************************************************************************
  125.      * Performs the given action on all the pairs (key, value) contained in this map.
  126.      *
  127.      * @param action  the action
  128.      * @param <T>     the value type
  129.      * @since         3.2-ALPHA-10
  130.      **********************************************************************************************************************************************************/
  131.     public <T> void forEach (@Nonnull BiConsumer<? super Key<T>, ? super T> action);

  132.     /***********************************************************************************************************************************************************
  133.      * Create a new instance with an additional pair (key, value=
  134.      *
  135.      * @param   <T>   the type
  136.      * @param   key   the key
  137.      * @param   value the value
  138.      * @return        the new instance
  139.      * @since         3.2-ALPHA-2
  140.      **********************************************************************************************************************************************************/
  141.     @Nonnull
  142.     public <T> TypeSafeMap with (@Nonnull final Key<T> key, @Nonnull final T value);

  143.     /***********************************************************************************************************************************************************
  144.      * Creates a new empty instance.
  145.      *
  146.      * @return        the new instance
  147.      * @since         3.2-ALPHA-2
  148.      **********************************************************************************************************************************************************/
  149.     @Nonnull
  150.     public static TypeSafeMap newInstance()
  151.       {
  152.         return new TypeSafeHashMap(Collections.emptyMap());
  153.       }

  154.     /***********************************************************************************************************************************************************
  155.      * Creates an instance cloning the given map.
  156.      *
  157.      * @param   map   the map to clone
  158.      * @return        the new instance
  159.      * @since         3.2-ALPHA-2
  160.      **********************************************************************************************************************************************************/
  161.     @Nonnull
  162.     public static TypeSafeMap ofCloned (@Nonnull final Map<? extends Key<?>, Object> map)
  163.       {
  164.         return new TypeSafeHashMap(map);
  165.       }
  166.   }