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.fields;
16  
17  import static java.lang.String.format;
18  import static org.basepom.mojo.propertyhelper.IgnoreWarnFail.checkIgnoreWarnFailState;
19  
20  import org.basepom.mojo.propertyhelper.Field;
21  import org.basepom.mojo.propertyhelper.FieldContext;
22  import org.basepom.mojo.propertyhelper.ValueProvider;
23  import org.basepom.mojo.propertyhelper.definitions.StringDefinition;
24  
25  import java.util.List;
26  import java.util.Optional;
27  import java.util.StringJoiner;
28  
29  import com.google.common.annotations.VisibleForTesting;
30  import com.google.common.base.Strings;
31  import com.google.common.collect.Lists;
32  
33  public final class StringField extends Field<String, StringDefinition> {
34  
35      private final ValueProvider valueProvider;
36  
37      @VisibleForTesting
38      public static StringField forTesting(StringDefinition stringDefinition, ValueProvider valueProvider) {
39          return new StringField(stringDefinition, valueProvider, FieldContext.forTesting());
40      }
41  
42      public StringField(final StringDefinition stringDefinition, final ValueProvider valueProvider,
43          FieldContext fieldContext) {
44          super(stringDefinition, fieldContext);
45  
46          this.valueProvider = valueProvider;
47      }
48  
49      @Override
50      public String getFieldName() {
51          // This is not the property name (because many definitions can map onto one prop)
52          // but the actual id.
53          return fieldDefinition.getId();
54      }
55  
56      @Override
57      public String getValue() {
58          final List<String> values = Lists.newArrayList();
59  
60          final Optional<String> propValue = valueProvider.getValue();
61          final List<String> definedValues = fieldDefinition.getValues();
62  
63          // Only add the value from the provider if it is not null.
64          propValue.ifPresent(values::add);
65  
66          values.addAll(definedValues);
67  
68          for (String value : values) {
69              var stringResult = Strings.nullToEmpty(value);
70              if (fieldDefinition.isBlankIsValid() || !stringResult.isBlank()) {
71                  return formatResult(value);
72              }
73          }
74  
75          checkIgnoreWarnFailState(false, fieldDefinition.getOnMissingValue(),
76              () -> "",
77              () -> format("No value for string field %s found, using an empty value!", getFieldName()));
78  
79          return "";
80      }
81  
82      @Override
83      public String toString() {
84          return new StringJoiner(", ", StringField.class.getSimpleName() + "[", "]")
85              .add("stringDefinition=" + fieldDefinition)
86              .add("valueProvider=" + valueProvider)
87              .toString();
88      }
89  }