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 com.google.common.base.Preconditions.checkNotNull;
18
19 import org.basepom.mojo.propertyhelper.Field;
20 import org.basepom.mojo.propertyhelper.FieldContext;
21 import org.basepom.mojo.propertyhelper.ValueProvider;
22 import org.basepom.mojo.propertyhelper.definitions.UuidDefinition;
23
24 import java.util.Optional;
25 import java.util.Random;
26 import java.util.StringJoiner;
27 import java.util.UUID;
28
29 import com.google.common.annotations.VisibleForTesting;
30
31 public final class UuidField extends Field<String, UuidDefinition> {
32
33 private final ValueProvider valueProvider;
34 private final Random secureRandom;
35
36 @VisibleForTesting
37 public static UuidField forTesting(UuidDefinition uuidDefinition, ValueProvider valueProvider) {
38 return new UuidField(uuidDefinition, valueProvider, FieldContext.forTesting());
39 }
40
41 public UuidField(final UuidDefinition uuidDefinition, final ValueProvider valueProvider,
42 FieldContext fieldContext) {
43 super(uuidDefinition, fieldContext);
44
45 this.valueProvider = checkNotNull(valueProvider, "valueProvider is null");
46
47 this.secureRandom = fieldContext.getRandom();
48 }
49
50 @Override
51 public String getFieldName() {
52 return fieldDefinition.getId();
53 }
54
55 @Override
56 public String getValue() {
57 final Optional<String> propValue = valueProvider.getValue();
58
59
60 UUID result = propValue.map(UUID::fromString)
61 .orElse(fieldDefinition.getValue()
62 .orElseGet(this::createRandomUUID));
63
64 valueProvider.setValue(result.toString());
65 return formatResult(result.toString());
66 }
67
68 private UUID createRandomUUID() {
69 long upperValue = secureRandom.nextLong();
70 long lowerValue = secureRandom.nextLong();
71
72
73 lowerValue &= 0xff0fffffffffffffL;
74 lowerValue |= 0x0040000000000000L;
75 upperValue &= 0xffffffffffffff3fL;
76 upperValue |= 0x0000000000000080L;
77
78 return new UUID(upperValue, lowerValue);
79 }
80
81
82 @Override
83 public String toString() {
84 return new StringJoiner(", ", UuidField.class.getSimpleName() + "[", "]")
85 .add("uuidDefinition=" + fieldDefinition)
86 .add("valueProvider=" + valueProvider)
87 .toString();
88 }
89 }