View Javadoc
1   /*
2    * Oceanus: Java Utilities
3    * Copyright 2012-2026. Tony Washer
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
6    * use this file except in compliance with the License.  You may obtain a copy
7    * of the License at
8    *
9    *   http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
14   * License for the specific language governing permissions and limitations under
15   * the License.
16   */
17  package io.github.tonywasher.joceanus.oceanus.resource;
18  
19  import java.lang.ref.WeakReference;
20  import java.util.Map;
21  import java.util.ResourceBundle;
22  import java.util.function.Function;
23  
24  /**
25   * Bundle Loader.
26   */
27  public final class OceanusBundleLoader {
28      /**
29       * String Builder.
30       */
31      private final StringBuilder theBuilder = new StringBuilder();
32  
33      /**
34       * BundleName.
35       */
36      private final String theBundleName;
37  
38      /**
39       * BundleLoader.
40       */
41      private final Function<String, ResourceBundle> theLoader;
42  
43      /**
44       * ResourceBundle.
45       */
46      private WeakReference<ResourceBundle> theBundle;
47  
48      /**
49       * Standard constructor.
50       *
51       * @param pBundleName the Bundle Name
52       * @param pLoader     the loader
53       */
54      private OceanusBundleLoader(final String pBundleName,
55                                  final Function<String, ResourceBundle> pLoader) {
56          theBundleName = pBundleName;
57          theLoader = pLoader;
58      }
59  
60      /**
61       * Obtain standard loader.
62       *
63       * @param pBundleName the Bundle Name
64       * @param pLoader     the bundle loader
65       * @return the loader
66       */
67      public static OceanusBundleLoader getLoader(final String pBundleName,
68                                                  final Function<String, ResourceBundle> pLoader) {
69          return new OceanusBundleLoader(pBundleName, pLoader);
70      }
71  
72      /**
73       * Build name of resource for Key.
74       *
75       * @param <K>  the resource key data type
76       * @param pKey the resource Key
77       * @return the resourceName
78       */
79      public <K extends Enum<K> & OceanusBundleId> String getValue(final K pKey) {
80          /* Build the required name */
81          theBuilder.setLength(0);
82          theBuilder.append(pKey.getNameSpace());
83          theBuilder.append('.');
84          theBuilder.append(pKey.getKeyName());
85  
86          /* Access the cached bundle */
87          ResourceBundle myBundle = theBundle == null
88                  ? null
89                  : theBundle.get();
90  
91          /* If the bundle is not cached */
92          if (myBundle == null) {
93              /* Access and cache the bundle */
94              myBundle = theLoader.apply(theBundleName);
95              theBundle = new WeakReference<>(myBundle);
96          }
97  
98          /* Access the required resource */
99          return myBundle.getString(theBuilder.toString());
100     }
101 
102     /**
103      * Obtain key for enum.
104      *
105      * @param <E>    the enum type
106      * @param pMap   the map
107      * @param pValue the enum value
108      * @return the resource key
109      */
110     public static <E extends Enum<E>> OceanusBundleId getKeyForEnum(final Map<E, OceanusBundleId> pMap,
111                                                                     final E pValue) {
112         final OceanusBundleId myId = pMap.get(pValue);
113         if (myId == null) {
114             throw new IllegalArgumentException(OceanusResourceLoader.getErrorNoResource(pValue));
115         }
116         return myId;
117     }
118 }