You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GlobalBundleCache.java 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * Copyright (C) 2010, Sasa Zivkov <sasa.zivkov@sap.com> and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.nls;
  11. import java.lang.reflect.InvocationTargetException;
  12. import java.util.HashMap;
  13. import java.util.Locale;
  14. import java.util.Map;
  15. import org.eclipse.jgit.errors.TranslationBundleLoadingException;
  16. import org.eclipse.jgit.errors.TranslationStringMissingException;
  17. /**
  18. * Global cache of translation bundles.
  19. * <p>
  20. * Every translation bundle will be cached here when it gets loaded for the
  21. * first time from a thread. Another lookup for the same translation bundle
  22. * (same locale and type) from the same or a different thread will return the
  23. * cached one.
  24. * <p>
  25. * Note that NLS instances maintain per-thread Map of loaded translation
  26. * bundles. Once a thread accesses a translation bundle it will keep reference
  27. * to it and will not call {@link #lookupBundle(Locale, Class)} again for the
  28. * same translation bundle as long as its locale doesn't change.
  29. */
  30. class GlobalBundleCache {
  31. private static final Map<Locale, Map<Class, TranslationBundle>> cachedBundles
  32. = new HashMap<>();
  33. /**
  34. * Looks up for a translation bundle in the global cache. If found returns
  35. * the cached bundle. If not found creates a new instance puts it into the
  36. * cache and returns it.
  37. *
  38. * @param <T>
  39. * required bundle type
  40. * @param locale
  41. * the preferred locale
  42. * @param type
  43. * required bundle type
  44. * @return an instance of the required bundle type
  45. * @exception TranslationBundleLoadingException see {@link TranslationBundle#load(Locale)}
  46. * @exception TranslationStringMissingException see {@link TranslationBundle#load(Locale)}
  47. */
  48. @SuppressWarnings("unchecked")
  49. static synchronized <T extends TranslationBundle> T lookupBundle(Locale locale, Class<T> type) {
  50. try {
  51. Map<Class, TranslationBundle> bundles = cachedBundles.get(locale);
  52. if (bundles == null) {
  53. bundles = new HashMap<>();
  54. cachedBundles.put(locale, bundles);
  55. }
  56. TranslationBundle bundle = bundles.get(type);
  57. if (bundle == null) {
  58. bundle = type.getDeclaredConstructor().newInstance();
  59. bundle.load(locale);
  60. bundles.put(type, bundle);
  61. }
  62. return (T) bundle;
  63. } catch (InstantiationException | IllegalAccessException
  64. | InvocationTargetException | NoSuchMethodException e) {
  65. throw new Error(e);
  66. }
  67. }
  68. static void clear() {
  69. cachedBundles.clear();
  70. }
  71. }