AbstractResourceBundleLoader.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.core.resources.ResourceHandler;
21 import griffon.util.ResourceBundleLoader;
22 
23 import javax.annotation.Nonnull;
24 import javax.annotation.Nullable;
25 import javax.inject.Inject;
26 import java.net.URL;
27 import java.util.List;
28 
29 import static griffon.util.GriffonNameUtils.requireNonBlank;
30 import static java.util.Objects.requireNonNull;
31 
32 /**
33  @author Andres Almiray
34  @since 2.11.0
35  */
36 public abstract class AbstractResourceBundleLoader implements ResourceBundleLoader {
37     protected static final String ERROR_FILENAME_BLANK = "Argument 'fileName' must not be blank";
38     protected static final String ERROR_SUFFIX_BLANK = "Argument 'suffix' must not be blank";
39     protected static final String ERROR_RESOURCE_HANDLER_NULL = "Argument 'resourceHandler' must not be null";
40 
41     protected final ResourceHandler resourceHandler;
42 
43     @Inject
44     public AbstractResourceBundleLoader(@Nonnull ResourceHandler resourceHandler) {
45         this.resourceHandler = requireNonNull(resourceHandler, ERROR_RESOURCE_HANDLER_NULL);
46     }
47 
48     @Nonnull
49     protected ResourceHandler getResourceHandler() {
50         return resourceHandler;
51     }
52 
53     @Nullable
54     protected URL getResourceAsURL(@Nonnull String fileName, @Nonnull String suffix) {
55         requireNonBlank(fileName, ERROR_FILENAME_BLANK);
56         requireNonBlank(suffix, ERROR_SUFFIX_BLANK);
57         return resourceHandler.getResourceAsURL(fileName + suffix);
58     }
59 
60     @Nullable
61     protected List<URL> getResources(@Nonnull String fileName, @Nonnull String suffix) {
62         requireNonBlank(fileName, ERROR_FILENAME_BLANK);
63         requireNonBlank(suffix, ERROR_SUFFIX_BLANK);
64         return resourceHandler.getResources(fileName + suffix);
65     }
66 }