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.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       * Value for this uuid. Field injected by Maven.
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  }