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.UuidDefinition;
20  
21  import java.io.IOException;
22  import java.util.List;
23  import java.util.Optional;
24  import java.util.UUID;
25  
26  import com.google.common.collect.ImmutableList;
27  import com.google.common.collect.ImmutableList.Builder;
28  
29  public class UuidField
30          implements PropertyElement {
31  
32      private final UuidDefinition uuidDefinition;
33      private final ValueProvider valueProvider;
34  
35      public UuidField(final UuidDefinition uuidDefinition, final ValueProvider valueProvider) {
36          this.uuidDefinition = checkNotNull(uuidDefinition, "uuidDefinition is null");
37          this.valueProvider = checkNotNull(valueProvider, "valueProvider is null");
38      }
39  
40      public static List<UuidField> createUuids(final ValueCache valueCache, final UuidDefinition... uuidDefinitions)
41              throws IOException {
42          checkNotNull(valueCache, "valueCache is null");
43  
44          final Builder<UuidField> result = ImmutableList.builder();
45  
46          for (UuidDefinition uuidDefinition : uuidDefinitions) {
47              uuidDefinition.check();
48              final ValueProvider uuidValue = valueCache.getValueProvider(uuidDefinition);
49              final UuidField uuidField = new UuidField(uuidDefinition, uuidValue);
50              result.add(uuidField);
51          }
52  
53          return result.build();
54      }
55  
56      @Override
57      public String getPropertyName() {
58          return uuidDefinition.getId();
59      }
60  
61      @Override
62      public Optional<String> getPropertyValue() {
63          // Only add the value from the provider if it is not null.
64          UUID result = null;
65          final Optional<String> propValue = valueProvider.getValue();
66  
67          if (propValue.isPresent()) {
68              result = UUID.fromString(propValue.get());
69          }
70  
71          if (result == null) {
72              final Optional<UUID> definedValue = uuidDefinition.getValue();
73              if (definedValue.isPresent()) {
74                  result = definedValue.get();
75              }
76          }
77  
78          if (result == null) {
79              result = UUID.randomUUID();
80          }
81  
82          valueProvider.setValue(result.toString());
83          return uuidDefinition.formatResult(result.toString());
84      }
85  
86      @Override
87      public boolean isExport() {
88          return uuidDefinition.isExport();
89      }
90  
91      @Override
92      public String toString() {
93          return getPropertyValue().orElse("");
94      }
95  }