001 /*
002 * SPDX-License-Identifier: Apache-2.0
003 *
004 * Copyright 2008-2017 the original author or authors.
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018 package org.codehaus.griffon.runtime.core.resources;
019
020 import griffon.core.resources.NoSuchResourceException;
021 import griffon.core.resources.ResourceResolver;
022
023 import javax.annotation.Nonnull;
024 import javax.annotation.Nullable;
025 import java.beans.PropertyEditor;
026 import java.util.List;
027 import java.util.Locale;
028 import java.util.Map;
029
030 import static griffon.core.editors.PropertyEditorResolver.findEditor;
031 import static java.util.Objects.requireNonNull;
032
033 /**
034 * @author Andres Almiray
035 * @since 2.4.0
036 */
037 public class ResourceResolverDecorator implements ResourceResolver {
038 private final ResourceResolver delegate;
039
040 public ResourceResolverDecorator(@Nonnull ResourceResolver delegate) {
041 this.delegate = requireNonNull(delegate, "Argument 'delegate' must not be null");
042 }
043
044 @Nonnull
045 protected ResourceResolver getDelegate() {
046 return delegate;
047 }
048
049 @Override
050 @Nonnull
051 public Object resolveResource(@Nonnull String key) throws NoSuchResourceException {
052 return getDelegate().resolveResource(key);
053 }
054
055 @Override
056 @Nonnull
057 public Object resolveResource(@Nonnull String key, @Nonnull Locale locale) throws NoSuchResourceException {
058 return getDelegate().resolveResource(key, locale);
059 }
060
061 @Override
062 @Nonnull
063 public Object resolveResource(@Nonnull String key, @Nonnull Object[] args) throws NoSuchResourceException {
064 return getDelegate().resolveResource(key, args);
065 }
066
067 @Override
068 @Nonnull
069 public Object resolveResource(@Nonnull String key, @Nonnull Object[] args, @Nonnull Locale locale) throws NoSuchResourceException {
070 return getDelegate().resolveResource(key, args, locale);
071 }
072
073 @Override
074 @Nonnull
075 public Object resolveResource(@Nonnull String key, @Nonnull List<?> args) throws NoSuchResourceException {
076 return getDelegate().resolveResource(key, args);
077 }
078
079 @Override
080 @Nonnull
081 public Object resolveResource(@Nonnull String key, @Nonnull List<?> args, @Nonnull Locale locale) throws NoSuchResourceException {
082 return getDelegate().resolveResource(key, args, locale);
083 }
084
085 @Override
086 @Nullable
087 public Object resolveResource(@Nonnull String key, @Nullable Object defaultValue) {
088 return getDelegate().resolveResource(key, defaultValue);
089 }
090
091 @Override
092 @Nullable
093 public Object resolveResource(@Nonnull String key, @Nonnull Locale locale, @Nullable Object defaultValue) {
094 return getDelegate().resolveResource(key, locale, defaultValue);
095 }
096
097 @Override
098 @Nullable
099 public Object resolveResource(@Nonnull String key, @Nonnull Object[] args, @Nullable Object defaultValue) {
100 return getDelegate().resolveResource(key, args, defaultValue);
101 }
102
103 @Override
104 @Nullable
105 public Object resolveResource(@Nonnull String key, @Nonnull Object[] args, @Nonnull Locale locale, @Nullable Object defaultValue) {
106 return getDelegate().resolveResource(key, args, locale, defaultValue);
107 }
108
109 @Override
110 @Nullable
111 public Object resolveResource(@Nonnull String key, @Nonnull List<?> args, @Nullable Object defaultValue) {
112 return getDelegate().resolveResource(key, args, defaultValue);
113 }
114
115 @Override
116 @Nullable
117 public Object resolveResource(@Nonnull String key, @Nonnull List<?> args, @Nonnull Locale locale, @Nullable Object defaultValue) {
118 return getDelegate().resolveResource(key, args, locale, defaultValue);
119 }
120
121 @Override
122 @Nonnull
123 public Object resolveResource(@Nonnull String key, @Nonnull Map<String, Object> args) throws NoSuchResourceException {
124 return getDelegate().resolveResource(key, args);
125 }
126
127 @Override
128 @Nonnull
129 public Object resolveResource(@Nonnull String key, @Nonnull Map<String, Object> args, @Nonnull Locale locale) throws NoSuchResourceException {
130 return getDelegate().resolveResource(key, args, locale);
131 }
132
133 @Override
134 @Nullable
135 public Object resolveResource(@Nonnull String key, @Nonnull Map<String, Object> args, @Nullable Object defaultValue) {
136 return getDelegate().resolveResource(key, args, defaultValue);
137 }
138
139 @Override
140 @Nullable
141 public Object resolveResource(@Nonnull String key, @Nonnull Map<String, Object> args, @Nonnull Locale locale, @Nullable Object defaultValue) {
142 return getDelegate().resolveResource(key, args, locale, defaultValue);
143 }
144
145 @Override
146 @Nonnull
147 public Object resolveResourceValue(@Nonnull String key, @Nonnull Locale locale) throws NoSuchResourceException {
148 return getDelegate().resolveResourceValue(key, locale);
149 }
150
151 @Override
152 @Nonnull
153 public String formatResource(@Nonnull String resource, @Nonnull List<?> args) {
154 return getDelegate().formatResource(resource, args);
155 }
156
157 @Override
158 @Nonnull
159 public String formatResource(@Nonnull String resource, @Nonnull Object[] args) {
160 return getDelegate().formatResource(resource, args);
161 }
162
163 @Override
164 @Nonnull
165 public String formatResource(@Nonnull String resource, @Nonnull Map<String, Object> args) {
166 return getDelegate().formatResource(resource, args);
167 }
168
169 @Nullable
170 @Override
171 public <T> T resolveResourceConverted(@Nonnull String key, @Nonnull List<?> args, @Nullable T defaultValue, @Nonnull Class<T> type) {
172 return convertValue(resolveResource(key, args, defaultValue), type);
173 }
174
175 @Nullable
176 @Override
177 public <T> T resolveResourceConverted(@Nonnull String key, @Nonnull List<?> args, @Nonnull Locale locale, @Nullable T defaultValue, @Nonnull Class<T> type) {
178 return convertValue(resolveResource(key, args, locale, defaultValue), type);
179 }
180
181 @Nonnull
182 @Override
183 public <T> T resolveResourceConverted(@Nonnull String key, @Nonnull List<?> args, @Nonnull Locale locale, @Nonnull Class<T> type) throws NoSuchResourceException {
184 return convertValue(resolveResource(key, args, locale), type);
185 }
186
187 @Nonnull
188 @Override
189 public <T> T resolveResourceConverted(@Nonnull String key, @Nonnull List<?> args, @Nonnull Class<T> type) throws NoSuchResourceException {
190 return convertValue(resolveResource(key, args), type);
191 }
192
193 @Nullable
194 @Override
195 public <T> T resolveResourceConverted(@Nonnull String key, @Nonnull Map<String, Object> args, @Nullable T defaultValue, @Nonnull Class<T> type) {
196 return convertValue(resolveResource(key, args, defaultValue), type);
197 }
198
199 @Nullable
200 @Override
201 public <T> T resolveResourceConverted(@Nonnull String key, @Nonnull Map<String, Object> args, @Nonnull Locale locale, @Nullable T defaultValue, @Nonnull Class<T> type) {
202 return convertValue(resolveResource(key, args, locale, defaultValue), type);
203 }
204
205 @Nonnull
206 @Override
207 public <T> T resolveResourceConverted(@Nonnull String key, @Nonnull Map<String, Object> args, @Nonnull Locale locale, @Nonnull Class<T> type) throws NoSuchResourceException {
208 return convertValue(resolveResource(key, args, locale), type);
209 }
210
211 @Nonnull
212 @Override
213 public <T> T resolveResourceConverted(@Nonnull String key, @Nonnull Map<String, Object> args, @Nonnull Class<T> type) throws NoSuchResourceException {
214 return convertValue(resolveResource(key, args), type);
215 }
216
217 @Nullable
218 @Override
219 public <T> T resolveResourceConverted(@Nonnull String key, @Nonnull Object[] args, @Nullable T defaultValue, @Nonnull Class<T> type) {
220 return convertValue(resolveResource(key, args, defaultValue), type);
221 }
222
223 @Nullable
224 @Override
225 public <T> T resolveResourceConverted(@Nonnull String key, @Nonnull Object[] args, @Nonnull Locale locale, @Nullable T defaultValue, @Nonnull Class<T> type) {
226 return convertValue(resolveResource(key, args, locale, defaultValue), type);
227 }
228
229 @Nonnull
230 @Override
231 public <T> T resolveResourceConverted(@Nonnull String key, @Nonnull Object[] args, @Nonnull Locale locale, @Nonnull Class<T> type) throws NoSuchResourceException {
232 return convertValue(resolveResource(key, args, locale), type);
233 }
234
235 @Nonnull
236 @Override
237 public <T> T resolveResourceConverted(@Nonnull String key, @Nonnull Object[] args, @Nonnull Class<T> type) throws NoSuchResourceException {
238 return convertValue(resolveResource(key, args), type);
239 }
240
241 @Nullable
242 @Override
243 public <T> T resolveResourceConverted(@Nonnull String key, @Nullable T defaultValue, @Nonnull Class<T> type) {
244 return convertValue(resolveResource(key, defaultValue), type);
245 }
246
247 @Nullable
248 @Override
249 public <T> T resolveResourceConverted(@Nonnull String key, @Nonnull Locale locale, @Nullable T defaultValue, @Nonnull Class<T> type) {
250 return convertValue(resolveResource(key, locale, defaultValue), type);
251 }
252
253 @Nonnull
254 @Override
255 public <T> T resolveResourceConverted(@Nonnull String key, @Nonnull Locale locale, @Nonnull Class<T> type) throws NoSuchResourceException {
256 return convertValue(resolveResource(key, locale), type);
257 }
258
259 @Nonnull
260 @Override
261 public <T> T resolveResourceConverted(@Nonnull String key, @Nonnull Class<T> type) throws NoSuchResourceException {
262 return convertValue(resolveResource(key), type);
263 }
264
265 @SuppressWarnings("unchecked")
266 protected <T> T convertValue(@Nullable Object value, @Nonnull Class<T> type) {
267 if (value != null) {
268 if (type.isAssignableFrom(value.getClass())) {
269 return (T) value;
270 } else {
271 PropertyEditor editor = findEditor(type);
272 editor.setValue(value);
273 return (T) editor.getValue();
274 }
275 }
276 return null;
277 }
278 }
|