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