1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.basepom.mojo.propertyhelper.definitions;
16
17 import static com.google.common.base.Preconditions.checkNotNull;
18
19 import org.basepom.mojo.propertyhelper.FieldContext;
20 import org.basepom.mojo.propertyhelper.ValueCache;
21 import org.basepom.mojo.propertyhelper.ValueProvider;
22 import org.basepom.mojo.propertyhelper.fields.UuidField;
23
24 import java.io.IOException;
25 import java.util.Objects;
26 import java.util.Optional;
27 import java.util.StringJoiner;
28 import java.util.UUID;
29
30 import com.google.common.annotations.VisibleForTesting;
31
32 public class UuidDefinition extends FieldDefinition<String> {
33
34
35
36
37 String value = null;
38
39 public UuidDefinition() {}
40
41 @VisibleForTesting
42 UuidDefinition(String id) {
43 super(id);
44 }
45
46 public Optional<UUID> getValue() {
47 return Optional.ofNullable(value).map(UUID::fromString);
48 }
49
50 @Override
51 public UuidField createField(FieldContext context, ValueCache valueCache) throws IOException {
52 checkNotNull(context, "context is null");
53 checkNotNull(valueCache, "valueCache is null");
54
55 check();
56
57 final ValueProvider uuidValue = valueCache.getValueProvider(this);
58 return new UuidField(this, uuidValue, context);
59 }
60
61 @Override
62 public String toString() {
63 return new StringJoiner(", ", UuidDefinition.class.getSimpleName() + "[", "]")
64 .add("value='" + value + "'")
65 .add(super.toString())
66 .toString();
67 }
68
69 @Override
70 public boolean equals(Object o) {
71 if (this == o) {
72 return true;
73 }
74 if (o == null || getClass() != o.getClass()) {
75 return false;
76 }
77 if (!super.equals(o)) {
78 return false;
79 }
80 UuidDefinition that = (UuidDefinition) o;
81 return Objects.equals(value, that.value);
82 }
83
84 @Override
85 public int hashCode() {
86 return Objects.hash(super.hashCode(), value);
87 }
88 }