1
2
3
4
5
6
7
8
9
10
11
12
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
38
39 public class MacroDefinition extends FieldDefinition<String> {
40
41
42
43
44 String macroType = null;
45
46
47
48
49 String macroClass = null;
50
51
52
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 }