01 /*
02 * Copyright 2008-2017 the original author or authors.
03 *
04 * Licensed under the Apache License, Version 2.0 (the "License");
05 * you may not use this file except in compliance with the License.
06 * You may obtain a copy of the License at
07 *
08 * http://www.apache.org/licenses/LICENSE-2.0
09 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.codehaus.griffon.runtime.core.configuration;
17
18 import griffon.util.AbstractMapResourceBundle;
19
20 import javax.annotation.Nonnull;
21 import javax.annotation.Nullable;
22 import java.util.Collections;
23 import java.util.Map;
24 import java.util.MissingResourceException;
25 import java.util.ResourceBundle;
26
27 import static griffon.util.ConfigUtils.getConfigValue;
28 import static griffon.util.GriffonNameUtils.requireNonBlank;
29 import static java.util.Objects.requireNonNull;
30
31 /**
32 * @author Andres Almiray
33 * @since 2.11.0
34 */
35 public abstract class AbstractMapConfiguration extends AbstractConfiguration {
36 protected static final String ERROR_KEY_BLANK = "Argument 'key' must not be blank";
37 private static final String ERROR_MAP_NULL = "Argument 'map' must not be null";
38 private final Map<String, Object> map;
39
40 protected AbstractMapConfiguration(@Nonnull Map<String, Object> map) {
41 this.map = requireNonNull(map, ERROR_MAP_NULL);
42 }
43
44 @Override
45 public boolean containsKey(@Nonnull String key) {
46 return map.containsKey(requireNonBlank(key, ERROR_KEY_BLANK));
47 }
48
49 @Nonnull
50 @Override
51 public Map<String, Object> asFlatMap() {
52 return Collections.unmodifiableMap(map);
53 }
54
55 @Nonnull
56 @Override
57 public ResourceBundle asResourceBundle() {
58 return new AbstractMapResourceBundle() {
59 @Override
60 protected void initialize(@Nonnull Map<String, Object> entries) {
61 entries.putAll(map);
62 }
63 };
64 }
65
66 @Nullable
67 @Override
68 public Object get(@Nonnull String key) {
69 try {
70 return getConfigValue(map, key);
71 } catch (MissingResourceException mre) {
72 return null;
73 }
74 }
75 }
|