AbstractCompositeResourceBundleBuilder.java
01 /*
02  * SPDX-License-Identifier: Apache-2.0
03  *
04  * Copyright 2008-2017 the original author or authors.
05  *
06  * Licensed under the Apache License, Version 2.0 (the "License");
07  * you may not use this file except in compliance with the License.
08  * You may obtain a copy of the License at
09  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 package org.codehaus.griffon.runtime.util;
19 
20 import griffon.util.CompositeResourceBundle;
21 import griffon.util.CompositeResourceBundleBuilder;
22 
23 import javax.annotation.Nonnull;
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import java.util.List;
27 import java.util.Locale;
28 import java.util.ResourceBundle;
29 
30 import static griffon.util.GriffonNameUtils.requireNonBlank;
31 import static java.util.Objects.requireNonNull;
32 
33 /**
34  @author Andres Almiray
35  @since 2.0.0
36  */
37 public abstract class AbstractCompositeResourceBundleBuilder implements CompositeResourceBundleBuilder {
38     protected static final String ERROR_BASENAME_BLANK = "Argument 'basename' must not be blank";
39     protected static final String ERROR_LOCALE_NULL = "Argument 'locale' must not be null";
40 
41     @Override
42     @Nonnull
43     public ResourceBundle create(@Nonnull String basename) {
44         return create(basename, Locale.getDefault());
45     }
46 
47     @Override
48     @Nonnull
49     public ResourceBundle create(@Nonnull String basename, @Nonnull Locale locale) {
50         requireNonBlank(basename, ERROR_BASENAME_BLANK);
51         requireNonNull(locale, ERROR_LOCALE_NULL);
52 
53         initialize();
54 
55         String[] combinations = {
56             locale.getLanguage() "_" + locale.getCountry() "_" + locale.getVariant(),
57             locale.getLanguage() "_" + locale.getCountry(),
58             locale.getLanguage()
59         };
60 
61         basename = basename.replace('.''/');
62         List<ResourceBundle> bundles = new ArrayList<>();
63         for (String suffix : combinations) {
64             if (suffix.endsWith("_")) { continue}
65             bundles.addAll(loadBundlesFor(basename + "_" + suffix));
66         }
67         bundles.addAll(loadBundlesFor(basename));
68         if (bundles.isEmpty()) {
69             throw new IllegalArgumentException("There are no ResourceBundle resources matching " + basename);
70         }
71 
72         return new CompositeResourceBundle(bundles);
73     }
74 
75     protected abstract void initialize();
76 
77     @Nonnull
78     protected abstract Collection<ResourceBundle> loadBundlesFor(@Nonnull String basename);
79 }