Skip to content

Commit d6e8dd8

Browse files
Merge pull request #241 from SpineEventEngine/yet-more-style
Yet more style
2 parents 787c0a0 + 2a1bbbd commit d6e8dd8

File tree

11 files changed

+247
-149
lines changed

11 files changed

+247
-149
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ buildscript {
3232
ext {
3333
guavaVersion = '20.0'
3434
protobufGradlePluginVerison = '0.8.0'
35-
spineVersion = '0.6.15-SNAPSHOT'
35+
spineVersion = '0.6.16-SNAPSHOT'
3636
//TODO:2016-12-12:alexander.yevsyukov: Advance the plug-in version together with all the components and change
3737
// the version of the dependency below to `spineVersion` defined above.
3838
spineProtobufPluginVersion = '0.6.6-SNAPSHOT'

client/src/main/java/org/spine3/change/Changes.java

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@
2323
import com.google.protobuf.ByteString;
2424
import com.google.protobuf.Timestamp;
2525

26-
import static com.google.common.base.Preconditions.checkArgument;
2726
import static com.google.common.base.Preconditions.checkNotNull;
27+
import static org.spine3.change.Preconditions.checkNewValueNotEmpty;
28+
import static org.spine3.change.Preconditions.checkNotEqual;
2829

2930
/**
3031
* Utility class for working with field changes.
@@ -35,12 +36,6 @@
3536
@SuppressWarnings("OverlyCoupledClass") /* ... because we want one utility class for all the Changes classes. */
3637
public class Changes {
3738

38-
public interface ErrorMessage {
39-
String VALUES_CANNOT_BE_EQUAL = "newValue cannot be equal to previousValue";
40-
String NEW_VALUE_CANNOT_BE_EMPTY = "newValue cannot be empty";
41-
String MUST_BE_A_POSITIVE_VALUE = "%s must be a positive value";
42-
}
43-
4439
private Changes() {
4540
}
4641

@@ -52,8 +47,8 @@ private Changes() {
5247
public static StringChange of(String previousValue, String newValue) {
5348
checkNotNull(previousValue);
5449
checkNotNull(newValue);
55-
checkArgument(!newValue.isEmpty(), ErrorMessage.NEW_VALUE_CANNOT_BE_EMPTY);
56-
checkArgument(!newValue.equals(previousValue), ErrorMessage.VALUES_CANNOT_BE_EQUAL);
50+
checkNewValueNotEmpty(newValue);
51+
checkNotEqual(previousValue, newValue);
5752

5853
final StringChange result = StringChange.newBuilder()
5954
.setPreviousValue(previousValue)
@@ -70,7 +65,7 @@ public static StringChange of(String previousValue, String newValue) {
7065
public static TimestampChange of(Timestamp previousValue, Timestamp newValue) {
7166
checkNotNull(previousValue);
7267
checkNotNull(newValue);
73-
checkArgument(!newValue.equals(previousValue), ErrorMessage.VALUES_CANNOT_BE_EQUAL);
68+
checkNotEqual(previousValue, newValue);
7469

7570
final TimestampChange result = TimestampChange.newBuilder()
7671
.setPreviousValue(previousValue)
@@ -85,7 +80,7 @@ public static TimestampChange of(Timestamp previousValue, Timestamp newValue) {
8580
* <p>Passed values cannot be equal.
8681
*/
8782
public static DoubleChange of(double previousValue, double newValue) {
88-
checkArgument(Double.compare(newValue, previousValue) != 0, ErrorMessage.VALUES_CANNOT_BE_EQUAL);
83+
checkNotEqual(previousValue, newValue);
8984

9085
final DoubleChange result = DoubleChange.newBuilder()
9186
.setPreviousValue(previousValue)
@@ -100,7 +95,7 @@ public static DoubleChange of(double previousValue, double newValue) {
10095
* <p>Passed values cannot be equal.
10196
*/
10297
public static FloatChange of(float previousValue, float newValue) {
103-
checkArgument(Float.compare(newValue, previousValue) != 0, ErrorMessage.VALUES_CANNOT_BE_EQUAL);
98+
checkNotEqual(previousValue, newValue);
10499

105100
final FloatChange result = FloatChange.newBuilder()
106101
.setPreviousValue(previousValue)
@@ -115,7 +110,7 @@ public static FloatChange of(float previousValue, float newValue) {
115110
* <p>Passed values cannot be equal.
116111
*/
117112
public static Int32Change ofInt32(int previousValue, int newValue) {
118-
checkArgument(Integer.compare(newValue, previousValue) != 0, ErrorMessage.VALUES_CANNOT_BE_EQUAL);
113+
checkNotEqual(previousValue, newValue);
119114

120115
final Int32Change result = Int32Change.newBuilder()
121116
.setPreviousValue(previousValue)
@@ -130,7 +125,7 @@ public static Int32Change ofInt32(int previousValue, int newValue) {
130125
* <p>Passed values cannot be equal.
131126
*/
132127
public static Int64Change ofInt64(long previousValue, long newValue) {
133-
checkArgument(Long.compare(newValue, previousValue) != 0, ErrorMessage.VALUES_CANNOT_BE_EQUAL);
128+
checkNotEqual(previousValue, newValue);
134129

135130
final Int64Change result = Int64Change.newBuilder()
136131
.setPreviousValue(previousValue)
@@ -145,7 +140,7 @@ public static Int64Change ofInt64(long previousValue, long newValue) {
145140
* <p>Passed values cannot be equal.
146141
*/
147142
public static UInt32Change ofUInt32(int previousValue, int newValue) {
148-
checkArgument(Integer.compare(newValue, previousValue) != 0, ErrorMessage.VALUES_CANNOT_BE_EQUAL);
143+
checkNotEqual(previousValue, newValue);
149144

150145
final UInt32Change result = UInt32Change.newBuilder()
151146
.setPreviousValue(previousValue)
@@ -160,7 +155,7 @@ public static UInt32Change ofUInt32(int previousValue, int newValue) {
160155
* <p>Passed values cannot be equal.
161156
*/
162157
public static UInt64Change ofUInt64(long previousValue, long newValue) {
163-
checkArgument(Long.compare(newValue, previousValue) != 0, ErrorMessage.VALUES_CANNOT_BE_EQUAL);
158+
checkNotEqual(previousValue, newValue);
164159

165160
final UInt64Change result = UInt64Change.newBuilder()
166161
.setPreviousValue(previousValue)
@@ -175,7 +170,7 @@ public static UInt64Change ofUInt64(long previousValue, long newValue) {
175170
* <p>Passed values cannot be equal.
176171
*/
177172
public static SInt32Change ofSInt32(int previousValue, int newValue) {
178-
checkArgument(Integer.compare(newValue, previousValue) != 0, ErrorMessage.VALUES_CANNOT_BE_EQUAL);
173+
checkNotEqual(previousValue, newValue);
179174

180175
final SInt32Change result = SInt32Change.newBuilder()
181176
.setPreviousValue(previousValue)
@@ -190,7 +185,7 @@ public static SInt32Change ofSInt32(int previousValue, int newValue) {
190185
* <p>Passed values cannot be equal.
191186
*/
192187
public static SInt64Change ofSInt64(long previousValue, long newValue) {
193-
checkArgument(Long.compare(newValue, previousValue) != 0, ErrorMessage.VALUES_CANNOT_BE_EQUAL);
188+
checkNotEqual(previousValue, newValue);
194189

195190
final SInt64Change result = SInt64Change.newBuilder()
196191
.setPreviousValue(previousValue)
@@ -205,7 +200,7 @@ public static SInt64Change ofSInt64(long previousValue, long newValue) {
205200
* <p>Passed values cannot be equal.
206201
*/
207202
public static Fixed32Change ofFixed32(int previousValue, int newValue) {
208-
checkArgument(Integer.compare(newValue, previousValue) != 0, ErrorMessage.VALUES_CANNOT_BE_EQUAL);
203+
checkNotEqual(previousValue, newValue);
209204

210205
final Fixed32Change result = Fixed32Change.newBuilder()
211206
.setPreviousValue(previousValue)
@@ -220,7 +215,7 @@ public static Fixed32Change ofFixed32(int previousValue, int newValue) {
220215
* <p>Passed values cannot be equal.
221216
*/
222217
public static Fixed64Change ofFixed64(long previousValue, long newValue) {
223-
checkArgument(Long.compare(newValue, previousValue) != 0, ErrorMessage.VALUES_CANNOT_BE_EQUAL);
218+
checkNotEqual(previousValue, newValue);
224219

225220
final Fixed64Change result = Fixed64Change.newBuilder()
226221
.setPreviousValue(previousValue)
@@ -235,7 +230,7 @@ public static Fixed64Change ofFixed64(long previousValue, long newValue) {
235230
* <p>Passed values cannot be equal.
236231
*/
237232
public static Sfixed32Change ofSfixed32(int previousValue, int newValue) {
238-
checkArgument(Integer.compare(newValue, previousValue) != 0, ErrorMessage.VALUES_CANNOT_BE_EQUAL);
233+
checkNotEqual(previousValue, newValue);
239234

240235
final Sfixed32Change result = Sfixed32Change.newBuilder()
241236
.setPreviousValue(previousValue)
@@ -250,7 +245,7 @@ public static Sfixed32Change ofSfixed32(int previousValue, int newValue) {
250245
* <p>Passed values cannot be equal.
251246
*/
252247
public static Sfixed64Change ofSfixed64(long previousValue, long newValue) {
253-
checkArgument(Long.compare(newValue, previousValue) != 0, ErrorMessage.VALUES_CANNOT_BE_EQUAL);
248+
checkNotEqual(previousValue, newValue);
254249

255250
final Sfixed64Change result = Sfixed64Change.newBuilder()
256251
.setPreviousValue(previousValue)
@@ -267,8 +262,8 @@ public static Sfixed64Change ofSfixed64(long previousValue, long newValue) {
267262
public static BytesChange of(ByteString previousValue, ByteString newValue) {
268263
checkNotNull(previousValue);
269264
checkNotNull(newValue);
270-
checkArgument(!newValue.isEmpty(), ErrorMessage.NEW_VALUE_CANNOT_BE_EMPTY);
271-
checkArgument(!newValue.equals(previousValue), ErrorMessage.VALUES_CANNOT_BE_EQUAL);
265+
checkNewValueNotEmpty(newValue);
266+
checkNotEqual(previousValue, newValue);
272267

273268
final BytesChange result = BytesChange.newBuilder()
274269
.setPreviousValue(previousValue)
@@ -283,7 +278,7 @@ public static BytesChange of(ByteString previousValue, ByteString newValue) {
283278
* <p>Passed values cannot be equal.
284279
*/
285280
public static BooleanChange of(boolean previousValue, boolean newValue) {
286-
checkArgument(Boolean.compare(newValue, previousValue) != 0, ErrorMessage.VALUES_CANNOT_BE_EQUAL);
281+
checkNotEqual(previousValue, newValue);
287282

288283
final BooleanChange result = BooleanChange.newBuilder()
289284
.setPreviousValue(previousValue)
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
* Copyright 2016, TeamDev Ltd. All rights reserved.
3+
*
4+
* Redistribution and use in source and/or binary forms, with or without
5+
* modification, must retain the above copyright notice and the following
6+
* disclaimer.
7+
*
8+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
9+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
10+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
11+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
12+
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
13+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
14+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
15+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
16+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
17+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
18+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
19+
*/
20+
21+
package org.spine3.change;
22+
23+
import com.google.protobuf.ByteString;
24+
25+
import static com.google.common.base.Preconditions.checkArgument;
26+
27+
/**
28+
* Checking of parameters for working with changes.
29+
*
30+
* @author Alexander Yevsyukov
31+
*/
32+
@SuppressWarnings("OverloadedMethodsWithSameNumberOfParameters")
33+
public class Preconditions {
34+
35+
private static final String NEW_VALUE_CANNOT_BE_EMPTY = "newValue cannot be empty";
36+
private static final String VALUES_CANNOT_BE_EQUAL = "newValue cannot be equal to previousValue";
37+
38+
private Preconditions() {
39+
}
40+
41+
/**
42+
* Ensures that parameters are not equal.
43+
*
44+
* @throws IllegalArgumentException in case if values are equal
45+
*/
46+
public static void checkNotEqual(int previousValue, int newValue) {
47+
checkArgument(Integer.compare(newValue, previousValue) != 0, VALUES_CANNOT_BE_EQUAL);
48+
}
49+
50+
/**
51+
* Ensures that parameters are not equal.
52+
*
53+
* @throws IllegalArgumentException in case if values are equal
54+
*/
55+
public static void checkNotEqual(long previousValue, long newValue) {
56+
checkArgument(Long.compare(newValue, previousValue) != 0, VALUES_CANNOT_BE_EQUAL);
57+
}
58+
59+
/**
60+
* Ensures that parameters are not equal.
61+
*
62+
* @throws IllegalArgumentException in case if values are equal
63+
*/
64+
public static void checkNotEqual(float previousValue, float newValue) {
65+
checkArgument(Float.compare(newValue, previousValue) != 0, VALUES_CANNOT_BE_EQUAL);
66+
}
67+
68+
/**
69+
* Ensures that parameters are not equal.
70+
*
71+
* @throws IllegalArgumentException in case if values are equal
72+
*/
73+
public static void checkNotEqual(double previousValue, double newValue) {
74+
checkArgument(Double.compare(newValue, previousValue) != 0, VALUES_CANNOT_BE_EQUAL);
75+
}
76+
77+
/**
78+
* Ensures that parameters are not equal.
79+
*
80+
* @throws IllegalArgumentException in case if values are equal
81+
*/
82+
public static <T> void checkNotEqual(T previousValue, T newValue) {
83+
checkArgument(!newValue.equals(previousValue), VALUES_CANNOT_BE_EQUAL);
84+
}
85+
86+
/**
87+
* Ensures that parameter size is more than 0.
88+
*
89+
* @throws IllegalArgumentException in case if parameter is empty
90+
*/
91+
public static void checkNewValueNotEmpty(ByteString newValue) {
92+
checkArgument(!newValue.isEmpty(), NEW_VALUE_CANNOT_BE_EMPTY);
93+
}
94+
95+
/**
96+
* Ensures that parameter size is more than 0.
97+
*
98+
* @throws IllegalArgumentException in case if parameter is empty
99+
*/
100+
public static void checkNewValueNotEmpty(String newValue) {
101+
checkArgument(!newValue.isEmpty(), NEW_VALUE_CANNOT_BE_EMPTY);
102+
}
103+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright 2016, TeamDev Ltd. All rights reserved.
3+
*
4+
* Redistribution and use in source and/or binary forms, with or without
5+
* modification, must retain the above copyright notice and the following
6+
* disclaimer.
7+
*
8+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
9+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
10+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
11+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
12+
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
13+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
14+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
15+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
16+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
17+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
18+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
19+
*/
20+
21+
package org.spine3.validate;
22+
23+
import java.util.List;
24+
25+
/**
26+
* Utility class for working with {@link ConstraintViolation}s.
27+
*
28+
* @author Alexander Yevsyukov
29+
*/
30+
public class ConstraintViolations {
31+
32+
private ConstraintViolations() {
33+
}
34+
35+
/**
36+
* Returns a formatted string using the format string and parameters from the violation.
37+
*
38+
* @param violation violation which contains the format string and
39+
* arguments referenced by the format specifiers in it
40+
* @return a formatted string
41+
* @see String#format(String, Object...)
42+
*/
43+
public static String toText(ConstraintViolation violation) {
44+
final String format = violation.getMsgFormat();
45+
final List<String> params = violation.getParamList();
46+
final String result = String.format(format, params.toArray());
47+
return result;
48+
}
49+
50+
/**
51+
* Returns a formatted string using the specified format string and parameters from the violation.
52+
*
53+
* @param format a format string
54+
* @param violation violation which contains arguments referenced by the format specifiers in the format string
55+
* @return a formatted string
56+
* @see String#format(String, Object...)
57+
*/
58+
public static String toText(String format, ConstraintViolation violation) {
59+
final List<String> params = violation.getParamList();
60+
final String result = String.format(format, params.toArray());
61+
return result;
62+
}
63+
}

0 commit comments

Comments
 (0)