|
17 | 17 | * under the License.
|
18 | 18 | */
|
19 | 19 |
|
20 |
| - |
21 | 20 | package org.logstash.ackedqueue;
|
22 | 21 |
|
23 | 22 | /**
|
24 | 23 | * Persistent queue settings implementation.
|
25 | 24 | * */
|
26 | 25 | public class SettingsImpl implements Settings {
|
27 |
| - private String dirForFiles; |
28 |
| - private Class<? extends Queueable> elementClass; |
29 |
| - private int capacity; |
30 |
| - private long queueMaxBytes; |
31 |
| - private int maxUnread; |
32 |
| - private int checkpointMaxAcks; |
33 |
| - private int checkpointMaxWrites; |
34 |
| - private boolean checkpointRetry; |
| 26 | + private final String dirForFiles; |
| 27 | + private final Class<? extends Queueable> elementClass; |
| 28 | + private final int capacity; |
| 29 | + private final long queueMaxBytes; |
| 30 | + private final int maxUnread; |
| 31 | + private final int checkpointMaxAcks; |
| 32 | + private final int checkpointMaxWrites; |
| 33 | + private final boolean checkpointRetry; |
35 | 34 |
|
36 | 35 | public static Builder builder(final Settings settings) {
|
37 |
| - return new BuilderImpl(settings.getDirPath(), settings.getElementClass(), settings.getCapacity(), |
38 |
| - settings.getQueueMaxBytes(), settings.getMaxUnread(), settings.getCheckpointMaxAcks(), |
39 |
| - settings.getCheckpointMaxWrites(), settings.getCheckpointRetry() |
40 |
| - ); |
| 36 | + return new BuilderImpl(settings); |
41 | 37 | }
|
42 | 38 |
|
43 | 39 | public static Builder fileSettingsBuilder(final String dirForFiles) {
|
44 | 40 | return new BuilderImpl(dirForFiles);
|
45 | 41 | }
|
46 | 42 |
|
47 |
| - private SettingsImpl(final String dirForFiles, final Class<? extends Queueable> elementClass, |
48 |
| - final int capacity, final long queueMaxBytes, final int maxUnread, |
49 |
| - final int checkpointMaxAcks, final int checkpointMaxWrites, boolean checkpointRetry) { |
50 |
| - this.dirForFiles = dirForFiles; |
51 |
| - this.elementClass = elementClass; |
52 |
| - this.capacity = capacity; |
53 |
| - this.queueMaxBytes = queueMaxBytes; |
54 |
| - this.maxUnread = maxUnread; |
55 |
| - this.checkpointMaxAcks = checkpointMaxAcks; |
56 |
| - this.checkpointMaxWrites = checkpointMaxWrites; |
57 |
| - this.checkpointRetry = checkpointRetry; |
| 43 | + private SettingsImpl(final BuilderImpl builder) { |
| 44 | + this.dirForFiles = builder.dirForFiles; |
| 45 | + this.elementClass = builder.elementClass; |
| 46 | + this.capacity = builder.capacity; |
| 47 | + this.queueMaxBytes = builder.queueMaxBytes; |
| 48 | + this.maxUnread = builder.maxUnread; |
| 49 | + this.checkpointMaxAcks = builder.checkpointMaxAcks; |
| 50 | + this.checkpointMaxWrites = builder.checkpointMaxWrites; |
| 51 | + this.checkpointRetry = builder.checkpointRetry; |
58 | 52 | }
|
59 | 53 |
|
60 | 54 | @Override
|
@@ -132,103 +126,87 @@ private static final class BuilderImpl implements Builder {
|
132 | 126 |
|
133 | 127 | private final String dirForFiles;
|
134 | 128 |
|
135 |
| - private final Class<? extends Queueable> elementClass; |
| 129 | + private Class<? extends Queueable> elementClass; |
136 | 130 |
|
137 |
| - private final int capacity; |
| 131 | + private int capacity; |
138 | 132 |
|
139 |
| - private final long queueMaxBytes; |
| 133 | + private long queueMaxBytes; |
140 | 134 |
|
141 |
| - private final int maxUnread; |
| 135 | + private int maxUnread; |
142 | 136 |
|
143 |
| - private final int checkpointMaxAcks; |
| 137 | + private int checkpointMaxAcks; |
144 | 138 |
|
145 |
| - private final int checkpointMaxWrites; |
| 139 | + private int checkpointMaxWrites; |
146 | 140 |
|
147 |
| - private final boolean checkpointRetry; |
| 141 | + private boolean checkpointRetry; |
148 | 142 |
|
149 | 143 | private BuilderImpl(final String dirForFiles) {
|
150 |
| - this(dirForFiles, null, DEFAULT_CAPACITY, DEFAULT_MAX_QUEUE_BYTES, |
151 |
| - DEFAULT_MAX_UNREAD, DEFAULT_CHECKPOINT_MAX_ACKS, DEFAULT_CHECKPOINT_MAX_WRITES, false |
152 |
| - ); |
| 144 | + this.dirForFiles = dirForFiles; |
| 145 | + this.elementClass = null; |
| 146 | + this.capacity = DEFAULT_CAPACITY; |
| 147 | + this.queueMaxBytes = DEFAULT_MAX_QUEUE_BYTES; |
| 148 | + this.maxUnread = DEFAULT_MAX_UNREAD; |
| 149 | + this.checkpointMaxAcks = DEFAULT_CHECKPOINT_MAX_ACKS; |
| 150 | + this.checkpointMaxWrites = DEFAULT_CHECKPOINT_MAX_WRITES; |
| 151 | + this.checkpointRetry = false; |
153 | 152 | }
|
154 | 153 |
|
155 |
| - private BuilderImpl(final String dirForFiles, final Class<? extends Queueable> elementClass, |
156 |
| - final int capacity, final long queueMaxBytes, final int maxUnread, |
157 |
| - final int checkpointMaxAcks, final int checkpointMaxWrites, final boolean checkpointRetry) { |
158 |
| - this.dirForFiles = dirForFiles; |
159 |
| - this.elementClass = elementClass; |
160 |
| - this.capacity = capacity; |
161 |
| - this.queueMaxBytes = queueMaxBytes; |
162 |
| - this.maxUnread = maxUnread; |
163 |
| - this.checkpointMaxAcks = checkpointMaxAcks; |
164 |
| - this.checkpointMaxWrites = checkpointMaxWrites; |
165 |
| - this.checkpointRetry = checkpointRetry; |
| 154 | + private BuilderImpl(final Settings settings) { |
| 155 | + this.dirForFiles = settings.getDirPath(); |
| 156 | + this.elementClass = settings.getElementClass(); |
| 157 | + this.capacity = settings.getCapacity(); |
| 158 | + this.queueMaxBytes = settings.getQueueMaxBytes(); |
| 159 | + this.maxUnread = settings.getMaxUnread(); |
| 160 | + this.checkpointMaxAcks = settings.getCheckpointMaxAcks(); |
| 161 | + this.checkpointMaxWrites = settings.getCheckpointMaxWrites(); |
| 162 | + this.checkpointRetry = settings.getCheckpointRetry(); |
166 | 163 | }
|
167 | 164 |
|
168 | 165 | @Override
|
169 | 166 | public Builder elementClass(final Class<? extends Queueable> elementClass) {
|
170 |
| - return new BuilderImpl( |
171 |
| - this.dirForFiles, elementClass, this.capacity, this.queueMaxBytes, this.maxUnread, |
172 |
| - this.checkpointMaxAcks, this.checkpointMaxWrites, this.checkpointRetry |
173 |
| - ); |
| 167 | + this.elementClass = elementClass; |
| 168 | + return this; |
174 | 169 | }
|
175 | 170 |
|
176 | 171 | @Override
|
177 | 172 | public Builder capacity(final int capacity) {
|
178 |
| - return new BuilderImpl( |
179 |
| - this.dirForFiles, this.elementClass, capacity, this.queueMaxBytes, this.maxUnread, |
180 |
| - this.checkpointMaxAcks, this.checkpointMaxWrites, this.checkpointRetry |
181 |
| - ); |
| 173 | + this.capacity = capacity; |
| 174 | + return this; |
182 | 175 | }
|
183 | 176 |
|
184 | 177 | @Override
|
185 | 178 | public Builder queueMaxBytes(final long size) {
|
186 |
| - return new BuilderImpl( |
187 |
| - this.dirForFiles, this.elementClass, this.capacity, size, this.maxUnread, |
188 |
| - this.checkpointMaxAcks, this.checkpointMaxWrites, this.checkpointRetry |
189 |
| - ); |
| 179 | + this.queueMaxBytes = size; |
| 180 | + return this; |
190 | 181 | }
|
191 | 182 |
|
192 | 183 | @Override
|
193 | 184 | public Builder maxUnread(final int maxUnread) {
|
194 |
| - return new BuilderImpl( |
195 |
| - this.dirForFiles, this.elementClass, |
196 |
| - this.capacity, this.queueMaxBytes, maxUnread, this.checkpointMaxAcks, |
197 |
| - this.checkpointMaxWrites, this.checkpointRetry |
198 |
| - ); |
| 185 | + this.maxUnread = maxUnread; |
| 186 | + return this; |
199 | 187 | }
|
200 | 188 |
|
201 | 189 | @Override
|
202 | 190 | public Builder checkpointMaxAcks(final int checkpointMaxAcks) {
|
203 |
| - return new BuilderImpl( |
204 |
| - this.dirForFiles, this.elementClass, |
205 |
| - this.capacity, this.queueMaxBytes, this.maxUnread, checkpointMaxAcks, |
206 |
| - this.checkpointMaxWrites, this.checkpointRetry |
207 |
| - ); |
| 191 | + this.checkpointMaxAcks = checkpointMaxAcks; |
| 192 | + return this; |
208 | 193 | }
|
209 | 194 |
|
210 | 195 | @Override
|
211 | 196 | public Builder checkpointMaxWrites(final int checkpointMaxWrites) {
|
212 |
| - return new BuilderImpl( |
213 |
| - this.dirForFiles, this.elementClass, this.capacity, this.queueMaxBytes, |
214 |
| - this.maxUnread, this.checkpointMaxAcks, checkpointMaxWrites, this.checkpointRetry |
215 |
| - ); |
| 197 | + this.checkpointMaxWrites = checkpointMaxWrites; |
| 198 | + return this; |
216 | 199 | }
|
217 | 200 |
|
218 | 201 | @Override
|
219 | 202 | public Builder checkpointRetry(final boolean checkpointRetry) {
|
220 |
| - return new BuilderImpl( |
221 |
| - this.dirForFiles, this.elementClass, this.capacity, this.queueMaxBytes, |
222 |
| - this.maxUnread, this.checkpointMaxAcks, checkpointMaxWrites, checkpointRetry |
223 |
| - ); |
| 203 | + this.checkpointRetry = checkpointRetry; |
| 204 | + return this; |
224 | 205 | }
|
225 | 206 |
|
226 | 207 | @Override
|
227 | 208 | public Settings build() {
|
228 |
| - return new SettingsImpl( |
229 |
| - this.dirForFiles, this.elementClass, this.capacity, this.queueMaxBytes, |
230 |
| - this.maxUnread, this.checkpointMaxAcks, this.checkpointMaxWrites, this.checkpointRetry |
231 |
| - ); |
| 209 | + return Settings.ensureValid(new SettingsImpl(this)); |
232 | 210 | }
|
233 | 211 | }
|
234 | 212 | }
|
0 commit comments