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