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.definitions;
16  
17  import static com.google.common.base.Preconditions.checkNotNull;
18  import static com.google.common.base.Preconditions.checkState;
19  
20  import org.basepom.mojo.propertyhelper.FieldContext;
21  import org.basepom.mojo.propertyhelper.ValueCache;
22  import org.basepom.mojo.propertyhelper.ValueProvider;
23  import org.basepom.mojo.propertyhelper.fields.MacroField;
24  
25  import java.io.IOException;
26  import java.util.Map;
27  import java.util.Objects;
28  import java.util.Optional;
29  import java.util.Properties;
30  import java.util.StringJoiner;
31  
32  import com.google.common.annotations.VisibleForTesting;
33  import com.google.common.collect.ImmutableMap;
34  import com.google.common.collect.Maps;
35  
36  /**
37   * Represents a macro configuration in the POM.
38   */
39  public class MacroDefinition extends FieldDefinition<String> {
40  
41      /**
42       * Macro type. This value is used to look up a plexus component with this value as hint. Field injected by Maven.
43       */
44      String macroType = null;
45  
46      /**
47       * Class for this macro. If no macro type is given and this value is provided, the plugin will try to instantiate this class. Field injected by Maven.
48       */
49      String macroClass = null;
50  
51      /**
52       * Macro specific properties. Field injected by Maven.
53       */
54      Properties properties = new Properties();
55  
56      public MacroDefinition() {
57      }
58  
59      @VisibleForTesting
60      MacroDefinition(String id) {
61          super(id);
62      }
63  
64      public Optional<String> getMacroType() {
65          return Optional.ofNullable(macroType);
66      }
67  
68      public Optional<String> getMacroClass() {
69          return Optional.ofNullable(macroClass);
70      }
71  
72      public Map<String, String> getProperties() {
73          return ImmutableMap.copyOf(Maps.fromProperties(properties));
74      }
75  
76      @Override
77      public void check() {
78          super.check();
79  
80          checkState(macroClass != null || macroType != null, "neither macro class nor macro type is defined!");
81      }
82  
83      @Override
84      public MacroField createField(FieldContext context, ValueCache valueCache) throws IOException {
85          checkNotNull(context, "context is null");
86          checkNotNull(valueCache, "valueCache is null");
87  
88          check();
89  
90          final ValueProvider macroValue = valueCache.getValueProvider(this);
91          return new MacroField(this, macroValue, context);
92      }
93  
94      @Override
95      public String toString() {
96          return new StringJoiner(", ", MacroDefinition.class.getSimpleName() + "[", "]")
97              .add("macroType='" + macroType + "'")
98              .add("macroClass='" + macroClass + "'")
99              .add("properties=" + properties)
100             .add(super.toString())
101             .toString();
102     }
103 
104     @Override
105     public boolean equals(Object o) {
106         if (this == o) {
107             return true;
108         }
109         if (o == null || getClass() != o.getClass()) {
110             return false;
111         }
112         if (!super.equals(o)) {
113             return false;
114         }
115         MacroDefinition that = (MacroDefinition) o;
116         return Objects.equals(macroType, that.macroType) && Objects.equals(macroClass, that.macroClass) && Objects.equals(properties,
117             that.properties);
118     }
119 
120     @Override
121     public int hashCode() {
122         return Objects.hash(super.hashCode(), macroType, macroClass, properties);
123     }
124 }