|
| 1 | +/* |
| 2 | + This file is part of the iText (R) project. |
| 3 | + Copyright (c) 1998-2023 Apryse Group NV |
| 4 | + Authors: Apryse Software. |
| 5 | +
|
| 6 | + This program is offered under a commercial and under the AGPL license. |
| 7 | + For commercial licensing, contact us at https://itextpdf.com/sales. For AGPL licensing, see below. |
| 8 | +
|
| 9 | + AGPL licensing: |
| 10 | + This program is free software: you can redistribute it and/or modify |
| 11 | + it under the terms of the GNU Affero General Public License as published by |
| 12 | + the Free Software Foundation, either version 3 of the License, or |
| 13 | + (at your option) any later version. |
| 14 | +
|
| 15 | + This program is distributed in the hope that it will be useful, |
| 16 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | + GNU Affero General Public License for more details. |
| 19 | +
|
| 20 | + You should have received a copy of the GNU Affero General Public License |
| 21 | + along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 22 | + */ |
| 23 | +package com.itextpdf.commons.utils; |
| 24 | + |
| 25 | +import java.util.concurrent.ConcurrentHashMap; |
| 26 | +import java.util.function.Supplier; |
| 27 | + |
| 28 | +/** |
| 29 | + * A simple dependency injection container. |
| 30 | + * <p> |
| 31 | + * The container is thread-safe. |
| 32 | + */ |
| 33 | +public class DIContainer { |
| 34 | + |
| 35 | + private static final ConcurrentHashMap<Class<?>, Supplier<Object>> instances = new ConcurrentHashMap<>(); |
| 36 | + |
| 37 | + private final ConcurrentHashMap<Class<?>, Object> localInstances = new ConcurrentHashMap<>(); |
| 38 | + |
| 39 | + static { |
| 40 | + DIContainerConfigurations.loadDefaultConfigurations(); |
| 41 | + } |
| 42 | + |
| 43 | + |
| 44 | + /** |
| 45 | + * Creates a new instance of {@link DIContainer}. |
| 46 | + */ |
| 47 | + public DIContainer() { |
| 48 | + // Empty constructor |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Registers a default instance for a class. |
| 53 | + * |
| 54 | + * @param clazz the class |
| 55 | + * @param supplier supplier of the instance |
| 56 | + */ |
| 57 | + public static void registerDefault(Class<?> clazz, Supplier<Object> supplier) { |
| 58 | + instances.put(clazz, supplier); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Registers an instance for a class. |
| 63 | + * |
| 64 | + * @param clazz the class |
| 65 | + * @param inst the instance |
| 66 | + */ |
| 67 | + public void register(Class<?> clazz, Object inst) { |
| 68 | + localInstances.put(clazz, inst); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Gets an instance of a class. |
| 73 | + * |
| 74 | + * @param clazz the class |
| 75 | + * @param <T> the type of the class |
| 76 | + * |
| 77 | + * @return the instance |
| 78 | + */ |
| 79 | + public <T> T getInstance(Class<T> clazz) { |
| 80 | + Object supplier = localInstances.get(clazz); |
| 81 | + if (supplier == null) { |
| 82 | + supplier = instances.get(clazz).get(); |
| 83 | + } |
| 84 | + if (supplier == null) { |
| 85 | + throw new RuntimeException("No instance registered for class " + clazz.getName()); |
| 86 | + } |
| 87 | + return (T) supplier; |
| 88 | + } |
| 89 | + |
| 90 | + |
| 91 | +} |
| 92 | + |
0 commit comments