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  
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   * Defines a string field. This is a config element that is populated by maven.
35   */
36  public class StringDefinition extends FieldDefinition<String> {
37  
38      private List<String> values = ImmutableList.of();
39  
40  
41      /**
42       * called by maven
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       * Whether a blank string is a valid value. Field injected by Maven.
52       */
53      boolean blankIsValid = true;
54  
55      /**
56       * Default action on missing value.
57       */
58      private IgnoreWarnFail onMissingValue = IgnoreWarnFail.FAIL;
59  
60      /**
61       * called by maven
62       */
63      public void setOnMissingValue(String onMissingValue) {
64          this.onMissingValue = IgnoreWarnFail.forString(onMissingValue);
65      }
66  
67      /**
68       * called by maven
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 }