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.spi; 27 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 lombok.RequiredArgsConstructor; 36 import static java.util.Collections.emptyList; 37 38 /*************************************************************************************************************************************************************** 39 * 40 * A support class for implementing a {@link Finder} that provides filtering by id. 41 * 42 * @param <T> the product abstract type 43 * @param <I> the product concrete type 44 * @param <F> the {@code Finder} type 45 * @since 3.2-ALPHA-15 46 * 47 * @author Fabrizio Giudici 48 * @it.tidalwave.javadoc.experimental 49 * 50 **************************************************************************************************************************************************************/ 51 @RequiredArgsConstructor 52 public class FinderWithIdSupport<T, I extends T, F extends ExtendedFinderSupport<T, F>> 53 extends HierarchicFinderSupport<T, F> implements FinderWithId<T, F> 54 { 55 private static final long serialVersionUID = 2L; 56 57 @Nonnull 58 /* package */ final Optional<Id> id; 59 60 public FinderWithIdSupport() 61 { 62 id = Optional.empty(); 63 } 64 65 public FinderWithIdSupport (@Nonnull final FinderWithIdSupport<T, I, F> other, @Nonnull final Object override) 66 { 67 super(other, override); 68 final FinderWithIdSupport<T, I, F> source = getSource(FinderWithIdSupport.class, other, override); 69 this.id = source.id; 70 } 71 72 @Override @Nonnull 73 public F withId (@Nonnull final Id id) 74 { 75 return clonedWith(new FinderWithIdSupport<>(Optional.of(id))); 76 } 77 78 @Override @Nonnull 79 protected List<T> computeResults() 80 { 81 return id.map(id -> findById(id).map(Collections::singletonList).orElse(emptyList())).orElse(findAll()); 82 } 83 84 @Nonnull 85 protected Optional<T> findById (@Nonnull final Id id) 86 { 87 throw new UnsupportedOperationException("Must be overridden"); 88 } 89 90 @Nonnull 91 protected List<T> findAll() 92 { 93 throw new UnsupportedOperationException("Must be overridden"); 94 } 95 96 @Nonnull 97 protected Stream<I> streamImpl() 98 { 99 return (Stream<I>)stream(); 100 } 101 } 102