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 28 import javax.annotation.Nonnull; 29 import java.util.ArrayList; 30 import java.util.Collections; 31 import java.util.HashMap; 32 import java.util.List; 33 import java.util.Map; 34 import java.util.Optional; 35 import it.tidalwave.util.Id; 36 import lombok.RequiredArgsConstructor; 37 38 /*************************************************************************************************************************************************************** 39 * 40 * An implementation of {@link FinderWithIdSupport} based on a {@link Map}. 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 FinderWithIdMapSupport<T, I extends T, F extends ExtendedFinderSupport<T, F>> 53 extends FinderWithIdSupport<T, I, F> 54 { 55 private static final long serialVersionUID = 1L; 56 57 @Nonnull 58 private final Map<Id, I> mapById; 59 60 public FinderWithIdMapSupport() 61 { 62 mapById = Collections.emptyMap(); 63 } 64 65 public FinderWithIdMapSupport (@Nonnull final FinderWithIdMapSupport<T, I, F> other, 66 @Nonnull final Object override) 67 { 68 super(other, override); 69 final FinderWithIdMapSupport<T, I, F> source = getSource(FinderWithIdMapSupport.class, other, override); 70 mapById = new HashMap<>(source.mapById); 71 } 72 73 @Override @Nonnull 74 protected List<T> findAll() 75 { 76 return new ArrayList<>(mapById.values()); 77 } 78 79 @Override @Nonnull 80 protected Optional<T> findById (@Nonnull final Id id) 81 { 82 return Optional.ofNullable(mapById.get(id)); 83 } 84 } 85