From ff6b0ada72106d67aef1ee21d51bacac2f3af7ac Mon Sep 17 00:00:00 2001 From: pascalschumacher Date: Wed, 19 Apr 2017 21:07:05 +0200 Subject: [PATCH] use Validate#isTrue to validate arguments --- .../org/apache/commons/lang3/CharRange.java | 4 +- .../org/apache/commons/lang3/CharUtils.java | 12 +-- .../commons/lang3/SerializationUtils.java | 12 +-- .../org/apache/commons/lang3/ThreadUtils.java | 36 ++----- .../commons/lang3/builder/DiffBuilder.java | 93 ++++++------------- .../commons/lang3/builder/DiffResult.java | 17 +--- .../lang3/builder/HashCodeBuilder.java | 5 +- .../builder/ReflectionToStringBuilder.java | 5 +- .../lang3/builder/ToStringBuilder.java | 5 +- .../CallableBackgroundInitializer.java | 6 +- .../MultiBackgroundInitializer.java | 12 +-- .../lang3/exception/ExceptionUtils.java | 9 +- .../apache/commons/lang3/math/Fraction.java | 18 ++-- .../commons/lang3/math/IEEE754rUtils.java | 21 +---- .../commons/lang3/math/NumberUtils.java | 4 +- .../apache/commons/lang3/time/DateUtils.java | 33 +++---- 16 files changed, 86 insertions(+), 206 deletions(-) diff --git a/src/main/java/org/apache/commons/lang3/CharRange.java b/src/main/java/org/apache/commons/lang3/CharRange.java index 2ccda7c4e..bf82805bc 100644 --- a/src/main/java/org/apache/commons/lang3/CharRange.java +++ b/src/main/java/org/apache/commons/lang3/CharRange.java @@ -179,9 +179,7 @@ final class CharRange implements Iterable, Serializable { * @throws IllegalArgumentException if {@code null} input */ public boolean contains(final CharRange range) { - if (range == null) { - throw new IllegalArgumentException("The Range must not be null"); - } + Validate.isTrue(range != null, "The Range must not be null"); if (negated) { if (range.negated) { return start >= range.start && end <= range.end; diff --git a/src/main/java/org/apache/commons/lang3/CharUtils.java b/src/main/java/org/apache/commons/lang3/CharUtils.java index 92a8d44f3..bb45e5f45 100644 --- a/src/main/java/org/apache/commons/lang3/CharUtils.java +++ b/src/main/java/org/apache/commons/lang3/CharUtils.java @@ -128,9 +128,7 @@ public class CharUtils { * @throws IllegalArgumentException if the Character is null */ public static char toChar(final Character ch) { - if (ch == null) { - throw new IllegalArgumentException("The Character must not be null"); - } + Validate.isTrue(ch != null, "The Character must not be null"); return ch.charValue(); } @@ -171,9 +169,7 @@ public class CharUtils { * @throws IllegalArgumentException if the String is empty */ public static char toChar(final String str) { - if (StringUtils.isEmpty(str)) { - throw new IllegalArgumentException("The String must not be empty"); - } + Validate.isTrue(StringUtils.isNotEmpty(str), "The String must not be empty"); return str.charAt(0); } @@ -261,9 +257,7 @@ public class CharUtils { * @throws IllegalArgumentException if the Character is not ASCII numeric or is null */ public static int toIntValue(final Character ch) { - if (ch == null) { - throw new IllegalArgumentException("The character must not be null"); - } + Validate.isTrue(ch != null, "The character must not be null"); return toIntValue(ch.charValue()); } diff --git a/src/main/java/org/apache/commons/lang3/SerializationUtils.java b/src/main/java/org/apache/commons/lang3/SerializationUtils.java index f1a499306..670ef1838 100644 --- a/src/main/java/org/apache/commons/lang3/SerializationUtils.java +++ b/src/main/java/org/apache/commons/lang3/SerializationUtils.java @@ -133,9 +133,7 @@ public class SerializationUtils { * @throws SerializationException (runtime) if the serialization fails */ public static void serialize(final Serializable obj, final OutputStream outputStream) { - if (outputStream == null) { - throw new IllegalArgumentException("The OutputStream must not be null"); - } + Validate.isTrue(outputStream != null, "The OutputStream must not be null"); try (ObjectOutputStream out = new ObjectOutputStream(outputStream)){ out.writeObject(obj); } catch (final IOException ex) { @@ -190,9 +188,7 @@ public class SerializationUtils { * (runtime) if the serialization fails */ public static T deserialize(final InputStream inputStream) { - if (inputStream == null) { - throw new IllegalArgumentException("The InputStream must not be null"); - } + Validate.isTrue(inputStream != null, "The InputStream must not be null"); try (ObjectInputStream in = new ObjectInputStream(inputStream)) { @SuppressWarnings("unchecked") final T obj = (T) in.readObject(); @@ -223,9 +219,7 @@ public class SerializationUtils { * (runtime) if the serialization fails */ public static T deserialize(final byte[] objectData) { - if (objectData == null) { - throw new IllegalArgumentException("The byte[] must not be null"); - } + Validate.isTrue(objectData != null, "The byte[] must not be null"); return SerializationUtils.deserialize(new ByteArrayInputStream(objectData)); } diff --git a/src/main/java/org/apache/commons/lang3/ThreadUtils.java b/src/main/java/org/apache/commons/lang3/ThreadUtils.java index 93a48b7f1..9282ed3f7 100644 --- a/src/main/java/org/apache/commons/lang3/ThreadUtils.java +++ b/src/main/java/org/apache/commons/lang3/ThreadUtils.java @@ -50,9 +50,7 @@ public class ThreadUtils { * thread groups from this thread's thread group up to the system thread group */ public static Thread findThreadById(final long threadId, final ThreadGroup threadGroup) { - if (threadGroup == null) { - throw new IllegalArgumentException("The thread group must not be null"); - } + Validate.isTrue(threadGroup != null, "The thread group must not be null"); final Thread thread = findThreadById(threadId); if(thread != null && threadGroup.equals(thread.getThreadGroup())) { return thread; @@ -75,9 +73,7 @@ public class ThreadUtils { * thread groups from this thread's thread group up to the system thread group */ public static Thread findThreadById(final long threadId, final String threadGroupName) { - if (threadGroupName == null) { - throw new IllegalArgumentException("The thread group name must not be null"); - } + Validate.isTrue(threadGroupName != null, "The thread group name must not be null"); final Thread thread = findThreadById(threadId); if(thread != null && thread.getThreadGroup() != null && thread.getThreadGroup().getName().equals(threadGroupName)) { return thread; @@ -118,12 +114,8 @@ public class ThreadUtils { * thread groups from this thread's thread group up to the system thread group */ public static Collection findThreadsByName(final String threadName, final String threadGroupName) { - if (threadName == null) { - throw new IllegalArgumentException("The thread name must not be null"); - } - if (threadGroupName == null) { - throw new IllegalArgumentException("The thread group name must not be null"); - } + Validate.isTrue(threadName != null, "The thread name must not be null"); + Validate.isTrue(threadGroupName != null, "The thread group name must not be null"); final Collection threadGroups = findThreadGroups(new NamePredicate(threadGroupName)); @@ -311,9 +303,7 @@ public class ThreadUtils { */ public NamePredicate(final String name) { super(); - if (name == null) { - throw new IllegalArgumentException("The name must not be null"); - } + Validate.isTrue(name != null, "The name must not be null"); this.name = name; } @@ -398,12 +388,8 @@ public class ThreadUtils { * thread groups from this thread's thread group up to the system thread group */ public static Collection findThreads(final ThreadGroup group, final boolean recurse, final ThreadPredicate predicate) { - if (group == null) { - throw new IllegalArgumentException("The group must not be null"); - } - if (predicate == null) { - throw new IllegalArgumentException("The predicate must not be null"); - } + Validate.isTrue(group != null, "The group must not be null"); + Validate.isTrue(predicate != null, "The predicate must not be null"); int count = group.activeCount(); Thread[] threads; @@ -434,12 +420,8 @@ public class ThreadUtils { * thread groups from this thread's thread group up to the system thread group */ public static Collection findThreadGroups(final ThreadGroup group, final boolean recurse, final ThreadGroupPredicate predicate){ - if (group == null) { - throw new IllegalArgumentException("The group must not be null"); - } - if (predicate == null) { - throw new IllegalArgumentException("The predicate must not be null"); - } + Validate.isTrue(group != null, "The group must not be null"); + Validate.isTrue(predicate != null, "The predicate must not be null"); int count = group.activeGroupCount(); ThreadGroup[] threadGroups; diff --git a/src/main/java/org/apache/commons/lang3/builder/DiffBuilder.java b/src/main/java/org/apache/commons/lang3/builder/DiffBuilder.java index bbbcad97c..ed2cfe93d 100644 --- a/src/main/java/org/apache/commons/lang3/builder/DiffBuilder.java +++ b/src/main/java/org/apache/commons/lang3/builder/DiffBuilder.java @@ -21,6 +21,7 @@ import java.util.Arrays; import java.util.List; import org.apache.commons.lang3.ArrayUtils; +import org.apache.commons.lang3.Validate; /** *

@@ -102,12 +103,8 @@ public class DiffBuilder implements Builder { public DiffBuilder(final Object lhs, final Object rhs, final ToStringStyle style, final boolean testTriviallyEqual) { - if (lhs == null) { - throw new IllegalArgumentException("lhs cannot be null"); - } - if (rhs == null) { - throw new IllegalArgumentException("rhs cannot be null"); - } + Validate.isTrue(lhs != null, "lhs cannot be null"); + Validate.isTrue(rhs != null, "rhs cannot be null"); this.diffs = new ArrayList<>(); this.left = lhs; @@ -167,9 +164,7 @@ public class DiffBuilder implements Builder { */ public DiffBuilder append(final String fieldName, final boolean lhs, final boolean rhs) { - if (fieldName == null) { - throw new IllegalArgumentException("Field name cannot be null"); - } + validateFieldNameNotNull(fieldName); if (objectsTriviallyEqual) { return this; @@ -209,9 +204,7 @@ public class DiffBuilder implements Builder { */ public DiffBuilder append(final String fieldName, final boolean[] lhs, final boolean[] rhs) { - if (fieldName == null) { - throw new IllegalArgumentException("Field name cannot be null"); - } + validateFieldNameNotNull(fieldName); if (objectsTriviallyEqual) { return this; } @@ -250,9 +243,7 @@ public class DiffBuilder implements Builder { */ public DiffBuilder append(final String fieldName, final byte lhs, final byte rhs) { - if (fieldName == null) { - throw new IllegalArgumentException("Field name cannot be null"); - } + validateFieldNameNotNull(fieldName); if (objectsTriviallyEqual) { return this; } @@ -291,9 +282,7 @@ public class DiffBuilder implements Builder { */ public DiffBuilder append(final String fieldName, final byte[] lhs, final byte[] rhs) { - if (fieldName == null) { - throw new IllegalArgumentException("Field name cannot be null"); - } + validateFieldNameNotNull(fieldName); if (objectsTriviallyEqual) { return this; @@ -333,9 +322,7 @@ public class DiffBuilder implements Builder { */ public DiffBuilder append(final String fieldName, final char lhs, final char rhs) { - if (fieldName == null) { - throw new IllegalArgumentException("Field name cannot be null"); - } + validateFieldNameNotNull(fieldName); if (objectsTriviallyEqual) { return this; @@ -375,9 +362,7 @@ public class DiffBuilder implements Builder { */ public DiffBuilder append(final String fieldName, final char[] lhs, final char[] rhs) { - if (fieldName == null) { - throw new IllegalArgumentException("Field name cannot be null"); - } + validateFieldNameNotNull(fieldName); if (objectsTriviallyEqual) { return this; @@ -417,9 +402,7 @@ public class DiffBuilder implements Builder { */ public DiffBuilder append(final String fieldName, final double lhs, final double rhs) { - if (fieldName == null) { - throw new IllegalArgumentException("Field name cannot be null"); - } + validateFieldNameNotNull(fieldName); if (objectsTriviallyEqual) { return this; @@ -459,9 +442,7 @@ public class DiffBuilder implements Builder { */ public DiffBuilder append(final String fieldName, final double[] lhs, final double[] rhs) { - if (fieldName == null) { - throw new IllegalArgumentException("Field name cannot be null"); - } + validateFieldNameNotNull(fieldName); if (objectsTriviallyEqual) { return this; @@ -501,9 +482,7 @@ public class DiffBuilder implements Builder { */ public DiffBuilder append(final String fieldName, final float lhs, final float rhs) { - if (fieldName == null) { - throw new IllegalArgumentException("Field name cannot be null"); - } + validateFieldNameNotNull(fieldName); if (objectsTriviallyEqual) { return this; @@ -543,9 +522,7 @@ public class DiffBuilder implements Builder { */ public DiffBuilder append(final String fieldName, final float[] lhs, final float[] rhs) { - if (fieldName == null) { - throw new IllegalArgumentException("Field name cannot be null"); - } + validateFieldNameNotNull(fieldName); if (objectsTriviallyEqual) { return this; @@ -585,9 +562,7 @@ public class DiffBuilder implements Builder { */ public DiffBuilder append(final String fieldName, final int lhs, final int rhs) { - if (fieldName == null) { - throw new IllegalArgumentException("Field name cannot be null"); - } + validateFieldNameNotNull(fieldName); if (objectsTriviallyEqual) { return this; @@ -627,9 +602,7 @@ public class DiffBuilder implements Builder { */ public DiffBuilder append(final String fieldName, final int[] lhs, final int[] rhs) { - if (fieldName == null) { - throw new IllegalArgumentException("Field name cannot be null"); - } + validateFieldNameNotNull(fieldName); if (objectsTriviallyEqual) { return this; @@ -669,9 +642,7 @@ public class DiffBuilder implements Builder { */ public DiffBuilder append(final String fieldName, final long lhs, final long rhs) { - if (fieldName == null) { - throw new IllegalArgumentException("Field name cannot be null"); - } + validateFieldNameNotNull(fieldName); if (objectsTriviallyEqual) { return this; @@ -711,9 +682,7 @@ public class DiffBuilder implements Builder { */ public DiffBuilder append(final String fieldName, final long[] lhs, final long[] rhs) { - if (fieldName == null) { - throw new IllegalArgumentException("Field name cannot be null"); - } + validateFieldNameNotNull(fieldName); if (objectsTriviallyEqual) { return this; @@ -753,9 +722,7 @@ public class DiffBuilder implements Builder { */ public DiffBuilder append(final String fieldName, final short lhs, final short rhs) { - if (fieldName == null) { - throw new IllegalArgumentException("Field name cannot be null"); - } + validateFieldNameNotNull(fieldName); if (objectsTriviallyEqual) { return this; @@ -795,9 +762,7 @@ public class DiffBuilder implements Builder { */ public DiffBuilder append(final String fieldName, final short[] lhs, final short[] rhs) { - if (fieldName == null) { - throw new IllegalArgumentException("Field name cannot be null"); - } + validateFieldNameNotNull(fieldName); if (objectsTriviallyEqual) { return this; @@ -837,9 +802,7 @@ public class DiffBuilder implements Builder { */ public DiffBuilder append(final String fieldName, final Object lhs, final Object rhs) { - if (fieldName == null) { - throw new IllegalArgumentException("Field name cannot be null"); - } + validateFieldNameNotNull(fieldName); if (objectsTriviallyEqual) { return this; } @@ -923,9 +886,7 @@ public class DiffBuilder implements Builder { */ public DiffBuilder append(final String fieldName, final Object[] lhs, final Object[] rhs) { - if (fieldName == null) { - throw new IllegalArgumentException("Field name cannot be null"); - } + validateFieldNameNotNull(fieldName); if (objectsTriviallyEqual) { return this; } @@ -987,12 +948,8 @@ public class DiffBuilder implements Builder { */ public DiffBuilder append(final String fieldName, final DiffResult diffResult) { - if (fieldName == null) { - throw new IllegalArgumentException("Field name cannot be null"); - } - if (diffResult == null) { - throw new IllegalArgumentException("Diff result cannot be null"); - } + validateFieldNameNotNull(fieldName); + Validate.isTrue(diffResult != null, "Diff result cannot be null"); if (objectsTriviallyEqual) { return this; } @@ -1019,4 +976,8 @@ public class DiffBuilder implements Builder { return new DiffResult(left, right, diffs, style); } + private void validateFieldNameNotNull(final String fieldName) { + Validate.isTrue(fieldName != null, "Field name cannot be null"); + } + } diff --git a/src/main/java/org/apache/commons/lang3/builder/DiffResult.java b/src/main/java/org/apache/commons/lang3/builder/DiffResult.java index 2dbd85eb6..4bcba6ba3 100644 --- a/src/main/java/org/apache/commons/lang3/builder/DiffResult.java +++ b/src/main/java/org/apache/commons/lang3/builder/DiffResult.java @@ -20,6 +20,8 @@ import java.util.Collections; import java.util.Iterator; import java.util.List; +import org.apache.commons.lang3.Validate; + /** *

* A {@code DiffResult} contains a collection of the differences between two @@ -71,18 +73,9 @@ public class DiffResult implements Iterable> { */ DiffResult(final Object lhs, final Object rhs, final List> diffs, final ToStringStyle style) { - if (lhs == null) { - throw new IllegalArgumentException( - "Left hand object cannot be null"); - } - if (rhs == null) { - throw new IllegalArgumentException( - "Right hand object cannot be null"); - } - if (diffs == null) { - throw new IllegalArgumentException( - "List of differences cannot be null"); - } + Validate.isTrue(lhs != null, "Left hand object cannot be null"); + Validate.isTrue(rhs != null, "Right hand object cannot be null"); + Validate.isTrue(diffs != null, "List of differences cannot be null"); this.diffs = diffs; this.lhs = lhs; diff --git a/src/main/java/org/apache/commons/lang3/builder/HashCodeBuilder.java b/src/main/java/org/apache/commons/lang3/builder/HashCodeBuilder.java index 8511cd7f3..d68e36b42 100644 --- a/src/main/java/org/apache/commons/lang3/builder/HashCodeBuilder.java +++ b/src/main/java/org/apache/commons/lang3/builder/HashCodeBuilder.java @@ -354,10 +354,7 @@ public class HashCodeBuilder implements Builder { */ public static int reflectionHashCode(final int initialNonZeroOddNumber, final int multiplierNonZeroOddNumber, final T object, final boolean testTransients, final Class reflectUpToClass, final String... excludeFields) { - - if (object == null) { - throw new IllegalArgumentException("The object to build a hash code for must not be null"); - } + Validate.isTrue(object != null, "The object to build a hash code for must not be null"); final HashCodeBuilder builder = new HashCodeBuilder(initialNonZeroOddNumber, multiplierNonZeroOddNumber); Class clazz = object.getClass(); reflectionAppend(object, clazz, builder, testTransients, excludeFields); diff --git a/src/main/java/org/apache/commons/lang3/builder/ReflectionToStringBuilder.java b/src/main/java/org/apache/commons/lang3/builder/ReflectionToStringBuilder.java index d40a838a3..19faa9bed 100644 --- a/src/main/java/org/apache/commons/lang3/builder/ReflectionToStringBuilder.java +++ b/src/main/java/org/apache/commons/lang3/builder/ReflectionToStringBuilder.java @@ -27,6 +27,7 @@ import java.util.List; import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.ClassUtils; +import org.apache.commons.lang3.Validate; /** *

@@ -364,9 +365,7 @@ public class ReflectionToStringBuilder extends ToStringBuilder { } private static Object checkNotNull(final Object obj) { - if (obj == null) { - throw new IllegalArgumentException("The Object passed in should not be null."); - } + Validate.isTrue(obj != null, "The Object passed in should not be null."); return obj; } diff --git a/src/main/java/org/apache/commons/lang3/builder/ToStringBuilder.java b/src/main/java/org/apache/commons/lang3/builder/ToStringBuilder.java index 43637516e..20dd748eb 100644 --- a/src/main/java/org/apache/commons/lang3/builder/ToStringBuilder.java +++ b/src/main/java/org/apache/commons/lang3/builder/ToStringBuilder.java @@ -17,6 +17,7 @@ package org.apache.commons.lang3.builder; import org.apache.commons.lang3.ObjectUtils; +import org.apache.commons.lang3.Validate; /** *

Assists in implementing {@link Object#toString()} methods.

@@ -132,9 +133,7 @@ public class ToStringBuilder implements Builder { * @throws IllegalArgumentException if the style is null */ public static void setDefaultStyle(final ToStringStyle style) { - if (style == null) { - throw new IllegalArgumentException("The style must not be null"); - } + Validate.isTrue(style != null, "The style must not be null"); defaultStyle = style; } diff --git a/src/main/java/org/apache/commons/lang3/concurrent/CallableBackgroundInitializer.java b/src/main/java/org/apache/commons/lang3/concurrent/CallableBackgroundInitializer.java index 19414003a..c1d07353e 100644 --- a/src/main/java/org/apache/commons/lang3/concurrent/CallableBackgroundInitializer.java +++ b/src/main/java/org/apache/commons/lang3/concurrent/CallableBackgroundInitializer.java @@ -19,6 +19,8 @@ package org.apache.commons.lang3.concurrent; import java.util.concurrent.Callable; import java.util.concurrent.ExecutorService; +import org.apache.commons.lang3.Validate; + /** *

* A specialized {@link BackgroundInitializer} implementation that wraps a @@ -118,8 +120,6 @@ public class CallableBackgroundInitializer extends BackgroundInitializer { * @throws IllegalArgumentException if the {@code Callable} is null */ private void checkCallable(final Callable call) { - if (call == null) { - throw new IllegalArgumentException("Callable must not be null!"); - } + Validate.isTrue(call != null, "Callable must not be null!"); } } diff --git a/src/main/java/org/apache/commons/lang3/concurrent/MultiBackgroundInitializer.java b/src/main/java/org/apache/commons/lang3/concurrent/MultiBackgroundInitializer.java index c67253899..6a6a9a092 100644 --- a/src/main/java/org/apache/commons/lang3/concurrent/MultiBackgroundInitializer.java +++ b/src/main/java/org/apache/commons/lang3/concurrent/MultiBackgroundInitializer.java @@ -23,6 +23,8 @@ import java.util.NoSuchElementException; import java.util.Set; import java.util.concurrent.ExecutorService; +import org.apache.commons.lang3.Validate; + /** *

* A specialized {@link BackgroundInitializer} implementation that can deal with @@ -131,14 +133,8 @@ public class MultiBackgroundInitializer * @throws IllegalStateException if {@code start()} has already been called */ public void addInitializer(final String name, final BackgroundInitializer init) { - if (name == null) { - throw new IllegalArgumentException( - "Name of child initializer must not be null!"); - } - if (init == null) { - throw new IllegalArgumentException( - "Child initializer must not be null!"); - } + Validate.isTrue(name != null, "Name of child initializer must not be null!"); + Validate.isTrue(init != null, "Child initializer must not be null!"); synchronized (this) { if (isStarted()) { diff --git a/src/main/java/org/apache/commons/lang3/exception/ExceptionUtils.java b/src/main/java/org/apache/commons/lang3/exception/ExceptionUtils.java index 39e270be3..63510f00f 100644 --- a/src/main/java/org/apache/commons/lang3/exception/ExceptionUtils.java +++ b/src/main/java/org/apache/commons/lang3/exception/ExceptionUtils.java @@ -29,6 +29,7 @@ import java.util.StringTokenizer; import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.ClassUtils; import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.Validate; /** *

Provides utilities for manipulating and examining @@ -454,9 +455,7 @@ public class ExceptionUtils { if (throwable == null) { return; } - if (stream == null) { - throw new IllegalArgumentException("The PrintStream must not be null"); - } + Validate.isTrue(stream != null, "The PrintStream must not be null"); final String trace[] = getRootCauseStackTrace(throwable); for (final String element : trace) { stream.println(element); @@ -487,9 +486,7 @@ public class ExceptionUtils { if (throwable == null) { return; } - if (writer == null) { - throw new IllegalArgumentException("The PrintWriter must not be null"); - } + Validate.isTrue(writer != null, "The PrintWriter must not be null"); final String trace[] = getRootCauseStackTrace(throwable); for (final String element : trace) { writer.println(element); diff --git a/src/main/java/org/apache/commons/lang3/math/Fraction.java b/src/main/java/org/apache/commons/lang3/math/Fraction.java index 4d5c53f81..d4b8ae079 100644 --- a/src/main/java/org/apache/commons/lang3/math/Fraction.java +++ b/src/main/java/org/apache/commons/lang3/math/Fraction.java @@ -18,6 +18,8 @@ package org.apache.commons.lang3.math; import java.math.BigInteger; +import org.apache.commons.lang3.Validate; + /** *

Fraction is a Number implementation that * stores fractions accurately.

@@ -313,9 +315,7 @@ public final class Fraction extends Number implements Comparable { * @throws NumberFormatException if the number format is invalid */ public static Fraction getFraction(String str) { - if (str == null) { - throw new IllegalArgumentException("The string must not be null"); - } + Validate.isTrue(str != null, "The string must not be null"); // parse double format int pos = str.indexOf('.'); if (pos >= 0) { @@ -733,9 +733,7 @@ public final class Fraction extends Number implements Comparable { * cannot be represented in an int. */ private Fraction addSub(final Fraction fraction, final boolean isAdd) { - if (fraction == null) { - throw new IllegalArgumentException("The fraction must not be null"); - } + Validate.isTrue(fraction != null, "The fraction must not be null"); // zero is identity for addition. if (numerator == 0) { return isAdd ? fraction : fraction.negate(); @@ -783,9 +781,7 @@ public final class Fraction extends Number implements Comparable { * Integer.MAX_VALUE */ public Fraction multiplyBy(final Fraction fraction) { - if (fraction == null) { - throw new IllegalArgumentException("The fraction must not be null"); - } + Validate.isTrue(fraction != null, "The fraction must not be null"); if (numerator == 0 || fraction.numerator == 0) { return ZERO; } @@ -808,9 +804,7 @@ public final class Fraction extends Number implements Comparable { * Integer.MAX_VALUE */ public Fraction divideBy(final Fraction fraction) { - if (fraction == null) { - throw new IllegalArgumentException("The fraction must not be null"); - } + Validate.isTrue(fraction != null, "The fraction must not be null"); if (fraction.numerator == 0) { throw new ArithmeticException("The fraction to divide by must not be zero"); } diff --git a/src/main/java/org/apache/commons/lang3/math/IEEE754rUtils.java b/src/main/java/org/apache/commons/lang3/math/IEEE754rUtils.java index 6d34e62d0..6142832c5 100644 --- a/src/main/java/org/apache/commons/lang3/math/IEEE754rUtils.java +++ b/src/main/java/org/apache/commons/lang3/math/IEEE754rUtils.java @@ -37,12 +37,8 @@ public class IEEE754rUtils { * @since 3.4 Changed signature from min(double[]) to min(double...) */ public static double min(final double... array) { - // Validates input - if (array == null) { - throw new IllegalArgumentException("The Array must not be null"); - } + Validate.isTrue(array != null, "The Array must not be null"); Validate.isTrue(array.length != 0, "Array cannot be empty."); - // Finds and returns min double min = array[0]; @@ -63,10 +59,7 @@ public class IEEE754rUtils { * @since 3.4 Changed signature from min(float[]) to min(float...) */ public static float min(final float... array) { - // Validates input - if (array == null) { - throw new IllegalArgumentException("The Array must not be null"); - } + Validate.isTrue(array != null, "The Array must not be null"); Validate.isTrue(array.length != 0, "Array cannot be empty."); // Finds and returns min @@ -156,10 +149,7 @@ public class IEEE754rUtils { * @since 3.4 Changed signature from max(double[]) to max(double...) */ public static double max(final double... array) { - // Validates input - if (array== null) { - throw new IllegalArgumentException("The Array must not be null"); - } + Validate.isTrue(array != null, "The Array must not be null"); Validate.isTrue(array.length != 0, "Array cannot be empty."); // Finds and returns max @@ -181,10 +171,7 @@ public class IEEE754rUtils { * @since 3.4 Changed signature from max(float[]) to max(float...) */ public static float max(final float... array) { - // Validates input - if (array == null) { - throw new IllegalArgumentException("The Array must not be null"); - } + Validate.isTrue(array != null, "The Array must not be null"); Validate.isTrue(array.length != 0, "Array cannot be empty."); // Finds and returns max diff --git a/src/main/java/org/apache/commons/lang3/math/NumberUtils.java b/src/main/java/org/apache/commons/lang3/math/NumberUtils.java index 3dcafac25..f77bf1057 100644 --- a/src/main/java/org/apache/commons/lang3/math/NumberUtils.java +++ b/src/main/java/org/apache/commons/lang3/math/NumberUtils.java @@ -1113,9 +1113,7 @@ public class NumberUtils { * @throws IllegalArgumentException if {@code array} is either {@code null} or empty */ private static void validateArray(final Object array) { - if (array == null) { - throw new IllegalArgumentException("The Array must not be null"); - } + Validate.isTrue(array != null, "The Array must not be null"); Validate.isTrue(Array.getLength(array) != 0, "Array cannot be empty."); } diff --git a/src/main/java/org/apache/commons/lang3/time/DateUtils.java b/src/main/java/org/apache/commons/lang3/time/DateUtils.java index 859011a92..7f3bc9747 100644 --- a/src/main/java/org/apache/commons/lang3/time/DateUtils.java +++ b/src/main/java/org/apache/commons/lang3/time/DateUtils.java @@ -26,6 +26,8 @@ import java.util.NoSuchElementException; import java.util.TimeZone; import java.util.concurrent.TimeUnit; +import org.apache.commons.lang3.Validate; + /** *

A suite of utilities surrounding the use of the * {@link java.util.Calendar} and {@link java.util.Date} object.

@@ -511,9 +513,7 @@ public class DateUtils { * @throws IllegalArgumentException if the date is null */ private static Date add(final Date date, final int calendarField, final int amount) { - if (date == null) { - throw new IllegalArgumentException("The date must not be null"); - } + validateDateNotNull(date); final Calendar c = Calendar.getInstance(); c.setTime(date); c.add(calendarField, amount); @@ -640,9 +640,7 @@ public class DateUtils { * @since 2.4 */ private static Date set(final Date date, final int calendarField, final int amount) { - if (date == null) { - throw new IllegalArgumentException("The date must not be null"); - } + validateDateNotNull(date); // getInstance() returns a new object, so this method is thread safe. final Calendar c = Calendar.getInstance(); c.setLenient(false); @@ -708,9 +706,7 @@ public class DateUtils { * @throws ArithmeticException if the year is over 280 million */ public static Date round(final Date date, final int field) { - if (date == null) { - throw new IllegalArgumentException("The date must not be null"); - } + validateDateNotNull(date); final Calendar gval = Calendar.getInstance(); gval.setTime(date); modify(gval, field, ModifyType.ROUND); @@ -811,9 +807,7 @@ public class DateUtils { * @throws ArithmeticException if the year is over 280 million */ public static Date truncate(final Date date, final int field) { - if (date == null) { - throw new IllegalArgumentException("The date must not be null"); - } + validateDateNotNull(date); final Calendar gval = Calendar.getInstance(); gval.setTime(date); modify(gval, field, ModifyType.TRUNCATE); @@ -891,9 +885,7 @@ public class DateUtils { * @since 2.5 */ public static Date ceiling(final Date date, final int field) { - if (date == null) { - throw new IllegalArgumentException("The date must not be null"); - } + validateDateNotNull(date); final Calendar gval = Calendar.getInstance(); gval.setTime(date); modify(gval, field, ModifyType.CEILING); @@ -1130,9 +1122,7 @@ public class DateUtils { * @throws IllegalArgumentException if the rangeStyle is invalid */ public static Iterator iterator(final Date focus, final int rangeStyle) { - if (focus == null) { - throw new IllegalArgumentException("The date must not be null"); - } + validateDateNotNull(focus); final Calendar gval = Calendar.getInstance(); gval.setTime(focus); return iterator(gval, rangeStyle); @@ -1655,9 +1645,7 @@ public class DateUtils { * @since 2.4 */ private static long getFragment(final Date date, final int fragment, final TimeUnit unit) { - if(date == null) { - throw new IllegalArgumentException("The date must not be null"); - } + validateDateNotNull(date); final Calendar calendar = Calendar.getInstance(); calendar.setTime(date); return getFragment(calendar, fragment, unit); @@ -1794,6 +1782,9 @@ public class DateUtils { return truncatedDate1.compareTo(truncatedDate2); } + private static void validateDateNotNull(final Date date) { + Validate.isTrue(date != null, "The date must not be null"); + } //----------------------------------------------------------------------- /**