ConcurrentHashMapWithOptionals.java

  1. /*
  2.  * *************************************************************************************************************************************************************
  3.  *
  4.  * TheseFoolishThings: Miscellaneous utilities
  5.  * http://tidalwave.it/projects/thesefoolishthings
  6.  *
  7.  * Copyright (C) 2009 - 2024 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.Nonnull;
  28. import java.util.Optional;
  29. import java.util.concurrent.ConcurrentHashMap;

  30. /***************************************************************************************************************************************************************
  31.  *
  32.  * A specialisation of {@link ConcurrentHashMap} with {@link Optional} support.
  33.  *
  34.  * @param <K>   the type of the key
  35.  * @param <V>   the type of the value
  36.  *
  37.  * @since   3.1-ALPHA-2
  38.  * @author  Fabrizio Giudici
  39.  *
  40.  **************************************************************************************************************************************************************/
  41. public class ConcurrentHashMapWithOptionals<K, V> extends ConcurrentHashMap<K, V>
  42.   {
  43.     private static final long serialVersionUID = -1771492882183193296L;

  44.     /***********************************************************************************************************************************************************
  45.      * If the map doesn't contain the given key, put the new pair(key, value), and return the key itself. Otherwise,
  46.      * do nothing and return an empty {@link Optional}. The map manipulation is atomically performed by calling
  47.      * {@link #putIfAbsent(java.lang.Object, java.lang.Object)}.
  48.      *
  49.      * If {@code optionalKey} is empty, do nothing and return nothing.
  50.      *
  51.      * @param   optionalKey     the key
  52.      * @param   value           the value
  53.      * @return                  the new key, or nothing
  54.      **********************************************************************************************************************************************************/
  55.     @Nonnull
  56.     public Optional<K> putIfAbsentAndGetNewKey (@Nonnull final Optional<? extends K> optionalKey, @Nonnull final V value)
  57.       {
  58.         return optionalKey.flatMap(key -> putIfAbsentAndGetNewKey(key, value));
  59.       }

  60.     /***********************************************************************************************************************************************************
  61.      * If the map doesn't contain the given key, put the new pair(key, value), and return the key itself. Otherwise,
  62.      * do nothing and return an empty {@link Optional}. The map manipulation is atomically performed by calling
  63.      * {@link #putIfAbsent(java.lang.Object, java.lang.Object)}.
  64.      *
  65.      * @param   key     the key
  66.      * @param   value   the value
  67.      * @return          the new key, or nothing
  68.      **********************************************************************************************************************************************************/
  69.     @Nonnull
  70.     public Optional<K> putIfAbsentAndGetNewKey (@Nonnull final K key, @Nonnull final V value)
  71.       {
  72.         return (putIfAbsent(key, value) == null) ? Optional.of(key) : Optional.empty();
  73.       }
  74.   }