1
2
3
4
5
6
7
8
9
10
11
12
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
52
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
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 }