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;
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          // This is not the property name (because many definitions can map onto one prop)
59          // but the actual id.
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          // Only add the value from the provider if it is not null.
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  }