From 746d36b3bc3bff55b0ae17262ff8dd93993cda11 Mon Sep 17 00:00:00 2001 From: Alexander Yevsyukov Date: Wed, 28 Dec 2016 16:56:50 +0200 Subject: [PATCH 1/5] Extract checks into change-specific `Preconditions` class --- .../main/java/org/spine3/change/Changes.java | 45 ++++++------ .../java/org/spine3/change/Preconditions.java | 68 +++++++++++++++++++ .../spine3/validate/ConstraintViolations.java | 63 +++++++++++++++++ .../java/org/spine3/validate/Validate.java | 38 ++--------- .../org/spine3/validate/ValidateShould.java | 4 +- .../server/command/CommandValidator.java | 5 +- .../java/org/spine3/time/change/Changes.java | 15 ++-- 7 files changed, 169 insertions(+), 69 deletions(-) create mode 100644 client/src/main/java/org/spine3/change/Preconditions.java create mode 100644 client/src/main/java/org/spine3/validate/ConstraintViolations.java diff --git a/client/src/main/java/org/spine3/change/Changes.java b/client/src/main/java/org/spine3/change/Changes.java index 71725ff58df..028ae128204 100644 --- a/client/src/main/java/org/spine3/change/Changes.java +++ b/client/src/main/java/org/spine3/change/Changes.java @@ -23,8 +23,9 @@ import com.google.protobuf.ByteString; import com.google.protobuf.Timestamp; -import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkNotNull; +import static org.spine3.change.Preconditions.checkNewValueNotEmpty; +import static org.spine3.change.Preconditions.checkNotEqual; /** * Utility class for working with field changes. @@ -35,12 +36,6 @@ @SuppressWarnings("OverlyCoupledClass") /* ... because we want one utility class for all the Changes classes. */ public class Changes { - public interface ErrorMessage { - String VALUES_CANNOT_BE_EQUAL = "newValue cannot be equal to previousValue"; - String NEW_VALUE_CANNOT_BE_EMPTY = "newValue cannot be empty"; - String MUST_BE_A_POSITIVE_VALUE = "%s must be a positive value"; - } - private Changes() { } @@ -52,8 +47,8 @@ private Changes() { public static StringChange of(String previousValue, String newValue) { checkNotNull(previousValue); checkNotNull(newValue); - checkArgument(!newValue.isEmpty(), ErrorMessage.NEW_VALUE_CANNOT_BE_EMPTY); - checkArgument(!newValue.equals(previousValue), ErrorMessage.VALUES_CANNOT_BE_EQUAL); + checkNewValueNotEmpty(newValue); + checkNotEqual(previousValue, newValue); final StringChange result = StringChange.newBuilder() .setPreviousValue(previousValue) @@ -70,7 +65,7 @@ public static StringChange of(String previousValue, String newValue) { public static TimestampChange of(Timestamp previousValue, Timestamp newValue) { checkNotNull(previousValue); checkNotNull(newValue); - checkArgument(!newValue.equals(previousValue), ErrorMessage.VALUES_CANNOT_BE_EQUAL); + checkNotEqual(previousValue, newValue); final TimestampChange result = TimestampChange.newBuilder() .setPreviousValue(previousValue) @@ -85,7 +80,7 @@ public static TimestampChange of(Timestamp previousValue, Timestamp newValue) { *

Passed values cannot be equal. */ public static DoubleChange of(double previousValue, double newValue) { - checkArgument(Double.compare(newValue, previousValue) != 0, ErrorMessage.VALUES_CANNOT_BE_EQUAL); + checkNotEqual(previousValue, newValue); final DoubleChange result = DoubleChange.newBuilder() .setPreviousValue(previousValue) @@ -100,7 +95,7 @@ public static DoubleChange of(double previousValue, double newValue) { *

Passed values cannot be equal. */ public static FloatChange of(float previousValue, float newValue) { - checkArgument(Float.compare(newValue, previousValue) != 0, ErrorMessage.VALUES_CANNOT_BE_EQUAL); + checkNotEqual(previousValue, newValue); final FloatChange result = FloatChange.newBuilder() .setPreviousValue(previousValue) @@ -115,7 +110,7 @@ public static FloatChange of(float previousValue, float newValue) { *

Passed values cannot be equal. */ public static Int32Change ofInt32(int previousValue, int newValue) { - checkArgument(Integer.compare(newValue, previousValue) != 0, ErrorMessage.VALUES_CANNOT_BE_EQUAL); + checkNotEqual(previousValue, newValue); final Int32Change result = Int32Change.newBuilder() .setPreviousValue(previousValue) @@ -130,7 +125,7 @@ public static Int32Change ofInt32(int previousValue, int newValue) { *

Passed values cannot be equal. */ public static Int64Change ofInt64(long previousValue, long newValue) { - checkArgument(Long.compare(newValue, previousValue) != 0, ErrorMessage.VALUES_CANNOT_BE_EQUAL); + checkNotEqual(previousValue, newValue); final Int64Change result = Int64Change.newBuilder() .setPreviousValue(previousValue) @@ -145,7 +140,7 @@ public static Int64Change ofInt64(long previousValue, long newValue) { *

Passed values cannot be equal. */ public static UInt32Change ofUInt32(int previousValue, int newValue) { - checkArgument(Integer.compare(newValue, previousValue) != 0, ErrorMessage.VALUES_CANNOT_BE_EQUAL); + checkNotEqual(previousValue, newValue); final UInt32Change result = UInt32Change.newBuilder() .setPreviousValue(previousValue) @@ -160,7 +155,7 @@ public static UInt32Change ofUInt32(int previousValue, int newValue) { *

Passed values cannot be equal. */ public static UInt64Change ofUInt64(long previousValue, long newValue) { - checkArgument(Long.compare(newValue, previousValue) != 0, ErrorMessage.VALUES_CANNOT_BE_EQUAL); + checkNotEqual(previousValue, newValue); final UInt64Change result = UInt64Change.newBuilder() .setPreviousValue(previousValue) @@ -175,7 +170,7 @@ public static UInt64Change ofUInt64(long previousValue, long newValue) { *

Passed values cannot be equal. */ public static SInt32Change ofSInt32(int previousValue, int newValue) { - checkArgument(Integer.compare(newValue, previousValue) != 0, ErrorMessage.VALUES_CANNOT_BE_EQUAL); + checkNotEqual(previousValue, newValue); final SInt32Change result = SInt32Change.newBuilder() .setPreviousValue(previousValue) @@ -190,7 +185,7 @@ public static SInt32Change ofSInt32(int previousValue, int newValue) { *

Passed values cannot be equal. */ public static SInt64Change ofSInt64(long previousValue, long newValue) { - checkArgument(Long.compare(newValue, previousValue) != 0, ErrorMessage.VALUES_CANNOT_BE_EQUAL); + checkNotEqual(previousValue, newValue); final SInt64Change result = SInt64Change.newBuilder() .setPreviousValue(previousValue) @@ -205,7 +200,7 @@ public static SInt64Change ofSInt64(long previousValue, long newValue) { *

Passed values cannot be equal. */ public static Fixed32Change ofFixed32(int previousValue, int newValue) { - checkArgument(Integer.compare(newValue, previousValue) != 0, ErrorMessage.VALUES_CANNOT_BE_EQUAL); + checkNotEqual(previousValue, newValue); final Fixed32Change result = Fixed32Change.newBuilder() .setPreviousValue(previousValue) @@ -220,7 +215,7 @@ public static Fixed32Change ofFixed32(int previousValue, int newValue) { *

Passed values cannot be equal. */ public static Fixed64Change ofFixed64(long previousValue, long newValue) { - checkArgument(Long.compare(newValue, previousValue) != 0, ErrorMessage.VALUES_CANNOT_BE_EQUAL); + checkNotEqual(previousValue, newValue); final Fixed64Change result = Fixed64Change.newBuilder() .setPreviousValue(previousValue) @@ -235,7 +230,7 @@ public static Fixed64Change ofFixed64(long previousValue, long newValue) { *

Passed values cannot be equal. */ public static Sfixed32Change ofSfixed32(int previousValue, int newValue) { - checkArgument(Integer.compare(newValue, previousValue) != 0, ErrorMessage.VALUES_CANNOT_BE_EQUAL); + checkNotEqual(previousValue, newValue); final Sfixed32Change result = Sfixed32Change.newBuilder() .setPreviousValue(previousValue) @@ -250,7 +245,7 @@ public static Sfixed32Change ofSfixed32(int previousValue, int newValue) { *

Passed values cannot be equal. */ public static Sfixed64Change ofSfixed64(long previousValue, long newValue) { - checkArgument(Long.compare(newValue, previousValue) != 0, ErrorMessage.VALUES_CANNOT_BE_EQUAL); + checkNotEqual(previousValue, newValue); final Sfixed64Change result = Sfixed64Change.newBuilder() .setPreviousValue(previousValue) @@ -267,8 +262,8 @@ public static Sfixed64Change ofSfixed64(long previousValue, long newValue) { public static BytesChange of(ByteString previousValue, ByteString newValue) { checkNotNull(previousValue); checkNotNull(newValue); - checkArgument(!newValue.isEmpty(), ErrorMessage.NEW_VALUE_CANNOT_BE_EMPTY); - checkArgument(!newValue.equals(previousValue), ErrorMessage.VALUES_CANNOT_BE_EQUAL); + checkNewValueNotEmpty(newValue); + checkNotEqual(previousValue, newValue); final BytesChange result = BytesChange.newBuilder() .setPreviousValue(previousValue) @@ -283,7 +278,7 @@ public static BytesChange of(ByteString previousValue, ByteString newValue) { *

Passed values cannot be equal. */ public static BooleanChange of(boolean previousValue, boolean newValue) { - checkArgument(Boolean.compare(newValue, previousValue) != 0, ErrorMessage.VALUES_CANNOT_BE_EQUAL); + checkNotEqual(previousValue, newValue); final BooleanChange result = BooleanChange.newBuilder() .setPreviousValue(previousValue) diff --git a/client/src/main/java/org/spine3/change/Preconditions.java b/client/src/main/java/org/spine3/change/Preconditions.java new file mode 100644 index 00000000000..dce3c8ba00e --- /dev/null +++ b/client/src/main/java/org/spine3/change/Preconditions.java @@ -0,0 +1,68 @@ +/* + * Copyright 2016, TeamDev Ltd. All rights reserved. + * + * Redistribution and use in source and/or binary forms, with or without + * modification, must retain the above copyright notice and the following + * disclaimer. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.spine3.change; + +import com.google.protobuf.ByteString; + +import static com.google.common.base.Preconditions.checkArgument; + +/** + * Checking of parameters for working with changes. + * + * @author Alexander Yevsyukov + */ +@SuppressWarnings("OverloadedMethodsWithSameNumberOfParameters") +public class Preconditions { + + private static final String NEW_VALUE_CANNOT_BE_EMPTY = "newValue cannot be empty"; + private static final String VALUES_CANNOT_BE_EQUAL = "newValue cannot be equal to previousValue"; + + private Preconditions() { + } + + public static void checkNotEqual(int previousValue, int newValue) { + com.google.common.base.Preconditions.checkArgument(Integer.compare(newValue, previousValue) != 0, VALUES_CANNOT_BE_EQUAL); + } + + public static void checkNotEqual(long previousValue, long newValue) { + com.google.common.base.Preconditions.checkArgument(Long.compare(newValue, previousValue) != 0, VALUES_CANNOT_BE_EQUAL); + } + + public static void checkNotEqual(float previousValue, float newValue) { + checkArgument(Float.compare(newValue, previousValue) != 0, VALUES_CANNOT_BE_EQUAL); + } + + public static void checkNotEqual(double previousValue, double newValue) { + checkArgument(Double.compare(newValue, previousValue) != 0, VALUES_CANNOT_BE_EQUAL); + } + + public static void checkNotEqual(T previousValue, T newValue) { + com.google.common.base.Preconditions.checkArgument(!newValue.equals(previousValue), VALUES_CANNOT_BE_EQUAL); + } + + public static void checkNewValueNotEmpty(ByteString newValue) { + com.google.common.base.Preconditions.checkArgument(!newValue.isEmpty(), NEW_VALUE_CANNOT_BE_EMPTY); + } + + public static void checkNewValueNotEmpty(String newValue) { + com.google.common.base.Preconditions.checkArgument(!newValue.isEmpty(), NEW_VALUE_CANNOT_BE_EMPTY); + } +} diff --git a/client/src/main/java/org/spine3/validate/ConstraintViolations.java b/client/src/main/java/org/spine3/validate/ConstraintViolations.java new file mode 100644 index 00000000000..f03e032b5eb --- /dev/null +++ b/client/src/main/java/org/spine3/validate/ConstraintViolations.java @@ -0,0 +1,63 @@ +/* + * Copyright 2016, TeamDev Ltd. All rights reserved. + * + * Redistribution and use in source and/or binary forms, with or without + * modification, must retain the above copyright notice and the following + * disclaimer. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.spine3.validate; + +import java.util.List; + +/** + * Utility class for working with {@link ConstraintViolation}s. + * + * @author Alexander Yevsyukov + */ +public class ConstraintViolations { + + private ConstraintViolations() { + } + + /** + * Returns a formatted string using the format string and parameters from the violation. + * + * @param violation violation which contains the format string and + * arguments referenced by the format specifiers in it + * @return a formatted string + * @see String#format(String, Object...) + */ + public static String toText(ConstraintViolation violation) { + final String format = violation.getMsgFormat(); + final List params = violation.getParamList(); + final String result = String.format(format, params.toArray()); + return result; + } + + /** + * Returns a formatted string using the specified format string and parameters from the violation. + * + * @param format a format string + * @param violation violation which contains arguments referenced by the format specifiers in the format string + * @return a formatted string + * @see String#format(String, Object...) + */ + public static String toText(String format, ConstraintViolation violation) { + final List params = violation.getParamList(); + final String result = String.format(format, params.toArray()); + return result; + } +} diff --git a/client/src/main/java/org/spine3/validate/Validate.java b/client/src/main/java/org/spine3/validate/Validate.java index 9bca136e0a3..b1c9f76cc09 100644 --- a/client/src/main/java/org/spine3/validate/Validate.java +++ b/client/src/main/java/org/spine3/validate/Validate.java @@ -24,11 +24,9 @@ import com.google.protobuf.Timestamp; import org.spine3.base.CommandId; import org.spine3.base.EventId; -import org.spine3.change.Changes.ErrorMessage; import org.spine3.protobuf.TypeUrl; import javax.annotation.Nullable; -import java.util.List; import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkNotNull; @@ -43,6 +41,8 @@ */ public class Validate { + private static final String MUST_BE_A_POSITIVE_VALUE = "%s must be a positive value"; + private Validate() { } @@ -223,13 +223,15 @@ public static Timestamp checkPositive(Timestamp timestamp, String argumentName) * @throws IllegalArgumentException if requirement is not met */ public static void checkPositive(int value, String argumentName) { - checkParameter(value > 0, argumentName, ErrorMessage.MUST_BE_A_POSITIVE_VALUE); + checkParameter(value > 0, argumentName, MUST_BE_A_POSITIVE_VALUE); } + @SuppressWarnings("OverloadedMethodsWithSameNumberOfParameters") public static void checkPositive(int value) { checkPositive(value, ""); } + @SuppressWarnings("OverloadedMethodsWithSameNumberOfParameters") public static void checkPositive(long value) { checkPositive(value, ""); } @@ -242,7 +244,7 @@ public static void checkPositive(long value) { * @throws IllegalArgumentException if requirement is not met */ public static void checkPositive(long value, String argumentName) { - checkParameter(value > 0L, argumentName, ErrorMessage.MUST_BE_A_POSITIVE_VALUE); + checkParameter(value > 0L, argumentName, MUST_BE_A_POSITIVE_VALUE); } public static void checkPositiveOrZero(long value) { @@ -274,32 +276,4 @@ public static CommandId checkValid(CommandId id) { return id; } - /** - * Returns a formatted string using the format string and parameters from the violation. - * - * @param violation violation which contains the format string and - * arguments referenced by the format specifiers in it - * @return a formatted string - * @see String#format(String, Object...) - */ - public static String toText(ConstraintViolation violation) { - final String format = violation.getMsgFormat(); - final List params = violation.getParamList(); - final String result = String.format(format, params.toArray()); - return result; - } - - /** - * Returns a formatted string using the specified format string and parameters from the violation. - * - * @param format a format string - * @param violation violation which contains arguments referenced by the format specifiers in the format string - * @return a formatted string - * @see String#format(String, Object...) - */ - public static String toText(String format, ConstraintViolation violation) { - final List params = violation.getParamList(); - final String result = String.format(format, params.toArray()); - return result; - } } diff --git a/client/src/test/java/org/spine3/validate/ValidateShould.java b/client/src/test/java/org/spine3/validate/ValidateShould.java index bb8f2ac52cc..46aefab94a9 100644 --- a/client/src/test/java/org/spine3/validate/ValidateShould.java +++ b/client/src/test/java/org/spine3/validate/ValidateShould.java @@ -176,7 +176,7 @@ public void format_message_from_constraint_violation() { .addParam("1") .addParam("2") .build(); - final String formatted = Validate.toText(violation); + final String formatted = ConstraintViolations.toText(violation); assertEquals("test 1 test 2", formatted); } @@ -187,7 +187,7 @@ public void format_message_using_params_from_constraint_violation() { .addParam("1") .addParam("2") .build(); - final String formatted = Validate.toText("abc %s abc %s", violation); + final String formatted = ConstraintViolations.toText("abc %s abc %s", violation); assertEquals("abc 1 abc 2", formatted); } diff --git a/server/src/main/java/org/spine3/server/command/CommandValidator.java b/server/src/main/java/org/spine3/server/command/CommandValidator.java index 6496ae3b480..16a15465a2c 100644 --- a/server/src/main/java/org/spine3/server/command/CommandValidator.java +++ b/server/src/main/java/org/spine3/server/command/CommandValidator.java @@ -49,13 +49,14 @@ public class CommandValidator { private static final String COMMAND_TARGET_ENTITY_ID_CANNOT_BE_EMPTY_OR_BLANK = "Command target entity ID cannot be empty or blank."; + private CommandValidator() { + } + /** Returns a validator instance. */ public static CommandValidator getInstance() { return Singleton.INSTANCE.value; } - private CommandValidator() {} - /** * Validates a command checking that its required fields are valid and * validates a command message according to Spine custom protobuf options. diff --git a/values/src/main/java/org/spine3/time/change/Changes.java b/values/src/main/java/org/spine3/time/change/Changes.java index 7ec8a56c00d..bf2dd622927 100644 --- a/values/src/main/java/org/spine3/time/change/Changes.java +++ b/values/src/main/java/org/spine3/time/change/Changes.java @@ -19,7 +19,6 @@ */ package org.spine3.time.change; -import org.spine3.change.Changes.ErrorMessage; import org.spine3.time.Interval; import org.spine3.time.LocalDate; import org.spine3.time.LocalTime; @@ -27,8 +26,8 @@ import org.spine3.time.OffsetDateTime; import org.spine3.time.OffsetTime; -import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkNotNull; +import static org.spine3.change.Preconditions.checkNotEqual; /** * Utility class for working with field changes. @@ -48,7 +47,7 @@ private Changes() { public static IntervalChange of(Interval previousValue, Interval newValue) { checkNotNull(previousValue); checkNotNull(newValue); - checkArgument(!newValue.equals(previousValue), ErrorMessage.VALUES_CANNOT_BE_EQUAL); + checkNotEqual(previousValue, newValue); final IntervalChange result = IntervalChange.newBuilder() .setPreviousValue(previousValue) @@ -65,7 +64,7 @@ public static IntervalChange of(Interval previousValue, Interval newValue) { public static LocalDateChange of(LocalDate previousValue, LocalDate newValue) { checkNotNull(previousValue); checkNotNull(newValue); - checkArgument(!newValue.equals(previousValue), ErrorMessage.VALUES_CANNOT_BE_EQUAL); + checkNotEqual(previousValue, newValue); final LocalDateChange result = LocalDateChange.newBuilder() .setPreviousValue(previousValue) @@ -82,7 +81,7 @@ public static LocalDateChange of(LocalDate previousValue, LocalDate newValue) { public static LocalTimeChange of(LocalTime previousValue, LocalTime newValue) { checkNotNull(previousValue); checkNotNull(newValue); - checkArgument(!newValue.equals(previousValue), ErrorMessage.VALUES_CANNOT_BE_EQUAL); + checkNotEqual(previousValue, newValue); final LocalTimeChange result = LocalTimeChange.newBuilder() .setPreviousValue(previousValue) @@ -99,7 +98,7 @@ public static LocalTimeChange of(LocalTime previousValue, LocalTime newValue) { public static OffsetTimeChange of(OffsetTime previousValue, OffsetTime newValue) { checkNotNull(previousValue); checkNotNull(newValue); - checkArgument(!newValue.equals(previousValue), ErrorMessage.VALUES_CANNOT_BE_EQUAL); + checkNotEqual(previousValue, newValue); final OffsetTimeChange result = OffsetTimeChange.newBuilder() .setPreviousValue(previousValue) @@ -116,7 +115,7 @@ public static OffsetTimeChange of(OffsetTime previousValue, OffsetTime newValue) public static OffsetDateChange of(OffsetDate previousValue, OffsetDate newValue) { checkNotNull(previousValue); checkNotNull(newValue); - checkArgument(!newValue.equals(previousValue), ErrorMessage.VALUES_CANNOT_BE_EQUAL); + checkNotEqual(previousValue, newValue); final OffsetDateChange result = OffsetDateChange.newBuilder() .setPreviousValue(previousValue) @@ -133,7 +132,7 @@ public static OffsetDateChange of(OffsetDate previousValue, OffsetDate newValue) public static OffsetDateTimeChange of(OffsetDateTime previousValue, OffsetDateTime newValue) { checkNotNull(previousValue); checkNotNull(newValue); - checkArgument(!newValue.equals(previousValue), ErrorMessage.VALUES_CANNOT_BE_EQUAL); + checkNotEqual(previousValue, newValue); final OffsetDateTimeChange result = OffsetDateTimeChange.newBuilder() .setPreviousValue(previousValue) From 60bd9409b11cc737682fcab52f1dbc6bf7f7d28a Mon Sep 17 00:00:00 2001 From: Alexander Yevsyukov Date: Wed, 28 Dec 2016 17:01:12 +0200 Subject: [PATCH 2/5] Remove unused exception classes. Advance Spine version to 0.6.16-SNAPSHOT --- build.gradle | 2 +- .../spine3/error/AccessLevelException.java | 34 ------------ .../error/DuplicateApplierException.java | 55 ------------------- 3 files changed, 1 insertion(+), 90 deletions(-) delete mode 100644 server/src/main/java/org/spine3/error/AccessLevelException.java delete mode 100644 server/src/main/java/org/spine3/server/aggregate/error/DuplicateApplierException.java diff --git a/build.gradle b/build.gradle index 6e4058d7037..38585dd4ea1 100644 --- a/build.gradle +++ b/build.gradle @@ -32,7 +32,7 @@ buildscript { ext { guavaVersion = '20.0' protobufGradlePluginVerison = '0.8.0' - spineVersion = '0.6.15-SNAPSHOT' + spineVersion = '0.6.16-SNAPSHOT' //TODO:2016-12-12:alexander.yevsyukov: Advance the plug-in version together with all the components and change // the version of the dependency below to `spineVersion` defined above. spineProtobufPluginVersion = '0.6.6-SNAPSHOT' diff --git a/server/src/main/java/org/spine3/error/AccessLevelException.java b/server/src/main/java/org/spine3/error/AccessLevelException.java deleted file mode 100644 index a1c0f58c216..00000000000 --- a/server/src/main/java/org/spine3/error/AccessLevelException.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright 2016, TeamDev Ltd. All rights reserved. - * - * Redistribution and use in source and/or binary forms, with or without - * modification, must retain the above copyright notice and the following - * disclaimer. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ -package org.spine3.error; - -/** - * This exception is thrown if a handler method is defined with wrong access level modifier. - * - * @author Alexander Yevsyukov - */ -public class AccessLevelException extends RuntimeException { - - private static final long serialVersionUID = 0L; - - public AccessLevelException(String message) { - super(message); - } -} diff --git a/server/src/main/java/org/spine3/server/aggregate/error/DuplicateApplierException.java b/server/src/main/java/org/spine3/server/aggregate/error/DuplicateApplierException.java deleted file mode 100644 index ff98f3d14d5..00000000000 --- a/server/src/main/java/org/spine3/server/aggregate/error/DuplicateApplierException.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright 2016, TeamDev Ltd. All rights reserved. - * - * Redistribution and use in source and/or binary forms, with or without - * modification, must retain the above copyright notice and the following - * disclaimer. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ -package org.spine3.server.aggregate.error; - -import org.spine3.server.type.EventClass; - -/** - * Exception that is thrown when more than one applier - * of the same event class is found in a declaring class. - * - * @author Mikhail Melnik - * @author Alexander Yevsyukov - */ -public class DuplicateApplierException extends RuntimeException { - - private static final long serialVersionUID = 0L; - - /** - * Creates new exception. - * - * @param eventClass a class of the event - * @param currentApplier a name of the method currently registered - * @param discoveredApplier another applier method name for the same event class - */ - public DuplicateApplierException( - Class declaringClass, - EventClass eventClass, - String currentApplier, - String discoveredApplier) { - - super(String.format("The class %s defines more than one applier method for the event class %s. " + - "Applier methods encountered: %s, %s.", - declaringClass.getName(), - eventClass, - currentApplier, - discoveredApplier)); - } -} From af3858b2761020815cb4e2f3a3fced808693155a Mon Sep 17 00:00:00 2001 From: Alexander Yevsyukov Date: Wed, 28 Dec 2016 17:09:53 +0200 Subject: [PATCH 3/5] Reduce code duplication --- .../java/org/spine3/validate/Validate.java | 28 ++++++++----------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/client/src/main/java/org/spine3/validate/Validate.java b/client/src/main/java/org/spine3/validate/Validate.java index b1c9f76cc09..5f0b1bc8524 100644 --- a/client/src/main/java/org/spine3/validate/Validate.java +++ b/client/src/main/java/org/spine3/validate/Validate.java @@ -204,49 +204,44 @@ public static String checkNotEmptyOrBlank(String stringToCheck, String fieldName * * * @param timestamp the timestamp to check - * @param argumentName the name of the checked timestamp to be used in the error message + * @param argumentName the name of the checked value to be used in the error message * @return the passed timestamp * @throws IllegalArgumentException if any of the requirements are not met */ public static Timestamp checkPositive(Timestamp timestamp, String argumentName) { - checkNotNull(timestamp, argumentName + " is null."); + checkNotNull(timestamp, argumentName); checkParameter(timestamp.getSeconds() > 0, argumentName, "%s must have a positive number of seconds."); checkParameter(timestamp.getNanos() >= 0, argumentName, "%s must not have a negative number of nanoseconds."); return timestamp; } /** - * Ensures that the passed int value is positive: + * Ensures that the passed value is positive. * * @param value the value to check - * @param argumentName the name of the checked timestamp to be used in the error message * @throws IllegalArgumentException if requirement is not met */ - public static void checkPositive(int value, String argumentName) { - checkParameter(value > 0, argumentName, MUST_BE_A_POSITIVE_VALUE); - } - - @SuppressWarnings("OverloadedMethodsWithSameNumberOfParameters") - public static void checkPositive(int value) { - checkPositive(value, ""); - } - - @SuppressWarnings("OverloadedMethodsWithSameNumberOfParameters") public static void checkPositive(long value) { checkPositive(value, ""); } /** - * Ensures that the passed long value is positive: + * Ensures that the passed value is positive. * * @param value the value to check - * @param argumentName the name of the checked timestamp to be used in the error message + * @param argumentName the name of the checked value to be used in the error message * @throws IllegalArgumentException if requirement is not met */ public static void checkPositive(long value, String argumentName) { checkParameter(value > 0L, argumentName, MUST_BE_A_POSITIVE_VALUE); } + /** + * Ensures that the passed value is positive or zero. + * + * @param value the value to check + * @throws IllegalArgumentException if requirement is not met + */ public static void checkPositiveOrZero(long value) { checkArgument(value >= 0); } @@ -275,5 +270,4 @@ public static CommandId checkValid(CommandId id) { checkArgument(!idStr.equals(EMPTY_ID), "Command ID must not be an empty string."); return id; } - } From a6902ae920ebc8941ae9d8f005b64bbb76b9d2bb Mon Sep 17 00:00:00 2001 From: "a.aleksandrov" Date: Wed, 28 Dec 2016 18:08:44 +0200 Subject: [PATCH 4/5] Add javadoc for `Preconditions`, and tests. Add tests to cover `throwOverflow` in `Math`. --- .../java/org/spine3/change/Preconditions.java | 35 ++++++++++++++ .../spine3/change/PreconditionsShould.java | 48 +++++++++++++++++++ .../test/java/org/spine3/util/MathShould.java | 10 ++++ 3 files changed, 93 insertions(+) create mode 100644 client/src/test/java/org/spine3/change/PreconditionsShould.java diff --git a/client/src/main/java/org/spine3/change/Preconditions.java b/client/src/main/java/org/spine3/change/Preconditions.java index dce3c8ba00e..a32345eae9c 100644 --- a/client/src/main/java/org/spine3/change/Preconditions.java +++ b/client/src/main/java/org/spine3/change/Preconditions.java @@ -38,30 +38,65 @@ public class Preconditions { private Preconditions() { } + /** + * Ensures that parameters are not equal. + * + * @throws IllegalArgumentException in case if values are equal + */ public static void checkNotEqual(int previousValue, int newValue) { com.google.common.base.Preconditions.checkArgument(Integer.compare(newValue, previousValue) != 0, VALUES_CANNOT_BE_EQUAL); } + /** + * Ensures that parameters are not equal. + * + * @throws IllegalArgumentException in case if values are equal + */ public static void checkNotEqual(long previousValue, long newValue) { com.google.common.base.Preconditions.checkArgument(Long.compare(newValue, previousValue) != 0, VALUES_CANNOT_BE_EQUAL); } + /** + * Ensures that parameters are not equal. + * + * @throws IllegalArgumentException in case if values are equal + */ public static void checkNotEqual(float previousValue, float newValue) { checkArgument(Float.compare(newValue, previousValue) != 0, VALUES_CANNOT_BE_EQUAL); } + /** + * Ensures that parameters are not equal. + * + * @throws IllegalArgumentException in case if values are equal + */ public static void checkNotEqual(double previousValue, double newValue) { checkArgument(Double.compare(newValue, previousValue) != 0, VALUES_CANNOT_BE_EQUAL); } + /** + * Ensures that parameters are not equal. + * + * @throws IllegalArgumentException in case if values are equal + */ public static void checkNotEqual(T previousValue, T newValue) { com.google.common.base.Preconditions.checkArgument(!newValue.equals(previousValue), VALUES_CANNOT_BE_EQUAL); } + /** + * Ensures that parameter size is more than 0. + * + * @throws IllegalArgumentException in case if parameter is empty + */ public static void checkNewValueNotEmpty(ByteString newValue) { com.google.common.base.Preconditions.checkArgument(!newValue.isEmpty(), NEW_VALUE_CANNOT_BE_EMPTY); } + /** + * Ensures that parameter size is more than 0. + * + * @throws IllegalArgumentException in case if parameter is empty + */ public static void checkNewValueNotEmpty(String newValue) { com.google.common.base.Preconditions.checkArgument(!newValue.isEmpty(), NEW_VALUE_CANNOT_BE_EMPTY); } diff --git a/client/src/test/java/org/spine3/change/PreconditionsShould.java b/client/src/test/java/org/spine3/change/PreconditionsShould.java new file mode 100644 index 00000000000..71326bbb02a --- /dev/null +++ b/client/src/test/java/org/spine3/change/PreconditionsShould.java @@ -0,0 +1,48 @@ +/* + * Copyright 2016, TeamDev Ltd. All rights reserved. + * + * Redistribution and use in source and/or binary forms, with or without + * modification, must retain the above copyright notice and the following + * disclaimer. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.spine3.change; + +import com.google.protobuf.ByteString; +import org.junit.Test; + +import static org.junit.Assert.assertTrue; +import static org.spine3.change.Preconditions.checkNewValueNotEmpty; +import static org.spine3.test.Tests.hasPrivateUtilityConstructor; + +public class PreconditionsShould { + + @Test + public void have_private_constructor() { + assertTrue(hasPrivateUtilityConstructor(Preconditions.class)); + } + + @Test(expected = IllegalArgumentException.class) + public void not_accept_empty_ByteString_value() { + final ByteString str = ByteString.EMPTY; + checkNewValueNotEmpty(str); + } + + @Test(expected = IllegalArgumentException.class) + public void not_accept_empty_String_value() { + final String str = ""; + checkNewValueNotEmpty(str); + } +} diff --git a/client/src/test/java/org/spine3/util/MathShould.java b/client/src/test/java/org/spine3/util/MathShould.java index d8aed120783..e6696a220ed 100644 --- a/client/src/test/java/org/spine3/util/MathShould.java +++ b/client/src/test/java/org/spine3/util/MathShould.java @@ -53,4 +53,14 @@ public void quickly_return_zero_when_multiplying_by_zero() { public void quickly_return_same_value_when_multiplying_by_one() { assertEquals(8, Math.safeMultiply(8, 1)); } + + @Test(expected = ArithmeticException.class) + public void not_allow_multiply_LongMinValue_by_negative_amount() { + Math.safeMultiply(Long.MIN_VALUE, -1); + } + + @Test(expected = ArithmeticException.class) + public void not_allow_multiply_LongMaxValue_by_any_amount_except_zero_one_and_minus_one() { + Math.safeMultiply(Long.MAX_VALUE, -2); + } } From 2ca5a10e4c0f9e5b0afc1abbd376bc78e4d86f99 Mon Sep 17 00:00:00 2001 From: Alexander Yevsyukov Date: Thu, 29 Dec 2016 13:54:53 +0200 Subject: [PATCH 5/5] Add static import for `checkArgument` --- .../src/main/java/org/spine3/change/Preconditions.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/client/src/main/java/org/spine3/change/Preconditions.java b/client/src/main/java/org/spine3/change/Preconditions.java index a32345eae9c..03843e0af29 100644 --- a/client/src/main/java/org/spine3/change/Preconditions.java +++ b/client/src/main/java/org/spine3/change/Preconditions.java @@ -44,7 +44,7 @@ private Preconditions() { * @throws IllegalArgumentException in case if values are equal */ public static void checkNotEqual(int previousValue, int newValue) { - com.google.common.base.Preconditions.checkArgument(Integer.compare(newValue, previousValue) != 0, VALUES_CANNOT_BE_EQUAL); + checkArgument(Integer.compare(newValue, previousValue) != 0, VALUES_CANNOT_BE_EQUAL); } /** @@ -53,7 +53,7 @@ public static void checkNotEqual(int previousValue, int newValue) { * @throws IllegalArgumentException in case if values are equal */ public static void checkNotEqual(long previousValue, long newValue) { - com.google.common.base.Preconditions.checkArgument(Long.compare(newValue, previousValue) != 0, VALUES_CANNOT_BE_EQUAL); + checkArgument(Long.compare(newValue, previousValue) != 0, VALUES_CANNOT_BE_EQUAL); } /** @@ -80,7 +80,7 @@ public static void checkNotEqual(double previousValue, double newValue) { * @throws IllegalArgumentException in case if values are equal */ public static void checkNotEqual(T previousValue, T newValue) { - com.google.common.base.Preconditions.checkArgument(!newValue.equals(previousValue), VALUES_CANNOT_BE_EQUAL); + checkArgument(!newValue.equals(previousValue), VALUES_CANNOT_BE_EQUAL); } /** @@ -89,7 +89,7 @@ public static void checkNotEqual(T previousValue, T newValue) { * @throws IllegalArgumentException in case if parameter is empty */ public static void checkNewValueNotEmpty(ByteString newValue) { - com.google.common.base.Preconditions.checkArgument(!newValue.isEmpty(), NEW_VALUE_CANNOT_BE_EMPTY); + checkArgument(!newValue.isEmpty(), NEW_VALUE_CANNOT_BE_EMPTY); } /** @@ -98,6 +98,6 @@ public static void checkNewValueNotEmpty(ByteString newValue) { * @throws IllegalArgumentException in case if parameter is empty */ public static void checkNewValueNotEmpty(String newValue) { - com.google.common.base.Preconditions.checkArgument(!newValue.isEmpty(), NEW_VALUE_CANNOT_BE_EMPTY); + checkArgument(!newValue.isEmpty(), NEW_VALUE_CANNOT_BE_EMPTY); } }