Package: LocalizationAdapter

LocalizationAdapter

nameinstructionbranchcomplexitylinemethod
adapt(Class)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
adapt(Class, Locale)
M: 24 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
static {...}
M: 8 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2016, 2017 Eurotech and/or its affiliates and others
3: *
4: * All rights reserved. This program and the accompanying materials
5: * are made available under the terms of the Eclipse Public License v1.0
6: * which accompanies this distribution, and is available at
7: * http://www.eclipse.org/legal/epl-v10.html
8: *
9: * Contributors:
10: * Eurotech
11: * Amit Kumar Mondal
12: *
13: *******************************************************************************/
14: package org.eclipse.kura.localization;
15:
16: import static java.util.Objects.requireNonNull;
17:
18: import java.lang.reflect.InvocationHandler;
19: import java.lang.reflect.Method;
20: import java.lang.reflect.Proxy;
21: import java.util.HashMap;
22: import java.util.Locale;
23: import java.util.Map;
24:
25: import com.github.rodionmoiseev.c10n.C10N;
26: import com.github.rodionmoiseev.c10n.annotations.DefaultC10NAnnotations;
27:
28: /**
29: * The {@link LocalizationAdapter} is an utility class to adapt the message
30: * resources to their {@code C10N} instance for internationalization
31: *
32: * The available locale options for resources are as follows:
33: *
34: * <ul>
35: * <li>EN : English</li>
36: * <li>DE : German</li>
37: * <li>FR : French</li>
38: * <li>IT : Italian</li>
39: * <li>JA : Japanese</li>
40: * <li>KO : Korean</li>
41: * <li>RU : Russian</li>
42: * <li>ZH : Chinese</li>
43: * </ul>
44: */
45: public final class LocalizationAdapter {
46:
47: private static final Locale CURRENT_LOCALE;
48:
49: static {
50: C10N.configure(new DefaultC10NAnnotations());
51: CURRENT_LOCALE = Locale.getDefault();
52: }
53:
54: private LocalizationAdapter() {
55: // Static Factory Methods container. No need to instantiate.
56: }
57:
58: private static class C10NWrapper<T> implements InvocationHandler {
59:
60: private final T wrapped;
61: private final Map<Method, String> cache = new HashMap<>();
62:
63: public C10NWrapper(final T wrapped) {
64: this.wrapped = wrapped;
65: }
66:
67: @Override
68: public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
69: if (args != null && args.length > 0) {
70: return method.invoke(this.wrapped, args);
71: }
72:
73: String cached = this.cache.get(method);
74: if (cached != null) {
75: return cached;
76: }
77:
78: cached = (String) method.invoke(this.wrapped, args);
79:
80: this.cache.put(method, cached);
81: return cached;
82: }
83: }
84:
85: /**
86: * Adapt the provided message resource to its {@code C10N} type with the current locale.
87: *
88: * @param <T>
89: * the generic type
90: * @param clazz
91: * the message resource
92: * @throws NullPointerException
93: * if the argument is null
94: * @return the instance of the {@code C10N} resource
95: */
96: public static <T> T adapt(final Class<T> clazz) {
97: return adapt(clazz, CURRENT_LOCALE);
98: }
99:
100: /**
101: * Adapt the provided message resource to its {@code C10N} type with provided locale.
102: *
103: * @param <T>
104: * the generic type
105: * @param clazz
106: * the message resource
107: * @param locale
108: * the {@link Locale} instance to use
109: * @throws NullPointerException
110: * if any of the arguments is null
111: * @return the instance of the {@code C10N} resource adapting the specified {@link Locale} instance
112: */
113: @SuppressWarnings("unchecked")
114: public static <T> T adapt(final Class<T> clazz, final Locale locale) {
115: requireNonNull(clazz, "Class instance of localization resource cannot be null");
116: requireNonNull(clazz, "Locale instance cannot be null");
117: return (T) Proxy.newProxyInstance(clazz.getClassLoader(), new Class<?>[] { clazz },
118: new C10NWrapper<>(C10N.get(clazz, locale)));
119: }
120: }