AbstractObjectFactory.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.core.storage;
19 
20 
21 import griffon.core.Configuration;
22 import griffon.core.GriffonApplication;
23 import griffon.core.storage.ObjectFactory;
24 
25 import javax.annotation.Nonnull;
26 import javax.inject.Inject;
27 import java.util.Collections;
28 import java.util.List;
29 import java.util.Map;
30 
31 import static griffon.util.ConfigUtils.getConfigValue;
32 import static griffon.util.GriffonNameUtils.requireNonBlank;
33 import static java.util.Objects.requireNonNull;
34 
35 /**
36  @author Andres Almiray
37  @since 2.0.0
38  */
39 public abstract class AbstractObjectFactory<T> implements ObjectFactory<T> {
40     private final Configuration configuration;
41     private final GriffonApplication application;
42 
43     @Inject
44     public AbstractObjectFactory(@Nonnull Configuration configuration, @Nonnull GriffonApplication application) {
45         this.configuration = requireNonNull(configuration, "Argument 'configuration' must not be null");
46         this.application = requireNonNull(application, "Argument 'application' must not be null");
47     }
48 
49     @Nonnull
50     public Configuration getConfiguration() {
51         return configuration;
52     }
53 
54     @Nonnull
55     public GriffonApplication getApplication() {
56         return application;
57     }
58 
59     @Nonnull
60     protected abstract String getSingleKey();
61 
62     @Nonnull
63     protected abstract String getPluralKey();
64 
65     protected void event(@Nonnull String eventName, @Nonnull List<?> args) {
66         application.getEventRouter().publishEvent(eventName, args);
67     }
68 
69     @Nonnull
70     @SuppressWarnings({"unchecked""ConstantConditions"})
71     protected Map<String, Object> narrowConfig(@Nonnull String name) {
72         requireNonBlank(name, "Argument 'name' must not be blank");
73         if (KEY_DEFAULT.equals(name&& configuration.containsKey(getSingleKey())) {
74             return (Map<String, Object>configuration.get(getSingleKey());
75         else if (configuration.containsKey(getPluralKey())) {
76             Map<String, Object> elements = (Map<String, Object>configuration.get(getPluralKey());
77             return getConfigValue(elements, name, Collections.<String, Object>emptyMap());
78         }
79         return Collections.emptyMap();
80     }
81 }