Skip to content
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
45 changes: 20 additions & 25 deletions client/src/main/java/org/spine3/change/Changes.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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() {
}

Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -85,7 +80,7 @@ public static TimestampChange of(Timestamp previousValue, Timestamp newValue) {
* <p>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)
Expand All @@ -100,7 +95,7 @@ public static DoubleChange of(double previousValue, double newValue) {
* <p>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)
Expand All @@ -115,7 +110,7 @@ public static FloatChange of(float previousValue, float newValue) {
* <p>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)
Expand All @@ -130,7 +125,7 @@ public static Int32Change ofInt32(int previousValue, int newValue) {
* <p>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)
Expand All @@ -145,7 +140,7 @@ public static Int64Change ofInt64(long previousValue, long newValue) {
* <p>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)
Expand All @@ -160,7 +155,7 @@ public static UInt32Change ofUInt32(int previousValue, int newValue) {
* <p>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)
Expand All @@ -175,7 +170,7 @@ public static UInt64Change ofUInt64(long previousValue, long newValue) {
* <p>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)
Expand All @@ -190,7 +185,7 @@ public static SInt32Change ofSInt32(int previousValue, int newValue) {
* <p>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)
Expand All @@ -205,7 +200,7 @@ public static SInt64Change ofSInt64(long previousValue, long newValue) {
* <p>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)
Expand All @@ -220,7 +215,7 @@ public static Fixed32Change ofFixed32(int previousValue, int newValue) {
* <p>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)
Expand All @@ -235,7 +230,7 @@ public static Fixed64Change ofFixed64(long previousValue, long newValue) {
* <p>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)
Expand All @@ -250,7 +245,7 @@ public static Sfixed32Change ofSfixed32(int previousValue, int newValue) {
* <p>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)
Expand All @@ -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)
Expand All @@ -283,7 +278,7 @@ public static BytesChange of(ByteString previousValue, ByteString newValue) {
* <p>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)
Expand Down
103 changes: 103 additions & 0 deletions client/src/main/java/org/spine3/change/Preconditions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* 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() {
}

/**
* Ensures that parameters are not equal.
*
* @throws IllegalArgumentException in case if values are equal
*/
public static void checkNotEqual(int previousValue, int newValue) {
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) {
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 <T> void checkNotEqual(T previousValue, T newValue) {
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) {
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) {
checkArgument(!newValue.isEmpty(), NEW_VALUE_CANNOT_BE_EMPTY);
}
}
63 changes: 63 additions & 0 deletions client/src/main/java/org/spine3/validate/ConstraintViolations.java
Original file line number Diff line number Diff line change
@@ -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<String> 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<String> params = violation.getParamList();
final String result = String.format(format, params.toArray());
return result;
}
}
Loading