1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.basepom.mojo.propertyhelper;
16
17 import static com.google.common.base.Preconditions.checkNotNull;
18
19 import org.basepom.mojo.propertyhelper.beans.IgnoreWarnFail;
20 import org.basepom.mojo.propertyhelper.beans.StringDefinition;
21
22 import java.io.IOException;
23 import java.util.List;
24 import java.util.Optional;
25
26 import com.google.common.collect.ImmutableList;
27 import com.google.common.collect.ImmutableList.Builder;
28 import com.google.common.collect.Lists;
29
30 public class StringField
31 implements PropertyElement {
32
33 private final StringDefinition stringDefinition;
34 private final ValueProvider valueProvider;
35
36 public StringField(final StringDefinition stringDefinition, final ValueProvider valueProvider) {
37 this.stringDefinition = stringDefinition;
38 this.valueProvider = valueProvider;
39 }
40
41 public static List<StringField> createStrings(final ValueCache valueCache, final StringDefinition... stringDefinitions)
42 throws IOException {
43 checkNotNull(valueCache, "valueCache is null");
44
45 final Builder<StringField> result = ImmutableList.builder();
46
47 for (StringDefinition stringDefinition : stringDefinitions) {
48 stringDefinition.check();
49 final ValueProvider stringValue = valueCache.getValueProvider(stringDefinition);
50 final StringField stringField = new StringField(stringDefinition, stringValue);
51 result.add(stringField);
52 }
53 return result.build();
54 }
55
56 @Override
57 public String getPropertyName() {
58
59
60 return stringDefinition.getId();
61 }
62
63 @Override
64 public Optional<String> getPropertyValue() {
65 final List<String> values = Lists.newArrayList();
66
67 final Optional<String> propValue = valueProvider.getValue();
68 final List<String> definedValues = stringDefinition.getValues();
69
70
71 propValue.ifPresent(values::add);
72
73 values.addAll(definedValues);
74
75 for (String value : values) {
76 if (stringDefinition.isBlankIsValid() || (value != null && !value.trim().isEmpty())) {
77 return stringDefinition.formatResult(value);
78 }
79 }
80
81 IgnoreWarnFail.checkState(stringDefinition.getOnMissingValue(), false, "value");
82
83 return Optional.empty();
84 }
85
86 @Override
87 public boolean isExport() {
88 return stringDefinition.isExport();
89 }
90
91 @Override
92 public String toString() {
93 return getPropertyValue().orElse("");
94 }
95 }