FinderWithIdSupport.java

  1. /*
  2.  * *********************************************************************************************************************
  3.  *
  4.  * TheseFoolishThings: Miscellaneous utilities
  5.  * http://tidalwave.it/projects/thesefoolishthings
  6.  *
  7.  * Copyright (C) 2009 - 2023 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
  12.  * the License. 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
  17.  * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the
  18.  * specific language governing permissions and limitations under the License.
  19.  *
  20.  * *********************************************************************************************************************
  21.  *
  22.  * git clone https://bitbucket.org/tidalwave/thesefoolishthings-src
  23.  * git clone https://github.com/tidalwave-it/thesefoolishthings-src
  24.  *
  25.  * *********************************************************************************************************************
  26.  */
  27. package it.tidalwave.util.spi;

  28. import javax.annotation.Nonnull;
  29. import java.util.Collections;
  30. import java.util.List;
  31. import java.util.Optional;
  32. import java.util.stream.Stream;
  33. import it.tidalwave.util.Finder;
  34. import it.tidalwave.util.Id;
  35. import static java.util.Collections.*;
  36. import lombok.RequiredArgsConstructor;

  37. /***********************************************************************************************************************
  38.  *
  39.  * A support class for implementing a {@link Finder} that provides filtering by id.
  40.  *
  41.  * @param <T>     the product abstract type
  42.  * @param <I>     the product concrete type
  43.  * @param <F>     the {@code Finder} type
  44.  * @since         3.2-ALPHA-15
  45.  *
  46.  * @author  Fabrizio Giudici
  47.  * @it.tidalwave.javadoc.experimental
  48.  *
  49.  **********************************************************************************************************************/
  50. @RequiredArgsConstructor
  51. public class FinderWithIdSupport<T, I extends T, F extends ExtendedFinderSupport<T, F>>
  52.         extends HierarchicFinderSupport<T, F> implements FinderWithId<T, F>
  53.   {
  54.     private static final long serialVersionUID = 2L;

  55.     @Nonnull
  56.     /* package */ final Optional<Id> id;

  57.     public FinderWithIdSupport()
  58.       {
  59.         id = Optional.empty();
  60.       }

  61.     public FinderWithIdSupport (@Nonnull final FinderWithIdSupport<T, I, F> other, @Nonnull final Object override)
  62.       {
  63.         super(other, override);
  64.         final FinderWithIdSupport<T, I, F> source = getSource(FinderWithIdSupport.class, other, override);
  65.         this.id = source.id;
  66.       }

  67.     @Override @Nonnull
  68.     public F withId (@Nonnull final Id id)
  69.       {
  70.         return clonedWith(new FinderWithIdSupport<>(Optional.of(id)));
  71.       }

  72.     @Override @Nonnull
  73.     protected List<T> computeResults()
  74.       {
  75.         return id.map(id -> findById(id).map(Collections::singletonList).orElse(emptyList())).orElse(findAll());
  76.       }

  77.     @Nonnull
  78.     protected Optional<T> findById (@Nonnull final Id id)
  79.       {
  80.         throw new UnsupportedOperationException("Must be overridden");
  81.       }

  82.     @Nonnull
  83.     protected List<T> findAll()
  84.       {
  85.         throw new UnsupportedOperationException("Must be overridden");
  86.       }

  87.     @Nonnull
  88.     protected Stream<I> streamImpl()
  89.       {
  90.         return (Stream<I>)stream();
  91.       }
  92.   }