diff options
56 files changed, 762 insertions, 524 deletions
diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml index 953c456a8..4800243fd 100644 --- a/.github/workflows/codeql-analysis.yml +++ b/.github/workflows/codeql-analysis.yml @@ -45,7 +45,7 @@ jobs: steps: - name: Checkout repository - uses: actions/checkout@44c2b7a8a4ea60a981eaca3cf939b5f4305c123b # 4.1.5 + uses: actions/checkout@a5ac7e51b41094c92402da3b24376905380afc29 # 4.1.6 with: persist-credentials: false - uses: actions/cache@0c45773b623bea8c8e75f6c82b208c3cf94ea4f9 # v4.0.2 @@ -57,7 +57,7 @@ jobs: # Initializes the CodeQL tools for scanning. - name: Initialize CodeQL - uses: github/codeql-action/init@d39d31e687223d841ef683f52467bd88e9b21c14 # 3.25.3 + uses: github/codeql-action/init@9fdb3e49720b44c48891d036bb502feb25684276 # 3.25.6 with: languages: ${{ matrix.language }} # If you wish to specify custom queries, you can do so here or in a config file. @@ -68,7 +68,7 @@ jobs: # Autobuild attempts to build any compiled languages (C/C++, C#, or Java). # If this step fails, then you should remove it and run the build manually (see below) - name: Autobuild - uses: github/codeql-action/autobuild@d39d31e687223d841ef683f52467bd88e9b21c14 # 3.25.3 + uses: github/codeql-action/autobuild@9fdb3e49720b44c48891d036bb502feb25684276 # 3.25.6 # ℹ️ Command-line programs to run using the OS shell. # 📚 https://git.io/JvXDl @@ -82,4 +82,4 @@ jobs: # make release - name: Perform CodeQL Analysis - uses: github/codeql-action/analyze@d39d31e687223d841ef683f52467bd88e9b21c14 # 3.25.3 + uses: github/codeql-action/analyze@9fdb3e49720b44c48891d036bb502feb25684276 # 3.25.6 diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml index 9331f2c7c..8cbd776dc 100644 --- a/.github/workflows/coverage.yml +++ b/.github/workflows/coverage.yml @@ -29,7 +29,7 @@ jobs: java: [ 8 ] steps: - - uses: actions/checkout@44c2b7a8a4ea60a981eaca3cf939b5f4305c123b # 4.1.5 + - uses: actions/checkout@a5ac7e51b41094c92402da3b24376905380afc29 # 4.1.6 with: persist-credentials: false - uses: actions/cache@0c45773b623bea8c8e75f6c82b208c3cf94ea4f9 # v4.0.2 @@ -47,6 +47,6 @@ jobs: run: mvn --show-version --batch-mode --no-transfer-progress test jacoco:report - name: Upload coverage to Codecov - uses: codecov/codecov-action@5ecb98a3c6b747ed38dc09f787459979aebb39be # v4.3.1 + uses: codecov/codecov-action@125fc84a9a348dbcf27191600683ec096ec9021c # v4.4.1 with: files: ./target/site/jacoco/jacoco.xml diff --git a/.github/workflows/maven.yml b/.github/workflows/maven.yml index e529f2193..5219a1d6e 100644 --- a/.github/workflows/maven.yml +++ b/.github/workflows/maven.yml @@ -35,7 +35,7 @@ jobs: # experimental: true steps: - - uses: actions/checkout@44c2b7a8a4ea60a981eaca3cf939b5f4305c123b # v4.1.5 + - uses: actions/checkout@a5ac7e51b41094c92402da3b24376905380afc29 # v4.1.6 with: persist-credentials: false - uses: actions/cache@0c45773b623bea8c8e75f6c82b208c3cf94ea4f9 # v4.0.2 @@ -50,4 +50,4 @@ jobs: distribution: 'temurin' java-version: ${{ matrix.java }} - name: Build with Maven - run: mvn --show-version --batch-mode --no-transfer-progress -Ddoclint=all + run: mvn --errors --show-version --batch-mode --no-transfer-progress -Ddoclint=all diff --git a/.github/workflows/scorecards-analysis.yml b/.github/workflows/scorecards-analysis.yml index 06f76b746..e4bfebb31 100644 --- a/.github/workflows/scorecards-analysis.yml +++ b/.github/workflows/scorecards-analysis.yml @@ -40,7 +40,7 @@ jobs: steps: - name: "Checkout code" - uses: actions/checkout@44c2b7a8a4ea60a981eaca3cf939b5f4305c123b # 4.1.5 + uses: actions/checkout@a5ac7e51b41094c92402da3b24376905380afc29 # 4.1.6 with: persist-credentials: false @@ -64,6 +64,6 @@ jobs: retention-days: 5 - name: "Upload to code-scanning" - uses: github/codeql-action/upload-sarif@d39d31e687223d841ef683f52467bd88e9b21c14 # 3.25.3 + uses: github/codeql-action/upload-sarif@9fdb3e49720b44c48891d036bb502feb25684276 # 3.25.6 with: sarif_file: results.sarif diff --git a/.mvn/.gitignore b/.mvn/.gitignore new file mode 100644 index 000000000..f26bafb8b --- /dev/null +++ b/.mvn/.gitignore @@ -0,0 +1,4 @@ +# +# Empty file (Maven 4 wants the directory .mvn to be present, +# and we want Git to create it.) +# @@ -22,7 +22,7 @@ <parent> <groupId>org.apache.commons</groupId> <artifactId>commons-parent</artifactId> - <version>69</version> + <version>70</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>commons-lang3</artifactId> @@ -190,7 +190,10 @@ <version>${commons.pmd.version}</version> <configuration> <targetJdk>${maven.compiler.target}</targetJdk> - <excludeFromFailureFile>src/conf/exclude-pmd.properties</excludeFromFailureFile> + <excludeFromFailureFile>src/conf/pmd-exclude.properties</excludeFromFailureFile> + <rulesets> + <ruleset>src/conf/pmd-ruleset.xml</ruleset> + </rulesets> </configuration> </plugin> <plugin> @@ -217,7 +220,6 @@ <notimestamp>true</notimestamp> <links> <link>https://commons.apache.org/proper/commons-text/apidocs</link> - <link>https://docs.oracle.com/javase/8/docs/api</link> <link>https://docs.oracle.com/javaee/6/api</link> </links> <validateLinks>true</validateLinks> @@ -323,7 +325,6 @@ <notimestamp>true</notimestamp> <links> <link>https://commons.apache.org/proper/commons-text/apidocs</link> - <link>https://docs.oracle.com/javase/8/docs/api</link> <link>https://docs.oracle.com/javaee/6/api</link> </links> <validateLinks>true</validateLinks> @@ -499,7 +500,7 @@ <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> - <version>3.2.0</version> + <version>3.3.0</version> <executions> <execution> <id>benchmark</id> diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 359100624..878007a9b 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -60,10 +60,11 @@ The <action> type attribute can be add,update,fix,remove. <action type="add" dev="ggregory" due-to="Gary Gregory">Add T ArrayUtils.arraycopy(T, int, int, int, Function) fluent style.</action> <action type="add" dev="ggregory" due-to="Gary Gregory">Add SystemUtils.IS_JAVA_22.</action> <action type="add" dev="ggregory" due-to="Gary Gregory">Add JavaVersion.JAVA_22.</action> - <action type="add" dev="ggregory" due-to="Gary Gregory">Add org.apache.commons.lang3.SystemProperties.getUserName(Supplier<String>).</action> - <action type="add" dev="ggregory" due-to="Gary Gregory">Add org.apache.commons.lang3.SystemProperties.getLineSeparator(Supplier<String>).</action> - <action type="add" dev="ggregory" due-to="Gary Gregory">Add org.apache.commons.lang3.SystemProperties.getJavaSpecificationVersion(Supplier<String>).</action> + <action type="add" dev="ggregory" due-to="Gary Gregory">Add SystemProperties.getUserName(Supplier<String>).</action> + <action type="add" dev="ggregory" due-to="Gary Gregory">Add SystemProperties.getLineSeparator(Supplier<String>).</action> + <action type="add" dev="ggregory" due-to="Gary Gregory">Add SystemProperties.getJavaSpecificationVersion(Supplier<String>).</action> <action type="add" dev="ggregory" due-to="Gary Gregory">Add SystemProperties constants and methods for system properties as of Java 22.</action> + <action type="add" dev="ggregory" due-to="Gary Gregory">Add MethodUtils.getMethodObject(Class, String, Class...).</action> <!-- FIX --> <action type="fix" dev="ggregory" due-to="Miklós Karakó, Gary Gregory">Improve Javadoc in ExceptionUtils #1136.</action> <action type="fix" dev="ggregory" due-to="Saiharshith Karuneegar Ramesh, Gary Gregory">Fixed two non-deterministic tests in EnumUtilsTest.java #1131.</action> @@ -126,10 +127,20 @@ The <action> type attribute can be add,update,fix,remove. <action type="fix" dev="ggregory" due-to="Stephan Peters, Gary Gregory, Bernd">StringUtils.stripAccents() should handle ligatures, UTF32 math blocks, etc. #1201.</action> <action issue="LANG-1524" type="fix" dev="ggregory" due-to="kijong.youn, Aakash Gupta, Gary Gregory">TypeUtils.toString(Type) StackOverflowError for an inner class in the inner class parameterized enclosing class #657.</action> <action type="fix" dev="ggregory" due-to="Gary Gregory">Deprecate SystemUtils.getUserName(String) in favor of SystemProperties.getUserName(Supplier).</action> + <action type="fix" dev="ggregory" due-to="Gary Gregory">Make LockVisitor.acceptReadLocked(FailableConsumer) null-safe.</action> + <action type="fix" dev="ggregory" due-to="Gary Gregory">Make LockVisitor.applyWriteLocked(FailableConsumer) null-safe.</action> + <action type="fix" dev="ggregory" due-to="Gary Gregory">Make ObjectUtils.getFirstNonNull(Supplier...) null-safe.</action> + <action type="fix" dev="ggregory" due-to="Gary Gregory">Make SystemProperties.getLineSeparator(Supplier).</action> + <action type="fix" dev="ggregory" due-to="hunghhdev">StringUtils.stripAccents(String) doesn't handle "\u0111" and "\u0110" (Vietnamese) #1216.</action> + <action type="fix" dev="ggregory" due-to="Gary Gregory">StringUtils.stripAccents(String) doesn't handle I with bar.</action> + <action type="fix" dev="ggregory" due-to="Gary Gregory">StringUtils.stripAccents(String) doesn't handle U with bar.</action> + <action type="fix" dev="ggregory" due-to="Gary Gregory">StringUtils.stripAccents(String) doesn't handle T with stroke.</action> + <action issue="LANG-1735" type="fix" dev="ggregory" due-to="Tobias Kiecker">Fix Javadoc for FluentBitSet.setInclusive(int, int) #1222.</action> + <action type="fix" dev="ggregory" due-to="Tobias Kiecker">Same Javadoc changes as [TEXT-234] #1223.</action> <!-- UPDATE --> - <action type="update" dev="sebb" due-to="Dependabot">Bump commons-parent from 64 to 69 #1194.</action> - <action type="update" dev="ggregory" due-to="Dependabot">Bump org.codehaus.mojo:exec-maven-plugin from 3.1.1 to 3.2.0 #1175.</action> - <action type="update" dev="ggregory" due-to="Dependabot">Bump org.apache.commons:commons-text from 1.11.0 to 1.12.0 #1200.</action> + <action type="update" dev="sebb" due-to="Dependabot, Gary Gregory">Bump commons-parent from 64 to 70 #1194.</action> + <action type="update" dev="ggregory" due-to="Dependabot">Bump org.codehaus.mojo:exec-maven-plugin from 3.1.1 to 3.3.0 #1175, #1224.</action> + <action type="update" dev="ggregory" due-to="Dependabot">Bump org.apache.commons:commons-text from 1.11.0 to 1.12.0 #1200.</action> <!-- REMOVE --> <action type="remove" dev="ggregory" due-to="Paranoïd User">Drop obsolete JDK 13 Maven profile #1142.</action> </release> diff --git a/src/conf/exclude-pmd.properties b/src/conf/pmd-exclude.properties index b838bd5f6..b838bd5f6 100644 --- a/src/conf/exclude-pmd.properties +++ b/src/conf/pmd-exclude.properties diff --git a/src/conf/pmd-ruleset.xml b/src/conf/pmd-ruleset.xml new file mode 100644 index 000000000..9dc743db0 --- /dev/null +++ b/src/conf/pmd-ruleset.xml @@ -0,0 +1,27 @@ +<?xml version="1.0"?> +<!-- +Licensed to the Apache Software Foundation (ASF) under one or more +contributor license agreements. See the NOTICE file distributed with +this work for additional information regarding copyright ownership. +The ASF licenses this file to You under the Apache License, Version 2.0 +(the "License"); you may not use this file except in compliance with +the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +--> +<ruleset name="mybraces" xmlns="http://pmd.sourceforge.net/ruleset/2.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 http://pmd.sourceforge.net/ruleset_2_0_0.xsd"> + <description>Excludes from default PMD rules.</description> + <rule ref="category/java/errorprone.xml/EmptyCatchBlock"> + <properties> + <property name="allowCommentedBlocks" value="true" /> + <property name="allowExceptionNameRegex" value="^(ignored|expected)$" /> + </properties> + </rule> +</ruleset> diff --git a/src/conf/spotbugs-exclude-filter.xml b/src/conf/spotbugs-exclude-filter.xml index 6ff15c47c..d935cb51d 100644 --- a/src/conf/spotbugs-exclude-filter.xml +++ b/src/conf/spotbugs-exclude-filter.xml @@ -203,4 +203,18 @@ <Method name="requireNonNull" /> <Bug pattern="NP_PARAMETER_MUST_BE_NONNULL_BUT_MARKED_AS_NULLABLE" /> </Match> + <!-- What can be done?--> + <Match> + <Class name="org.apache.commons.lang3.event.EventListenerSupport" /> + <Method name="readObject" /> + <Bug pattern="MC_OVERRIDABLE_METHOD_CALL_IN_READ_OBJECT" /> + </Match> + <!-- Fundamental disagreement with SB: It should be OK to have predefined instances and allow new instances. --> + <Match> + <Bug pattern="SING_SINGLETON_HAS_NONPRIVATE_CONSTRUCTOR" /> + </Match> + <!-- Fundamental disagreement with SB: It should be OK to have predefined instances and allow new instances. --> + <Match> + <Bug pattern="SING_SINGLETON_IMPLEMENTS_SERIALIZABLE" /> + </Match> </FindBugsFilter> diff --git a/src/main/java/org/apache/commons/lang3/CharSetUtils.java b/src/main/java/org/apache/commons/lang3/CharSetUtils.java index 376cdb916..fc5af2ab3 100644 --- a/src/main/java/org/apache/commons/lang3/CharSetUtils.java +++ b/src/main/java/org/apache/commons/lang3/CharSetUtils.java @@ -161,7 +161,7 @@ public class CharSetUtils { } /** - * Implementation of delete and keep + * Implements delete and keep. * * @param str String to modify characters within * @param set String[] set of characters to modify diff --git a/src/main/java/org/apache/commons/lang3/ObjectUtils.java b/src/main/java/org/apache/commons/lang3/ObjectUtils.java index 083773943..8974f2528 100644 --- a/src/main/java/org/apache/commons/lang3/ObjectUtils.java +++ b/src/main/java/org/apache/commons/lang3/ObjectUtils.java @@ -19,8 +19,6 @@ package org.apache.commons.lang3; import java.io.IOException; import java.io.Serializable; import java.lang.reflect.Array; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; import java.time.Duration; import java.util.ArrayList; import java.util.Collection; @@ -114,7 +112,7 @@ public class ObjectUtils { public static final Null NULL = new Null(); /** - * Checks if all values in the array are not {@code nulls}. + * Tests if all values in the array are not {@code nulls}. * * <p> * If any value is {@code null} or the array is {@code null} then @@ -143,7 +141,7 @@ public class ObjectUtils { } /** - * Checks if all values in the given array are {@code null}. + * Tests if all values in the given array are {@code null}. * * <p> * If all the values are {@code null} or the array is {@code null} @@ -169,7 +167,7 @@ public class ObjectUtils { } /** - * Checks if any value in the given array is not {@code null}. + * Tests if any value in the given array is not {@code null}. * * <p> * If all the values are {@code null} or the array is {@code null} @@ -196,7 +194,7 @@ public class ObjectUtils { } /** - * Checks if any value in the given array is {@code null}. + * Tests if any value in the given array is {@code null}. * * <p> * If any of the values are {@code null} or the array is {@code null}, @@ -224,7 +222,7 @@ public class ObjectUtils { } /** - * Clone an object. + * Clones an object. * * @param <T> the type of the object * @param obj the object to clone, null returns null @@ -235,8 +233,9 @@ public class ObjectUtils { public static <T> T clone(final T obj) { if (obj instanceof Cloneable) { final Object result; + final Class<? extends Object> objClass = obj.getClass(); if (isArray(obj)) { - final Class<?> componentType = obj.getClass().getComponentType(); + final Class<?> componentType = objClass.getComponentType(); if (componentType.isPrimitive()) { int length = Array.getLength(obj); result = Array.newInstance(componentType, length); @@ -248,18 +247,9 @@ public class ObjectUtils { } } else { try { - final Method clone = obj.getClass().getMethod("clone"); - result = clone.invoke(obj); - } catch (final NoSuchMethodException e) { - throw new CloneFailedException("Cloneable type " - + obj.getClass().getName() - + " has no clone method", e); - } catch (final IllegalAccessException e) { - throw new CloneFailedException("Cannot clone Cloneable type " - + obj.getClass().getName(), e); - } catch (final InvocationTargetException e) { - throw new CloneFailedException("Exception cloning Cloneable type " - + obj.getClass().getName(), e.getCause()); + result = objClass.getMethod("clone").invoke(obj); + } catch (final ReflectiveOperationException e) { + throw new CloneFailedException("Exception cloning Cloneable type " + objClass.getName(), e); } } return (T) result; @@ -269,7 +259,7 @@ public class ObjectUtils { } /** - * Clone an object if possible. + * Clones an object if possible. * * <p>This method is similar to {@link #clone(Object)}, but will return the provided * instance as the return value instead of {@code null} if the instance @@ -332,7 +322,7 @@ public class ObjectUtils { } /** - * This method returns the provided value unchanged. + * Returns the provided value unchanged. * This can prevent javac from inlining a constant * field, e.g., * @@ -353,7 +343,7 @@ public class ObjectUtils { } /** - * This method returns the provided value unchanged. + * Returns the provided value unchanged. * This can prevent javac from inlining a constant * field, e.g., * @@ -374,7 +364,7 @@ public class ObjectUtils { } /** - * This method returns the provided value unchanged. + * Returns the provided value unchanged. * This can prevent javac from inlining a constant * field, e.g., * @@ -395,7 +385,7 @@ public class ObjectUtils { } /** - * This method returns the provided value unchanged. + * Returns the provided value unchanged. * This can prevent javac from inlining a constant * field, e.g., * @@ -416,7 +406,7 @@ public class ObjectUtils { } /** - * This method returns the provided value unchanged. + * Returns the provided value unchanged. * This can prevent javac from inlining a constant * field, e.g., * @@ -437,7 +427,7 @@ public class ObjectUtils { } /** - * This method returns the provided value unchanged. + * Returns the provided value unchanged. * This can prevent javac from inlining a constant * field, e.g., * @@ -458,7 +448,7 @@ public class ObjectUtils { } /** - * This method returns the provided value unchanged. + * Returns the provided value unchanged. * This can prevent javac from inlining a constant * field, e.g., * @@ -479,7 +469,7 @@ public class ObjectUtils { } /** - * This method returns the provided value unchanged. + * Returns the provided value unchanged. * This can prevent javac from inlining a constant * field, e.g., * @@ -500,7 +490,7 @@ public class ObjectUtils { } /** - * This method returns the provided value unchanged. + * Returns the provided value unchanged. * This can prevent javac from inlining a constant * field, e.g., * @@ -522,7 +512,7 @@ public class ObjectUtils { } /** - * This method returns the provided value unchanged. + * Returns the provided value unchanged. * This can prevent javac from inlining a constant * field, e.g., * @@ -549,7 +539,7 @@ public class ObjectUtils { } /** - * This method returns the provided value unchanged. + * Returns the provided value unchanged. * This can prevent javac from inlining a constant * field, e.g., * @@ -689,7 +679,7 @@ public class ObjectUtils { */ @SafeVarargs public static <T> T getFirstNonNull(final Supplier<T>... suppliers) { - return Streams.of(suppliers).map(s -> s != null ? s.get() : null).filter(Objects::nonNull).findFirst().orElse(null); + return Streams.of(suppliers).filter(Objects::nonNull).map(Supplier::get).filter(Objects::nonNull).findFirst().orElse(null); } /** diff --git a/src/main/java/org/apache/commons/lang3/StringUtils.java b/src/main/java/org/apache/commons/lang3/StringUtils.java index 16dd46e96..6e786b004 100644 --- a/src/main/java/org/apache/commons/lang3/StringUtils.java +++ b/src/main/java/org/apache/commons/lang3/StringUtils.java @@ -1406,10 +1406,67 @@ public class StringUtils { private static void convertRemainingAccentCharacters(final StringBuilder decomposed) { for (int i = 0; i < decomposed.length(); i++) { - if (decomposed.charAt(i) == '\u0141') { + final char charAt = decomposed.charAt(i); + switch (charAt) { + case '\u0141': decomposed.setCharAt(i, 'L'); - } else if (decomposed.charAt(i) == '\u0142') { + break; + case '\u0142': decomposed.setCharAt(i, 'l'); + break; + // D with stroke + case '\u0110': + // LATIN CAPITAL LETTER D WITH STROKE + decomposed.setCharAt(i, 'D'); + break; + case '\u0111': + // LATIN SMALL LETTER D WITH STROKE + decomposed.setCharAt(i, 'd'); + break; + // I with bar + case '\u0197': + decomposed.setCharAt(i, 'I'); + break; + case '\u0268': + decomposed.setCharAt(i, 'i'); + break; + case '\u1D7B': + decomposed.setCharAt(i, 'I'); + break; + case '\u1DA4': + decomposed.setCharAt(i, 'i'); + break; + case '\u1DA7': + decomposed.setCharAt(i, 'I'); + break; + // U with bar + case '\u0244': + // LATIN CAPITAL LETTER U BAR + decomposed.setCharAt(i, 'U'); + break; + case '\u0289': + // LATIN SMALL LETTER U BAR + decomposed.setCharAt(i, 'u'); + break; + case '\u1D7E': + // LATIN SMALL CAPITAL LETTER U WITH STROKE + decomposed.setCharAt(i, 'U'); + break; + case '\u1DB6': + // MODIFIER LETTER SMALL U BAR + decomposed.setCharAt(i, 'u'); + break; + // T with stroke + case '\u0166': + // LATIN CAPITAL LETTER T WITH STROKE + decomposed.setCharAt(i, 'T'); + break; + case '\u0167': + // LATIN SMALL LETTER T WITH STROKE + decomposed.setCharAt(i, 't'); + break; + default: + break; } } } diff --git a/src/main/java/org/apache/commons/lang3/SystemProperties.java b/src/main/java/org/apache/commons/lang3/SystemProperties.java index d7a314307..54137332d 100644 --- a/src/main/java/org/apache/commons/lang3/SystemProperties.java +++ b/src/main/java/org/apache/commons/lang3/SystemProperties.java @@ -3721,7 +3721,7 @@ public final class SystemProperties { static String getProperty(final String property, final Supplier<String> defaultIfAbsent) { try { if (StringUtils.isEmpty(property)) { - return defaultIfAbsent.get(); + return Suppliers.get(defaultIfAbsent); } final String value = System.getProperty(property); return StringUtils.getIfEmpty(value, defaultIfAbsent); diff --git a/src/main/java/org/apache/commons/lang3/builder/CompareToBuilder.java b/src/main/java/org/apache/commons/lang3/builder/CompareToBuilder.java index e8f7020e2..0782e04ad 100644 --- a/src/main/java/org/apache/commons/lang3/builder/CompareToBuilder.java +++ b/src/main/java/org/apache/commons/lang3/builder/CompareToBuilder.java @@ -338,7 +338,7 @@ public class CompareToBuilder implements Builder<Integer> { * * @param lhs left-hand side value * @param rhs right-hand side value - * @return this + * @return {@code this} instance. */ public CompareToBuilder append(final boolean lhs, final boolean rhs) { if (comparison != 0) { @@ -368,7 +368,7 @@ public class CompareToBuilder implements Builder<Integer> { * * @param lhs left-hand side array * @param rhs right-hand side array - * @return this + * @return {@code this} instance. */ public CompareToBuilder append(final boolean[] lhs, final boolean[] rhs) { if (comparison != 0) { @@ -401,7 +401,7 @@ public class CompareToBuilder implements Builder<Integer> { * * @param lhs left-hand side value * @param rhs right-hand side value - * @return this + * @return {@code this} instance. */ public CompareToBuilder append(final byte lhs, final byte rhs) { if (comparison != 0) { @@ -424,7 +424,7 @@ public class CompareToBuilder implements Builder<Integer> { * * @param lhs left-hand side array * @param rhs right-hand side array - * @return this + * @return {@code this} instance. */ public CompareToBuilder append(final byte[] lhs, final byte[] rhs) { if (comparison != 0) { @@ -457,7 +457,7 @@ public class CompareToBuilder implements Builder<Integer> { * * @param lhs left-hand side value * @param rhs right-hand side value - * @return this + * @return {@code this} instance. */ public CompareToBuilder append(final char lhs, final char rhs) { if (comparison != 0) { @@ -480,7 +480,7 @@ public class CompareToBuilder implements Builder<Integer> { * * @param lhs left-hand side array * @param rhs right-hand side array - * @return this + * @return {@code this} instance. */ public CompareToBuilder append(final char[] lhs, final char[] rhs) { if (comparison != 0) { @@ -518,7 +518,7 @@ public class CompareToBuilder implements Builder<Integer> { * * @param lhs left-hand side value * @param rhs right-hand side value - * @return this + * @return {@code this} instance. */ public CompareToBuilder append(final double lhs, final double rhs) { if (comparison != 0) { @@ -541,7 +541,7 @@ public class CompareToBuilder implements Builder<Integer> { * * @param lhs left-hand side array * @param rhs right-hand side array - * @return this + * @return {@code this} instance. */ public CompareToBuilder append(final double[] lhs, final double[] rhs) { if (comparison != 0) { @@ -579,7 +579,7 @@ public class CompareToBuilder implements Builder<Integer> { * * @param lhs left-hand side value * @param rhs right-hand side value - * @return this + * @return {@code this} instance. */ public CompareToBuilder append(final float lhs, final float rhs) { if (comparison != 0) { @@ -602,7 +602,7 @@ public class CompareToBuilder implements Builder<Integer> { * * @param lhs left-hand side array * @param rhs right-hand side array - * @return this + * @return {@code this} instance. */ public CompareToBuilder append(final float[] lhs, final float[] rhs) { if (comparison != 0) { @@ -635,7 +635,7 @@ public class CompareToBuilder implements Builder<Integer> { * * @param lhs left-hand side value * @param rhs right-hand side value - * @return this + * @return {@code this} instance. */ public CompareToBuilder append(final int lhs, final int rhs) { if (comparison != 0) { @@ -658,7 +658,7 @@ public class CompareToBuilder implements Builder<Integer> { * * @param lhs left-hand side array * @param rhs right-hand side array - * @return this + * @return {@code this} instance. */ public CompareToBuilder append(final int[] lhs, final int[] rhs) { if (comparison != 0) { @@ -691,7 +691,7 @@ public class CompareToBuilder implements Builder<Integer> { * * @param lhs left-hand side value * @param rhs right-hand side value - * @return this + * @return {@code this} instance. */ public CompareToBuilder append(final long lhs, final long rhs) { if (comparison != 0) { @@ -714,7 +714,7 @@ public class CompareToBuilder implements Builder<Integer> { * * @param lhs left-hand side array * @param rhs right-hand side array - * @return this + * @return {@code this} instance. */ public CompareToBuilder append(final long[] lhs, final long[] rhs) { if (comparison != 0) { @@ -756,7 +756,7 @@ public class CompareToBuilder implements Builder<Integer> { * * @param lhs left-hand side object * @param rhs right-hand side object - * @return this + * @return {@code this} instance. * @throws ClassCastException if {@code rhs} is not assignment-compatible * with {@code lhs} */ @@ -784,7 +784,7 @@ public class CompareToBuilder implements Builder<Integer> { * @param rhs right-hand side object * @param comparator {@link Comparator} used to compare the objects, * {@code null} means treat lhs as {@link Comparable} - * @return this + * @return {@code this} instance. * @throws ClassCastException if {@code rhs} is not assignment-compatible * with {@code lhs} * @since 2.0 @@ -836,7 +836,7 @@ public class CompareToBuilder implements Builder<Integer> { * * @param lhs left-hand side array * @param rhs right-hand side array - * @return this + * @return {@code this} instance. * @throws ClassCastException if {@code rhs} is not assignment-compatible * with {@code lhs} */ @@ -862,7 +862,7 @@ public class CompareToBuilder implements Builder<Integer> { * @param rhs right-hand side array * @param comparator {@link Comparator} to use to compare the array elements, * {@code null} means to treat {@code lhs} elements as {@link Comparable}. - * @return this + * @return {@code this} instance. * @throws ClassCastException if {@code rhs} is not assignment-compatible * with {@code lhs} * @since 2.0 @@ -898,7 +898,7 @@ public class CompareToBuilder implements Builder<Integer> { * * @param lhs left-hand side value * @param rhs right-hand side value - * @return this + * @return {@code this} instance. */ public CompareToBuilder append(final short lhs, final short rhs) { if (comparison != 0) { @@ -921,7 +921,7 @@ public class CompareToBuilder implements Builder<Integer> { * * @param lhs left-hand side array * @param rhs right-hand side array - * @return this + * @return {@code this} instance. */ public CompareToBuilder append(final short[] lhs, final short[] rhs) { if (comparison != 0) { @@ -980,7 +980,7 @@ public class CompareToBuilder implements Builder<Integer> { * result of the superclass. * * @param superCompareTo result of calling {@code super.compareTo(Object)} - * @return this + * @return {@code this} instance. * @since 2.0 */ public CompareToBuilder appendSuper(final int superCompareTo) { diff --git a/src/main/java/org/apache/commons/lang3/builder/Diff.java b/src/main/java/org/apache/commons/lang3/builder/Diff.java index da0335aad..3208424dc 100644 --- a/src/main/java/org/apache/commons/lang3/builder/Diff.java +++ b/src/main/java/org/apache/commons/lang3/builder/Diff.java @@ -17,6 +17,7 @@ package org.apache.commons.lang3.builder; import java.lang.reflect.Type; +import java.util.Objects; import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.reflect.TypeUtils; @@ -54,13 +55,13 @@ public abstract class Diff<T> extends Pair<T, T> { * the field name */ protected Diff(final String fieldName) { + this.fieldName = Objects.requireNonNull(fieldName); this.type = ObjectUtils.defaultIfNull(TypeUtils.getTypeArguments(getClass(), Diff.class).get(Diff.class.getTypeParameters()[0]), Object.class); - this.fieldName = fieldName; } Diff(final String fieldName, final Type type) { - this.type = type; - this.fieldName = fieldName; + this.fieldName = Objects.requireNonNull(fieldName); + this.type = Objects.requireNonNull(type); } /** 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 378729145..180ad959c 100644 --- a/src/main/java/org/apache/commons/lang3/builder/DiffBuilder.java +++ b/src/main/java/org/apache/commons/lang3/builder/DiffBuilder.java @@ -100,7 +100,7 @@ public class DiffBuilder<T> implements Builder<DiffResult<T>> { * Sets the left object. * * @param left the left object. - * @return this. + * @return {@code this} instance. */ public Builder<T> setLeft(final T left) { this.left = left; @@ -111,7 +111,7 @@ public class DiffBuilder<T> implements Builder<DiffResult<T>> { * Sets the right object. * * @param right the left object. - * @return this. + * @return {@code this} instance. */ public Builder<T> setRight(final T right) { this.right = right; @@ -122,7 +122,7 @@ public class DiffBuilder<T> implements Builder<DiffResult<T>> { * Sets the style will to use when outputting the objects, {@code null} uses the default. * * @param style the style to use when outputting the objects, {@code null} uses the default. - * @return this. + * @return {@code this} instance. */ public Builder<T> setStyle(final ToStringStyle style) { this.style = style != null ? style : ToStringStyle.DEFAULT_STYLE; @@ -137,7 +137,7 @@ public class DiffBuilder<T> implements Builder<DiffResult<T>> { * @param testObjectsEquals If true, this will test if lhs and rhs are the same or equal. All of the append(fieldName, left, right) methods will abort * without creating a field {@link Diff} if the trivially equal test is enabled and returns true. The result of this test is * never changed throughout the life of this {@link DiffBuilder}. - * @return this. + * @return {@code this} instance. */ public Builder<T> setTestObjectsEquals(final boolean testObjectsEquals) { this.testObjectsEquals = testObjectsEquals; @@ -148,7 +148,7 @@ public class DiffBuilder<T> implements Builder<DiffResult<T>> { * Sets the two-argument format string for {@link String#format(String, Object...)}, for example {@code "%s differs from %s"}. * * @param toStringFormat {@code null} uses the default. - * @return this. + * @return {@code this} instance. */ public Builder<T> setToStringFormat(final String toStringFormat) { this.toStringFormat = toStringFormat != null ? toStringFormat : TO_STRING_FORMAT; @@ -159,23 +159,23 @@ public class DiffBuilder<T> implements Builder<DiffResult<T>> { private static final class SDiff<T> extends Diff<T> { private static final long serialVersionUID = 1L; - private final transient Supplier<T> leftS; - private final transient Supplier<T> rightS; + private final transient Supplier<T> leftSupplier; + private final transient Supplier<T> rightSupplier; - private SDiff(final String fieldName, final Supplier<T> leftS, final Supplier<T> rightS, final Class<T> type) { + private SDiff(final String fieldName, final Supplier<T> leftSupplier, final Supplier<T> rightSupplier, final Class<T> type) { super(fieldName, type); - this.leftS = leftS; - this.rightS = rightS; + this.leftSupplier = Objects.requireNonNull(leftSupplier); + this.rightSupplier = Objects.requireNonNull(rightSupplier); } @Override public T getLeft() { - return leftS.get(); + return leftSupplier.get(); } @Override public T getRight() { - return rightS.get(); + return rightSupplier.get(); } } @@ -257,7 +257,7 @@ public class DiffBuilder<T> implements Builder<DiffResult<T>> { } private <F> DiffBuilder<T> add(final String fieldName, final Supplier<F> left, final Supplier<F> right, final Class<F> type) { - diffs.add(new SDiff<>(Objects.requireNonNull(fieldName, "fieldName"), left, right, type)); + diffs.add(new SDiff<>(fieldName, left, right, type)); return this; } @@ -267,7 +267,7 @@ public class DiffBuilder<T> implements Builder<DiffResult<T>> { * @param fieldName the field name * @param lhs the left-hand side {@code boolean} * @param rhs the right-hand side {@code boolean} - * @return this + * @return {@code this} instance. * @throws NullPointerException if field name is {@code null} */ public DiffBuilder<T> append(final String fieldName, final boolean lhs, final boolean rhs) { @@ -280,7 +280,7 @@ public class DiffBuilder<T> implements Builder<DiffResult<T>> { * @param fieldName the field name * @param lhs the left-hand side {@code boolean[]} * @param rhs the right-hand side {@code boolean[]} - * @return this + * @return {@code this} instance. * @throws NullPointerException if field name is {@code null} */ public DiffBuilder<T> append(final String fieldName, final boolean[] lhs, final boolean[] rhs) { @@ -293,7 +293,7 @@ public class DiffBuilder<T> implements Builder<DiffResult<T>> { * @param fieldName the field name * @param lhs the left-hand side {@code byte} * @param rhs the right-hand side {@code byte} - * @return this + * @return {@code this} instance. * @throws NullPointerException if field name is {@code null} */ public DiffBuilder<T> append(final String fieldName, final byte lhs, final byte rhs) { @@ -306,7 +306,7 @@ public class DiffBuilder<T> implements Builder<DiffResult<T>> { * @param fieldName the field name * @param lhs the left-hand side {@code byte[]} * @param rhs the right-hand side {@code byte[]} - * @return this + * @return {@code this} instance. * @throws NullPointerException if field name is {@code null} */ public DiffBuilder<T> append(final String fieldName, final byte[] lhs, final byte[] rhs) { @@ -319,7 +319,7 @@ public class DiffBuilder<T> implements Builder<DiffResult<T>> { * @param fieldName the field name * @param lhs the left-hand side {@code char} * @param rhs the right-hand side {@code char} - * @return this + * @return {@code this} instance. * @throws NullPointerException if field name is {@code null} */ public DiffBuilder<T> append(final String fieldName, final char lhs, final char rhs) { @@ -332,7 +332,7 @@ public class DiffBuilder<T> implements Builder<DiffResult<T>> { * @param fieldName the field name * @param lhs the left-hand side {@code char[]} * @param rhs the right-hand side {@code char[]} - * @return this + * @return {@code this} instance. * @throws NullPointerException if field name is {@code null} */ public DiffBuilder<T> append(final String fieldName, final char[] lhs, final char[] rhs) { @@ -364,7 +364,7 @@ public class DiffBuilder<T> implements Builder<DiffResult<T>> { * * @param fieldName the field name * @param diffResult the {@link DiffResult} to append - * @return this + * @return {@code this} instance. * @throws NullPointerException if field name is {@code null} or diffResult is {@code null} * @since 3.5 */ @@ -383,7 +383,7 @@ public class DiffBuilder<T> implements Builder<DiffResult<T>> { * @param fieldName the field name * @param lhs the left-hand side {@code double} * @param rhs the right-hand side {@code double} - * @return this + * @return {@code this} instance. * @throws NullPointerException if field name is {@code null} */ public DiffBuilder<T> append(final String fieldName, final double lhs, final double rhs) { @@ -397,7 +397,7 @@ public class DiffBuilder<T> implements Builder<DiffResult<T>> { * @param fieldName the field name * @param lhs the left-hand side {@code double[]} * @param rhs the right-hand side {@code double[]} - * @return this + * @return {@code this} instance. * @throws NullPointerException if field name is {@code null} */ public DiffBuilder<T> append(final String fieldName, final double[] lhs, final double[] rhs) { @@ -410,7 +410,7 @@ public class DiffBuilder<T> implements Builder<DiffResult<T>> { * @param fieldName the field name * @param lhs the left-hand side {@code float} * @param rhs the right-hand side {@code float} - * @return this + * @return {@code this} instance. * @throws NullPointerException if field name is {@code null} */ public DiffBuilder<T> append(final String fieldName, final float lhs, final float rhs) { @@ -424,7 +424,7 @@ public class DiffBuilder<T> implements Builder<DiffResult<T>> { * @param fieldName the field name * @param lhs the left-hand side {@code float[]} * @param rhs the right-hand side {@code float[]} - * @return this + * @return {@code this} instance. * @throws NullPointerException if field name is {@code null} */ public DiffBuilder<T> append(final String fieldName, final float[] lhs, final float[] rhs) { @@ -437,7 +437,7 @@ public class DiffBuilder<T> implements Builder<DiffResult<T>> { * @param fieldName the field name * @param lhs the left-hand side {@code int} * @param rhs the right-hand side {@code int} - * @return this + * @return {@code this} instance. * @throws NullPointerException if field name is {@code null} */ public DiffBuilder<T> append(final String fieldName, final int lhs, final int rhs) { @@ -450,7 +450,7 @@ public class DiffBuilder<T> implements Builder<DiffResult<T>> { * @param fieldName the field name * @param lhs the left-hand side {@code int[]} * @param rhs the right-hand side {@code int[]} - * @return this + * @return {@code this} instance. * @throws NullPointerException if field name is {@code null} */ public DiffBuilder<T> append(final String fieldName, final int[] lhs, final int[] rhs) { @@ -463,7 +463,7 @@ public class DiffBuilder<T> implements Builder<DiffResult<T>> { * @param fieldName the field name * @param lhs the left-hand side {@code long} * @param rhs the right-hand side {@code long} - * @return this + * @return {@code this} instance. * @throws NullPointerException if field name is {@code null} */ public DiffBuilder<T> append(final String fieldName, final long lhs, final long rhs) { @@ -476,7 +476,7 @@ public class DiffBuilder<T> implements Builder<DiffResult<T>> { * @param fieldName the field name * @param lhs the left-hand side {@code long[]} * @param rhs the right-hand side {@code long[]} - * @return this + * @return {@code this} instance. * @throws NullPointerException if field name is {@code null} */ public DiffBuilder<T> append(final String fieldName, final long[] lhs, final long[] rhs) { @@ -489,7 +489,7 @@ public class DiffBuilder<T> implements Builder<DiffResult<T>> { * @param fieldName the field name * @param lhs the left-hand side {@link Object} * @param rhs the right-hand side {@link Object} - * @return this + * @return {@code this} instance. * @throws NullPointerException if field name is {@code null} */ public DiffBuilder<T> append(final String fieldName, final Object lhs, final Object rhs) { @@ -535,7 +535,7 @@ public class DiffBuilder<T> implements Builder<DiffResult<T>> { * @param fieldName the field name * @param lhs the left-hand side {@code Object[]} * @param rhs the right-hand side {@code Object[]} - * @return this + * @return {@code this} instance. * @throws NullPointerException if field name is {@code null} */ public DiffBuilder<T> append(final String fieldName, final Object[] lhs, final Object[] rhs) { @@ -548,7 +548,7 @@ public class DiffBuilder<T> implements Builder<DiffResult<T>> { * @param fieldName the field name * @param lhs the left-hand side {@code short} * @param rhs the right-hand side {@code short} - * @return this + * @return {@code this} instance. * @throws NullPointerException if field name is {@code null} */ public DiffBuilder<T> append(final String fieldName, final short lhs, final short rhs) { @@ -561,7 +561,7 @@ public class DiffBuilder<T> implements Builder<DiffResult<T>> { * @param fieldName the field name * @param lhs the left-hand side {@code short[]} * @param rhs the right-hand side {@code short[]} - * @return this + * @return {@code this} instance. * @throws NullPointerException if field name is {@code null} */ public DiffBuilder<T> append(final String fieldName, final short[] lhs, final short[] rhs) { diff --git a/src/main/java/org/apache/commons/lang3/builder/EqualsBuilder.java b/src/main/java/org/apache/commons/lang3/builder/EqualsBuilder.java index bae95f9d4..a4adb1485 100644 --- a/src/main/java/org/apache/commons/lang3/builder/EqualsBuilder.java +++ b/src/main/java/org/apache/commons/lang3/builder/EqualsBuilder.java @@ -384,7 +384,7 @@ public class EqualsBuilder implements Builder<Boolean> { * * @param lhs the left-hand side {@code boolean} * @param rhs the right-hand side {@code boolean} - * @return this + * @return {@code this} instance. */ public EqualsBuilder append(final boolean lhs, final boolean rhs) { if (!isEquals) { @@ -402,7 +402,7 @@ public class EqualsBuilder implements Builder<Boolean> { * * @param lhs the left-hand side {@code boolean[]} * @param rhs the right-hand side {@code boolean[]} - * @return this + * @return {@code this} instance. */ public EqualsBuilder append(final boolean[] lhs, final boolean[] rhs) { if (!isEquals) { @@ -430,7 +430,7 @@ public class EqualsBuilder implements Builder<Boolean> { * * @param lhs the left-hand side {@code byte} * @param rhs the right-hand side {@code byte} - * @return this + * @return {@code this} instance. */ public EqualsBuilder append(final byte lhs, final byte rhs) { if (isEquals) { @@ -447,7 +447,7 @@ public class EqualsBuilder implements Builder<Boolean> { * * @param lhs the left-hand side {@code byte[]} * @param rhs the right-hand side {@code byte[]} - * @return this + * @return {@code this} instance. */ public EqualsBuilder append(final byte[] lhs, final byte[] rhs) { if (!isEquals) { @@ -475,7 +475,7 @@ public class EqualsBuilder implements Builder<Boolean> { * * @param lhs the left-hand side {@code char} * @param rhs the right-hand side {@code char} - * @return this + * @return {@code this} instance. */ public EqualsBuilder append(final char lhs, final char rhs) { if (isEquals) { @@ -492,7 +492,7 @@ public class EqualsBuilder implements Builder<Boolean> { * * @param lhs the left-hand side {@code char[]} * @param rhs the right-hand side {@code char[]} - * @return this + * @return {@code this} instance. */ public EqualsBuilder append(final char[] lhs, final char[] rhs) { if (!isEquals) { @@ -526,7 +526,7 @@ public class EqualsBuilder implements Builder<Boolean> { * * @param lhs the left-hand side {@code double} * @param rhs the right-hand side {@code double} - * @return this + * @return {@code this} instance. */ public EqualsBuilder append(final double lhs, final double rhs) { if (isEquals) { @@ -543,7 +543,7 @@ public class EqualsBuilder implements Builder<Boolean> { * * @param lhs the left-hand side {@code double[]} * @param rhs the right-hand side {@code double[]} - * @return this + * @return {@code this} instance. */ public EqualsBuilder append(final double[] lhs, final double[] rhs) { if (!isEquals) { @@ -577,7 +577,7 @@ public class EqualsBuilder implements Builder<Boolean> { * * @param lhs the left-hand side {@code float} * @param rhs the right-hand side {@code float} - * @return this + * @return {@code this} instance. */ public EqualsBuilder append(final float lhs, final float rhs) { if (isEquals) { @@ -594,7 +594,7 @@ public class EqualsBuilder implements Builder<Boolean> { * * @param lhs the left-hand side {@code float[]} * @param rhs the right-hand side {@code float[]} - * @return this + * @return {@code this} instance. */ public EqualsBuilder append(final float[] lhs, final float[] rhs) { if (!isEquals) { @@ -622,7 +622,7 @@ public class EqualsBuilder implements Builder<Boolean> { * * @param lhs the left-hand side {@code int} * @param rhs the right-hand side {@code int} - * @return this + * @return {@code this} instance. */ public EqualsBuilder append(final int lhs, final int rhs) { if (isEquals) { @@ -639,7 +639,7 @@ public class EqualsBuilder implements Builder<Boolean> { * * @param lhs the left-hand side {@code int[]} * @param rhs the right-hand side {@code int[]} - * @return this + * @return {@code this} instance. */ public EqualsBuilder append(final int[] lhs, final int[] rhs) { if (!isEquals) { @@ -669,7 +669,7 @@ public class EqualsBuilder implements Builder<Boolean> { * the left-hand side {@code long} * @param rhs * the right-hand side {@code long} - * @return this + * @return {@code this} instance. */ public EqualsBuilder append(final long lhs, final long rhs) { if (isEquals) { @@ -686,7 +686,7 @@ public class EqualsBuilder implements Builder<Boolean> { * * @param lhs the left-hand side {@code long[]} * @param rhs the right-hand side {@code long[]} - * @return this + * @return {@code this} instance. */ public EqualsBuilder append(final long[] lhs, final long[] rhs) { if (!isEquals) { @@ -718,7 +718,7 @@ public class EqualsBuilder implements Builder<Boolean> { * * @param lhs the left-hand side object * @param rhs the right-hand side object - * @return this + * @return {@code this} instance. */ public EqualsBuilder append(final Object lhs, final Object rhs) { if (!isEquals) { @@ -756,7 +756,7 @@ public class EqualsBuilder implements Builder<Boolean> { * * @param lhs the left-hand side {@code Object[]} * @param rhs the right-hand side {@code Object[]} - * @return this + * @return {@code this} instance. */ public EqualsBuilder append(final Object[] lhs, final Object[] rhs) { if (!isEquals) { @@ -784,7 +784,7 @@ public class EqualsBuilder implements Builder<Boolean> { * * @param lhs the left-hand side {@code short} * @param rhs the right-hand side {@code short} - * @return this + * @return {@code this} instance. */ public EqualsBuilder append(final short lhs, final short rhs) { if (isEquals) { @@ -801,7 +801,7 @@ public class EqualsBuilder implements Builder<Boolean> { * * @param lhs the left-hand side {@code short[]} * @param rhs the right-hand side {@code short[]} - * @return this + * @return {@code this} instance. */ public EqualsBuilder append(final short[] lhs, final short[] rhs) { if (!isEquals) { @@ -862,7 +862,7 @@ public class EqualsBuilder implements Builder<Boolean> { * Adds the result of {@code super.equals()} to this builder. * * @param superEquals the result of calling {@code super.equals()} - * @return this + * @return {@code this} instance. * @since 2.0 */ public EqualsBuilder appendSuper(final boolean superEquals) { @@ -922,7 +922,7 @@ public class EqualsBuilder implements Builder<Boolean> { * * @param lhs the left-hand side object * @param rhs the right-hand side object - * @return this + * @return {@code this} instance. */ public EqualsBuilder reflectionAppend(final Object lhs, final Object rhs) { if (!isEquals) { @@ -1041,7 +1041,7 @@ public class EqualsBuilder implements Builder<Boolean> { * your own set of classes here, remember to include {@link String} class, too.</p> * * @param bypassReflectionClasses classes to bypass reflection test - * @return this + * @return {@code this} instance. * @see #setTestRecursive(boolean) * @since 3.8 */ @@ -1064,7 +1064,7 @@ public class EqualsBuilder implements Builder<Boolean> { * Sets field names to be excluded by reflection tests. * * @param excludeFields the fields to exclude - * @return this + * @return {@code this} instance. * @since 3.6 */ public EqualsBuilder setExcludeFields(final String... excludeFields) { @@ -1076,7 +1076,7 @@ public class EqualsBuilder implements Builder<Boolean> { * Sets the superclass to reflect up to at reflective tests. * * @param reflectUpToClass the super class to reflect up to - * @return this + * @return {@code this} instance. * @since 3.6 */ public EqualsBuilder setReflectUpToClass(final Class<?> reflectUpToClass) { @@ -1090,7 +1090,7 @@ public class EqualsBuilder implements Builder<Boolean> { * You may specify other exceptions by calling {@link #setBypassReflectionClasses(List)}. * * @param testRecursive whether to do a recursive test - * @return this + * @return {@code this} instance. * @see #setBypassReflectionClasses(List) * @since 3.6 */ @@ -1103,7 +1103,7 @@ public class EqualsBuilder implements Builder<Boolean> { * Sets whether to include transient fields when reflectively comparing objects. * * @param testTransients whether to test transient fields - * @return this + * @return {@code this} instance. * @since 3.6 */ public EqualsBuilder setTestTransients(final boolean testTransients) { 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 ad258a061..c347ed654 100644 --- a/src/main/java/org/apache/commons/lang3/builder/HashCodeBuilder.java +++ b/src/main/java/org/apache/commons/lang3/builder/HashCodeBuilder.java @@ -547,7 +547,7 @@ public class HashCodeBuilder implements Builder<Integer> { * * @param value * the boolean to add to the {@code hashCode} - * @return this + * @return {@code this} instance. */ public HashCodeBuilder append(final boolean value) { iTotal = iTotal * iConstant + (value ? 0 : 1); @@ -559,7 +559,7 @@ public class HashCodeBuilder implements Builder<Integer> { * * @param array * the array to add to the {@code hashCode} - * @return this + * @return {@code this} instance. */ public HashCodeBuilder append(final boolean[] array) { if (array == null) { @@ -577,7 +577,7 @@ public class HashCodeBuilder implements Builder<Integer> { * * @param value * the byte to add to the {@code hashCode} - * @return this + * @return {@code this} instance. */ public HashCodeBuilder append(final byte value) { iTotal = iTotal * iConstant + value; @@ -589,7 +589,7 @@ public class HashCodeBuilder implements Builder<Integer> { * * @param array * the array to add to the {@code hashCode} - * @return this + * @return {@code this} instance. */ public HashCodeBuilder append(final byte[] array) { if (array == null) { @@ -607,7 +607,7 @@ public class HashCodeBuilder implements Builder<Integer> { * * @param value * the char to add to the {@code hashCode} - * @return this + * @return {@code this} instance. */ public HashCodeBuilder append(final char value) { iTotal = iTotal * iConstant + value; @@ -619,7 +619,7 @@ public class HashCodeBuilder implements Builder<Integer> { * * @param array * the array to add to the {@code hashCode} - * @return this + * @return {@code this} instance. */ public HashCodeBuilder append(final char[] array) { if (array == null) { @@ -637,7 +637,7 @@ public class HashCodeBuilder implements Builder<Integer> { * * @param value * the double to add to the {@code hashCode} - * @return this + * @return {@code this} instance. */ public HashCodeBuilder append(final double value) { return append(Double.doubleToLongBits(value)); @@ -648,7 +648,7 @@ public class HashCodeBuilder implements Builder<Integer> { * * @param array * the array to add to the {@code hashCode} - * @return this + * @return {@code this} instance. */ public HashCodeBuilder append(final double[] array) { if (array == null) { @@ -666,7 +666,7 @@ public class HashCodeBuilder implements Builder<Integer> { * * @param value * the float to add to the {@code hashCode} - * @return this + * @return {@code this} instance. */ public HashCodeBuilder append(final float value) { iTotal = iTotal * iConstant + Float.floatToIntBits(value); @@ -678,7 +678,7 @@ public class HashCodeBuilder implements Builder<Integer> { * * @param array * the array to add to the {@code hashCode} - * @return this + * @return {@code this} instance. */ public HashCodeBuilder append(final float[] array) { if (array == null) { @@ -696,7 +696,7 @@ public class HashCodeBuilder implements Builder<Integer> { * * @param value * the int to add to the {@code hashCode} - * @return this + * @return {@code this} instance. */ public HashCodeBuilder append(final int value) { iTotal = iTotal * iConstant + value; @@ -708,7 +708,7 @@ public class HashCodeBuilder implements Builder<Integer> { * * @param array * the array to add to the {@code hashCode} - * @return this + * @return {@code this} instance. */ public HashCodeBuilder append(final int[] array) { if (array == null) { @@ -726,7 +726,7 @@ public class HashCodeBuilder implements Builder<Integer> { * * @param value * the long to add to the {@code hashCode} - * @return this + * @return {@code this} instance. */ // NOTE: This method uses >> and not >>> as Effective Java and // Long.hashCode do. Ideally we should switch to >>> at @@ -742,7 +742,7 @@ public class HashCodeBuilder implements Builder<Integer> { * * @param array * the array to add to the {@code hashCode} - * @return this + * @return {@code this} instance. */ public HashCodeBuilder append(final long[] array) { if (array == null) { @@ -760,7 +760,7 @@ public class HashCodeBuilder implements Builder<Integer> { * * @param object * the Object to add to the {@code hashCode} - * @return this + * @return {@code this} instance. */ public HashCodeBuilder append(final Object object) { if (object == null) { @@ -781,7 +781,7 @@ public class HashCodeBuilder implements Builder<Integer> { * * @param array * the array to add to the {@code hashCode} - * @return this + * @return {@code this} instance. */ public HashCodeBuilder append(final Object[] array) { if (array == null) { @@ -799,7 +799,7 @@ public class HashCodeBuilder implements Builder<Integer> { * * @param value * the short to add to the {@code hashCode} - * @return this + * @return {@code this} instance. */ public HashCodeBuilder append(final short value) { iTotal = iTotal * iConstant + value; @@ -811,7 +811,7 @@ public class HashCodeBuilder implements Builder<Integer> { * * @param array * the array to add to the {@code hashCode} - * @return this + * @return {@code this} instance. */ public HashCodeBuilder append(final short[] array) { if (array == null) { @@ -860,7 +860,7 @@ public class HashCodeBuilder implements Builder<Integer> { * * @param superHashCode * the result of calling {@code super.hashCode()} - * @return this + * @return {@code this} instance. * @since 2.0 */ public HashCodeBuilder appendSuper(final int superHashCode) { diff --git a/src/main/java/org/apache/commons/lang3/builder/MultilineRecursiveToStringStyle.java b/src/main/java/org/apache/commons/lang3/builder/MultilineRecursiveToStringStyle.java index 861a55fe2..54cb160c7 100644 --- a/src/main/java/org/apache/commons/lang3/builder/MultilineRecursiveToStringStyle.java +++ b/src/main/java/org/apache/commons/lang3/builder/MultilineRecursiveToStringStyle.java @@ -18,6 +18,7 @@ package org.apache.commons.lang3.builder; import org.apache.commons.lang3.ClassUtils; +import org.apache.commons.lang3.StringUtils; /** * Works with {@link ToStringBuilder} to create a "deep" {@code toString}. @@ -206,12 +207,8 @@ public class MultilineRecursiveToStringStyle extends RecursiveToStringStyle { * @param spaces how far to indent * @return a StringBuilder with {spaces} leading space characters. */ - private StringBuilder spacer(final int spaces) { - final StringBuilder sb = new StringBuilder(); - for (int i = 0; i < spaces; i++) { - sb.append(" "); - } - return sb; + private String spacer(final int spaces) { + return StringUtils.repeat(' ', spaces); } } diff --git a/src/main/java/org/apache/commons/lang3/builder/ReflectionDiffBuilder.java b/src/main/java/org/apache/commons/lang3/builder/ReflectionDiffBuilder.java index 05f08d597..222cd249b 100644 --- a/src/main/java/org/apache/commons/lang3/builder/ReflectionDiffBuilder.java +++ b/src/main/java/org/apache/commons/lang3/builder/ReflectionDiffBuilder.java @@ -99,7 +99,7 @@ public class ReflectionDiffBuilder<T> implements Builder<DiffResult<T>> { * Sets the DiffBuilder. * * @param diffBuilder the DiffBuilder. - * @return this. + * @return {@code this} instance. */ public Builder<T> setDiffBuilder(final DiffBuilder<T> diffBuilder) { this.diffBuilder = diffBuilder; @@ -110,7 +110,7 @@ public class ReflectionDiffBuilder<T> implements Builder<DiffResult<T>> { * Sets field names to exclude from output. Intended for fields like {@code "password"} or {@code "lastModificationDate"}. * * @param excludeFieldNames field names to exclude. - * @return this. + * @return {@code this} instance. */ public Builder<T> setExcludeFieldNames(final String... excludeFieldNames) { this.excludeFieldNames = toExcludeFieldNames(excludeFieldNames); 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 7950a208e..e6c83b8e0 100644 --- a/src/main/java/org/apache/commons/lang3/builder/ReflectionToStringBuilder.java +++ b/src/main/java/org/apache/commons/lang3/builder/ReflectionToStringBuilder.java @@ -746,7 +746,7 @@ public class ReflectionToStringBuilder extends ToStringBuilder { * * @param array * the array to add to the {@code toString} - * @return this + * @return {@code this} instance. */ public ReflectionToStringBuilder reflectionAppendArray(final Object array) { this.getStyle().reflectionAppendArrayDetail(this.getStringBuffer(), null, array); 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 f08eaa45e..3fa79db66 100644 --- a/src/main/java/org/apache/commons/lang3/builder/ToStringBuilder.java +++ b/src/main/java/org/apache/commons/lang3/builder/ToStringBuilder.java @@ -261,7 +261,7 @@ public class ToStringBuilder implements Builder<String> { * value. * * @param value the value to add to the {@code toString} - * @return this + * @return {@code this} instance. */ public ToStringBuilder append(final boolean value) { style.append(buffer, null, value); @@ -273,7 +273,7 @@ public class ToStringBuilder implements Builder<String> { * array. * * @param array the array to add to the {@code toString} - * @return this + * @return {@code this} instance. */ public ToStringBuilder append(final boolean[] array) { style.append(buffer, null, array, null); @@ -285,7 +285,7 @@ public class ToStringBuilder implements Builder<String> { * value. * * @param value the value to add to the {@code toString} - * @return this + * @return {@code this} instance. */ public ToStringBuilder append(final byte value) { style.append(buffer, null, value); @@ -297,7 +297,7 @@ public class ToStringBuilder implements Builder<String> { * array. * * @param array the array to add to the {@code toString} - * @return this + * @return {@code this} instance. */ public ToStringBuilder append(final byte[] array) { style.append(buffer, null, array, null); @@ -309,7 +309,7 @@ public class ToStringBuilder implements Builder<String> { * value. * * @param value the value to add to the {@code toString} - * @return this + * @return {@code this} instance. */ public ToStringBuilder append(final char value) { style.append(buffer, null, value); @@ -321,7 +321,7 @@ public class ToStringBuilder implements Builder<String> { * array. * * @param array the array to add to the {@code toString} - * @return this + * @return {@code this} instance. */ public ToStringBuilder append(final char[] array) { style.append(buffer, null, array, null); @@ -333,7 +333,7 @@ public class ToStringBuilder implements Builder<String> { * value. * * @param value the value to add to the {@code toString} - * @return this + * @return {@code this} instance. */ public ToStringBuilder append(final double value) { style.append(buffer, null, value); @@ -345,7 +345,7 @@ public class ToStringBuilder implements Builder<String> { * array. * * @param array the array to add to the {@code toString} - * @return this + * @return {@code this} instance. */ public ToStringBuilder append(final double[] array) { style.append(buffer, null, array, null); @@ -357,7 +357,7 @@ public class ToStringBuilder implements Builder<String> { * value. * * @param value the value to add to the {@code toString} - * @return this + * @return {@code this} instance. */ public ToStringBuilder append(final float value) { style.append(buffer, null, value); @@ -369,7 +369,7 @@ public class ToStringBuilder implements Builder<String> { * array. * * @param array the array to add to the {@code toString} - * @return this + * @return {@code this} instance. */ public ToStringBuilder append(final float[] array) { style.append(buffer, null, array, null); @@ -381,7 +381,7 @@ public class ToStringBuilder implements Builder<String> { * value. * * @param value the value to add to the {@code toString} - * @return this + * @return {@code this} instance. */ public ToStringBuilder append(final int value) { style.append(buffer, null, value); @@ -393,7 +393,7 @@ public class ToStringBuilder implements Builder<String> { * array. * * @param array the array to add to the {@code toString} - * @return this + * @return {@code this} instance. */ public ToStringBuilder append(final int[] array) { style.append(buffer, null, array, null); @@ -405,7 +405,7 @@ public class ToStringBuilder implements Builder<String> { * value. * * @param value the value to add to the {@code toString} - * @return this + * @return {@code this} instance. */ public ToStringBuilder append(final long value) { style.append(buffer, null, value); @@ -417,7 +417,7 @@ public class ToStringBuilder implements Builder<String> { * array. * * @param array the array to add to the {@code toString} - * @return this + * @return {@code this} instance. */ public ToStringBuilder append(final long[] array) { style.append(buffer, null, array, null); @@ -429,7 +429,7 @@ public class ToStringBuilder implements Builder<String> { * value. * * @param obj the value to add to the {@code toString} - * @return this + * @return {@code this} instance. */ public ToStringBuilder append(final Object obj) { style.append(buffer, null, obj, null); @@ -441,7 +441,7 @@ public class ToStringBuilder implements Builder<String> { * array. * * @param array the array to add to the {@code toString} - * @return this + * @return {@code this} instance. */ public ToStringBuilder append(final Object[] array) { style.append(buffer, null, array, null); @@ -453,7 +453,7 @@ public class ToStringBuilder implements Builder<String> { * value. * * @param value the value to add to the {@code toString} - * @return this + * @return {@code this} instance. */ public ToStringBuilder append(final short value) { style.append(buffer, null, value); @@ -465,7 +465,7 @@ public class ToStringBuilder implements Builder<String> { * array. * * @param array the array to add to the {@code toString} - * @return this + * @return {@code this} instance. */ public ToStringBuilder append(final short[] array) { style.append(buffer, null, array, null); @@ -478,7 +478,7 @@ public class ToStringBuilder implements Builder<String> { * * @param fieldName the field name * @param value the value to add to the {@code toString} - * @return this + * @return {@code this} instance. */ public ToStringBuilder append(final String fieldName, final boolean value) { style.append(buffer, fieldName, value); @@ -491,7 +491,7 @@ public class ToStringBuilder implements Builder<String> { * * @param fieldName the field name * @param array the array to add to the {@code hashCode} - * @return this + * @return {@code this} instance. */ public ToStringBuilder append(final String fieldName, final boolean[] array) { style.append(buffer, fieldName, array, null); @@ -511,7 +511,7 @@ public class ToStringBuilder implements Builder<String> { * @param array the array to add to the {@code toString} * @param fullDetail {@code true} for detail, {@code false} * for summary info - * @return this + * @return {@code this} instance. */ public ToStringBuilder append(final String fieldName, final boolean[] array, final boolean fullDetail) { style.append(buffer, fieldName, array, Boolean.valueOf(fullDetail)); @@ -524,7 +524,7 @@ public class ToStringBuilder implements Builder<String> { * * @param fieldName the field name * @param value the value to add to the {@code toString} - * @return this + * @return {@code this} instance. */ public ToStringBuilder append(final String fieldName, final byte value) { style.append(buffer, fieldName, value); @@ -536,7 +536,7 @@ public class ToStringBuilder implements Builder<String> { * * @param fieldName the field name * @param array the array to add to the {@code toString} - * @return this + * @return {@code this} instance. */ public ToStringBuilder append(final String fieldName, final byte[] array) { style.append(buffer, fieldName, array, null); @@ -556,7 +556,7 @@ public class ToStringBuilder implements Builder<String> { * @param array the array to add to the {@code toString} * @param fullDetail {@code true} for detail, {@code false} * for summary info - * @return this + * @return {@code this} instance. */ public ToStringBuilder append(final String fieldName, final byte[] array, final boolean fullDetail) { style.append(buffer, fieldName, array, Boolean.valueOf(fullDetail)); @@ -569,7 +569,7 @@ public class ToStringBuilder implements Builder<String> { * * @param fieldName the field name * @param value the value to add to the {@code toString} - * @return this + * @return {@code this} instance. */ public ToStringBuilder append(final String fieldName, final char value) { style.append(buffer, fieldName, value); @@ -582,7 +582,7 @@ public class ToStringBuilder implements Builder<String> { * * @param fieldName the field name * @param array the array to add to the {@code toString} - * @return this + * @return {@code this} instance. */ public ToStringBuilder append(final String fieldName, final char[] array) { style.append(buffer, fieldName, array, null); @@ -602,7 +602,7 @@ public class ToStringBuilder implements Builder<String> { * @param array the array to add to the {@code toString} * @param fullDetail {@code true} for detail, {@code false} * for summary info - * @return this + * @return {@code this} instance. */ public ToStringBuilder append(final String fieldName, final char[] array, final boolean fullDetail) { style.append(buffer, fieldName, array, Boolean.valueOf(fullDetail)); @@ -615,7 +615,7 @@ public class ToStringBuilder implements Builder<String> { * * @param fieldName the field name * @param value the value to add to the {@code toString} - * @return this + * @return {@code this} instance. */ public ToStringBuilder append(final String fieldName, final double value) { style.append(buffer, fieldName, value); @@ -628,7 +628,7 @@ public class ToStringBuilder implements Builder<String> { * * @param fieldName the field name * @param array the array to add to the {@code toString} - * @return this + * @return {@code this} instance. */ public ToStringBuilder append(final String fieldName, final double[] array) { style.append(buffer, fieldName, array, null); @@ -648,7 +648,7 @@ public class ToStringBuilder implements Builder<String> { * @param array the array to add to the {@code toString} * @param fullDetail {@code true} for detail, {@code false} * for summary info - * @return this + * @return {@code this} instance. */ public ToStringBuilder append(final String fieldName, final double[] array, final boolean fullDetail) { style.append(buffer, fieldName, array, Boolean.valueOf(fullDetail)); @@ -661,7 +661,7 @@ public class ToStringBuilder implements Builder<String> { * * @param fieldName the field name * @param value the value to add to the {@code toString} - * @return this + * @return {@code this} instance. */ public ToStringBuilder append(final String fieldName, final float value) { style.append(buffer, fieldName, value); @@ -674,7 +674,7 @@ public class ToStringBuilder implements Builder<String> { * * @param fieldName the field name * @param array the array to add to the {@code toString} - * @return this + * @return {@code this} instance. */ public ToStringBuilder append(final String fieldName, final float[] array) { style.append(buffer, fieldName, array, null); @@ -694,7 +694,7 @@ public class ToStringBuilder implements Builder<String> { * @param array the array to add to the {@code toString} * @param fullDetail {@code true} for detail, {@code false} * for summary info - * @return this + * @return {@code this} instance. */ public ToStringBuilder append(final String fieldName, final float[] array, final boolean fullDetail) { style.append(buffer, fieldName, array, Boolean.valueOf(fullDetail)); @@ -707,7 +707,7 @@ public class ToStringBuilder implements Builder<String> { * * @param fieldName the field name * @param value the value to add to the {@code toString} - * @return this + * @return {@code this} instance. */ public ToStringBuilder append(final String fieldName, final int value) { style.append(buffer, fieldName, value); @@ -720,7 +720,7 @@ public class ToStringBuilder implements Builder<String> { * * @param fieldName the field name * @param array the array to add to the {@code toString} - * @return this + * @return {@code this} instance. */ public ToStringBuilder append(final String fieldName, final int[] array) { style.append(buffer, fieldName, array, null); @@ -740,7 +740,7 @@ public class ToStringBuilder implements Builder<String> { * @param array the array to add to the {@code toString} * @param fullDetail {@code true} for detail, {@code false} * for summary info - * @return this + * @return {@code this} instance. */ public ToStringBuilder append(final String fieldName, final int[] array, final boolean fullDetail) { style.append(buffer, fieldName, array, Boolean.valueOf(fullDetail)); @@ -753,7 +753,7 @@ public class ToStringBuilder implements Builder<String> { * * @param fieldName the field name * @param value the value to add to the {@code toString} - * @return this + * @return {@code this} instance. */ public ToStringBuilder append(final String fieldName, final long value) { style.append(buffer, fieldName, value); @@ -766,7 +766,7 @@ public class ToStringBuilder implements Builder<String> { * * @param fieldName the field name * @param array the array to add to the {@code toString} - * @return this + * @return {@code this} instance. */ public ToStringBuilder append(final String fieldName, final long[] array) { style.append(buffer, fieldName, array, null); @@ -786,7 +786,7 @@ public class ToStringBuilder implements Builder<String> { * @param array the array to add to the {@code toString} * @param fullDetail {@code true} for detail, {@code false} * for summary info - * @return this + * @return {@code this} instance. */ public ToStringBuilder append(final String fieldName, final long[] array, final boolean fullDetail) { style.append(buffer, fieldName, array, Boolean.valueOf(fullDetail)); @@ -799,7 +799,7 @@ public class ToStringBuilder implements Builder<String> { * * @param fieldName the field name * @param obj the value to add to the {@code toString} - * @return this + * @return {@code this} instance. */ public ToStringBuilder append(final String fieldName, final Object obj) { style.append(buffer, fieldName, obj, null); @@ -814,7 +814,7 @@ public class ToStringBuilder implements Builder<String> { * @param obj the value to add to the {@code toString} * @param fullDetail {@code true} for detail, * {@code false} for summary info - * @return this + * @return {@code this} instance. */ public ToStringBuilder append(final String fieldName, final Object obj, final boolean fullDetail) { style.append(buffer, fieldName, obj, Boolean.valueOf(fullDetail)); @@ -827,7 +827,7 @@ public class ToStringBuilder implements Builder<String> { * * @param fieldName the field name * @param array the array to add to the {@code toString} - * @return this + * @return {@code this} instance. */ public ToStringBuilder append(final String fieldName, final Object[] array) { style.append(buffer, fieldName, array, null); @@ -847,7 +847,7 @@ public class ToStringBuilder implements Builder<String> { * @param array the array to add to the {@code toString} * @param fullDetail {@code true} for detail, {@code false} * for summary info - * @return this + * @return {@code this} instance. */ public ToStringBuilder append(final String fieldName, final Object[] array, final boolean fullDetail) { style.append(buffer, fieldName, array, Boolean.valueOf(fullDetail)); @@ -860,7 +860,7 @@ public class ToStringBuilder implements Builder<String> { * * @param fieldName the field name * @param value the value to add to the {@code toString} - * @return this + * @return {@code this} instance. */ public ToStringBuilder append(final String fieldName, final short value) { style.append(buffer, fieldName, value); @@ -873,7 +873,7 @@ public class ToStringBuilder implements Builder<String> { * * @param fieldName the field name * @param array the array to add to the {@code toString} - * @return this + * @return {@code this} instance. */ public ToStringBuilder append(final String fieldName, final short[] array) { style.append(buffer, fieldName, array, null); @@ -893,7 +893,7 @@ public class ToStringBuilder implements Builder<String> { * @param array the array to add to the {@code toString} * @param fullDetail {@code true} for detail, {@code false} * for summary info - * @return this + * @return {@code this} instance. */ public ToStringBuilder append(final String fieldName, final short[] array, final boolean fullDetail) { style.append(buffer, fieldName, array, Boolean.valueOf(fullDetail)); @@ -906,7 +906,7 @@ public class ToStringBuilder implements Builder<String> { * {@link System#identityHashCode(Object)}. * * @param srcObject the {@link Object} whose class name and id to output - * @return this + * @return {@code this} instance. * @throws NullPointerException if {@code srcObject} is {@code null} * @since 2.0 */ @@ -924,7 +924,7 @@ public class ToStringBuilder implements Builder<String> { * <p>If {@code superToString} is {@code null}, no change is made.</p> * * @param superToString the result of {@code super.toString()} - * @return this + * @return {@code this} instance. * @since 2.0 */ public ToStringBuilder appendSuper(final String superToString) { @@ -958,7 +958,7 @@ public class ToStringBuilder implements Builder<String> { * <p>If the {@code toString} is {@code null}, no change is made.</p> * * @param toString the result of {@code toString()} on another object - * @return this + * @return {@code this} instance. * @since 2.0 */ public ToStringBuilder appendToString(final String toString) { diff --git a/src/main/java/org/apache/commons/lang3/concurrent/AbstractConcurrentInitializer.java b/src/main/java/org/apache/commons/lang3/concurrent/AbstractConcurrentInitializer.java index 003ea0ad7..7769b38af 100644 --- a/src/main/java/org/apache/commons/lang3/concurrent/AbstractConcurrentInitializer.java +++ b/src/main/java/org/apache/commons/lang3/concurrent/AbstractConcurrentInitializer.java @@ -25,7 +25,7 @@ import org.apache.commons.lang3.function.FailableConsumer; import org.apache.commons.lang3.function.FailableSupplier; /** - * Abstracts and defines operations for ConcurrentInitializer implementations. + * Abstracts and defines operations for {@link ConcurrentInitializer} implementations. * * @param <T> the type of the object managed by this initializer class. * @param <E> The exception type thrown by {@link #initialize()}. @@ -76,7 +76,7 @@ public abstract class AbstractConcurrentInitializer<T, E extends Exception> impl * Sets the closer consumer called by {@link #close()}. * * @param closer the consumer called by {@link #close()}. - * @return this + * @return {@code this} instance. */ public B setCloser(final FailableConsumer<T, ? extends Exception> closer) { this.closer = closer != null ? closer : FailableConsumer.nop(); @@ -87,7 +87,7 @@ public abstract class AbstractConcurrentInitializer<T, E extends Exception> impl * Sets the initializer supplier called by {@link #initialize()}. * * @param initializer the supplier called by {@link #initialize()}. - * @return this + * @return {@code this} instance. */ public B setInitializer(final FailableSupplier<T, ? extends Exception> initializer) { this.initializer = initializer != null ? initializer : FailableSupplier.nul(); diff --git a/src/main/java/org/apache/commons/lang3/concurrent/BackgroundInitializer.java b/src/main/java/org/apache/commons/lang3/concurrent/BackgroundInitializer.java index 32a02b01d..4c7a828ed 100644 --- a/src/main/java/org/apache/commons/lang3/concurrent/BackgroundInitializer.java +++ b/src/main/java/org/apache/commons/lang3/concurrent/BackgroundInitializer.java @@ -113,7 +113,7 @@ public class BackgroundInitializer<T> extends AbstractConcurrentInitializer<T, E * @see org.apache.commons.lang3.concurrent.BackgroundInitializer#setExternalExecutor(ExecutorService) * * @param externalExecutor the {@link ExecutorService} to be used. - * @return this + * @return {@code this} instance. */ public Builder<I, T> setExternalExecutor(final ExecutorService externalExecutor) { this.externalExecutor = externalExecutor; diff --git a/src/main/java/org/apache/commons/lang3/concurrent/locks/LockingVisitors.java b/src/main/java/org/apache/commons/lang3/concurrent/locks/LockingVisitors.java index 400eb1a77..d83f52350 100644 --- a/src/main/java/org/apache/commons/lang3/concurrent/locks/LockingVisitors.java +++ b/src/main/java/org/apache/commons/lang3/concurrent/locks/LockingVisitors.java @@ -26,6 +26,7 @@ import java.util.function.Supplier; import org.apache.commons.lang3.function.Failable; import org.apache.commons.lang3.function.FailableConsumer; import org.apache.commons.lang3.function.FailableFunction; +import org.apache.commons.lang3.function.Suppliers; /** * Combines the monitor and visitor pattern to work with {@link java.util.concurrent.locks.Lock locked objects}. Locked @@ -262,10 +263,12 @@ public class LockingVisitors { * @see #acceptWriteLocked(FailableConsumer) */ protected void lockAcceptUnlock(final Supplier<Lock> lockSupplier, final FailableConsumer<O, ?> consumer) { - final Lock lock = lockSupplier.get(); + final Lock lock = Objects.requireNonNull(Suppliers.get(lockSupplier), "lock"); lock.lock(); try { - consumer.accept(object); + if (consumer != null) { + consumer.accept(object); + } } catch (final Throwable t) { throw Failable.rethrow(t); } finally { @@ -289,7 +292,7 @@ public class LockingVisitors { * @see #applyWriteLocked(FailableFunction) */ protected <T> T lockApplyUnlock(final Supplier<Lock> lockSupplier, final FailableFunction<O, T, ?> function) { - final Lock lock = lockSupplier.get(); + final Lock lock = Objects.requireNonNull(Suppliers.get(lockSupplier), "lock"); lock.lock(); try { return function.apply(object); diff --git a/src/main/java/org/apache/commons/lang3/event/EventListenerSupport.java b/src/main/java/org/apache/commons/lang3/event/EventListenerSupport.java index c0ac4301c..08be680e7 100644 --- a/src/main/java/org/apache/commons/lang3/event/EventListenerSupport.java +++ b/src/main/java/org/apache/commons/lang3/event/EventListenerSupport.java @@ -175,7 +175,7 @@ public class EventListenerSupport<L> implements Serializable { private transient L[] prototypeArray; /** - * Create a new EventListenerSupport instance. + * Constructs a new EventListenerSupport instance. * Serialization-friendly constructor. */ private EventListenerSupport() { @@ -219,10 +219,6 @@ public class EventListenerSupport<L> implements Serializable { initializeTransientFields(listenerInterface, classLoader); } -//********************************************************************************************************************** -// Other Methods -//********************************************************************************************************************** - /** * Registers an event listener. * @@ -236,7 +232,7 @@ public class EventListenerSupport<L> implements Serializable { } /** - * Registers an event listener. Will not add a pre-existing listener + * Registers an event listener. Will not add a pre-existing listener * object to the list if {@code allowDuplicate} is false. * * @param listener the event listener (may not be {@code null}). @@ -254,8 +250,9 @@ public class EventListenerSupport<L> implements Serializable { } /** - * Create the {@link InvocationHandler} responsible for broadcasting calls - * to the managed listeners. Subclasses can override to provide custom behavior. + * Creates the {@link InvocationHandler} responsible for broadcasting calls + * to the managed listeners. Subclasses can override to provide custom behavior. + * * @return ProxyInvocationHandler */ protected InvocationHandler createInvocationHandler() { @@ -263,7 +260,8 @@ public class EventListenerSupport<L> implements Serializable { } /** - * Create the proxy object. + * Creates the proxy object. + * * @param listenerInterface the class of the listener interface * @param classLoader the class loader to be used */ @@ -285,7 +283,7 @@ public class EventListenerSupport<L> implements Serializable { } /** - * Returns the number of registered listeners. + * Gets the number of registered listeners. * * @return the number of registered listeners. */ @@ -304,7 +302,8 @@ public class EventListenerSupport<L> implements Serializable { } /** - * Initialize transient fields. + * Initializes transient fields. + * * @param listenerInterface the class of the listener interface * @param classLoader the class loader to be used */ @@ -315,7 +314,8 @@ public class EventListenerSupport<L> implements Serializable { } /** - * Deserialize. + * Deserializes. + * * @param objectInputStream the input stream * @throws IOException if an IO error occurs * @throws ClassNotFoundException if the class cannot be resolved @@ -323,11 +323,8 @@ public class EventListenerSupport<L> implements Serializable { private void readObject(final ObjectInputStream objectInputStream) throws IOException, ClassNotFoundException { @SuppressWarnings("unchecked") // Will throw CCE here if not correct final L[] srcListeners = (L[]) objectInputStream.readObject(); - this.listeners = new CopyOnWriteArrayList<>(srcListeners); - final Class<L> listenerInterface = ArrayUtils.getComponentType(srcListeners); - initializeTransientFields(listenerInterface, Thread.currentThread().getContextClassLoader()); } @@ -345,13 +342,13 @@ public class EventListenerSupport<L> implements Serializable { } /** - * Serialize. + * Serializes. + * * @param objectOutputStream the output stream * @throws IOException if an IO error occurs */ private void writeObject(final ObjectOutputStream objectOutputStream) throws IOException { final ArrayList<L> serializableListeners = new ArrayList<>(); - // don't just rely on instanceof Serializable: ObjectOutputStream testObjectOutputStream = new ObjectOutputStream(new ByteArrayOutputStream()); for (final L listener : listeners) { diff --git a/src/main/java/org/apache/commons/lang3/event/EventUtils.java b/src/main/java/org/apache/commons/lang3/event/EventUtils.java index 736a4ef5b..0708a5d41 100644 --- a/src/main/java/org/apache/commons/lang3/event/EventUtils.java +++ b/src/main/java/org/apache/commons/lang3/event/EventUtils.java @@ -18,7 +18,6 @@ package org.apache.commons.lang3.event; import java.lang.reflect.InvocationHandler; -import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.lang.reflect.Proxy; import java.util.Arrays; @@ -96,16 +95,10 @@ public class EventUtils { public static <L> void addEventListener(final Object eventSource, final Class<L> listenerType, final L listener) { try { MethodUtils.invokeMethod(eventSource, "add" + listenerType.getSimpleName(), listener); - } catch (final NoSuchMethodException e) { - throw new IllegalArgumentException("Class " + eventSource.getClass().getName() - + " does not have a public add" + listenerType.getSimpleName() + } catch (final ReflectiveOperationException e) { + throw new IllegalArgumentException("Unable to add listener for class " + eventSource.getClass().getName() + + " and public add" + listenerType.getSimpleName() + " method which takes a parameter of type " + listenerType.getName() + "."); - } catch (final IllegalAccessException e) { - throw new IllegalArgumentException("Class " + eventSource.getClass().getName() - + " does not have an accessible add" + listenerType.getSimpleName () - + " method which takes a parameter of type " + listenerType.getName() + "."); - } catch (final InvocationTargetException e) { - throw new IllegalArgumentException("Unable to add listener.", e.getCause()); } } 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 d847f548d..a458fcd1a 100644 --- a/src/main/java/org/apache/commons/lang3/exception/ExceptionUtils.java +++ b/src/main/java/org/apache/commons/lang3/exception/ExceptionUtils.java @@ -19,7 +19,6 @@ package org.apache.commons.lang3.exception; import java.io.PrintStream; import java.io.PrintWriter; import java.io.StringWriter; -import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.lang.reflect.UndeclaredThrowableException; import java.util.ArrayList; @@ -33,6 +32,7 @@ import java.util.stream.Stream; import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.ClassUtils; import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.reflect.MethodUtils; /** * Provides utilities for manipulating and examining @@ -248,17 +248,11 @@ public class ExceptionUtils { // TODO: Remove in Lang 4 private static Throwable getCauseUsingMethodName(final Throwable throwable, final String methodName) { if (methodName != null) { - Method method = null; - try { - method = throwable.getClass().getMethod(methodName); - } catch (final NoSuchMethodException | SecurityException ignored) { - // exception ignored - } - + Method method = MethodUtils.getMethodObject(throwable.getClass(), methodName); if (method != null && Throwable.class.isAssignableFrom(method.getReturnType())) { try { return (Throwable) method.invoke(throwable); - } catch (final IllegalAccessException | IllegalArgumentException | InvocationTargetException ignored) { + } catch (final ReflectiveOperationException ignored) { // exception ignored } } diff --git a/src/main/java/org/apache/commons/lang3/reflect/MemberUtils.java b/src/main/java/org/apache/commons/lang3/reflect/MemberUtils.java index 6dc74fed5..1612e6034 100644 --- a/src/main/java/org/apache/commons/lang3/reflect/MemberUtils.java +++ b/src/main/java/org/apache/commons/lang3/reflect/MemberUtils.java @@ -239,7 +239,8 @@ final class MemberUtils { /** * Tests whether a {@link Member} is accessible. - * @param member Member to test + * + * @param member Member to test, may be null. * @return {@code true} if {@code m} is accessible */ static boolean isAccessible(final Member member) { @@ -247,7 +248,7 @@ final class MemberUtils { } static boolean isMatchingConstructor(final Constructor<?> method, final Class<?>[] parameterTypes) { - return isMatchingExecutable(Executable.of(method), parameterTypes); + return isMatchingExecutable(Executable.of(method), parameterTypes); } private static boolean isMatchingExecutable(final Executable method, final Class<?>[] parameterTypes) { @@ -255,7 +256,6 @@ final class MemberUtils { if (ClassUtils.isAssignable(parameterTypes, methodParameterTypes, true)) { return true; } - if (method.isVarArgs()) { int i; for (i = 0; i < methodParameterTypes.length - 1 && i < parameterTypes.length; i++) { @@ -271,7 +271,6 @@ final class MemberUtils { } return true; } - return false; } @@ -281,6 +280,7 @@ final class MemberUtils { /** * Tests whether a given set of modifiers implies package access. + * * @param modifiers to test * @return {@code true} unless {@code package}/{@code protected}/{@code private} modifier detected */ @@ -290,7 +290,8 @@ final class MemberUtils { /** * Tests whether a {@link Member} is public. - * @param member Member to test + * + * @param member Member to test, may be null. * @return {@code true} if {@code m} is public */ static boolean isPublic(final Member member) { @@ -299,7 +300,8 @@ final class MemberUtils { /** * Tests whether a {@link Member} is static. - * @param member Member to test + * + * @param member Member to test, may be null. * @return {@code true} if {@code m} is static */ static boolean isStatic(final Member member) { @@ -308,7 +310,7 @@ final class MemberUtils { /** * Default access superclass workaround. - * + * <p> * When a {@code public} class has a default access superclass with {@code public} members, * these members are accessible. Calling them from compiled code works fine. * Unfortunately, on some JVMs, using reflection to invoke these members @@ -316,7 +318,9 @@ final class MemberUtils { * Calling {@code setAccessible(true)} solves the problem but will only work from * sufficiently privileged code. Better workarounds would be gratefully * accepted. - * @param obj the AccessibleObject to set as accessible + * </p> + * + * @param obj the AccessibleObject to set as accessible, may be null. * @return a boolean indicating whether the accessibility of the object was set to true. */ static <T extends AccessibleObject> T setAccessibleWorkaround(final T obj) { diff --git a/src/main/java/org/apache/commons/lang3/reflect/MethodUtils.java b/src/main/java/org/apache/commons/lang3/reflect/MethodUtils.java index a86cb616a..fe620c2de 100644 --- a/src/main/java/org/apache/commons/lang3/reflect/MethodUtils.java +++ b/src/main/java/org/apache/commons/lang3/reflect/MethodUtils.java @@ -66,6 +66,7 @@ public class MethodUtils { /** * Returns the aggregate number of inheritance hops between assignable argument class types. Returns -1 * if the arguments aren't assignable. Fills a specific purpose for getMatchingMethod and is not generalized. + * * @param fromClassArray the Class array to calculate the distance from. * @param toClassArray the Class array to calculate the distance to. * @return the aggregate number of inheritance hops between assignable argument class types. @@ -95,32 +96,24 @@ public class MethodUtils { } /** - * Returns an accessible method (that is, one that can be invoked via - * reflection) with given name and parameters. If no such method - * can be found, return {@code null}. - * This is just a convenience wrapper for - * {@link #getAccessibleMethod(Method)}. + * Gets an accessible method (that is, one that can be invoked via reflection) with given name and parameters. If no such method can be found, return + * {@code null}. This is just a convenience wrapper for {@link #getAccessibleMethod(Method)}. * - * @param cls get method from this class - * @param methodName get method with this name + * @param cls get method from this class + * @param methodName get method with this name * @param parameterTypes with these parameters types * @return The accessible method */ - public static Method getAccessibleMethod(final Class<?> cls, final String methodName, - final Class<?>... parameterTypes) { - try { - return getAccessibleMethod(cls.getMethod(methodName, parameterTypes)); - } catch (final NoSuchMethodException e) { - return null; - } + public static Method getAccessibleMethod(final Class<?> cls, final String methodName, final Class<?>... parameterTypes) { + return getAccessibleMethod(getMethodObject(cls, methodName, parameterTypes)); } /** - * Returns an accessible method (that is, one that can be invoked via + * Gets an accessible method (that is, one that can be invoked via * reflection) that implements the specified Method. If no such method * can be found, return {@code null}. * - * @param method The method that we wish to call + * @param method The method that we wish to call, may be null. * @return The accessible method */ public static Method getAccessibleMethod(Method method) { @@ -148,7 +141,7 @@ public class MethodUtils { } /** - * Returns an accessible method (that is, one that can be invoked via + * Gets an accessible method (that is, one that can be invoked via * reflection) that implements the specified method, by scanning through * all implemented interfaces and subinterfaces. If no such method * can be found, return {@code null}. @@ -196,7 +189,7 @@ public class MethodUtils { } /** - * Returns an accessible method (that is, one that can be invoked via + * Gets an accessible method (that is, one that can be invoked via * reflection) by scanning through the superclasses. If no such method * can be found, return {@code null}. * @@ -210,11 +203,7 @@ public class MethodUtils { Class<?> parentClass = cls.getSuperclass(); while (parentClass != null) { if (ClassUtils.isPublic(parentClass)) { - try { - return parentClass.getMethod(methodName, parameterTypes); - } catch (final NoSuchMethodException e) { - return null; - } + return getMethodObject(parentClass, methodName, parameterTypes); } parentClass = parentClass.getSuperclass(); } @@ -266,7 +255,7 @@ public class MethodUtils { * @param <A> * the annotation type * @param method - * the {@link Method} to query + * the {@link Method} to query, may be null. * @param annotationCls * the {@link Annotation} to check if is present on the method * @param searchSupers @@ -308,7 +297,7 @@ public class MethodUtils { } /** - * Finds an accessible method that matches the given name and has compatible parameters. + * Gets an accessible method that matches the given name and has compatible parameters. * Compatible parameters mean that every method parameter is assignable from * the given parameters. * In other words, it finds a method with the given name @@ -331,10 +320,9 @@ public class MethodUtils { */ public static Method getMatchingAccessibleMethod(final Class<?> cls, final String methodName, final Class<?>... parameterTypes) { - try { - return MemberUtils.setAccessibleWorkaround(cls.getMethod(methodName, parameterTypes)); - } catch (final NoSuchMethodException ignored) { - // Swallow the exception + final Method candidate = getMethodObject(cls, methodName, parameterTypes); + if (candidate != null) { + return MemberUtils.setAccessibleWorkaround(candidate); } // search through all methods final Method[] methods = cls.getMethods(); @@ -375,8 +363,9 @@ public class MethodUtils { } /** - * Retrieves a method whether or not it's accessible. If no such method + * Gets a method whether or not it's accessible. If no such method * can be found, return {@code null}. + * * @param cls The class that will be subjected to the method search * @param methodName The method that we wish to call * @param parameterTypes Argument class types @@ -436,6 +425,24 @@ public class MethodUtils { } /** + * Gets a Method or null if a {@link Class#getMethod(String, Class...) documented} exception is thrown. + * + * @param cls Receiver for {@link Class#getMethod(String, Class...)}. + * @param name the name of the method + * @param parameterTypes the list of parameters + * @return a Method or null. + * @since 3.15.0 + * @see Class#getMethod(String, Class...) + */ + public static Method getMethodObject(final Class<?> cls, final String name, final Class<?>... parameterTypes) { + try { + return cls.getMethod(name, parameterTypes); + } catch (final NoSuchMethodException | SecurityException e) { + return null; + } + } + + /** * Gets all class level public methods of the given class that are annotated with the given annotation. * @param cls * the {@link Class} to query @@ -452,6 +459,7 @@ public class MethodUtils { /** * Gets all methods of the given class that are annotated with the given annotation. + * * @param cls * the {@link Class} to query * @param annotationCls @@ -482,6 +490,7 @@ public class MethodUtils { /** * Gets all class level public methods of the given class that are annotated with the given annotation. + * * @param cls * the {@link Class} to query * @param annotationCls @@ -496,6 +505,7 @@ public class MethodUtils { /** * Gets all methods of the given class that are annotated with the given annotation. + * * @param cls * the {@link Class} to query * @param annotationCls @@ -515,6 +525,7 @@ public class MethodUtils { /** * Gets the hierarchy of overridden methods down to {@code result} respecting generics. + * * @param method lowest to consider * @param interfacesBehavior whether to search interfaces, {@code null} {@code implies} false * @return Set<Method> in ascending order from sub- to superclass @@ -559,8 +570,8 @@ public class MethodUtils { } /** - * Given an arguments array passed to a varargs method, return an array of arguments in the canonical form, - * i.e. an array with the declared number of parameters, and whose last parameter is an array of the varargs type. + * Gets an array of arguments in the canonical form, given an arguments array passed to a varargs method, + * for example an array with the declared number of parameters, and whose last parameter is an array of the varargs type. * * @param args the array of arguments passed to the varags method * @param methodParameterTypes the declared array of method parameter types diff --git a/src/main/java/org/apache/commons/lang3/text/StrBuilder.java b/src/main/java/org/apache/commons/lang3/text/StrBuilder.java index e8a91f4ee..3dd43bef5 100644 --- a/src/main/java/org/apache/commons/lang3/text/StrBuilder.java +++ b/src/main/java/org/apache/commons/lang3/text/StrBuilder.java @@ -272,7 +272,9 @@ public class StrBuilder implements CharSequence, Appendable, Serializable, Build /** Current size of the buffer. */ protected int size; // TODO make private? - /** The new line. */ + /** + * The new line, {@code null} means use the system default from {@link System#lineSeparator()}. + */ private String newLine; /** The null text. */ @@ -1142,14 +1144,18 @@ public class StrBuilder implements CharSequence, Appendable, Serializable, Build } /** - * Appends the new line string to this string builder. + * Appends this builder's new line string to this builder. + * <p> + * By default, the new line is the system default from {@link System#lineSeparator()}. + * </p> * <p> - * The new line string can be altered using {@link #setNewLineText(String)}. - * This might be used to force the output to always use UNIX line endings - * even when on Windows. + * The new line string can be changed using {@link #setNewLineText(String)}. For example, you can use this to force the output to always use UNIX line + * endings even when on Windows. * </p> * - * @return this, to enable chaining + * @return {@code this} instance. + * @see #getNewLineText() + * @see #setNewLineText(String) */ public StrBuilder appendNewLine() { if (newLine == null) { @@ -1940,9 +1946,9 @@ public class StrBuilder implements CharSequence, Appendable, Serializable, Build } /** - * Gets the text to be appended when a new line is added. + * Gets the text to be appended when a {@link #appendNewLine() new line} is added. * - * @return the new line text, null means use system default + * @return The new line text, {@code null} means use the system default from {@link System#lineSeparator()}. */ public String getNewLineText() { return newLine; @@ -2811,10 +2817,10 @@ public class StrBuilder implements CharSequence, Appendable, Serializable, Build } /** - * Sets the text to be appended when a new line is added. + * Sets the text to be appended when {@link #appendNewLine() new line} is called. * - * @param newLine the new line text, null means use system default - * @return this, to enable chaining + * @param newLine the new line text, {@code null} means use the system default from {@link System#lineSeparator()}. + * @return {@code this} instance. */ public StrBuilder setNewLineText(final String newLine) { this.newLine = newLine; diff --git a/src/main/java/org/apache/commons/lang3/text/translate/CharSequenceTranslator.java b/src/main/java/org/apache/commons/lang3/text/translate/CharSequenceTranslator.java index 677c0e1b3..8dbff38b5 100644 --- a/src/main/java/org/apache/commons/lang3/text/translate/CharSequenceTranslator.java +++ b/src/main/java/org/apache/commons/lang3/text/translate/CharSequenceTranslator.java @@ -92,6 +92,7 @@ public abstract class CharSequenceTranslator { * @param writer Writer to translate the text to * @throws IOException if and only if the Writer produces an IOException */ + @SuppressWarnings("resource") // Caller closes writer public final void translate(final CharSequence input, final Writer writer) throws IOException { Objects.requireNonNull(writer, "writer"); if (input == null) { diff --git a/src/main/java/org/apache/commons/lang3/text/translate/CodePointTranslator.java b/src/main/java/org/apache/commons/lang3/text/translate/CodePointTranslator.java index e315fad5b..ca8c743c2 100644 --- a/src/main/java/org/apache/commons/lang3/text/translate/CodePointTranslator.java +++ b/src/main/java/org/apache/commons/lang3/text/translate/CodePointTranslator.java @@ -32,7 +32,7 @@ import java.io.Writer; public abstract class CodePointTranslator extends CharSequenceTranslator { /** - * Implementation of translate that maps onto the abstract translate(int, Writer) method. + * Implements translate to map onto the abstract translate(int, Writer) method. * {@inheritDoc} */ @Override diff --git a/src/main/java/org/apache/commons/lang3/text/translate/UnicodeUnpairedSurrogateRemover.java b/src/main/java/org/apache/commons/lang3/text/translate/UnicodeUnpairedSurrogateRemover.java index d054742f1..132e075d1 100644 --- a/src/main/java/org/apache/commons/lang3/text/translate/UnicodeUnpairedSurrogateRemover.java +++ b/src/main/java/org/apache/commons/lang3/text/translate/UnicodeUnpairedSurrogateRemover.java @@ -28,8 +28,9 @@ import java.io.Writer; */ @Deprecated public class UnicodeUnpairedSurrogateRemover extends CodePointTranslator { + /** - * Implementation of translate that throws out unpaired surrogates. + * Implements translate that throws out unpaired surrogates. * {@inheritDoc} */ @Override 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 9b8dd530e..e0a9aa33c 100644 --- a/src/main/java/org/apache/commons/lang3/time/DateUtils.java +++ b/src/main/java/org/apache/commons/lang3/time/DateUtils.java @@ -131,7 +131,7 @@ public class DateUtils { * Number of milliseconds in a standard second. * @since 2.1 */ - public static final long MILLIS_PER_SECOND = 1000; + public static final long MILLIS_PER_SECOND = 1_000; /** * Number of milliseconds in a standard minute. * @since 2.1 diff --git a/src/main/java/org/apache/commons/lang3/time/DurationFormatUtils.java b/src/main/java/org/apache/commons/lang3/time/DurationFormatUtils.java index 5e0fbccc8..18dc6a49d 100644 --- a/src/main/java/org/apache/commons/lang3/time/DurationFormatUtils.java +++ b/src/main/java/org/apache/commons/lang3/time/DurationFormatUtils.java @@ -79,6 +79,12 @@ import org.apache.commons.lang3.Validate; */ public class DurationFormatUtils { + private static final int MINUTES_PER_HOUR = 60; + + private static final int SECONDS_PER_MINUTES = 60; + + private static final int HOURS_PER_DAY = 24; + /** * Element that is parsed from the format pattern. */ @@ -98,7 +104,7 @@ public class DurationFormatUtils { return Stream.of(tokens).anyMatch(token -> token.getValue() == value); } - private final Object value; + private final CharSequence value; private int count; private int optionalIndex = -1; @@ -109,7 +115,7 @@ public class DurationFormatUtils { * @param optional whether the token is optional * @param optionalIndex the index of the optional token within the pattern */ - Token(final Object value, final boolean optional, final int optionalIndex) { + Token(final CharSequence value, final boolean optional, final int optionalIndex) { this.value = Objects.requireNonNull(value, "value"); this.count = 1; if (optional) { @@ -257,9 +263,9 @@ public class DurationFormatUtils { } } if (isLiteral) { - if (!inOptional || !lastOutputZero) { - buffer.append(value.toString()); - } + if (!inOptional || !lastOutputZero) { + buffer.append(value.toString()); + } } else if (value.equals(y)) { lastOutputSeconds = false; lastOutputZero = years == 0; @@ -538,19 +544,19 @@ public class DurationFormatUtils { // each initial estimate is adjusted in case it is under 0 while (milliseconds < 0) { - milliseconds += 1000; + milliseconds += DateUtils.MILLIS_PER_SECOND; seconds -= 1; } while (seconds < 0) { - seconds += 60; + seconds += SECONDS_PER_MINUTES; minutes -= 1; } while (minutes < 0) { - minutes += 60; + minutes += MINUTES_PER_HOUR; hours -= 1; } while (hours < 0) { - hours += 24; + hours += HOURS_PER_DAY; days -= 1; } @@ -620,19 +626,19 @@ public class DurationFormatUtils { // number of months and get the real count and not just 0->11. if (!Token.containsTokenWithValue(tokens, d)) { - hours += 24 * days; + hours += HOURS_PER_DAY * days; days = 0; } if (!Token.containsTokenWithValue(tokens, H)) { - minutes += 60 * hours; + minutes += MINUTES_PER_HOUR * hours; hours = 0; } if (!Token.containsTokenWithValue(tokens, m)) { - seconds += 60 * minutes; + seconds += SECONDS_PER_MINUTES * minutes; minutes = 0; } if (!Token.containsTokenWithValue(tokens, s)) { - milliseconds += 1000 * seconds; + milliseconds += DateUtils.MILLIS_PER_SECOND * seconds; seconds = 0; } diff --git a/src/main/java/org/apache/commons/lang3/util/FluentBitSet.java b/src/main/java/org/apache/commons/lang3/util/FluentBitSet.java index 98e3cbcb8..07bf321c1 100644 --- a/src/main/java/org/apache/commons/lang3/util/FluentBitSet.java +++ b/src/main/java/org/apache/commons/lang3/util/FluentBitSet.java @@ -70,7 +70,7 @@ public final class FluentBitSet implements Cloneable, Serializable { * corresponding bit in the bit set argument also had the value {@code true}. * * @param set a bit set. - * @return this. + * @return {@code this} instance. */ public FluentBitSet and(final BitSet set) { bitSet.and(set); @@ -83,7 +83,7 @@ public final class FluentBitSet implements Cloneable, Serializable { * corresponding bit in the bit set argument also had the value {@code true}. * * @param set a bit set. - * @return this. + * @return {@code this} instance. */ public FluentBitSet and(final FluentBitSet set) { bitSet.and(set.bitSet); @@ -94,7 +94,7 @@ public final class FluentBitSet implements Cloneable, Serializable { * Clears all of the bits in this {@link BitSet} whose corresponding bit is set in the specified {@link BitSet}. * * @param set the {@link BitSet} with which to mask this {@link BitSet}. - * @return this. + * @return {@code this} instance. */ public FluentBitSet andNot(final BitSet set) { bitSet.andNot(set); @@ -105,7 +105,7 @@ public final class FluentBitSet implements Cloneable, Serializable { * Clears all of the bits in this {@link BitSet} whose corresponding bit is set in the specified {@link BitSet}. * * @param set the {@link BitSet} with which to mask this {@link BitSet}. - * @return this. + * @return {@code this} instance. */ public FluentBitSet andNot(final FluentBitSet set) { this.bitSet.andNot(set.bitSet); @@ -133,7 +133,7 @@ public final class FluentBitSet implements Cloneable, Serializable { /** * Sets all of the bits in this BitSet to {@code false}. * - * @return this. + * @return {@code this} instance. */ public FluentBitSet clear() { bitSet.clear(); @@ -145,7 +145,7 @@ public final class FluentBitSet implements Cloneable, Serializable { * * @param bitIndexArray the index of the bit to be cleared. * @throws IndexOutOfBoundsException if the specified index is negative. - * @return this. + * @return {@code this} instance. */ public FluentBitSet clear(final int... bitIndexArray) { for (final int e : bitIndexArray) { @@ -159,7 +159,7 @@ public final class FluentBitSet implements Cloneable, Serializable { * * @param bitIndex the index of the bit to be cleared. * @throws IndexOutOfBoundsException if the specified index is negative. - * @return this. + * @return {@code this} instance. */ public FluentBitSet clear(final int bitIndex) { bitSet.clear(bitIndex); @@ -174,7 +174,7 @@ public final class FluentBitSet implements Cloneable, Serializable { * @param toIndex index after the last bit to be cleared. * @throws IndexOutOfBoundsException if {@code fromIndex} is negative, or {@code toIndex} is negative, or * {@code fromIndex} is larger than {@code toIndex}. - * @return this. + * @return {@code this} instance. */ public FluentBitSet clear(final int fromIndex, final int toIndex) { bitSet.clear(fromIndex, toIndex); @@ -210,7 +210,7 @@ public final class FluentBitSet implements Cloneable, Serializable { * * @param bitIndex the index of the bit to flip. * @throws IndexOutOfBoundsException if the specified index is negative. - * @return this. + * @return {@code this} instance. */ public FluentBitSet flip(final int bitIndex) { bitSet.flip(bitIndex); @@ -225,7 +225,7 @@ public final class FluentBitSet implements Cloneable, Serializable { * @param toIndex index after the last bit to flip. * @throws IndexOutOfBoundsException if {@code fromIndex} is negative, or {@code toIndex} is negative, or * {@code fromIndex} is larger than {@code toIndex}. - * @return this. + * @return {@code this} instance. */ public FluentBitSet flip(final int fromIndex, final int toIndex) { bitSet.flip(fromIndex, toIndex); @@ -346,7 +346,7 @@ public final class FluentBitSet implements Cloneable, Serializable { * the bit set argument has the value {@code true}. * * @param set a bit set. - * @return this. + * @return {@code this} instance. */ public FluentBitSet or(final BitSet set) { bitSet.or(set); @@ -359,7 +359,7 @@ public final class FluentBitSet implements Cloneable, Serializable { * the bit set argument has the value {@code true}. * * @param set a bit set. - * @return this. + * @return {@code this} instance. */ public FluentBitSet or(final FluentBitSet... set) { for (final FluentBitSet e : set) { @@ -374,7 +374,7 @@ public final class FluentBitSet implements Cloneable, Serializable { * the bit set argument has the value {@code true}. * * @param set a bit set. - * @return this. + * @return {@code this} instance. */ public FluentBitSet or(final FluentBitSet set) { this.bitSet.or(set.bitSet); @@ -420,7 +420,7 @@ public final class FluentBitSet implements Cloneable, Serializable { * * @param bitIndexArray a bit index array. * @throws IndexOutOfBoundsException if the specified index is negative. - * @return this. + * @return {@code this} instance. */ public FluentBitSet set(final int... bitIndexArray) { for (final int e : bitIndexArray) { @@ -434,7 +434,7 @@ public final class FluentBitSet implements Cloneable, Serializable { * * @param bitIndex a bit index * @throws IndexOutOfBoundsException if the specified index is negative - * @return this. + * @return {@code this} instance. */ public FluentBitSet set(final int bitIndex) { bitSet.set(bitIndex); @@ -447,7 +447,7 @@ public final class FluentBitSet implements Cloneable, Serializable { * @param bitIndex a bit index. * @param value a boolean value to set. * @throws IndexOutOfBoundsException if the specified index is negative. - * @return this. + * @return {@code this} instance. */ public FluentBitSet set(final int bitIndex, final boolean value) { bitSet.set(bitIndex, value); @@ -462,7 +462,7 @@ public final class FluentBitSet implements Cloneable, Serializable { * @param toIndex index after the last bit to be set. * @throws IndexOutOfBoundsException if {@code fromIndex} is negative, or {@code toIndex} is negative, or * {@code fromIndex} is larger than {@code toIndex}. - * @return this. + * @return {@code this} instance. */ public FluentBitSet set(final int fromIndex, final int toIndex) { bitSet.set(fromIndex, toIndex); @@ -478,7 +478,7 @@ public final class FluentBitSet implements Cloneable, Serializable { * @param value value to set the selected bits to. * @throws IndexOutOfBoundsException if {@code fromIndex} is negative, or {@code toIndex} is negative, or * {@code fromIndex} is larger than {@code toIndex}. - * @return this. + * @return {@code this} instance. */ public FluentBitSet set(final int fromIndex, final int toIndex, final boolean value) { bitSet.set(fromIndex, toIndex, value); @@ -486,14 +486,14 @@ public final class FluentBitSet implements Cloneable, Serializable { } /** - * Sets the bits from the specified {@code fromIndex} (inclusive) to the specified {@code toIndex} (exclusive) to + * Sets the bits from the specified {@code fromIndex} (inclusive) to the specified {@code toIndex} (inclusive) to * {@code true}. * * @param fromIndex index of the first bit to be set * @param toIndex index of the last bit to be set * @throws IndexOutOfBoundsException if {@code fromIndex} is negative, or {@code toIndex} is negative, or * {@code fromIndex} is larger than {@code toIndex} - * @return this. + * @return {@code this} instance. */ public FluentBitSet setInclusive(final int fromIndex, final int toIndex) { bitSet.set(fromIndex, toIndex + 1); @@ -581,7 +581,7 @@ public final class FluentBitSet implements Cloneable, Serializable { * </ul> * * @param set a bit set - * @return this. + * @return {@code this} instance. */ public FluentBitSet xor(final BitSet set) { bitSet.xor(set); @@ -599,7 +599,7 @@ public final class FluentBitSet implements Cloneable, Serializable { * </ul> * * @param set a bit set - * @return this. + * @return {@code this} instance. */ public FluentBitSet xor(final FluentBitSet set) { bitSet.xor(set.bitSet); diff --git a/src/test/java/org/apache/commons/lang3/ArrayUtilsRemoveMultipleTest.java b/src/test/java/org/apache/commons/lang3/ArrayUtilsRemoveMultipleTest.java index 4b74d9e2d..4b2a58160 100644 --- a/src/test/java/org/apache/commons/lang3/ArrayUtilsRemoveMultipleTest.java +++ b/src/test/java/org/apache/commons/lang3/ArrayUtilsRemoveMultipleTest.java @@ -995,7 +995,6 @@ public class ArrayUtilsRemoveMultipleTest extends AbstractLangTest { } @Test - @SuppressWarnings("cast") public void testRemoveElementDoubleArray() { double[] array; @@ -1051,7 +1050,6 @@ public class ArrayUtilsRemoveMultipleTest extends AbstractLangTest { } @Test - @SuppressWarnings("cast") public void testRemoveElementFloatArray() { float[] array; diff --git a/src/test/java/org/apache/commons/lang3/ArrayUtilsRemoveTest.java b/src/test/java/org/apache/commons/lang3/ArrayUtilsRemoveTest.java index 9cbe26c4b..5284c1b3d 100644 --- a/src/test/java/org/apache/commons/lang3/ArrayUtilsRemoveTest.java +++ b/src/test/java/org/apache/commons/lang3/ArrayUtilsRemoveTest.java @@ -545,7 +545,6 @@ public class ArrayUtilsRemoveTest extends AbstractLangTest { } @Test - @SuppressWarnings("cast") public void testRemoveElementDoubleArray() { double[] array; array = ArrayUtils.removeElement(null, (double) 1); @@ -565,7 +564,6 @@ public class ArrayUtilsRemoveTest extends AbstractLangTest { } @Test - @SuppressWarnings("cast") public void testRemoveElementFloatArray() { float[] array; array = ArrayUtils.removeElement((float[]) null, 1); diff --git a/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java b/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java index 017d4479b..f952bb85c 100644 --- a/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java @@ -36,7 +36,10 @@ import java.util.Comparator; import java.util.Date; import java.util.Map; import java.util.Random; +import java.util.function.Function; +import java.util.function.Supplier; +import org.apache.commons.lang3.function.Suppliers; import org.junit.jupiter.api.Test; /** @@ -69,6 +72,22 @@ public class ArrayUtilsTest extends AbstractLangTest { assertFalse(ArrayUtils.isEquals(array2, array1)); } + @Test + public void testArraycopyFunction() { + String[] arr = { "a", "b" }; + assertThrows(NullPointerException.class, () -> ArrayUtils.arraycopy(null, 0, 0, 1, i -> new String[3])); + assertThrows(NullPointerException.class, () -> ArrayUtils.arraycopy(arr, 0, 0, 1, i -> null)); + assertThrows(NullPointerException.class, () -> ArrayUtils.arraycopy(arr, 0, 0, 1, (Function<Integer, String[]>) null)); + } + + @Test + public void testArraycopySupplier() { + String[] arr = { "a", "b" }; + assertThrows(NullPointerException.class, () -> ArrayUtils.arraycopy(null, 0, 0, 1, () -> new String[3])); + assertThrows(NullPointerException.class, () -> ArrayUtils.arraycopy(arr, 0, 0, 1, Suppliers.nul())); + assertThrows(NullPointerException.class, () -> ArrayUtils.arraycopy(arr, 0, 0, 1, (Supplier<String[]>) null)); + } + /** * Tests generic array creation with parameters of same type. */ @@ -234,14 +253,14 @@ public class ArrayUtilsTest extends AbstractLangTest { @Test public void testContainsAny() { final Object[] array = {"0", "1", "2", "3", null, "0"}; - assertFalse(ArrayUtils.containsAny(null, null)); + assertFalse(ArrayUtils.containsAny(null, (Object) null)); assertFalse(ArrayUtils.containsAny(null, "1")); assertTrue(ArrayUtils.containsAny(array, "0")); assertTrue(ArrayUtils.containsAny(array, "1")); assertTrue(ArrayUtils.containsAny(array, "2")); assertTrue(ArrayUtils.containsAny(array, "3")); assertFalse(ArrayUtils.containsAny(array, "notInArray")); - assertTrue(ArrayUtils.containsAny(array, new String[] {null})); + assertTrue(ArrayUtils.containsAny(array, (Object[]) new String[] { null })); } @Test @@ -280,7 +299,6 @@ public class ArrayUtilsTest extends AbstractLangTest { assertFalse(ArrayUtils.contains(array, 'e')); } - @SuppressWarnings("cast") @Test public void testContainsDouble() { double[] array = null; @@ -301,7 +319,6 @@ public class ArrayUtilsTest extends AbstractLangTest { assertTrue(ArrayUtils.contains(a, Double.NaN)); } - @SuppressWarnings("cast") @Test public void testContainsDoubleTolerance() { double[] array = null; @@ -313,7 +330,6 @@ public class ArrayUtilsTest extends AbstractLangTest { assertTrue(ArrayUtils.contains(array, 2.5, 0.51)); } - @SuppressWarnings("cast") @Test public void testContainsFloat() { float[] array = null; @@ -704,7 +720,6 @@ public class ArrayUtilsTest extends AbstractLangTest { assertEquals(emptySet, ArrayUtils.indexesOf(array, 99)); } - @SuppressWarnings("cast") @Test public void testIndexesOfDoubleTolerance() { double[] array = null; @@ -749,7 +764,6 @@ public class ArrayUtilsTest extends AbstractLangTest { assertEquals(emptySet, ArrayUtils.indexesOf(array, 99, 0)); } - @SuppressWarnings("cast") @Test public void testIndexesOfDoubleWithStartIndexTolerance() { double[] array = null; @@ -1066,7 +1080,6 @@ public class ArrayUtilsTest extends AbstractLangTest { assertEquals(-1, ArrayUtils.indexOf(array, 'a', 6)); } - @SuppressWarnings("cast") @Test public void testIndexOfDouble() { double[] array = null; @@ -1091,7 +1104,6 @@ public class ArrayUtilsTest extends AbstractLangTest { } - @SuppressWarnings("cast") @Test public void testIndexOfDoubleTolerance() { double[] array = null; @@ -1105,7 +1117,6 @@ public class ArrayUtilsTest extends AbstractLangTest { assertEquals(1, ArrayUtils.indexOf(array, 1.00001324, 0.0001)); } - @SuppressWarnings("cast") @Test public void testIndexOfDoubleWithStartIndex() { double[] array = null; @@ -1121,7 +1132,6 @@ public class ArrayUtilsTest extends AbstractLangTest { assertEquals(-1, ArrayUtils.indexOf(array, 0, 6)); } - @SuppressWarnings("cast") @Test public void testIndexOfDoubleWithStartIndexTolerance() { double[] array = null; @@ -1139,7 +1149,6 @@ public class ArrayUtilsTest extends AbstractLangTest { assertEquals(1, ArrayUtils.indexOf(array, 1.00001324, -300, 0.0001)); } - @SuppressWarnings("cast") @Test public void testIndexOfFloat() { float[] array = null; @@ -1162,7 +1171,6 @@ public class ArrayUtilsTest extends AbstractLangTest { assertEquals(2, ArrayUtils.indexOf(array, Float.POSITIVE_INFINITY)); } - @SuppressWarnings("cast") @Test public void testIndexOfFloatWithStartIndex() { float[] array = null; @@ -1758,7 +1766,6 @@ public class ArrayUtilsTest extends AbstractLangTest { assertEquals(4, ArrayUtils.lastIndexOf(array, 'a', 88)); } - @SuppressWarnings("cast") @Test public void testLastIndexOfDouble() { double[] array = null; @@ -1773,7 +1780,6 @@ public class ArrayUtilsTest extends AbstractLangTest { assertEquals(-1, ArrayUtils.lastIndexOf(array, 99)); } - @SuppressWarnings("cast") @Test public void testLastIndexOfDoubleTolerance() { double[] array = null; @@ -1787,7 +1793,6 @@ public class ArrayUtilsTest extends AbstractLangTest { assertEquals(1, ArrayUtils.lastIndexOf(array, 1.00001324, 0.0001)); } - @SuppressWarnings("cast") @Test public void testLastIndexOfDoubleWithStartIndex() { double[] array = null; @@ -1804,7 +1809,6 @@ public class ArrayUtilsTest extends AbstractLangTest { assertEquals(4, ArrayUtils.lastIndexOf(array, 0, 88)); } - @SuppressWarnings("cast") @Test public void testLastIndexOfDoubleWithStartIndexTolerance() { double[] array = null; @@ -1822,7 +1826,6 @@ public class ArrayUtilsTest extends AbstractLangTest { assertEquals(-1, ArrayUtils.lastIndexOf(array, 4.15, -200, 2.0)); } - @SuppressWarnings("cast") @Test public void testLastIndexOfFloat() { float[] array = null; @@ -1837,7 +1840,6 @@ public class ArrayUtilsTest extends AbstractLangTest { assertEquals(-1, ArrayUtils.lastIndexOf(array, 99)); } - @SuppressWarnings("cast") @Test public void testLastIndexOfFloatWithStartIndex() { float[] array = null; diff --git a/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java b/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java index efa1f6c48..0f6d43212 100644 --- a/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java @@ -286,6 +286,8 @@ public class ObjectUtilsTest extends AbstractLangTest { public void testCloneOfUncloneable() { final UncloneableString string = new UncloneableString("apache"); final CloneFailedException e = assertThrows(CloneFailedException.class, () -> ObjectUtils.clone(string)); + assertNotNull(e); + assertNotNull(e.getCause()); assertEquals(NoSuchMethodException.class, e.getCause().getClass()); } @@ -480,6 +482,7 @@ public class ObjectUtilsTest extends AbstractLangTest { @Test public void testGetFirstNonNull() { // first non-null + assertEquals("", ObjectUtils.getFirstNonNull(null, () -> "")); assertEquals("", ObjectUtils.getFirstNonNull(Suppliers.nul(), () -> "")); // first encountered value is used assertEquals("1", ObjectUtils.getFirstNonNull(Suppliers.nul(), () -> "1", () -> "2", Suppliers.nul())); @@ -818,8 +821,9 @@ public class ObjectUtilsTest extends AbstractLangTest { @Test public void testPossibleCloneOfUncloneable() { final UncloneableString string = new UncloneableString("apache"); - final CloneFailedException e = assertThrows(CloneFailedException.class, - () -> ObjectUtils.cloneIfPossible(string)); + final CloneFailedException e = assertThrows(CloneFailedException.class, () -> ObjectUtils.cloneIfPossible(string)); + assertNotNull(e); + assertNotNull(e.getCause()); assertEquals(NoSuchMethodException.class, e.getCause().getClass()); } diff --git a/src/test/java/org/apache/commons/lang3/SerializationUtilsTest.java b/src/test/java/org/apache/commons/lang3/SerializationUtilsTest.java index 3481a187a..9d85a7ce2 100644 --- a/src/test/java/org/apache/commons/lang3/SerializationUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/SerializationUtilsTest.java @@ -242,7 +242,7 @@ public class SerializationUtilsTest extends AbstractLangTest { } @Test - public void testNegativeByteArray() throws IOException { + public void testNegativeByteArray() { final byte[] byteArray = { (byte) -84, (byte) -19, (byte) 0, (byte) 5, (byte) 125, (byte) -19, (byte) 0, (byte) 5, (byte) 115, (byte) 114, (byte) -1, (byte) 97, (byte) 122, (byte) -48, (byte) -65 diff --git a/src/test/java/org/apache/commons/lang3/StringUtilsTest.java b/src/test/java/org/apache/commons/lang3/StringUtilsTest.java index 7de206358..e1102f74e 100644 --- a/src/test/java/org/apache/commons/lang3/StringUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/StringUtilsTest.java @@ -438,7 +438,7 @@ public class StringUtilsTest extends AbstractLangTest { assertNull(StringUtils.appendIfMissing(null, null, (CharSequence[]) null), "appendIfMissing(null,null,null)"); assertEquals("abc", StringUtils.appendIfMissing("abc", null, (CharSequence[]) null), "appendIfMissing(abc,null,null)"); assertEquals("xyz", StringUtils.appendIfMissing("", "xyz", (CharSequence[]) null), "appendIfMissing(\"\",xyz,null))"); - assertEquals("abcxyz", StringUtils.appendIfMissing("abc", "xyz", null), "appendIfMissing(abc,xyz,{null})"); + assertEquals("abcxyz", StringUtils.appendIfMissing("abc", "xyz", (CharSequence) null), "appendIfMissing(abc,xyz,{null})"); assertEquals("abc", StringUtils.appendIfMissing("abc", "xyz", ""), "appendIfMissing(abc,xyz,\"\")"); assertEquals("abcxyz", StringUtils.appendIfMissing("abc", "xyz", "mno"), "appendIfMissing(abc,xyz,mno)"); assertEquals("abcxyz", StringUtils.appendIfMissing("abcxyz", "xyz", "mno"), "appendIfMissing(abcxyz,xyz,mno)"); @@ -462,7 +462,7 @@ public class StringUtilsTest extends AbstractLangTest { assertNull(StringUtils.appendIfMissingIgnoreCase(null, null, (CharSequence[]) null), "appendIfMissingIgnoreCase(null,null,null)"); assertEquals("abc", StringUtils.appendIfMissingIgnoreCase("abc", null, (CharSequence[]) null), "appendIfMissingIgnoreCase(abc,null,null)"); assertEquals("xyz", StringUtils.appendIfMissingIgnoreCase("", "xyz", (CharSequence[]) null), "appendIfMissingIgnoreCase(\"\",xyz,null)"); - assertEquals("abcxyz", StringUtils.appendIfMissingIgnoreCase("abc", "xyz", null), "appendIfMissingIgnoreCase(abc,xyz,{null})"); + assertEquals("abcxyz", StringUtils.appendIfMissingIgnoreCase("abc", "xyz", (CharSequence) null), "appendIfMissingIgnoreCase(abc,xyz,{null})"); assertEquals("abc", StringUtils.appendIfMissingIgnoreCase("abc", "xyz", ""), "appendIfMissingIgnoreCase(abc,xyz,\"\")"); assertEquals("abcxyz", StringUtils.appendIfMissingIgnoreCase("abc", "xyz", "mno"), "appendIfMissingIgnoreCase(abc,xyz,mno)"); assertEquals("abcxyz", StringUtils.appendIfMissingIgnoreCase("abcxyz", "xyz", "mno"), "appendIfMissingIgnoreCase(abcxyz,xyz,mno)"); @@ -1603,7 +1603,7 @@ public class StringUtilsTest extends AbstractLangTest { assertNull(StringUtils.prependIfMissing(null, null, (CharSequence[]) null), "prependIfMissing(null,null null)"); assertEquals("abc", StringUtils.prependIfMissing("abc", null, (CharSequence[]) null), "prependIfMissing(abc,null,null)"); assertEquals("xyz", StringUtils.prependIfMissing("", "xyz", (CharSequence[]) null), "prependIfMissing(\"\",xyz,null)"); - assertEquals("xyzabc", StringUtils.prependIfMissing("abc", "xyz", null), "prependIfMissing(abc,xyz,{null})"); + assertEquals("xyzabc", StringUtils.prependIfMissing("abc", "xyz", (CharSequence) null), "prependIfMissing(abc,xyz,{null})"); assertEquals("abc", StringUtils.prependIfMissing("abc", "xyz", ""), "prependIfMissing(abc,xyz,\"\")"); assertEquals("xyzabc", StringUtils.prependIfMissing("abc", "xyz", "mno"), "prependIfMissing(abc,xyz,mno)"); assertEquals("xyzabc", StringUtils.prependIfMissing("xyzabc", "xyz", "mno"), "prependIfMissing(xyzabc,xyz,mno)"); @@ -1627,7 +1627,7 @@ public class StringUtilsTest extends AbstractLangTest { assertNull(StringUtils.prependIfMissingIgnoreCase(null, null, (CharSequence[]) null), "prependIfMissingIgnoreCase(null,null null)"); assertEquals("abc", StringUtils.prependIfMissingIgnoreCase("abc", null, (CharSequence[]) null), "prependIfMissingIgnoreCase(abc,null,null)"); assertEquals("xyz", StringUtils.prependIfMissingIgnoreCase("", "xyz", (CharSequence[]) null), "prependIfMissingIgnoreCase(\"\",xyz,null)"); - assertEquals("xyzabc", StringUtils.prependIfMissingIgnoreCase("abc", "xyz", null), "prependIfMissingIgnoreCase(abc,xyz,{null})"); + assertEquals("xyzabc", StringUtils.prependIfMissingIgnoreCase("abc", "xyz", (CharSequence) null), "prependIfMissingIgnoreCase(abc,xyz,{null})"); assertEquals("abc", StringUtils.prependIfMissingIgnoreCase("abc", "xyz", ""), "prependIfMissingIgnoreCase(abc,xyz,\"\")"); assertEquals("xyzabc", StringUtils.prependIfMissingIgnoreCase("abc", "xyz", "mno"), "prependIfMissingIgnoreCase(abc,xyz,mno)"); assertEquals("xyzabc", StringUtils.prependIfMissingIgnoreCase("xyzabc", "xyz", "mno"), "prependIfMissingIgnoreCase(xyzabc,xyz,mno)"); diff --git a/src/test/java/org/apache/commons/lang3/StringUtilsTrimStripTest.java b/src/test/java/org/apache/commons/lang3/StringUtilsTrimStripTest.java index 0b7339e00..aa827ddf5 100644 --- a/src/test/java/org/apache/commons/lang3/StringUtilsTrimStripTest.java +++ b/src/test/java/org/apache/commons/lang3/StringUtilsTrimStripTest.java @@ -84,8 +84,8 @@ public class StringUtilsTrimStripTest extends AbstractLangTest { assertEquals("", StringUtils.stripAccents(""), "Failed empty String"); assertEquals("control", StringUtils.stripAccents("control"), "Failed to handle non-accented text"); assertEquals("eclair", StringUtils.stripAccents("\u00E9clair"), "Failed to handle easy example"); - assertEquals("ALOSZZCN aloszzcn", - StringUtils.stripAccents("\u0104\u0141\u00D3\u015A\u017B\u0179\u0106\u0143 \u0105\u0142\u00F3\u015B\u017C\u017A\u0107\u0144")); + assertEquals("ALOSZZCND aloszzcnd", + StringUtils.stripAccents("\u0104\u0141\u00D3\u015A\u017B\u0179\u0106\u0143\u0110 \u0105\u0142\u00F3\u015B\u017C\u017A\u0107\u0144\u0111")); assertEquals("The cafe\u2019s pinata gave me deja vu.", StringUtils.stripAccents("The caf\u00e9\u2019s pi\u00f1ata gave me d\u00e9j\u00e0 vu."), "Failed to handle accented text"); assertEquals("fluid quest", StringUtils.stripAccents("\ufb02uid que\ufb06"), "Failed to handle ligatures"); @@ -101,9 +101,23 @@ public class StringUtilsTrimStripTest extends AbstractLangTest { @Disabled public void testStripAccents_Korean() { // LANG-1655 - final String input = "\uC78A\uC9C0\uB9C8 \uB10C \uD750\uB9B0 \uC5B4\uB460\uC0AC\uC774 " + "\uC67C\uC190\uC73C\uB85C \uADF8\uB9B0 \uBCC4 \uD558\uB098"; + final String input = "\uC78A\uC9C0\uB9C8 \uB10C \uD750\uB9B0 \uC5B4\uB460\uC0AC\uC774 \uC67C\uC190\uC73C\uB85C \uADF8\uB9B0 \uBCC4 \uD558\uB098"; assertEquals(input, StringUtils.stripAccents(input), "Failed to handle Korean text"); + } + + @Test + public void testStripAccentsIWithBar() { + assertEquals("I i I i I", StringUtils.stripAccents("\u0197 \u0268 \u1D7B \u1DA4 \u1DA7")); + } + @Test + public void testStripAccentsUWithBar() { + assertEquals("U u U u", StringUtils.stripAccents("\u0244 \u0289 \u1D7E \u1DB6")); + } + + @Test + public void testStripAccentsTWithStroke() { + assertEquals("T t", StringUtils.stripAccents("\u0166 \u0167")); } @Test diff --git a/src/test/java/org/apache/commons/lang3/SystemPropertiesTest.java b/src/test/java/org/apache/commons/lang3/SystemPropertiesTest.java index a7cd1defa..2544794e8 100644 --- a/src/test/java/org/apache/commons/lang3/SystemPropertiesTest.java +++ b/src/test/java/org/apache/commons/lang3/SystemPropertiesTest.java @@ -647,6 +647,7 @@ public class SystemPropertiesTest { @Test public void testGetLineSeparator() { assertNotNull(SystemProperties.getLineSeparator()); + assertNotNull(SystemProperties.getLineSeparator(null)); assertNotNull(SystemProperties.getLineSeparator(() -> "")); assertNotNull(SystemProperties.getLineSeparator(() -> "\n")); assertNotNull(SystemProperties.getLineSeparator(() -> null)); diff --git a/src/test/java/org/apache/commons/lang3/concurrent/AbstractConcurrentInitializerTest.java b/src/test/java/org/apache/commons/lang3/concurrent/AbstractConcurrentInitializerTest.java index f2b8517ee..da8f5636c 100644 --- a/src/test/java/org/apache/commons/lang3/concurrent/AbstractConcurrentInitializerTest.java +++ b/src/test/java/org/apache/commons/lang3/concurrent/AbstractConcurrentInitializerTest.java @@ -28,23 +28,49 @@ import org.junit.jupiter.api.Test; /** * <p> - * An abstract base class for tests of concrete {@code ConcurrentInitializer} - * implementations. + * An abstract base class for tests of concrete {@code ConcurrentInitializer} implementations. * </p> * <p> - * This class provides some basic tests for initializer implementations. Derived - * class have to create a {@link ConcurrentInitializer} object on which the - * tests are executed. + * This class provides some basic tests for initializer implementations. Derived class have to create a {@link ConcurrentInitializer} object on which the tests + * are executed. * </p> + * + * @param <T> Domain type. */ -public abstract class AbstractConcurrentInitializerTest extends AbstractLangTest { +public abstract class AbstractConcurrentInitializerTest<T> extends AbstractLangTest { + + static final class GetThread extends Thread { + + private Object object; + private final CountDownLatch startLatch; + private final ConcurrentInitializer<?> initializer; + + GetThread(final CountDownLatch startLatch, final ConcurrentInitializer<?> initializer) { + this.startLatch = startLatch; + this.initializer = initializer; + } + + @Override + public void run() { + try { + // wait until all threads are ready for maximum parallelism + startLatch.await(); + // access the initializer + object = initializer.get(); + } catch (final InterruptedException iex) { + // ignore + } catch (final ConcurrentException cex) { + object = cex; + } + } + } + /** - * Creates the {@link ConcurrentInitializer} object to be tested. This - * method is called whenever the test fixture needs to be obtained. + * Creates the {@link ConcurrentInitializer} object to be tested. This method is called whenever the test fixture needs to be obtained. * * @return the initializer object to be tested */ - protected abstract ConcurrentInitializer<Object> createInitializer(); + protected abstract ConcurrentInitializer<T> createInitializer(); /** * Tests a simple invocation of the get() method. @@ -57,39 +83,19 @@ public abstract class AbstractConcurrentInitializerTest extends AbstractLangTest } /** - * Tests whether get() can be invoked from multiple threads concurrently. - * Always the same object should be returned. + * Tests whether get() can be invoked from multiple threads concurrently. Always the same object should be returned. * * @throws org.apache.commons.lang3.concurrent.ConcurrentException because the object under test may throw it. - * @throws InterruptedException because the threading API my throw it. + * @throws InterruptedException because the threading API my throw it. */ @Test - public void testGetConcurrent() throws ConcurrentException, - InterruptedException { - final ConcurrentInitializer<Object> initializer = createInitializer(); + public void testGetConcurrent() throws ConcurrentException, InterruptedException { + final ConcurrentInitializer<T> initializer = createInitializer(); final int threadCount = 20; final CountDownLatch startLatch = new CountDownLatch(1); - final class GetThread extends Thread { - Object object; - - @Override - public void run() { - try { - // wait until all threads are ready for maximum parallelism - startLatch.await(); - // access the initializer - object = initializer.get(); - } catch (final InterruptedException iex) { - // ignore - } catch (final ConcurrentException cex) { - object = cex; - } - } - } - final GetThread[] threads = new GetThread[threadCount]; for (int i = 0; i < threadCount; i++) { - threads[i] = new GetThread(); + threads[i] = new GetThread(startLatch, initializer); threads[i].start(); } @@ -107,14 +113,13 @@ public abstract class AbstractConcurrentInitializerTest extends AbstractLangTest } /** - * Tests whether sequential get() invocations always return the same - * instance. + * Tests whether sequential get() invocations always return the same instance. * * @throws org.apache.commons.lang3.concurrent.ConcurrentException because the object under test may throw it. */ @Test public void testGetMultipleTimes() throws ConcurrentException { - final ConcurrentInitializer<Object> initializer = createInitializer(); + final ConcurrentInitializer<T> initializer = createInitializer(); final Object obj = initializer.get(); for (int i = 0; i < 10; i++) { assertEquals(obj, initializer.get(), "Got different object at " + i); @@ -123,12 +128,15 @@ public abstract class AbstractConcurrentInitializerTest extends AbstractLangTest /** * Tests a simple invocation of the isInitialized() method. + * + * @throws Throwable on test failure. */ @Test public void testisInitialized() throws Throwable { - final ConcurrentInitializer<Object> initializer = createInitializer(); + final ConcurrentInitializer<T> initializer = createInitializer(); if (initializer instanceof AbstractConcurrentInitializer) { - final AbstractConcurrentInitializer castedInitializer = (AbstractConcurrentInitializer) initializer; + @SuppressWarnings("unchecked") + final AbstractConcurrentInitializer<T, Exception> castedInitializer = (AbstractConcurrentInitializer<T, Exception>) initializer; assertFalse(castedInitializer.isInitialized(), "was initialized before get()"); assertNotNull(castedInitializer.get(), "No managed object"); assertTrue(castedInitializer.isInitialized(), "was not initialized after get()"); diff --git a/src/test/java/org/apache/commons/lang3/concurrent/AtomicInitializerNonObjectTest.java b/src/test/java/org/apache/commons/lang3/concurrent/AtomicInitializerNonObjectTest.java new file mode 100644 index 000000000..3a0966bfd --- /dev/null +++ b/src/test/java/org/apache/commons/lang3/concurrent/AtomicInitializerNonObjectTest.java @@ -0,0 +1,62 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.lang3.concurrent; + +import static org.junit.jupiter.api.Assertions.assertNull; + +import java.util.concurrent.atomic.AtomicInteger; + +import org.junit.jupiter.api.Test; + +/** + * Test class for {@code AtomicInitializer}. + */ +public class AtomicInitializerNonObjectTest extends AbstractConcurrentInitializerTest<Integer> { + + /** + * Returns the initializer to be tested. + * + * @return the {@code AtomicInitializer} + */ + @Override + protected ConcurrentInitializer<Integer> createInitializer() { + return new AtomicInitializer<Integer>() { + @Override + protected Integer initialize() { + return new Integer(0); + } + }; + } + + @Test + public void testGetThatReturnsNullFirstTime() throws ConcurrentException { + final AtomicInitializer<Integer> initializer = new AtomicInitializer<Integer>() { + final AtomicInteger firstRun = new AtomicInteger(1); + + @Override + protected Integer initialize() { + if (firstRun.getAndSet(0) == 1) { + return null; + } + return new Integer(0); + } + }; + + assertNull(initializer.get()); + assertNull(initializer.get()); + } +} diff --git a/src/test/java/org/apache/commons/lang3/concurrent/AtomicInitializerTest.java b/src/test/java/org/apache/commons/lang3/concurrent/AtomicInitializerObjectTest.java index a8e9d54d8..9ab1be5be 100644 --- a/src/test/java/org/apache/commons/lang3/concurrent/AtomicInitializerTest.java +++ b/src/test/java/org/apache/commons/lang3/concurrent/AtomicInitializerObjectTest.java @@ -25,7 +25,8 @@ import org.junit.jupiter.api.Test; /** * Test class for {@code AtomicInitializer}. */ -public class AtomicInitializerTest extends AbstractConcurrentInitializerTest { +public class AtomicInitializerObjectTest extends AbstractConcurrentInitializerTest<Object> { + /** * Returns the initializer to be tested. * diff --git a/src/test/java/org/apache/commons/lang3/concurrent/locks/LockingVisitorsTest.java b/src/test/java/org/apache/commons/lang3/concurrent/locks/LockingVisitorsTest.java index cdf78dae4..74343e891 100644 --- a/src/test/java/org/apache/commons/lang3/concurrent/locks/LockingVisitorsTest.java +++ b/src/test/java/org/apache/commons/lang3/concurrent/locks/LockingVisitorsTest.java @@ -93,8 +93,10 @@ public class LockingVisitorsTest extends AbstractLangTest { final AtomicInteger res = new AtomicInteger(); final ReadWriteLock rwLock = new ReentrantReadWriteLock(); LockingVisitors.create(res, rwLock).acceptReadLocked(AtomicInteger::incrementAndGet); + LockingVisitors.create(res, rwLock).acceptReadLocked(null); assertEquals(1, res.get()); LockingVisitors.create(res, rwLock).acceptWriteLocked(AtomicInteger::incrementAndGet); + LockingVisitors.create(res, rwLock).acceptWriteLocked(null); assertEquals(2, res.get()); } diff --git a/src/test/java/org/apache/commons/lang3/event/EventUtilsTest.java b/src/test/java/org/apache/commons/lang3/event/EventUtilsTest.java index defdbf924..4a896a2ea 100644 --- a/src/test/java/org/apache/commons/lang3/event/EventUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/event/EventUtilsTest.java @@ -163,10 +163,10 @@ public class EventUtilsTest extends AbstractLangTest { final PropertyChangeSource src = new PropertyChangeSource(); final EventCountingInvocationHandler handler = new EventCountingInvocationHandler(); final ObjectChangeListener listener = handler.createListener(ObjectChangeListener.class); - final IllegalArgumentException e = - assertThrows(IllegalArgumentException.class, () -> EventUtils.addEventListener(src, ObjectChangeListener.class, listener)); - assertEquals("Class " + src.getClass().getName() + " does not have a public add" + ObjectChangeListener.class.getSimpleName() + " method which takes a parameter of type " + ObjectChangeListener.class.getName() + ".", - e.getMessage()); + final IllegalArgumentException e = assertThrows(IllegalArgumentException.class, + () -> EventUtils.addEventListener(src, ObjectChangeListener.class, listener)); + assertEquals("Unable to add listener for class " + src.getClass().getName() + " and public add" + ObjectChangeListener.class.getSimpleName() + + " method which takes a parameter of type " + ObjectChangeListener.class.getName() + ".", e.getMessage()); } @Test @@ -174,10 +174,10 @@ public class EventUtilsTest extends AbstractLangTest { final PropertyChangeSource src = new PropertyChangeSource(); final EventCountingInvocationHandler handler = new EventCountingInvocationHandler(); final VetoableChangeListener listener = handler.createListener(VetoableChangeListener.class); - final IllegalArgumentException e = - assertThrows(IllegalArgumentException.class, () -> EventUtils.addEventListener(src, VetoableChangeListener.class, listener)); - assertEquals("Class " + src.getClass().getName() + " does not have a public add" + VetoableChangeListener.class.getSimpleName() + " method which takes a parameter of type " + VetoableChangeListener.class.getName() + ".", - e.getMessage()); + final IllegalArgumentException e = assertThrows(IllegalArgumentException.class, + () -> EventUtils.addEventListener(src, VetoableChangeListener.class, listener)); + assertEquals("Unable to add listener for class " + src.getClass().getName() + " and public add" + VetoableChangeListener.class.getSimpleName() + + " method which takes a parameter of type " + VetoableChangeListener.class.getName() + ".", e.getMessage()); } @Test diff --git a/src/test/java/org/apache/commons/lang3/exception/ExceptionUtilsTest.java b/src/test/java/org/apache/commons/lang3/exception/ExceptionUtilsTest.java index 6ce2fdd80..f3b4d217d 100644 --- a/src/test/java/org/apache/commons/lang3/exception/ExceptionUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/exception/ExceptionUtilsTest.java @@ -739,7 +739,6 @@ public class ExceptionUtilsTest extends AbstractLangTest { assertThrows(NullPointerException.class, () -> ExceptionUtils.removeCommonFrames(null, null)); } - @SuppressWarnings("deprecation") @Test public void testRethrow() { final Exception expected = new InterruptedException(); diff --git a/src/test/java/org/apache/commons/lang3/reflect/MethodUtilsTest.java b/src/test/java/org/apache/commons/lang3/reflect/MethodUtilsTest.java index 9709f7b9d..bc3ce272e 100644 --- a/src/test/java/org/apache/commons/lang3/reflect/MethodUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/reflect/MethodUtilsTest.java @@ -71,6 +71,7 @@ public class MethodUtilsTest extends AbstractLangTest { } private static final class GetMatchingMethodClass { + public void testMethod() { } @@ -686,6 +687,13 @@ public class MethodUtilsTest extends AbstractLangTest { } @Test + public void testGetMethodObject() throws Exception { + assertEquals(MutableObject.class.getMethod("getValue", ArrayUtils.EMPTY_CLASS_ARRAY), + MethodUtils.getMethodObject(MutableObject.class, "getValue", ArrayUtils.EMPTY_CLASS_ARRAY)); + assertNull(MethodUtils.getMethodObject(MutableObject.class, "does not exist, at all", ArrayUtils.EMPTY_CLASS_ARRAY)); + } + + @Test @Annotated public void testGetMethodsListWithAnnotation() throws NoSuchMethodException { assertEquals(0, MethodUtils.getMethodsListWithAnnotation(Object.class, Annotated.class).size()); diff --git a/src/test/java/org/apache/commons/lang3/reflect/TypeUtilsTest.java b/src/test/java/org/apache/commons/lang3/reflect/TypeUtilsTest.java index dca2096b5..d80a390b0 100644 --- a/src/test/java/org/apache/commons/lang3/reflect/TypeUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/reflect/TypeUtilsTest.java @@ -58,10 +58,13 @@ import org.junit.jupiter.api.Test; /** * Test fixture for https://issues.apache.org/jira/browse/LANG-1524 + * + * @param <T> Test fixture type, unused type parameter for test. */ class AAAAClass<T extends AAAAClass.BBBBClass.CCCClass> { public static class BBBBClass { public static class CCCClass { + // empty } } } @@ -72,9 +75,18 @@ final class AAAClass extends AAClass<String> { } } -@SuppressWarnings("unused") // Unused type parameter for test +/** + * Test fixture. + * + * @param <T> Test fixture type, unused type parameter for test. + */ class AAClass<T> { + /** + * Test fixture. + * + * @param <S> Test fixture type, unused type parameter for test. + */ public class BBClass<S> { // empty } @@ -156,13 +168,16 @@ abstract class Test1<G> { } /** - * Test TypeUtils + * Tests {@link TypeUtils}. + * + * @param <B> Type for test fixtures. */ -@SuppressWarnings({ "unchecked", "unused", "rawtypes" }) +@SuppressWarnings({ "unused", "rawtypes" }) //raw types, where used, are used purposely public class TypeUtilsTest<B> extends AbstractLangTest { public interface And<K, V> extends This<Number, Number> { + // empty } public static class ClassWithSuperClassWithGenericType extends ArrayList<Object> { @@ -175,24 +190,31 @@ public class TypeUtilsTest<B> extends AbstractLangTest { /** This non-static inner class is parameterized. */ private class MyInnerClass<T> { + // empty } public class Other<T> implements This<String, T> { + // empty } public class Tester implements This<String, B> { + // empty } public class That<K, V> implements This<K, V> { + // empty } public class The<K, V> extends That<Number, Number> implements And<String, String> { + // empty } public class Thing<Q> extends Other<B> { + // empty } public interface This<K, V> { + // empty } public static Comparable<String> stringComparable; diff --git a/src/test/java/org/apache/commons/lang3/time/DurationFormatUtilsTest.java b/src/test/java/org/apache/commons/lang3/time/DurationFormatUtilsTest.java index cbd4833e7..7df279573 100644 --- a/src/test/java/org/apache/commons/lang3/time/DurationFormatUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/time/DurationFormatUtilsTest.java @@ -32,6 +32,7 @@ import java.util.Calendar; import java.util.TimeZone; import org.apache.commons.lang3.AbstractLangTest; +import org.apache.commons.lang3.time.DurationFormatUtils.Token; import org.junit.jupiter.api.Test; import org.junitpioneer.jupiter.DefaultTimeZone; @@ -82,10 +83,13 @@ public class DurationFormatUtilsTest extends AbstractLangTest { } } - private DurationFormatUtils.Token createTokenWithCount(final Object value, final int count) { + private DurationFormatUtils.Token createTokenWithCount(final CharSequence value, final int count) { final DurationFormatUtils.Token token = new DurationFormatUtils.Token(value, false, -1); + // To help debugging, toString() on a Token should never blow up. + assertNotNull(token.toString()); for (int i = 1; i < count; i++) { token.increment(); + assertNotNull(token.toString()); } return token; } @@ -113,6 +117,7 @@ public class DurationFormatUtilsTest extends AbstractLangTest { new int[] { 2006, 1, 26, 10, 47, 34 }, "yyyy/MM/dd HH:mm:ss SSS"); } + @SuppressWarnings("deprecation") @Test public void testConstructor() { assertNotNull(new DurationFormatUtils()); @@ -141,101 +146,66 @@ public class DurationFormatUtilsTest extends AbstractLangTest { public void testEdgeDurations() { // This test case must use a time zone without DST TimeZone.setDefault(FastTimeZone.getGmtTimeZone()); - assertEqualDuration("01", new int[] { 2006, 0, 15, 0, 0, 0 }, - new int[] { 2006, 2, 10, 0, 0, 0 }, "MM"); - assertEqualDuration("12", new int[] { 2005, 0, 15, 0, 0, 0 }, - new int[] { 2006, 0, 15, 0, 0, 0 }, "MM"); - assertEqualDuration("12", new int[] { 2005, 0, 15, 0, 0, 0 }, - new int[] { 2006, 0, 16, 0, 0, 0 }, "MM"); - assertEqualDuration("11", new int[] { 2005, 0, 15, 0, 0, 0 }, - new int[] { 2006, 0, 14, 0, 0, 0 }, "MM"); - - assertEqualDuration("01 26", new int[] { 2006, 0, 15, 0, 0, 0 }, - new int[] { 2006, 2, 10, 0, 0, 0 }, "MM dd"); - assertEqualDuration("54", new int[] { 2006, 0, 15, 0, 0, 0 }, - new int[] { 2006, 2, 10, 0, 0, 0 }, "dd"); - - assertEqualDuration("09 12", new int[] { 2006, 1, 20, 0, 0, 0 }, - new int[] { 2006, 11, 4, 0, 0, 0 }, "MM dd"); - assertEqualDuration("287", new int[] { 2006, 1, 20, 0, 0, 0 }, - new int[] { 2006, 11, 4, 0, 0, 0 }, "dd"); - - assertEqualDuration("11 30", new int[] { 2006, 0, 2, 0, 0, 0 }, - new int[] { 2007, 0, 1, 0, 0, 0 }, "MM dd"); - assertEqualDuration("364", new int[] { 2006, 0, 2, 0, 0, 0 }, - new int[] { 2007, 0, 1, 0, 0, 0 }, "dd"); - - assertEqualDuration("12 00", new int[] { 2006, 0, 1, 0, 0, 0 }, - new int[] { 2007, 0, 1, 0, 0, 0 }, "MM dd"); - assertEqualDuration("365", new int[] { 2006, 0, 1, 0, 0, 0 }, - new int[] { 2007, 0, 1, 0, 0, 0 }, "dd"); - - assertEqualDuration("31", new int[] { 2006, 0, 1, 0, 0, 0 }, - new int[] { 2006, 1, 1, 0, 0, 0 }, "dd"); - - assertEqualDuration("92", new int[] { 2005, 9, 1, 0, 0, 0 }, - new int[] { 2006, 0, 1, 0, 0, 0 }, "dd"); - assertEqualDuration("77", new int[] { 2005, 9, 16, 0, 0, 0 }, - new int[] { 2006, 0, 1, 0, 0, 0 }, "dd"); + assertEqualDuration("01", new int[] { 2006, 0, 15, 0, 0, 0 }, new int[] { 2006, 2, 10, 0, 0, 0 }, "MM"); + assertEqualDuration("12", new int[] { 2005, 0, 15, 0, 0, 0 }, new int[] { 2006, 0, 15, 0, 0, 0 }, "MM"); + assertEqualDuration("12", new int[] { 2005, 0, 15, 0, 0, 0 }, new int[] { 2006, 0, 16, 0, 0, 0 }, "MM"); + assertEqualDuration("11", new int[] { 2005, 0, 15, 0, 0, 0 }, new int[] { 2006, 0, 14, 0, 0, 0 }, "MM"); + + assertEqualDuration("01 26", new int[] { 2006, 0, 15, 0, 0, 0 }, new int[] { 2006, 2, 10, 0, 0, 0 }, "MM dd"); + assertEqualDuration("54", new int[] { 2006, 0, 15, 0, 0, 0 }, new int[] { 2006, 2, 10, 0, 0, 0 }, "dd"); + + assertEqualDuration("09 12", new int[] { 2006, 1, 20, 0, 0, 0 }, new int[] { 2006, 11, 4, 0, 0, 0 }, "MM dd"); + assertEqualDuration("287", new int[] { 2006, 1, 20, 0, 0, 0 }, new int[] { 2006, 11, 4, 0, 0, 0 }, "dd"); + + assertEqualDuration("11 30", new int[] { 2006, 0, 2, 0, 0, 0 }, new int[] { 2007, 0, 1, 0, 0, 0 }, "MM dd"); + assertEqualDuration("364", new int[] { 2006, 0, 2, 0, 0, 0 }, new int[] { 2007, 0, 1, 0, 0, 0 }, "dd"); + + assertEqualDuration("12 00", new int[] { 2006, 0, 1, 0, 0, 0 }, new int[] { 2007, 0, 1, 0, 0, 0 }, "MM dd"); + assertEqualDuration("365", new int[] { 2006, 0, 1, 0, 0, 0 }, new int[] { 2007, 0, 1, 0, 0, 0 }, "dd"); + + assertEqualDuration("31", new int[] { 2006, 0, 1, 0, 0, 0 }, new int[] { 2006, 1, 1, 0, 0, 0 }, "dd"); + + assertEqualDuration("92", new int[] { 2005, 9, 1, 0, 0, 0 }, new int[] { 2006, 0, 1, 0, 0, 0 }, "dd"); + assertEqualDuration("77", new int[] { 2005, 9, 16, 0, 0, 0 }, new int[] { 2006, 0, 1, 0, 0, 0 }, "dd"); // test month larger in start than end - assertEqualDuration("136", new int[] { 2005, 9, 16, 0, 0, 0 }, - new int[] { 2006, 2, 1, 0, 0, 0 }, "dd"); + assertEqualDuration("136", new int[] { 2005, 9, 16, 0, 0, 0 }, new int[] { 2006, 2, 1, 0, 0, 0 }, "dd"); // test when start in leap year - assertEqualDuration("136", new int[] { 2004, 9, 16, 0, 0, 0 }, - new int[] { 2005, 2, 1, 0, 0, 0 }, "dd"); + assertEqualDuration("136", new int[] { 2004, 9, 16, 0, 0, 0 }, new int[] { 2005, 2, 1, 0, 0, 0 }, "dd"); // test when end in leap year - assertEqualDuration("137", new int[] { 2003, 9, 16, 0, 0, 0 }, - new int[] { 2004, 2, 1, 0, 0, 0 }, "dd"); + assertEqualDuration("137", new int[] { 2003, 9, 16, 0, 0, 0 }, new int[] { 2004, 2, 1, 0, 0, 0 }, "dd"); // test when end in leap year but less than end of feb - assertEqualDuration("135", new int[] { 2003, 9, 16, 0, 0, 0 }, - new int[] { 2004, 1, 28, 0, 0, 0 }, "dd"); + assertEqualDuration("135", new int[] { 2003, 9, 16, 0, 0, 0 }, new int[] { 2004, 1, 28, 0, 0, 0 }, "dd"); - assertEqualDuration("364", new int[] { 2007, 0, 2, 0, 0, 0 }, - new int[] { 2008, 0, 1, 0, 0, 0 }, "dd"); - assertEqualDuration("729", new int[] { 2006, 0, 2, 0, 0, 0 }, - new int[] { 2008, 0, 1, 0, 0, 0 }, "dd"); + assertEqualDuration("364", new int[] { 2007, 0, 2, 0, 0, 0 }, new int[] { 2008, 0, 1, 0, 0, 0 }, "dd"); + assertEqualDuration("729", new int[] { 2006, 0, 2, 0, 0, 0 }, new int[] { 2008, 0, 1, 0, 0, 0 }, "dd"); - assertEqualDuration("365", new int[] { 2007, 2, 2, 0, 0, 0 }, - new int[] { 2008, 2, 1, 0, 0, 0 }, "dd"); - assertEqualDuration("333", new int[] { 2007, 1, 2, 0, 0, 0 }, - new int[] { 2008, 0, 1, 0, 0, 0 }, "dd"); + assertEqualDuration("365", new int[] { 2007, 2, 2, 0, 0, 0 }, new int[] { 2008, 2, 1, 0, 0, 0 }, "dd"); + assertEqualDuration("333", new int[] { 2007, 1, 2, 0, 0, 0 }, new int[] { 2008, 0, 1, 0, 0, 0 }, "dd"); - assertEqualDuration("28", new int[] { 2008, 1, 2, 0, 0, 0 }, - new int[] { 2008, 2, 1, 0, 0, 0 }, "dd"); - assertEqualDuration("393", new int[] { 2007, 1, 2, 0, 0, 0 }, - new int[] { 2008, 2, 1, 0, 0, 0 }, "dd"); + assertEqualDuration("28", new int[] { 2008, 1, 2, 0, 0, 0 }, new int[] { 2008, 2, 1, 0, 0, 0 }, "dd"); + assertEqualDuration("393", new int[] { 2007, 1, 2, 0, 0, 0 }, new int[] { 2008, 2, 1, 0, 0, 0 }, "dd"); - assertEqualDuration("369", new int[] { 2004, 0, 29, 0, 0, 0 }, - new int[] { 2005, 1, 1, 0, 0, 0 }, "dd"); + assertEqualDuration("369", new int[] { 2004, 0, 29, 0, 0, 0 }, new int[] { 2005, 1, 1, 0, 0, 0 }, "dd"); - assertEqualDuration("338", new int[] { 2004, 1, 29, 0, 0, 0 }, - new int[] { 2005, 1, 1, 0, 0, 0 }, "dd"); + assertEqualDuration("338", new int[] { 2004, 1, 29, 0, 0, 0 }, new int[] { 2005, 1, 1, 0, 0, 0 }, "dd"); - assertEqualDuration("28", new int[] { 2004, 2, 8, 0, 0, 0 }, - new int[] { 2004, 3, 5, 0, 0, 0 }, "dd"); + assertEqualDuration("28", new int[] { 2004, 2, 8, 0, 0, 0 }, new int[] { 2004, 3, 5, 0, 0, 0 }, "dd"); - assertEqualDuration("48", new int[] { 1992, 1, 29, 0, 0, 0 }, - new int[] { 1996, 1, 29, 0, 0, 0 }, "M"); + assertEqualDuration("48", new int[] { 1992, 1, 29, 0, 0, 0 }, new int[] { 1996, 1, 29, 0, 0, 0 }, "M"); // this seems odd - and will fail if I throw it in as a brute force // below as it expects the answer to be 12. It's a tricky edge case - assertEqualDuration("11", new int[] { 1996, 1, 29, 0, 0, 0 }, - new int[] { 1997, 1, 28, 0, 0, 0 }, "M"); + assertEqualDuration("11", new int[] { 1996, 1, 29, 0, 0, 0 }, new int[] { 1997, 1, 28, 0, 0, 0 }, "M"); // again - this seems odd - assertEqualDuration("11 28", new int[] { 1996, 1, 29, 0, 0, 0 }, - new int[] { 1997, 1, 28, 0, 0, 0 }, "M d"); + assertEqualDuration("11 28", new int[] { 1996, 1, 29, 0, 0, 0 }, new int[] { 1997, 1, 28, 0, 0, 0 }, "M d"); } @Test public void testEmptyOptionals() { - assertEquals( - "", - DurationFormatUtils.formatDuration(0L, "[d'd'][H'h'][m'm'][s's']")); - assertEquals( - "", - DurationFormatUtils.formatDuration(0L, "['d''h''m''s's]")); + assertEquals("", DurationFormatUtils.formatDuration(0L, "[d'd'][H'h'][m'm'][s's']")); + assertEquals("", DurationFormatUtils.formatDuration(0L, "['d''h''m''s's]")); } @Test @@ -491,11 +461,16 @@ public class DurationFormatUtilsTest extends AbstractLangTest { cal.set(Calendar.MILLISECOND, 0); time = cal.getTime().getTime(); assertEquals("40", DurationFormatUtils.formatPeriod(time1970, time, "yM")); + assertEquals("4 years 0 months", DurationFormatUtils.formatPeriod(time1970, time, "y' ''years' M 'months'")); assertEquals("4 years 0 months", DurationFormatUtils.formatPeriod(time1970, time, "y' years 'M' months'")); + assertEquals("4years 0months", DurationFormatUtils.formatPeriod(time1970, time, "y'years 'M'months'")); assertEquals("04/00", DurationFormatUtils.formatPeriod(time1970, time, "yy/MM")); assertEquals("48", DurationFormatUtils.formatPeriod(time1970, time, "M")); assertEquals("48", DurationFormatUtils.formatPeriod(time1970, time, "MM")); assertEquals("048", DurationFormatUtils.formatPeriod(time1970, time, "MMM")); + // no date in result + assertEquals("hello", DurationFormatUtils.formatPeriod(time1970, time, "'hello'")); + assertEquals("helloworld", DurationFormatUtils.formatPeriod(time1970, time, "'hello''world'")); } @Test @@ -530,6 +505,8 @@ public class DurationFormatUtilsTest extends AbstractLangTest { assertEquals("P1Y1M2DT10H30M0.000S", text); // want a way to say 'don't print the seconds in format()' or other fields for that matter: // assertEquals("P1Y2M3DT10H30M", text); + // + // TODO Jacoco shows missing coverage for internal negative days } @Test @@ -547,19 +524,21 @@ public class DurationFormatUtilsTest extends AbstractLangTest { // https://issues.apache.org/jira/browse/LANG-281 @Test public void testJiraLang281() { - assertEqualDuration("09", new int[] { 2005, 11, 31, 0, 0, 0 }, - new int[] { 2006, 9, 6, 0, 0, 0 }, "MM"); + assertEqualDuration("09", new int[] { 2005, 11, 31, 0, 0, 0 }, new int[] { 2006, 9, 6, 0, 0, 0 }, "MM"); } - // Takes a minute to run, so generally turned off -// public void testBrutally() { -// Calendar c = Calendar.getInstance(); -// c.set(2004, 0, 1, 0, 0, 0); -// for (int i=0; i < FOUR_YEARS; i++) { -// bruteForce(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH), "d", Calendar.DAY_OF_MONTH ); -// c.add(Calendar.DAY_OF_MONTH, 1); -// } -// } + /** + * Takes 8 seconds to run. + */ + @Test + public void testFourYears() { + Calendar c = Calendar.getInstance(); + c.set(2004, 0, 1, 0, 0, 0); + for (int i = 0; i < FOUR_YEARS; i++) { + bruteForce(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH), "d", Calendar.DAY_OF_MONTH); + c.add(Calendar.DAY_OF_MONTH, 1); + } + } @Test public void testLANG815() { @@ -641,10 +620,14 @@ public class DurationFormatUtilsTest extends AbstractLangTest { // test failures in equals final DurationFormatUtils.Token token = createTokenWithCount(DurationFormatUtils.y, 4); + assertEquals(token, token); + assertEquals(token.hashCode(), token.hashCode()); assertNotEquals(token, new Object(), "Token equal to non-Token class. "); - assertNotEquals(token, createTokenWithCount(new Object(), 1), "Token equal to Token with wrong value class. "); + final Token token2 = createTokenWithCount("", 1); + assertNotEquals(token, token2, "Token equal to Token with wrong value class. "); + assertNotEquals(token.hashCode(), token2.hashCode()); assertNotEquals(token, createTokenWithCount(DurationFormatUtils.y, 1), "Token equal to Token with different count. "); - final DurationFormatUtils.Token numToken = createTokenWithCount(Integer.valueOf(1), 4); + final DurationFormatUtils.Token numToken = createTokenWithCount("1", 4); assertEquals(numToken, numToken, "Token with Number value not equal to itself. "); } diff --git a/src/test/java/org/apache/commons/lang3/tuple/PairTest.java b/src/test/java/org/apache/commons/lang3/tuple/PairTest.java index 134adb2bd..a72032d10 100644 --- a/src/test/java/org/apache/commons/lang3/tuple/PairTest.java +++ b/src/test/java/org/apache/commons/lang3/tuple/PairTest.java @@ -27,6 +27,7 @@ import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Map.Entry; +import java.util.concurrent.ConcurrentHashMap; import org.apache.commons.lang3.AbstractLangTest; import org.junit.jupiter.api.Test; @@ -102,6 +103,11 @@ public class PairTest extends AbstractLangTest { } @Test + public void testConcurrentHashMapEntry() { + testMapEntry(new ConcurrentHashMap<>()); + } + + @Test public void testEmptyArrayGenerics() { final Pair<Integer, String>[] empty = Pair.emptyArray(); assertEquals(0, empty.length); @@ -127,13 +133,25 @@ public class PairTest extends AbstractLangTest { } @Test - public void testMapEntry() { - final Pair<Integer, String> pair = ImmutablePair.of(0, "foo"); - final HashMap<Integer, String> map = new HashMap<>(); + public void testHashMapEntry() { + testMapEntry(new HashMap<>()); + } + + private void testMapEntry(final Map<Integer, String> map) { map.put(0, "foo"); final Entry<Integer, String> entry = map.entrySet().iterator().next(); + final Pair<Integer, String> pair = ImmutablePair.of(0, "foo"); assertEquals(pair, entry); assertEquals(pair.hashCode(), entry.hashCode()); + // LANG-1736: + map.clear(); + map.put(0, "value1"); + map.put(1, "value2"); + map.entrySet().forEach(e -> { + final Pair<Integer, String> p = ImmutablePair.of(e.getKey(), e.getValue()); + assertEquals(p, e); + assertEquals(p.hashCode(), e.hashCode()); + }); } @Test |