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.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
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 }