Skip to content

Package: TranslatorCache

TranslatorCache

nameinstructionbranchcomplexitylinemethod
TranslatorCache()
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
cacheTranslator(Class, Class, Translator)
M: 11 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
getCachedTranslator(Class, Class)
M: 9 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
static {...}
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2020, 2022 Eurotech and/or its affiliates and others
3: *
4: * This program and the accompanying materials are made
5: * available under the terms of the Eclipse Public License 2.0
6: * which is available at https://www.eclipse.org/legal/epl-2.0/
7: *
8: * SPDX-License-Identifier: EPL-2.0
9: *
10: * Contributors:
11: * Eurotech - initial API and implementation
12: *******************************************************************************/
13: package org.eclipse.kapua.translator.cache;
14:
15: import org.eclipse.kapua.commons.cache.Cache;
16: import org.eclipse.kapua.commons.cache.LocalCache;
17: import org.eclipse.kapua.message.Message;
18: import org.eclipse.kapua.translator.Translator;
19:
20: import javax.validation.constraints.NotNull;
21:
22: /**
23: * Local {@link Cache} for {@link Translator}.
24: * <p>
25: * This has been introduced to avoid each time to look throught the {@link java.util.ServiceLoader} available {@link Class}es.
26: *
27: * @see Cache
28: * @see LocalCache
29: * @since 1.2.0
30: */
31: public class TranslatorCache extends LocalCache<TranslatorCacheKey, Translator<?, ?>> implements Cache<TranslatorCacheKey, Translator<?, ?>> {
32:
33: private static final TranslatorCache TRANSLATOR_CACHE = new TranslatorCache();
34:
35: private TranslatorCache() {
36: super(50, null);
37: }
38:
39: /**
40: * Gets the {@link Translator} for the given {@link Message} classes if cached.
41: *
42: * @param fromMessageClass The {@link Message} type from which the {@link Translator} {@link Translator#translate(Message)} from.
43: * @param toMessageClass The {@link Message} type to which the {@link Translator} {@link Translator#translate(Message)} to.
44: * @return The matching cached {@link Translator} or {@code null} if not yet cached.
45: * @since 1.2.0
46: */
47: public static <FROM_M extends Message<?, ?>, TO_M extends Message<?, ?>, T extends Translator<FROM_M, TO_M>> T getCachedTranslator(@NotNull Class<? extends FROM_M> fromMessageClass, @NotNull Class<? extends TO_M> toMessageClass) {
48: return (T) TRANSLATOR_CACHE.get(new TranslatorCacheKey(fromMessageClass, toMessageClass));
49: }
50:
51: /**
52: * Caches the {@link Translator} for the given {@link Message} classes.
53: *
54: * @param fromMessageClass The {@link Message} type from which the {@link Translator} {@link Translator#translate(Message)} from.
55: * @param toMessageClass The {@link Message} type to which the {@link Translator} {@link Translator#translate(Message)} to.
56: * @param translator The {@link Translator} to cache.
57: * @since 1.2.0
58: */
59: public static <FROM_M extends Message<?, ?>, TO_M extends Message<?, ?>, T extends Translator<FROM_M, TO_M>> void cacheTranslator(@NotNull Class<? extends FROM_M> fromMessageClass, @NotNull Class<? extends TO_M> toMessageClass, T translator) {
60: TranslatorCacheKey key = new TranslatorCacheKey(fromMessageClass, toMessageClass);
61:
62: TRANSLATOR_CACHE.put(key, translator);
63: }
64: }