AbstractBuilderCustomizer.java
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.groovy.view;
017 
018 import griffon.util.BuilderCustomizer;
019 import groovy.lang.Closure;
020 import groovy.util.Factory;
021 
022 import javax.annotation.Nonnull;
023 import javax.annotation.Nullable;
024 import java.util.ArrayList;
025 import java.util.LinkedHashMap;
026 import java.util.List;
027 import java.util.Map;
028 
029 import static java.util.Collections.unmodifiableList;
030 import static java.util.Collections.unmodifiableMap;
031 import static java.util.Objects.requireNonNull;
032 
033 /**
034  @author Andres Almiray
035  @since 2.0.0
036  */
037 @SuppressWarnings("rawtypes")
038 public class AbstractBuilderCustomizer implements BuilderCustomizer {
039     private final Map<String, Object> variables = new LinkedHashMap<>();
040     private final Map<String, Factory> factories = new LinkedHashMap<>();
041     private final Map<String, Closure> methods = new LinkedHashMap<>();
042     private final Map<String, Closure[]> props = new LinkedHashMap<>();
043     private final List<Closure> attributeDelegates = new ArrayList<>();
044     private final List<Closure> preInstantiateDelegates = new ArrayList<>();
045     private final List<Closure> postInstantiateDelegates = new ArrayList<>();
046     private final List<Closure> postNodeCompletionDelegates = new ArrayList<>();
047     private final List<Closure> disposalClosures = new ArrayList<>();
048     private Closure methodMissingDelegate;
049     private Closure propertyMissingDelegate;
050 
051     @Nonnull
052     public Map<String, Object> getVariables() {
053         return unmodifiableMap(variables);
054     }
055 
056     public void setVariables(@Nonnull Map<String, Object> variables) {
057         requireNonNull(variables, "Argument 'variables' must not be null");
058         this.variables.clear();
059         this.variables.putAll(variables);
060     }
061 
062     @Nonnull
063     public Map<String, Factory> getFactories() {
064         return unmodifiableMap(factories);
065     }
066 
067     public void setFactories(@Nonnull Map<String, Factory> factories) {
068         requireNonNull(factories, "Argument 'factories' must not be null");
069         this.factories.clear();
070         this.factories.putAll(factories);
071     }
072 
073     @Nonnull
074     public Map<String, Closure> getMethods() {
075         return unmodifiableMap(methods);
076     }
077 
078     public void setMethods(@Nonnull Map<String, Closure> methods) {
079         requireNonNull(methods, "Argument 'methods' must not be null");
080         this.methods.clear();
081         this.methods.putAll(methods);
082     }
083 
084     @Nonnull
085     public Map<String, Closure[]> getProps() {
086         return unmodifiableMap(props);
087     }
088 
089     public void setProps(@Nonnull Map<String, Closure[]> props) {
090         requireNonNull(props, "Argument 'props' must not be null");
091         this.props.clear();
092         this.props.putAll(props);
093     }
094 
095     @Nonnull
096     public List<Closure> getAttributeDelegates() {
097         return unmodifiableList(attributeDelegates);
098     }
099 
100     public void setAttributeDelegates(@Nonnull List<Closure> attributeDelegates) {
101         requireNonNull(attributeDelegates, "Argument 'attributeDelegates' must not be null");
102         this.attributeDelegates.clear();
103         this.attributeDelegates.addAll(attributeDelegates);
104     }
105 
106     @Nonnull
107     public List<Closure> getPostInstantiateDelegates() {
108         return unmodifiableList(postInstantiateDelegates);
109     }
110 
111     public void setPostInstantiateDelegates(@Nonnull List<Closure> postInstantiateDelegates) {
112         requireNonNull(postInstantiateDelegates, "Argument 'postInstantiateDelegates' must not be null");
113         this.postInstantiateDelegates.clear();
114         this.postInstantiateDelegates.addAll(postInstantiateDelegates);
115     }
116 
117     @Nonnull
118     public List<Closure> getPostNodeCompletionDelegates() {
119         return unmodifiableList(postNodeCompletionDelegates);
120     }
121 
122     public void setPostNodeCompletionDelegates(@Nonnull List<Closure> postNodeCompletionDelegates) {
123         requireNonNull(postNodeCompletionDelegates, "Argument 'postNodeCompletionDelegates' must not be null");
124         this.postNodeCompletionDelegates.clear();
125         this.postNodeCompletionDelegates.addAll(postNodeCompletionDelegates);
126     }
127 
128     @Nonnull
129     public List<Closure> getPreInstantiateDelegates() {
130         return unmodifiableList(preInstantiateDelegates);
131     }
132 
133     public void setPreInstantiateDelegates(@Nonnull List<Closure> preInstantiateDelegates) {
134         requireNonNull(preInstantiateDelegates, "Argument 'preInstantiateDelegates' must not be null");
135         this.preInstantiateDelegates.clear();
136         this.preInstantiateDelegates.addAll(preInstantiateDelegates);
137     }
138 
139     @Nonnull
140     public List<Closure> getDisposalClosures() {
141         return unmodifiableList(disposalClosures);
142     }
143 
144     public void setDisposalClosures(@Nonnull List<Closure> disposalClosures) {
145         requireNonNull(disposalClosures, "Argument 'disposalClosures' must not be null");
146         this.disposalClosures.clear();
147         this.disposalClosures.addAll(disposalClosures);
148     }
149 
150     @Nullable
151     public Closure getMethodMissingDelegate() {
152         return methodMissingDelegate;
153     }
154 
155     public void setMethodMissingDelegate(@Nullable Closure methodMissingDelegate) {
156         this.methodMissingDelegate = methodMissingDelegate;
157     }
158 
159     @Nullable
160     public Closure getPropertyMissingDelegate() {
161         return propertyMissingDelegate;
162     }
163 
164     public void setPropertyMissingDelegate(@Nullable Closure propertyMissingDelegate) {
165         this.propertyMissingDelegate = propertyMissingDelegate;
166     }
167 }