FinderWithIdMapSupport.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.spi;

  27. import javax.annotation.Nonnull;
  28. import java.util.ArrayList;
  29. import java.util.Collections;
  30. import java.util.HashMap;
  31. import java.util.List;
  32. import java.util.Map;
  33. import java.util.Optional;
  34. import it.tidalwave.util.Id;
  35. import lombok.RequiredArgsConstructor;

  36. /***************************************************************************************************************************************************************
  37.  *
  38.  * An implementation of {@link FinderWithIdSupport} based on a {@link Map}.
  39.  *
  40.  * @param <T>     the product abstract type
  41.  * @param <I>     the product concrete type
  42.  * @param <F>     the {@code Finder} type
  43.  * @since         3.2-ALPHA-15
  44.  *
  45.  * @author  Fabrizio Giudici
  46.  * @it.tidalwave.javadoc.experimental
  47.  *
  48.  **************************************************************************************************************************************************************/
  49. @RequiredArgsConstructor
  50. public class FinderWithIdMapSupport<T, I extends T, F extends ExtendedFinderSupport<T, F>>
  51.         extends FinderWithIdSupport<T, I, F>
  52.   {
  53.     private static final long serialVersionUID = 1L;

  54.     @Nonnull
  55.     private final Map<Id, I> mapById;

  56.     public FinderWithIdMapSupport()
  57.       {
  58.         mapById = Collections.emptyMap();
  59.       }

  60.     public FinderWithIdMapSupport (@Nonnull final FinderWithIdMapSupport<T, I, F> other,
  61.                                    @Nonnull final Object override)
  62.       {
  63.         super(other, override);
  64.         final FinderWithIdMapSupport<T, I, F> source = getSource(FinderWithIdMapSupport.class, other, override);
  65.         mapById = new HashMap<>(source.mapById);
  66.       }

  67.     @Override @Nonnull
  68.     protected List<T> findAll()
  69.       {
  70.         return new ArrayList<>(mapById.values());
  71.       }

  72.     @Override @Nonnull
  73.     protected Optional<T> findById (@Nonnull final Id id)
  74.       {
  75.         return Optional.ofNullable(mapById.get(id));
  76.       }
  77.   }