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.mvc;
019
020 import griffon.core.mvc.MVCGroup;
021 import griffon.core.mvc.MVCGroupConfiguration;
022
023 import javax.annotation.Nonnull;
024 import javax.annotation.Nullable;
025 import java.util.Collections;
026 import java.util.LinkedHashMap;
027 import java.util.Map;
028
029 import static griffon.util.GriffonNameUtils.requireNonBlank;
030 import static java.util.Collections.unmodifiableMap;
031 import static java.util.Objects.requireNonNull;
032
033 /**
034 * Base implementation of the {@code MVCGroupConfiguration} interface
035 *
036 * @author Andres Almiray
037 * @since 2.0.0
038 */
039 public abstract class AbstractMVCGroupConfiguration implements MVCGroupConfiguration {
040 private static final String ERROR_MEMBERS_NULL = "Argument 'members' must not be null";
041 private static final String ERROR_ARGS_NULL = "Argument 'args' must not be null";
042 protected final Map<String, String> members = new LinkedHashMap<>();
043 protected final Map<String, Object> config = new LinkedHashMap<>();
044 protected final String mvcType;
045
046 public AbstractMVCGroupConfiguration(@Nonnull String mvcType, @Nonnull Map<String, String> members, @Nonnull Map<String, Object> config) {
047 this.mvcType = requireNonBlank(mvcType, "Argument 'mvcType' must not be blank");
048 this.members.putAll(requireNonNull(members, ERROR_MEMBERS_NULL));
049 this.config.putAll(requireNonNull(config, "Argument 'config' must not be null"));
050 }
051
052 @Override
053 public String toString() {
054 return "MVCGroupConfiguration{" +
055 "mvcType='" + mvcType + '\'' +
056 ", members=" + members +
057 ", config=" + config +
058 '}';
059 }
060
061 @Nonnull
062 @Override
063 public String getMvcType() {
064 return mvcType;
065 }
066
067 @Nonnull
068 @Override
069 public Map<String, String> getMembers() {
070 return unmodifiableMap(members);
071 }
072
073 @Nonnull
074 @Override
075 public Map<String, Object> getConfig() {
076 return unmodifiableMap(config);
077 }
078
079 @Nonnull
080 @Override
081 public MVCGroup create() {
082 return create(null, Collections.<String, Object>emptyMap());
083 }
084
085 @Nonnull
086 @Override
087 public MVCGroup create(@Nullable String mvcId) {
088 return create(mvcId, Collections.<String, Object>emptyMap());
089 }
090
091 @Nonnull
092 @Override
093 public MVCGroup create(@Nonnull Map<String, Object> args) {
094 return create(null, args);
095 }
096
097 @Nonnull
098 @Override
099 public MVCGroup create(@Nullable String mvcId, @Nonnull Map<String, Object> args) {
100 requireNonNull(args, ERROR_ARGS_NULL);
101 return instantiateMVCGroup(mvcId, args);
102 }
103
104 @Nonnull
105 protected abstract MVCGroup instantiateMVCGroup(@Nullable String mvcId, @Nonnull Map<String, Object> args);
106 }
|