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
19 import org.basepom.mojo.propertyhelper.FieldContext;
20 import org.basepom.mojo.propertyhelper.IgnoreWarnFail;
21 import org.basepom.mojo.propertyhelper.ValueCache;
22 import org.basepom.mojo.propertyhelper.ValueProvider;
23 import org.basepom.mojo.propertyhelper.fields.StringField;
24
25 import java.io.IOException;
26 import java.util.List;
27 import java.util.Objects;
28 import java.util.StringJoiner;
29
30 import com.google.common.annotations.VisibleForTesting;
31 import com.google.common.collect.ImmutableList;
32
33
34
35
36 public class StringDefinition extends FieldDefinition<String> {
37
38 private List<String> values = ImmutableList.of();
39
40
41
42
43
44 public void setValues(final List<String> values) {
45 checkNotNull(values, "values is null");
46 this.values = values.stream().map(v -> Objects.requireNonNullElse(v, "")).collect(ImmutableList.toImmutableList());
47 }
48
49
50
51
52
53 boolean blankIsValid = true;
54
55
56
57
58 private IgnoreWarnFail onMissingValue = IgnoreWarnFail.FAIL;
59
60
61
62
63 public void setOnMissingValue(String onMissingValue) {
64 this.onMissingValue = IgnoreWarnFail.forString(onMissingValue);
65 }
66
67
68
69
70 public StringDefinition() {
71 }
72
73 @VisibleForTesting
74 StringDefinition(String id) {
75 super(id);
76 }
77
78 public List<String> getValues() {
79 return values;
80 }
81
82 public boolean isBlankIsValid() {
83 return blankIsValid;
84 }
85
86 public IgnoreWarnFail getOnMissingValue() {
87 return onMissingValue;
88 }
89
90 @Override
91 public StringField createField(FieldContext context, ValueCache valueCache) throws IOException {
92 checkNotNull(context, "context is null");
93 checkNotNull(valueCache, "valueCache is null");
94
95 check();
96
97 final ValueProvider stringValue = valueCache.getValueProvider(this);
98 return new StringField(this, stringValue, context);
99 }
100
101 @Override
102 public String toString() {
103 return new StringJoiner(", ", StringDefinition.class.getSimpleName() + "[", "]")
104 .add("values=" + values)
105 .add("blankIsValid=" + blankIsValid)
106 .add("onMissingValue=" + onMissingValue)
107 .add(super.toString())
108 .toString();
109 }
110
111 @Override
112 public boolean equals(Object o) {
113 if (this == o) {
114 return true;
115 }
116 if (o == null || getClass() != o.getClass()) {
117 return false;
118 }
119 if (!super.equals(o)) {
120 return false;
121 }
122 StringDefinition that = (StringDefinition) o;
123 return blankIsValid == that.blankIsValid && Objects.equals(values, that.values) && onMissingValue == that.onMissingValue;
124 }
125
126 @Override
127 public int hashCode() {
128 return Objects.hash(super.hashCode(), values, blankIsValid, onMissingValue);
129 }
130 }