View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2024 the original author or authors.
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  ///////////////////////////////////////////////////////////////////////////////////////////////
19  
20  package com.puppycrawl.tools.checkstyle.bdd;
21  
22  import java.util.Collections;
23  import java.util.HashMap;
24  import java.util.Map;
25  
26  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
27  
28  public final class ModuleInputConfiguration {
29  
30      /** The module name. */
31      private final String moduleName;
32  
33      /** Map of default properties. */
34      private final Map<String, String> defaultProperties;
35  
36      /** Map of non default properties. */
37      private final Map<String, String> nonDefaultProperties;
38  
39      /** Map of module messages. */
40      private final Map<String, String> moduleMessages;
41  
42      private ModuleInputConfiguration(String moduleName,
43                                       Map<String, String> defaultProperties,
44                                       Map<String, String> nonDefaultProperties,
45                                       Map<String, String> moduleMessages) {
46          this.moduleName = moduleName;
47          this.defaultProperties = defaultProperties;
48          this.nonDefaultProperties = nonDefaultProperties;
49          this.moduleMessages = moduleMessages;
50      }
51  
52      public String getModuleName() {
53          return moduleName;
54      }
55  
56      public Map<String, String> getAllProperties() {
57          final Map<String, String> properties = new HashMap<>();
58          properties.putAll(defaultProperties);
59          properties.putAll(nonDefaultProperties);
60          return Collections.unmodifiableMap(properties);
61      }
62  
63      public Map<String, String> getDefaultProperties() {
64          return Collections.unmodifiableMap(defaultProperties);
65      }
66  
67      public Map<String, String> getNonDefaultProperties() {
68          return Collections.unmodifiableMap(nonDefaultProperties);
69      }
70  
71      public Map<String, String> getModuleMessages() {
72          return Collections.unmodifiableMap(moduleMessages);
73      }
74  
75      public DefaultConfiguration createConfiguration() {
76          final DefaultConfiguration parsedConfig = new DefaultConfiguration(moduleName);
77          nonDefaultProperties.forEach(parsedConfig::addProperty);
78          moduleMessages.forEach(parsedConfig::addMessage);
79          return parsedConfig;
80      }
81  
82      public String getDefaultPropertyValue(String key) {
83          return defaultProperties.get(key);
84      }
85  
86      public static final class Builder {
87  
88          private final Map<String, String> defaultProperties = new HashMap<>();
89  
90          private final Map<String, String> nonDefaultProperties = new HashMap<>();
91  
92          private final Map<String, String> moduleMessages = new HashMap<>();
93  
94          private String moduleName;
95  
96          public void setModuleName(String moduleName) {
97              this.moduleName = moduleName;
98          }
99  
100         public void addDefaultProperty(String propertyName, String defaultPropertyValue) {
101             defaultProperties.put(propertyName, defaultPropertyValue);
102         }
103 
104         public void addNonDefaultProperty(String propertyName, String nonDefaultPropertyValue) {
105             nonDefaultProperties.put(propertyName, nonDefaultPropertyValue);
106         }
107 
108         public void addModuleMessage(String messageKey, String messageString) {
109             moduleMessages.put(messageKey, messageString);
110         }
111 
112         public ModuleInputConfiguration build() {
113             return new ModuleInputConfiguration(
114                     moduleName,
115                     defaultProperties,
116                     nonDefaultProperties,
117                     moduleMessages
118             );
119         }
120     }
121 }