/
ProcessingSettings.java
154 lines (136 loc) · 4.29 KB
/
ProcessingSettings.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package ru.yoomoney.tech.dbqueue.settings;
import javax.annotation.Nonnull;
import java.util.Objects;
import java.util.StringJoiner;
import java.util.function.BiFunction;
/**
* Task processing settings.
*
* @author Oleg Kandaurov
* @since 01.10.2021
*/
public class ProcessingSettings extends DynamicSetting<ProcessingSettings> {
@Nonnull
private Integer threadCount;
@Nonnull
private ProcessingMode processingMode;
private ProcessingSettings(@Nonnull Integer threadCount,
@Nonnull ProcessingMode processingMode) {
this.threadCount = Objects.requireNonNull(threadCount, "threadCount must not be null");
this.processingMode = Objects.requireNonNull(processingMode, "processingMode must not be null");
if (threadCount < 0) {
throw new IllegalArgumentException("threadCount must not be negative");
}
}
/**
* Get number of threads for processing tasks in the queue.
*
* @return Number of processing threads.
*/
@Nonnull
public Integer getThreadCount() {
return threadCount;
}
/**
* Get task processing mode in the queue.
*
* @return Task processing mode.
*/
@Nonnull
public ProcessingMode getProcessingMode() {
return processingMode;
}
/**
* Create a new builder for processing settings.
*
* @return A new builder for processing settings.
*/
public static Builder builder() {
return new Builder();
}
@Nonnull
@Override
protected String getName() {
return "processingSettings";
}
@Nonnull
@Override
protected BiFunction<ProcessingSettings, ProcessingSettings, String> getDiffEvaluator() {
return (oldVal, newVal) -> {
StringJoiner diff = new StringJoiner(",", getName() + '(', ")");
if (!Objects.equals(oldVal.threadCount, newVal.threadCount)) {
diff.add("threadCount=" +
newVal.threadCount + '<' + oldVal.threadCount);
}
if (!Objects.equals(oldVal.processingMode, newVal.processingMode)) {
diff.add("processingMode=" +
newVal.processingMode + '<' + oldVal.processingMode);
}
return diff.toString();
};
}
@Nonnull
@Override
protected ProcessingSettings getThis() {
return this;
}
@Override
protected void copyFields(@Nonnull ProcessingSettings newValue) {
this.threadCount = newValue.threadCount;
this.processingMode = newValue.processingMode;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
ProcessingSettings that = (ProcessingSettings) obj;
return threadCount.equals(that.threadCount) && processingMode == that.processingMode;
}
@Override
public int hashCode() {
return Objects.hash(threadCount, processingMode);
}
@Override
public String toString() {
return "{" +
"threadCount=" + threadCount +
", processingMode=" + processingMode +
'}';
}
/**
* A builder for processing settings.
*/
public static class Builder {
private Integer threadCount;
private ProcessingMode processingMode;
private Builder() {
}
/**
* Set number of threads for processing tasks in the queue.
*
* @param threadCount Number of processing threads.
* @return Reference to the same builder.
*/
public Builder withThreadCount(@Nonnull Integer threadCount) {
this.threadCount = threadCount;
return this;
}
/**
* Set task processing mode in the queue.
*
* @param processingMode Task processing mode.
* @return Reference to the same builder.
*/
public Builder withProcessingMode(@Nonnull ProcessingMode processingMode) {
this.processingMode = processingMode;
return this;
}
public ProcessingSettings build() {
return new ProcessingSettings(threadCount, processingMode);
}
}
}