18
18
19
19
import java .math .BigInteger ;
20
20
import java .util .UUID ;
21
+ import javax .annotation .Nullable ;
21
22
23
+ import com .google .common .base .Preconditions ;
22
24
import org .joda .time .DateTime ;
23
25
24
26
public final class RepairSegment {
@@ -30,23 +32,25 @@ public final class RepairSegment {
30
32
private final int failCount ;
31
33
private final State state ;
32
34
private final String coordinatorHost ;
33
- private final Integer repairCommandId ; // received when triggering repair in Cassandra
34
35
private final DateTime startTime ;
35
36
private final DateTime endTime ;
36
37
37
- private RepairSegment (Builder builder , UUID id ) {
38
+ private RepairSegment (Builder builder , @ Nullable UUID id ) {
38
39
this .id = id ;
39
40
this .runId = builder .runId ;
40
41
this .repairUnitId = builder .repairUnitId ;
41
42
this .tokenRange = builder .tokenRange ;
42
43
this .failCount = builder .failCount ;
43
44
this .state = builder .state ;
44
45
this .coordinatorHost = builder .coordinatorHost ;
45
- this .repairCommandId = builder .repairCommandId ;
46
46
this .startTime = builder .startTime ;
47
47
this .endTime = builder .endTime ;
48
48
}
49
49
50
+ public static Builder builder (RingRange tokenRange , UUID repairUnitId ) {
51
+ return new Builder (tokenRange , repairUnitId );
52
+ }
53
+
50
54
public UUID getId () {
51
55
return id ;
52
56
}
@@ -79,18 +83,17 @@ public RepairSegment.State getState() {
79
83
return state ;
80
84
}
81
85
86
+ @ Nullable
82
87
public String getCoordinatorHost () {
83
88
return coordinatorHost ;
84
89
}
85
90
86
- public Integer getRepairCommandId () {
87
- return repairCommandId ;
88
- }
89
-
91
+ @ Nullable
90
92
public DateTime getStartTime () {
91
93
return startTime ;
92
94
}
93
95
96
+ @ Nullable
94
97
public DateTime getEndTime () {
95
98
return endTime ;
96
99
}
@@ -105,19 +108,20 @@ public enum State {
105
108
DONE
106
109
}
107
110
108
- public static class Builder {
111
+ public static final class Builder {
109
112
110
113
public final RingRange tokenRange ;
111
114
private final UUID repairUnitId ;
112
115
private UUID runId ;
113
116
private int failCount ;
114
117
private State state ;
115
118
private String coordinatorHost ;
116
- private Integer repairCommandId ;
117
119
private DateTime startTime ;
118
120
private DateTime endTime ;
119
121
120
- public Builder (RingRange tokenRange , UUID repairUnitId ) {
122
+ private Builder (RingRange tokenRange , UUID repairUnitId ) {
123
+ Preconditions .checkNotNull (tokenRange );
124
+ Preconditions .checkNotNull (repairUnitId );
121
125
this .repairUnitId = repairUnitId ;
122
126
this .tokenRange = tokenRange ;
123
127
this .failCount = 0 ;
@@ -131,12 +135,12 @@ private Builder(RepairSegment original) {
131
135
failCount = original .failCount ;
132
136
state = original .state ;
133
137
coordinatorHost = original .coordinatorHost ;
134
- repairCommandId = original .repairCommandId ;
135
138
startTime = original .startTime ;
136
139
endTime = original .endTime ;
137
140
}
138
141
139
142
public Builder withRunId (UUID runId ) {
143
+ Preconditions .checkNotNull (runId );
140
144
this .runId = runId ;
141
145
return this ;
142
146
}
@@ -147,32 +151,42 @@ public Builder failCount(int failCount) {
147
151
}
148
152
149
153
public Builder state (State state ) {
154
+ Preconditions .checkNotNull (state );
150
155
this .state = state ;
151
156
return this ;
152
157
}
153
158
154
- public Builder coordinatorHost (String coordinatorHost ) {
159
+ public Builder coordinatorHost (@ Nullable String coordinatorHost ) {
155
160
this .coordinatorHost = coordinatorHost ;
156
161
return this ;
157
162
}
158
163
159
- public Builder repairCommandId ( Integer repairCommandId ) {
160
- this . repairCommandId = repairCommandId ;
161
- return this ;
162
- }
164
+ public Builder startTime ( @ Nullable DateTime startTime ) {
165
+ Preconditions . checkState (
166
+ null != startTime || null == endTime ,
167
+ "unsetting startTime only permitted if endTime unset" );
163
168
164
- public Builder startTime (DateTime startTime ) {
165
169
this .startTime = startTime ;
166
170
return this ;
167
171
}
168
172
169
173
public Builder endTime (DateTime endTime ) {
174
+ Preconditions .checkNotNull (endTime );
170
175
this .endTime = endTime ;
171
176
return this ;
172
177
}
173
178
174
- public RepairSegment build (UUID id ) {
175
- return new RepairSegment (this , id );
179
+ public RepairSegment build (@ Nullable UUID segmentId ) {
180
+ // a null segmentId is a special case where the storage uses a sequence for it
181
+ Preconditions .checkNotNull (runId );
182
+ Preconditions .checkState (null != startTime || null == endTime , "if endTime is set, so must startTime be set" );
183
+ Preconditions .checkState (null == endTime || State .DONE == state , "endTime can only be set if segment is DONE" );
184
+
185
+ Preconditions .checkState (
186
+ null != startTime || State .NOT_STARTED == state ,
187
+ "startTime can only be unset if segment is NOT_STARTED" );
188
+
189
+ return new RepairSegment (this , segmentId );
176
190
}
177
191
}
178
192
}
0 commit comments