View Javadoc
1   /*
2    * Licensed under the Apache License, Version 2.0 (the "License");
3    * you may not use this file except in compliance with the License.
4    * You may obtain a copy of the License at
5    *
6    * http://www.apache.org/licenses/LICENSE-2.0
7    *
8    * Unless required by applicable law or agreed to in writing, software
9    * distributed under the License is distributed on an "AS IS" BASIS,
10   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11   * See the License for the specific language governing permissions and
12   * limitations under the License.
13   */
14  
15  package org.basepom.mojo.propertyhelper.beans;
16  
17  import static com.google.common.base.Preconditions.checkNotNull;
18  import static com.google.common.base.Preconditions.checkState;
19  
20  import java.util.Map;
21  import java.util.Optional;
22  import java.util.Properties;
23  import java.util.StringJoiner;
24  
25  import com.google.common.annotations.VisibleForTesting;
26  import com.google.common.collect.ImmutableMap;
27  import com.google.common.collect.Maps;
28  
29  public class MacroDefinition
30          extends AbstractDefinition<MacroDefinition> {
31  
32      /**
33       * Macro type. Field injected by Maven.
34       */
35      String macroType = null;
36  
37      /**
38       * Class for this macro. Field injected by Maven.
39       */
40      String macroClass = null;
41  
42      /**
43       * Macro specific properties. Field injected by Maven.
44       */
45      Properties properties = new Properties();
46  
47      public MacroDefinition() {
48          super();
49      }
50  
51      public Optional<String> getMacroType() {
52          return Optional.ofNullable(macroType);
53      }
54  
55      @VisibleForTesting
56      public MacroDefinition setMacroType(final String macroType) {
57          this.macroType = checkNotNull(macroType, "macroType is null");
58          return this;
59      }
60  
61      public Optional<String> getMacroClass() {
62          return Optional.ofNullable(macroClass);
63      }
64  
65      @VisibleForTesting
66      public MacroDefinition setMacroClass(final String macroClass) {
67          this.macroClass = checkNotNull(macroClass, "macroClass is null");
68          return this;
69      }
70  
71      public Map<String, String> getProperties() {
72          return ImmutableMap.copyOf(Maps.fromProperties(properties));
73      }
74  
75      @VisibleForTesting
76      public MacroDefinition setProperties(final Properties properties) {
77          this.properties = new Properties(checkNotNull(properties, "properties is null"));
78          return this;
79      }
80  
81      @Override
82      public void check() {
83          super.check();
84  
85          checkState(macroClass != null || macroType != null, "neither macro class nor macro type is defined!");
86      }
87  
88      @Override
89      public String toString() {
90          return new StringJoiner(", ", MacroDefinition.class.getSimpleName() + "[", "]")
91                  .add("macroType='" + macroType + "'")
92                  .add("macroClass='" + macroClass + "'")
93                  .add("properties=" + properties)
94                  .toString();
95      }
96  
97      @Override
98      public boolean equals(Object o) {
99          if (this == o) {
100             return true;
101         }
102         if (o == null || getClass() != o.getClass()) {
103             return false;
104         }
105         MacroDefinition that = (MacroDefinition) o;
106         return java.util.Objects.equals(macroType, that.macroType) && java.util.Objects.equals(
107                 macroClass,
108                 that.macroClass) && java.util.Objects.equals(properties, that.properties);
109     }
110 
111     @Override
112     public int hashCode() {
113         return java.util.Objects.hash(macroType, macroClass, properties);
114     }
115 
116 }