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 import static com.google.common.base.Preconditions.checkState;
19
20 import org.basepom.mojo.propertyhelper.FieldContext;
21 import org.basepom.mojo.propertyhelper.ValueCache;
22 import org.basepom.mojo.propertyhelper.ValueProvider;
23 import org.basepom.mojo.propertyhelper.fields.NumberField;
24
25 import java.io.IOException;
26 import java.util.Objects;
27 import java.util.Optional;
28 import java.util.StringJoiner;
29
30 import com.google.common.annotations.VisibleForTesting;
31
32 public class NumberDefinition extends FieldDefinition<String> {
33
34 public static final String INITIAL_VALUE = "0";
35
36
37
38
39 Integer fieldNumber;
40
41
42
43
44 int increment = 1;
45
46 public NumberDefinition() {
47 initialValue = INITIAL_VALUE;
48 }
49
50 @VisibleForTesting
51 NumberDefinition(String id) {
52 super(id);
53 initialValue = INITIAL_VALUE;
54 }
55
56
57 public Optional<Integer> getFieldNumber() {
58 return Optional.ofNullable(fieldNumber);
59 }
60
61 public int getIncrement() {
62 return increment;
63 }
64
65 @Override
66 public void check() {
67 super.check();
68 checkState(getInitialValue().isPresent(), "initial value must be defined");
69 getFieldNumber().ifPresent(fieldNumber -> checkState(fieldNumber >= 0, "the field number must be >= 0"));
70 }
71
72 @Override
73 public NumberField createField(FieldContext context, ValueCache valueCache) throws IOException {
74 checkNotNull(context, "context is null");
75 checkNotNull(valueCache, "valueCache is null");
76
77 check();
78
79 final ValueProvider numberValue = valueCache.getValueProvider(this);
80 return new NumberField(this, numberValue, context);
81 }
82
83 @Override
84 public String toString() {
85 return new StringJoiner(", ", NumberDefinition.class.getSimpleName() + "[", "]")
86 .add("fieldNumber=" + fieldNumber)
87 .add("increment=" + increment)
88 .add(super.toString())
89 .toString();
90 }
91
92 @Override
93 public boolean equals(Object o) {
94 if (this == o) {
95 return true;
96 }
97 if (o == null || getClass() != o.getClass()) {
98 return false;
99 }
100 if (!super.equals(o)) {
101 return false;
102 }
103 NumberDefinition that = (NumberDefinition) o;
104 return increment == that.increment && Objects.equals(fieldNumber, that.fieldNumber);
105 }
106
107 @Override
108 public int hashCode() {
109 return Objects.hash(super.hashCode(), fieldNumber, increment);
110 }
111 }