Repository: commons-text Updated Branches: refs/heads/master f651b78a6 -> 732450209
use Validate to check arguments Project: http://git-wip-us.apache.org/repos/asf/commons-text/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-text/commit/73245020 Tree: http://git-wip-us.apache.org/repos/asf/commons-text/tree/73245020 Diff: http://git-wip-us.apache.org/repos/asf/commons-text/diff/73245020 Branch: refs/heads/master Commit: 7324502096a1e7f02c510285aba087557dec0059 Parents: f651b78 Author: Pascal Schumacher <pascalschumac...@gmx.net> Authored: Fri May 5 14:10:06 2017 +0200 Committer: Pascal Schumacher <pascalschumac...@gmx.net> Committed: Fri May 5 14:43:01 2017 +0200 ---------------------------------------------------------------------- .../commons/text/RandomStringGenerator.java | 24 +++++++------------- .../org/apache/commons/text/StrSubstitutor.java | 18 +++++---------- .../java/org/apache/commons/text/WordUtils.java | 16 +++---------- .../text/similarity/EditDistanceFrom.java | 6 ++--- .../commons/text/similarity/RegexTokenizer.java | 7 +++--- .../text/similarity/SimilarityScoreFrom.java | 6 ++--- .../text/translate/CharSequenceTranslator.java | 6 ++--- 7 files changed, 30 insertions(+), 53 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-text/blob/73245020/src/main/java/org/apache/commons/text/RandomStringGenerator.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/text/RandomStringGenerator.java b/src/main/java/org/apache/commons/text/RandomStringGenerator.java index ab7fd9d..8596a9b 100644 --- a/src/main/java/org/apache/commons/text/RandomStringGenerator.java +++ b/src/main/java/org/apache/commons/text/RandomStringGenerator.java @@ -20,6 +20,8 @@ import java.util.HashSet; import java.util.Set; import java.util.concurrent.ThreadLocalRandom; +import org.apache.commons.lang3.Validate; + /** * <p> * Generates random Unicode strings containing the specified number of code points. @@ -119,9 +121,7 @@ public final class RandomStringGenerator { if (length == 0) { return ""; } - if (length < 0) { - throw new IllegalArgumentException(String.format("Length %d is smaller than zero.", length)); - } + Validate.isTrue(length > 0, "Length %d is smaller than zero.", length); final StringBuilder builder = new StringBuilder(length); long remaining = length; @@ -223,19 +223,11 @@ public final class RandomStringGenerator { * if {@code minimumCodePoint > maximumCodePoint} */ public Builder withinRange(final int minimumCodePoint, final int maximumCodePoint) { - if (minimumCodePoint > maximumCodePoint) { - throw new IllegalArgumentException(String.format( - "Minimum code point %d is larger than maximum code point %d", - minimumCodePoint, maximumCodePoint)); - } - if (minimumCodePoint < 0) { - throw new IllegalArgumentException( - String.format("Minimum code point %d is negative", minimumCodePoint)); - } - if (maximumCodePoint > Character.MAX_CODE_POINT) { - throw new IllegalArgumentException( - String.format("Value %d is larger than Character.MAX_CODE_POINT.", maximumCodePoint)); - } + Validate.isTrue(minimumCodePoint <= maximumCodePoint, + "Minimum code point %d is larger than maximum code point %d", minimumCodePoint, maximumCodePoint); + Validate.isTrue(minimumCodePoint >= 0, "Minimum code point %d is negative", minimumCodePoint); + Validate.isTrue(maximumCodePoint <= Character.MAX_CODE_POINT, + "Value %d is larger than Character.MAX_CODE_POINT.", maximumCodePoint); this.minimumCodePoint = minimumCodePoint; this.maximumCodePoint = maximumCodePoint; http://git-wip-us.apache.org/repos/asf/commons-text/blob/73245020/src/main/java/org/apache/commons/text/StrSubstitutor.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/text/StrSubstitutor.java b/src/main/java/org/apache/commons/text/StrSubstitutor.java index 3cb4b9b..27b2af4 100644 --- a/src/main/java/org/apache/commons/text/StrSubstitutor.java +++ b/src/main/java/org/apache/commons/text/StrSubstitutor.java @@ -23,6 +23,8 @@ import java.util.List; import java.util.Map; import java.util.Properties; +import org.apache.commons.lang3.Validate; + /** * Substitutes variables within a string by values. * <p> @@ -982,9 +984,7 @@ public class StrSubstitutor { * @throws IllegalArgumentException if the prefix matcher is null */ public StrSubstitutor setVariablePrefixMatcher(final StrMatcher prefixMatcher) { - if (prefixMatcher == null) { - throw new IllegalArgumentException("Variable prefix matcher must not be null!"); - } + Validate.isTrue(prefixMatcher != null, "Variable prefix matcher must not be null!"); this.prefixMatcher = prefixMatcher; return this; } @@ -1014,9 +1014,7 @@ public class StrSubstitutor { * @throws IllegalArgumentException if the prefix is null */ public StrSubstitutor setVariablePrefix(final String prefix) { - if (prefix == null) { - throw new IllegalArgumentException("Variable prefix must not be null!"); - } + Validate.isTrue(prefix != null, "Variable prefix must not be null!"); return setVariablePrefixMatcher(StrMatcher.stringMatcher(prefix)); } @@ -1047,9 +1045,7 @@ public class StrSubstitutor { * @throws IllegalArgumentException if the suffix matcher is null */ public StrSubstitutor setVariableSuffixMatcher(final StrMatcher suffixMatcher) { - if (suffixMatcher == null) { - throw new IllegalArgumentException("Variable suffix matcher must not be null!"); - } + Validate.isTrue(suffixMatcher != null, "Variable suffix matcher must not be null!"); this.suffixMatcher = suffixMatcher; return this; } @@ -1079,9 +1075,7 @@ public class StrSubstitutor { * @throws IllegalArgumentException if the suffix is null */ public StrSubstitutor setVariableSuffix(final String suffix) { - if (suffix == null) { - throw new IllegalArgumentException("Variable suffix must not be null!"); - } + Validate.isTrue(suffix != null, "Variable suffix must not be null!"); return setVariableSuffixMatcher(StrMatcher.stringMatcher(suffix)); } http://git-wip-us.apache.org/repos/asf/commons-text/blob/73245020/src/main/java/org/apache/commons/text/WordUtils.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/text/WordUtils.java b/src/main/java/org/apache/commons/text/WordUtils.java index 20920d6..47e774d 100644 --- a/src/main/java/org/apache/commons/text/WordUtils.java +++ b/src/main/java/org/apache/commons/text/WordUtils.java @@ -21,6 +21,7 @@ import java.util.regex.Pattern; import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.Validate; /** * <p>Operations on Strings that contain words.</p> @@ -698,7 +699,6 @@ public class WordUtils { * WordUtils.containsAllWords("abc def", "def", "abc") = true * </pre> * - * * @param word The CharSequence to check, may be null * @param words The array of String words to search for, may be null * @return {@code true} if all search words are found, {@code false} otherwise @@ -760,7 +760,6 @@ public class WordUtils { * @return the abbreviated String. * * <pre> - * * WordUtils.abbreviate("Now is the time for all good men", 0, 40, null)); = "Now" * WordUtils.abbreviate("Now is the time for all good men", 10, 40, null)); = "Now is the" * WordUtils.abbreviate("Now is the time for all good men", 20, 40, null)); = "Now is the time for all" @@ -777,20 +776,11 @@ public class WordUtils { * WordUtils.abbreviate("Now is the time for all good men", 1000, -1, "")); = "Now is the time for all good men" * WordUtils.abbreviate("Now is the time for all good men", 9, -10, null)); = IllegalArgumentException * WordUtils.abbreviate("Now is the time for all good men", 10, 5, null)); = IllegalArgumentException - * * </pre> */ public static String abbreviate(String str, int lower, int upper, String appendToEnd) { - - // throw IllegalArgumentException if upper limit is less than -1 which voids contact. - if (upper < -1) { - throw new IllegalArgumentException("upper value cannot be less than -1"); - } - - // throw IllegalArgumentException if upper value is less than lower value. - if (upper < lower && upper != -1) { - throw new IllegalArgumentException("upper value is less than lower value"); - } + Validate.isTrue(upper >= -1, "upper value cannot be less than -1"); + Validate.isTrue(upper >= lower || upper == -1, "upper value is less than lower value"); if (StringUtils.isEmpty(str)) { return str; http://git-wip-us.apache.org/repos/asf/commons-text/blob/73245020/src/main/java/org/apache/commons/text/similarity/EditDistanceFrom.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/text/similarity/EditDistanceFrom.java b/src/main/java/org/apache/commons/text/similarity/EditDistanceFrom.java index b67a41f..691fef3 100644 --- a/src/main/java/org/apache/commons/text/similarity/EditDistanceFrom.java +++ b/src/main/java/org/apache/commons/text/similarity/EditDistanceFrom.java @@ -16,6 +16,8 @@ */ package org.apache.commons.text.similarity; +import org.apache.commons.lang3.Validate; + /** * <p> * This stores a {@link EditDistance} implementation and a {@link CharSequence} "left" string. @@ -70,9 +72,7 @@ public class EditDistanceFrom<R> { * implementation may not accept nulls. */ public EditDistanceFrom(final EditDistance<R> editDistance, final CharSequence left) { - if (editDistance == null) { - throw new IllegalArgumentException("The edit distance may not be null."); - } + Validate.isTrue(editDistance != null, "The edit distance may not be null."); this.editDistance = editDistance; this.left = left; http://git-wip-us.apache.org/repos/asf/commons-text/blob/73245020/src/main/java/org/apache/commons/text/similarity/RegexTokenizer.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/text/similarity/RegexTokenizer.java b/src/main/java/org/apache/commons/text/similarity/RegexTokenizer.java index 1c9e268..cc009ef 100644 --- a/src/main/java/org/apache/commons/text/similarity/RegexTokenizer.java +++ b/src/main/java/org/apache/commons/text/similarity/RegexTokenizer.java @@ -21,6 +21,9 @@ import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.Validate; + /** * A simple word tokenizer that utilizes regex to find words. It applies a regex * {@code}(\w)+{@code} over the input text to extract words from a given character @@ -37,9 +40,7 @@ class RegexTokenizer implements Tokenizer<CharSequence> { */ @Override public CharSequence[] tokenize(final CharSequence text) { - if (text == null || text.toString().trim().equals("")) { - throw new IllegalArgumentException("Invalid text"); - } + Validate.isTrue(StringUtils.isNotBlank(text), "Invalid text"); final Pattern pattern = Pattern.compile("(\\w)+"); final Matcher matcher = pattern.matcher(text.toString()); final List<String> tokens = new ArrayList<>(); http://git-wip-us.apache.org/repos/asf/commons-text/blob/73245020/src/main/java/org/apache/commons/text/similarity/SimilarityScoreFrom.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/text/similarity/SimilarityScoreFrom.java b/src/main/java/org/apache/commons/text/similarity/SimilarityScoreFrom.java index b58ea4a..8d3e46e 100644 --- a/src/main/java/org/apache/commons/text/similarity/SimilarityScoreFrom.java +++ b/src/main/java/org/apache/commons/text/similarity/SimilarityScoreFrom.java @@ -16,6 +16,8 @@ */ package org.apache.commons.text.similarity; +import org.apache.commons.lang3.Validate; + /** * <p> * This stores a {@link SimilarityScore} implementation and a {@link CharSequence} "left" string. @@ -70,9 +72,7 @@ public class SimilarityScoreFrom<R> { * implementation may not accept nulls. */ public SimilarityScoreFrom(final SimilarityScore<R> similarityScore, final CharSequence left) { - if (similarityScore == null) { - throw new IllegalArgumentException("The edit distance may not be null."); - } + Validate.isTrue(similarityScore != null, "The edit distance may not be null."); this.similarityScore = similarityScore; this.left = left; http://git-wip-us.apache.org/repos/asf/commons-text/blob/73245020/src/main/java/org/apache/commons/text/translate/CharSequenceTranslator.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/text/translate/CharSequenceTranslator.java b/src/main/java/org/apache/commons/text/translate/CharSequenceTranslator.java index 4a27472..dd719fe 100644 --- a/src/main/java/org/apache/commons/text/translate/CharSequenceTranslator.java +++ b/src/main/java/org/apache/commons/text/translate/CharSequenceTranslator.java @@ -21,6 +21,8 @@ import java.io.StringWriter; import java.io.Writer; import java.util.Locale; +import org.apache.commons.lang3.Validate; + /** * An API for translating text. * Its core use is to escape and unescape text. Because escaping and unescaping @@ -80,9 +82,7 @@ public abstract class CharSequenceTranslator { * @throws IOException if and only if the Writer produces an IOException */ public final void translate(final CharSequence input, final Writer out) throws IOException { - if (out == null) { - throw new IllegalArgumentException("The Writer must not be null"); - } + Validate.isTrue(out != null, "The Writer must not be null"); if (input == null) { return; }