Merge branch 'master' of https://github.com/apache/commons-lang into fix_FastDateParserTest

This commit is contained in:
XenoAmess 2020-06-26 16:55:41 +08:00
commit c7c2dd783a
118 changed files with 3272 additions and 1714 deletions

10
pom.xml
View File

@ -22,7 +22,7 @@
<parent> <parent>
<groupId>org.apache.commons</groupId> <groupId>org.apache.commons</groupId>
<artifactId>commons-parent</artifactId> <artifactId>commons-parent</artifactId>
<version>50</version> <version>51</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<artifactId>commons-lang3</artifactId> <artifactId>commons-lang3</artifactId>
@ -612,8 +612,8 @@
<uberjar.name>benchmarks</uberjar.name> <uberjar.name>benchmarks</uberjar.name>
<commons.jacoco.version>0.8.5</commons.jacoco.version> <commons.jacoco.version>0.8.5</commons.jacoco.version>
<commons.surefire.version>3.0.0-M4</commons.surefire.version> <commons.surefire.version>3.0.0-M5</commons.surefire.version>
<commons.javadoc.version>3.1.1</commons.javadoc.version> <commons.javadoc.version>3.2.0</commons.javadoc.version>
<!-- never generate report if there are binary incompatible changes --> <!-- never generate report if there are binary incompatible changes -->
<commons.japicmp.breakBuildOnBinaryIncompatibleModifications>true</commons.japicmp.breakBuildOnBinaryIncompatibleModifications> <commons.japicmp.breakBuildOnBinaryIncompatibleModifications>true</commons.japicmp.breakBuildOnBinaryIncompatibleModifications>
@ -764,7 +764,7 @@
<dependency> <dependency>
<groupId>com.github.spotbugs</groupId> <groupId>com.github.spotbugs</groupId>
<artifactId>spotbugs</artifactId> <artifactId>spotbugs</artifactId>
<version>4.0.4</version> <version>4.0.6</version>
</dependency> </dependency>
</dependencies> </dependencies>
<configuration> <configuration>
@ -778,7 +778,7 @@
<dependency> <dependency>
<groupId>biz.aQute.bnd</groupId> <groupId>biz.aQute.bnd</groupId>
<artifactId>biz.aQute.bndlib</artifactId> <artifactId>biz.aQute.bndlib</artifactId>
<version>5.1.0</version> <version>5.1.1</version>
</dependency> </dependency>
</dependencies> </dependencies>
</plugin> </plugin>

View File

@ -48,10 +48,6 @@ The <action> type attribute can be add,update,fix,remove.
<release version="3.11" date="2020-MM-DD" description="New features and bug fixes.."> <release version="3.11" date="2020-MM-DD" description="New features and bug fixes..">
<action type="update" dev="kinow" due-to="XenoAmess">remove encoding and docEncoding and use inherited values from commons-parent</action> <action type="update" dev="kinow" due-to="XenoAmess">remove encoding and docEncoding and use inherited values from commons-parent</action>
<action type="fix" dev="kinow" due-to="contextshuffling">Fix Javadoc for StringUtils.appendIfMissingIgnoreCase() #507.</action> <action type="fix" dev="kinow" due-to="contextshuffling">Fix Javadoc for StringUtils.appendIfMissingIgnoreCase() #507.</action>
<action type="update" dev="ggregory">org.junit-pioneer:junit-pioneer 0.5.4 -> 0.6.0.</action>
<action type="update" dev="ggregory">org.junit.jupiter:junit-jupiter 5.6.0 -> 5.6.1.</action>
<action type="update" dev="ggregory">com.github.spotbugs:spotbugs 4.0.0 -> 4.0.4.</action>
<action type="update" dev="ggregory">com.puppycrawl.tools:checkstyle 8.29 -> 8.33.</action>
<action type="update" dev="ggregory" due-to="Isira Seneviratne, Bruno P. Kinoshita">Simplify null checks in Pair.hashCode() using Objects.hashCode(). #517.</action> <action type="update" dev="ggregory" due-to="Isira Seneviratne, Bruno P. Kinoshita">Simplify null checks in Pair.hashCode() using Objects.hashCode(). #517.</action>
<action type="update" dev="ggregory" due-to="Isira Seneviratne, Bruno P. Kinoshita">Simplify null checks in Triple.hashCode() using Objects.hashCode(). #516.</action> <action type="update" dev="ggregory" due-to="Isira Seneviratne, Bruno P. Kinoshita">Simplify null checks in Triple.hashCode() using Objects.hashCode(). #516.</action>
<action type="update" dev="ggregory" due-to="Isira Seneviratne, Bruno P. Kinoshita">Simplify some if statements in StringUtils. #521.</action> <action type="update" dev="ggregory" due-to="Isira Seneviratne, Bruno P. Kinoshita">Simplify some if statements in StringUtils. #521.</action>
@ -75,9 +71,20 @@ The <action> type attribute can be add,update,fix,remove.
<action issue="LANG-1558" type="update" dev="ggregory" due-to="XenoAmess">Simplify if as some conditions are covered by others #543.</action> <action issue="LANG-1558" type="update" dev="ggregory" due-to="XenoAmess">Simplify if as some conditions are covered by others #543.</action>
<action issue="LANG-1528" type="fix" dev="ggregory" due-to="Edwin Delgado H">StringUtils.replaceEachRepeatedly gives IllegalStateException #505.</action> <action issue="LANG-1528" type="fix" dev="ggregory" due-to="Edwin Delgado H">StringUtils.replaceEachRepeatedly gives IllegalStateException #505.</action>
<action issue="LANG-1570" type="add" dev="ggregory" due-to="Edgar Asatryan">Add JavaVersion enum constants for Java 14 and 15. #553.</action> <action issue="LANG-1570" type="add" dev="ggregory" due-to="Edgar Asatryan">Add JavaVersion enum constants for Java 14 and 15. #553.</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add JavaVersion enum constants for Java 16.</action>
<action issue="LANG-1556" type="add" dev="ggregory" due-to="XenoAmess">Use Java 8 lambdas and Map operations.</action> <action issue="LANG-1556" type="add" dev="ggregory" due-to="XenoAmess">Use Java 8 lambdas and Map operations.</action>
<action issue="LANG-1565" type="add" dev="ggregory" due-to="XenoAmess">Change removeLastFieldSeparator to use endsWith #550.</action> <action issue="LANG-1565" type="add" dev="ggregory" due-to="XenoAmess">Change removeLastFieldSeparator to use endsWith #550.</action>
<action issue="LANG-1557" type="add" dev="ggregory" due-to="XenoAmess, Gary Gregory">Change a Pattern to a static final field, for not letting it compile each time the function invoked. #542.</action> <action issue="LANG-1557" type="add" dev="ggregory" due-to="XenoAmess, Gary Gregory">Change a Pattern to a static final field, for not letting it compile each time the function invoked. #542.</action>
<action type="add" dev="ggregory">Add ImmutablePair factory methods left() and right().</action>
<action type="add" dev="ggregory">Add ObjectUtils.toString(Object, Supplier&lt;String&gt;).</action>
<action issue="LANG-1567" type="update" dev="ggregory" due-to="Miguel Muñoz, Bruno P. Kinoshita, Gary Gregory">Fixed Javadocs for setTestRecursive() #556.</action>
<action issue="LANG-1542" type="update" dev="ggregory" due-to=" Trần Ngọc Khoa, Gary Gregory">ToStringBuilder.reflectionToString - Wrong JSON format when object has a List of Enum.</action>
<action type="update" dev="ggregory">org.apache.commons:commons-parent 50 -> 51.</action>
<action type="update" dev="ggregory">org.junit-pioneer:junit-pioneer 0.5.4 -> 0.6.0.</action>
<action type="update" dev="ggregory">org.junit.jupiter:junit-jupiter 5.6.0 -> 5.6.1.</action>
<action type="update" dev="ggregory">com.github.spotbugs:spotbugs 4.0.0 -> 4.0.6.</action>
<action type="update" dev="ggregory">com.puppycrawl.tools:checkstyle 8.29 -> 8.33.</action>
<action type="update" dev="ggregory">commons.surefire.version 3.0.0-M4 -> 3.0.0-M5..</action>
</release> </release>
<release version="3.10" date="2020-03-22" description="New features and bug fixes. Requires Java 8, supports Java 9, 10, 11."> <release version="3.10" date="2020-03-22" description="New features and bug fixes. Requires Java 8, supports Java 9, 10, 11.">

View File

@ -16,12 +16,12 @@
*/ */
package org.apache.commons.lang3; package org.apache.commons.lang3;
import org.apache.commons.lang3.arch.Processor;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.stream.Stream; import java.util.stream.Stream;
import org.apache.commons.lang3.arch.Processor;
/** /**
* An utility class for the os.arch System Property. The class defines methods for * An utility class for the os.arch System Property. The class defines methods for
* identifying the architecture of the current JVM. * identifying the architecture of the current JVM.

View File

@ -295,8 +295,8 @@ static boolean regionMatches(final CharSequence cs, final boolean ignoreCase, fi
} }
// The real same check as in String.regionMatches(): // The real same check as in String.regionMatches():
char u1 = Character.toUpperCase(c1); final char u1 = Character.toUpperCase(c1);
char u2 = Character.toUpperCase(c2); final char u2 = Character.toUpperCase(c2);
if (u1 != u2 && Character.toLowerCase(u1) != Character.toLowerCase(u2)) { if (u1 != u2 && Character.toLowerCase(u1) != Character.toLowerCase(u2)) {
return false; return false;
} }

View File

@ -33,13 +33,6 @@
import org.apache.commons.lang3.Streams.FailableStream; import org.apache.commons.lang3.Streams.FailableStream;
import org.apache.commons.lang3.function.FailableBooleanSupplier; import org.apache.commons.lang3.function.FailableBooleanSupplier;
import org.apache.commons.lang3.function.FailableDoubleBinaryOperator;
import org.apache.commons.lang3.function.FailableDoubleConsumer;
import org.apache.commons.lang3.function.FailableDoubleSupplier;
import org.apache.commons.lang3.function.FailableIntConsumer;
import org.apache.commons.lang3.function.FailableIntSupplier;
import org.apache.commons.lang3.function.FailableLongConsumer;
import org.apache.commons.lang3.function.FailableLongSupplier;
/** /**
* This class provides utility functions, and classes for working with the {@code java.util.function} package, or more * This class provides utility functions, and classes for working with the {@code java.util.function} package, or more
@ -313,42 +306,6 @@ public static <O, T extends Throwable> void accept(final FailableConsumer<O, T>
run(() -> consumer.accept(object)); run(() -> consumer.accept(object));
} }
/**
* Consumes a consumer and rethrows any exception as a {@link RuntimeException}.
*
* @param consumer the consumer to consume
* @param value the value to consume by {@code consumer}
* @param <T> the type of checked exception the consumer may throw
* @since 3.11
*/
public static <T extends Throwable> void accept(final FailableDoubleConsumer<T> consumer, final double value) {
run(() -> consumer.accept(value));
}
/**
* Consumes a consumer and rethrows any exception as a {@link RuntimeException}.
*
* @param consumer the consumer to consume
* @param value the value to consume by {@code consumer}
* @param <T> the type of checked exception the consumer may throw
* @since 3.11
*/
public static <T extends Throwable> void accept(final FailableIntConsumer<T> consumer, final int value) {
run(() -> consumer.accept(value));
}
/**
* Consumes a consumer and rethrows any exception as a {@link RuntimeException}.
*
* @param consumer the consumer to consume
* @param value the value to consume by {@code consumer}
* @param <T> the type of checked exception the consumer may throw
* @since 3.11
*/
public static <T extends Throwable> void accept(final FailableLongConsumer<T> consumer, final long value) {
run(() -> consumer.accept(value));
}
/** /**
* Applies a function and rethrows any exception as a {@link RuntimeException}. * Applies a function and rethrows any exception as a {@link RuntimeException}.
* *
@ -380,21 +337,6 @@ public static <I, O, T extends Throwable> O apply(final FailableFunction<I, O, T
return get(() -> function.apply(input)); return get(() -> function.apply(input));
} }
/**
* Applies a function and rethrows any exception as a {@link RuntimeException}.
*
* @param function the function to apply
* @param left the first input to apply {@code function} on
* @param right the second input to apply {@code function} on
* @param <T> the type of checked exception the function may throw
* @return the value returned from the function
* @since 3.11
*/
public static <T extends Throwable> double applyAsDouble(final FailableDoubleBinaryOperator<T> function,
final double left, final double right) {
return getAsDouble(() -> function.applyAsDouble(left, right));
}
/** /**
* Converts the given {@link FailableBiConsumer} into a standard {@link BiConsumer}. * Converts the given {@link FailableBiConsumer} into a standard {@link BiConsumer}.
* *
@ -542,9 +484,8 @@ public static <O, T extends Throwable> O get(final FailableSupplier<O, T> suppli
* @param supplier The boolean supplier to invoke. * @param supplier The boolean supplier to invoke.
* @param <T> The type of checked exception, which the supplier can throw. * @param <T> The type of checked exception, which the supplier can throw.
* @return The boolean, which has been created by the supplier * @return The boolean, which has been created by the supplier
* @since 3.11
*/ */
public static <T extends Throwable> boolean getAsBoolean(final FailableBooleanSupplier<T> supplier) { private static <T extends Throwable> boolean getAsBoolean(final FailableBooleanSupplier<T> supplier) {
try { try {
return supplier.getAsBoolean(); return supplier.getAsBoolean();
} catch (final Throwable t) { } catch (final Throwable t) {
@ -552,54 +493,6 @@ public static <T extends Throwable> boolean getAsBoolean(final FailableBooleanSu
} }
} }
/**
* Invokes a double supplier, and returns the result.
*
* @param supplier The double supplier to invoke.
* @param <T> The type of checked exception, which the supplier can throw.
* @return The boolean, which has been created by the supplier
* @since 3.11
*/
public static <T extends Throwable> double getAsDouble(final FailableDoubleSupplier<T> supplier) {
try {
return supplier.getAsDouble();
} catch (final Throwable t) {
throw rethrow(t);
}
}
/**
* Invokes an int supplier, and returns the result.
*
* @param supplier The int supplier to invoke.
* @param <T> The type of checked exception, which the supplier can throw.
* @return The boolean, which has been created by the supplier
* @since 3.11
*/
public static <T extends Throwable> int getAsInt(final FailableIntSupplier<T> supplier) {
try {
return supplier.getAsInt();
} catch (final Throwable t) {
throw rethrow(t);
}
}
/**
* Invokes a long supplier, and returns the result.
*
* @param supplier The long supplier to invoke.
* @param <T> The type of checked exception, which the supplier can throw.
* @return The boolean, which has been created by the supplier
* @since 3.11
*/
public static <T extends Throwable> long getAsLong(final FailableLongSupplier<T> supplier) {
try {
return supplier.getAsLong();
} catch (final Throwable t) {
throw rethrow(t);
}
}
/** /**
* <p> * <p>
* Rethrows a {@link Throwable} as an unchecked exception. If the argument is already unchecked, namely a * Rethrows a {@link Throwable} as an unchecked exception. If the argument is already unchecked, namely a
@ -697,7 +590,7 @@ public static <O> FailableStream<O> stream(final Stream<O> stream) {
*/ */
public static <O1, O2, T extends Throwable> boolean test(final FailableBiPredicate<O1, O2, T> predicate, public static <O1, O2, T extends Throwable> boolean test(final FailableBiPredicate<O1, O2, T> predicate,
final O1 object1, final O2 object2) { final O1 object1, final O2 object2) {
return get(() -> predicate.test(object1, object2)); return getAsBoolean(() -> predicate.test(object1, object2));
} }
/** /**
@ -710,7 +603,7 @@ public static <O1, O2, T extends Throwable> boolean test(final FailableBiPredica
* @return the boolean value returned by the predicate * @return the boolean value returned by the predicate
*/ */
public static <O, T extends Throwable> boolean test(final FailablePredicate<O, T> predicate, final O object) { public static <O, T extends Throwable> boolean test(final FailablePredicate<O, T> predicate, final O object) {
return get(() -> predicate.test(object)); return getAsBoolean(() -> predicate.test(object));
} }
/** /**

View File

@ -129,6 +129,13 @@ public enum JavaVersion {
*/ */
JAVA_15(15.0f, "15"), JAVA_15(15.0f, "15"),
/**
* Java 16
*
* @since 3.11
*/
JAVA_16(16.0f, "16"),
/** /**
* The most recent java version. Mainly introduced to avoid to break when a new version of Java is used. * The most recent java version. Mainly introduced to avoid to break when a new version of Java is used.
*/ */
@ -242,6 +249,8 @@ static JavaVersion get(final String nom) {
return JAVA_14; return JAVA_14;
} else if ("15".equals(nom)) { } else if ("15".equals(nom)) {
return JAVA_15; return JAVA_15;
} else if ("16".equals(nom)) {
return JAVA_16;
} }
final float v = toFloatVersion(nom); final float v = toFloatVersion(nom);
if ((v - 1.) < 1.) { // then we need to check decimals > .9 if ((v - 1.) < 1.) { // then we need to check decimals > .9

File diff suppressed because it is too large Load Diff

View File

@ -21,11 +21,11 @@
import java.text.Normalizer; import java.text.Normalizer;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Objects; import java.util.Objects;
import java.util.HashSet;
import java.util.Set; import java.util.Set;
import java.util.function.Supplier; import java.util.function.Supplier;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -6693,8 +6693,8 @@ private static String replaceEach(
// if recursing, this shouldn't be less than 0 // if recursing, this shouldn't be less than 0
if (timeToLive < 0) { if (timeToLive < 0) {
Set<String> searchSet = new HashSet<>(Arrays.asList(searchList)); final Set<String> searchSet = new HashSet<>(Arrays.asList(searchList));
Set<String> replacementSet = new HashSet<>(Arrays.asList(replacementList)); final Set<String> replacementSet = new HashSet<>(Arrays.asList(replacementList));
searchSet.retainAll(replacementSet); searchSet.retainAll(replacementSet);
if (searchSet.size() > 0) { if (searchSet.size() > 0) {
throw new IllegalStateException("Aborting to protect against StackOverflowError - " + throw new IllegalStateException("Aborting to protect against StackOverflowError - " +
@ -8277,7 +8277,7 @@ public static String[] stripAll(final String... strs) {
* @return the stripped Strings, {@code null} if null array input * @return the stripped Strings, {@code null} if null array input
*/ */
public static String[] stripAll(final String[] strs, final String stripChars) { public static String[] stripAll(final String[] strs, final String stripChars) {
int strsLen = ArrayUtils.getLength(strs); final int strsLen = ArrayUtils.getLength(strs);
if (strsLen == 0) { if (strsLen == 0) {
return strs; return strs;
} }

View File

@ -245,9 +245,12 @@ public EqualsBuilder setTestTransients(final boolean testTransients) {
} }
/** /**
* Set whether to include transient fields when reflectively comparing objects. * Set whether to test fields recursively, instead of using their equals method, when reflectively comparing objects.
* String objects, which cache a hash value, are automatically excluded from recursive testing.
* You may specify other exceptions by calling {@link #setBypassReflectionClasses(List)}.
* @param testRecursive whether to do a recursive test * @param testRecursive whether to do a recursive test
* @return EqualsBuilder - used to chain calls. * @return EqualsBuilder - used to chain calls.
* @see #setBypassReflectionClasses(List)
* @since 3.6 * @since 3.6
*/ */
public EqualsBuilder setTestRecursive(final boolean testRecursive) { public EqualsBuilder setTestRecursive(final boolean testRecursive) {
@ -265,6 +268,7 @@ public EqualsBuilder setTestRecursive(final boolean testRecursive) {
* your own set of classes here, remember to include {@code String} class, too.</p> * your own set of classes here, remember to include {@code String} class, too.</p>
* @param bypassReflectionClasses classes to bypass reflection test * @param bypassReflectionClasses classes to bypass reflection test
* @return EqualsBuilder - used to chain calls. * @return EqualsBuilder - used to chain calls.
* @see #setTestRecursive(boolean)
* @since 3.8 * @since 3.8
*/ */
public EqualsBuilder setBypassReflectionClasses(final List<Class<?>> bypassReflectionClasses) { public EqualsBuilder setBypassReflectionClasses(final List<Class<?>> bypassReflectionClasses) {

View File

@ -19,8 +19,8 @@
import java.lang.annotation.ElementType; import java.lang.annotation.ElementType;
import java.lang.annotation.Retention; import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import java.lang.annotation.RetentionPolicy; import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/** /**
* Use this annotation to exclude a field from being used by * Use this annotation to exclude a field from being used by

View File

@ -624,6 +624,16 @@ protected void appendDetail(final StringBuffer buffer, final String fieldName, f
* {@code toString}, not {@code null} * {@code toString}, not {@code null}
*/ */
protected void appendDetail(final StringBuffer buffer, final String fieldName, final Collection<?> coll) { protected void appendDetail(final StringBuffer buffer, final String fieldName, final Collection<?> coll) {
if (coll != null && !coll.isEmpty()) {
buffer.append(arrayStart);
int i = 0;
for (final Object item : coll) {
appendDetail(buffer, fieldName, i++, item);
}
buffer.append(arrayEnd);
return;
}
buffer.append(coll); buffer.append(coll);
} }
@ -919,18 +929,31 @@ protected void appendDetail(final StringBuffer buffer, final String fieldName, f
buffer.append(arrayStart); buffer.append(arrayStart);
for (int i = 0; i < array.length; i++) { for (int i = 0; i < array.length; i++) {
final Object item = array[i]; final Object item = array[i];
appendDetail(buffer, fieldName, i, item);
}
buffer.append(arrayEnd);
}
/**
* <p>Append to the {@code toString} the detail of an
* {@code Object} array item.</p>
*
* @param buffer the {@code StringBuffer} to populate
* @param fieldName the field name, typically not used as already appended
* @param i the array item index to add
* @param item the array item to add
* @since 3.11
*/
protected void appendDetail(final StringBuffer buffer, final String fieldName, final int i, final Object item) {
if (i > 0) { if (i > 0) {
buffer.append(arraySeparator); buffer.append(arraySeparator);
} }
if (item == null) { if (item == null) {
appendNullText(buffer, fieldName); appendNullText(buffer, fieldName);
} else { } else {
appendInternal(buffer, fieldName, item, arrayContentDetail); appendInternal(buffer, fieldName, item, arrayContentDetail);
} }
} }
buffer.append(arrayEnd);
}
/** /**
* <p>Append to the {@code toString} the detail of an array type.</p> * <p>Append to the {@code toString} the detail of an array type.</p>
@ -946,15 +969,7 @@ protected void reflectionAppendArrayDetail(final StringBuffer buffer, final Stri
final int length = Array.getLength(array); final int length = Array.getLength(array);
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {
final Object item = Array.get(array, i); final Object item = Array.get(array, i);
if (i > 0) { appendDetail(buffer, fieldName, i, item);
buffer.append(arraySeparator);
}
if (item == null) {
appendNullText(buffer, fieldName);
} else {
appendInternal(buffer, fieldName, item, arrayContentDetail);
}
} }
buffer.append(arrayEnd); buffer.append(arrayEnd);
} }

View File

@ -39,30 +39,30 @@
* the lock.</li> * the lock.</li>
* <li>If you want to access the locked object, create a {@link FailableConsumer}. The consumer will receive the locked * <li>If you want to access the locked object, create a {@link FailableConsumer}. The consumer will receive the locked
* object as a parameter. For convenience, the consumer may be implemented as a Lambda. Then invoke * object as a parameter. For convenience, the consumer may be implemented as a Lambda. Then invoke
* {@link Locks.Lock#runReadLocked(FailableConsumer)}, or {@link Locks.Lock#runWriteLocked(FailableConsumer)}, passing * {@link Locks.Lock#acceptReadLocked(FailableConsumer)}, or {@link Locks.Lock#acceptWriteLocked(FailableConsumer)}, passing
* the consumer.</li> * the consumer.</li>
* <li>As an alternative, if you need to produce a result object, you may use a {@link FailableFunction}. This function * <li>As an alternative, if you need to produce a result object, you may use a {@link FailableFunction}. This function
* may also be implemented as a Lambda. To have the function executed, invoke * may also be implemented as a Lambda. To have the function executed, invoke
* {@link Locks.Lock#callReadLocked(FailableFunction)}, or {@link Locks.Lock#callWriteLocked(FailableFunction)}.</li> * {@link Locks.Lock#applyReadLocked(FailableFunction)}, or {@link Locks.Lock#applyWriteLocked(FailableFunction)}.</li>
* </ol> * </ol>
* *
* Example: A thread safe logger class. * Example: A thread safe logger class.
* *
* <pre> * <pre>
* public class SimpleLogger { * public class SimpleLogger {
*
* private final Lock&lt;PrintStream&gt; lock; * private final Lock&lt;PrintStream&gt; lock;
* *
* public SimpleLogger(OutputStream out) { * public SimpleLogger(OutputStream out) {
* PrintStream ps = new PrintStream(out); * lock = Locks.lock(new PrintStream(out));
* lock = Locks.lock(ps);
* } * }
* *
* public void log(String message) { * public void log(String message) {
* lock.runWriteLocked((ps) -&gt; ps.println(message)); * lock.acceptWriteLocked((ps) -&gt; ps.println(message));
* } * }
* *
* public void log(byte[] buffer) { * public void log(byte[] buffer) {
* lock.runWriteLocked((ps) -&gt; { ps.write(buffer); ps.println(); }); * lock.acceptWriteLocked((ps) -&gt; { ps.write(buffer); ps.println(); });
* } * }
* </pre> * </pre>
* *
@ -71,30 +71,31 @@
public class Locks { public class Locks {
public static class Lock<O extends Object> { public static class Lock<O extends Object> {
private final O lockedObject;
private final StampedLock lock = new StampedLock(); private final StampedLock lock = new StampedLock();
private final O lockedObject;
public Lock(final O lockedObject) { public Lock(final O lockedObject) {
this.lockedObject = Objects.requireNonNull(lockedObject, "Locked Object"); this.lockedObject = Objects.requireNonNull(lockedObject, "Locked Object");
} }
public void runReadLocked(final FailableConsumer<O, ?> consumer) { public void acceptReadLocked(final FailableConsumer<O, ?> consumer) {
acceptLocked(() -> lock.readLock(), consumer); lockAcceptUnlock(() -> lock.readLock(), consumer);
} }
public void runWriteLocked(final FailableConsumer<O, ?> consumer) { public void acceptWriteLocked(final FailableConsumer<O, ?> consumer) {
acceptLocked(() -> lock.writeLock(), consumer); lockAcceptUnlock(() -> lock.writeLock(), consumer);
} }
public <T> T callReadLocked(final FailableFunction<O, T, ?> function) { public <T> T applyReadLocked(final FailableFunction<O, T, ?> function) {
return applyLocked(() -> lock.readLock(), function); return lockApplyUnlock(() -> lock.readLock(), function);
} }
public <T> T callWriteLocked(final FailableFunction<O, T, ?> function) { public <T> T applyWriteLocked(final FailableFunction<O, T, ?> function) {
return applyLocked(() -> lock.writeLock(), function); return lockApplyUnlock(() -> lock.writeLock(), function);
} }
protected void acceptLocked(final LongSupplier stampSupplier, final FailableConsumer<O, ?> consumer) { protected void lockAcceptUnlock(final LongSupplier stampSupplier, final FailableConsumer<O, ?> consumer) {
final long stamp = stampSupplier.getAsLong(); final long stamp = stampSupplier.getAsLong();
try { try {
consumer.accept(lockedObject); consumer.accept(lockedObject);
@ -105,7 +106,7 @@ protected void acceptLocked(final LongSupplier stampSupplier, final FailableCons
} }
} }
protected <T> T applyLocked(final LongSupplier stampSupplier, final FailableFunction<O, T, ?> function) { protected <T> T lockApplyUnlock(final LongSupplier stampSupplier, final FailableFunction<O, T, ?> function) {
final long stamp = stampSupplier.getAsLong(); final long stamp = stampSupplier.getAsLong();
try { try {
return function.apply(lockedObject); return function.apply(lockedObject);

View File

@ -16,13 +16,13 @@
*/ */
package org.apache.commons.lang3.concurrent; package org.apache.commons.lang3.concurrent;
import org.apache.commons.lang3.Validate;
import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture; import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import org.apache.commons.lang3.Validate;
/** /**
* <p> * <p>
* A specialized <em>semaphore</em> implementation that provides a number of * A specialized <em>semaphore</em> implementation that provides a number of

View File

@ -24,12 +24,12 @@
import java.lang.reflect.UndeclaredThrowableException; import java.lang.reflect.UndeclaredThrowableException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Objects;
import java.util.StringTokenizer; import java.util.StringTokenizer;
import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.ClassUtils; import org.apache.commons.lang3.ClassUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;
/** /**
* <p>Provides utilities for manipulating and examining * <p>Provides utilities for manipulating and examining
@ -624,20 +624,21 @@ public static void printRootCauseStackTrace(final Throwable throwable) {
* that don't have nested causes.</p> * that don't have nested causes.</p>
* *
* @param throwable the throwable to output, may be null * @param throwable the throwable to output, may be null
* @param stream the stream to output to, may not be null * @param printStream the stream to output to, may not be null
* @throws IllegalArgumentException if the stream is {@code null} * @throws NullPointerException if the printStream is {@code null}
* @since 2.0 * @since 2.0
*/ */
public static void printRootCauseStackTrace(final Throwable throwable, final PrintStream stream) { @SuppressWarnings("resource")
public static void printRootCauseStackTrace(final Throwable throwable, final PrintStream printStream) {
if (throwable == null) { if (throwable == null) {
return; return;
} }
Validate.notNull(stream, "The PrintStream must not be null"); Objects.requireNonNull(printStream, "printStream");
final String[] trace = getRootCauseStackTrace(throwable); final String[] trace = getRootCauseStackTrace(throwable);
for (final String element : trace) { for (final String element : trace) {
stream.println(element); printStream.println(element);
} }
stream.flush(); printStream.flush();
} }
/** /**
@ -655,20 +656,21 @@ public static void printRootCauseStackTrace(final Throwable throwable, final Pri
* that don't have nested causes.</p> * that don't have nested causes.</p>
* *
* @param throwable the throwable to output, may be null * @param throwable the throwable to output, may be null
* @param writer the writer to output to, may not be null * @param printWriter the writer to output to, may not be null
* @throws IllegalArgumentException if the writer is {@code null} * @throws NullPointerException if the printWriter is {@code null}
* @since 2.0 * @since 2.0
*/ */
public static void printRootCauseStackTrace(final Throwable throwable, final PrintWriter writer) { @SuppressWarnings("resource")
public static void printRootCauseStackTrace(final Throwable throwable, final PrintWriter printWriter) {
if (throwable == null) { if (throwable == null) {
return; return;
} }
Validate.notNull(writer, "The PrintWriter must not be null"); Objects.requireNonNull(printWriter, "printWriter");
final String[] trace = getRootCauseStackTrace(throwable); final String[] trace = getRootCauseStackTrace(throwable);
for (final String element : trace) { for (final String element : trace) {
writer.println(element); printWriter.println(element);
} }
writer.flush(); printWriter.flush();
} }
/** /**

View File

@ -41,15 +41,14 @@
* constructs like: * constructs like:
* *
* <pre> * <pre>
* {@code * Consumer&lt;java.lang.reflect.Method-&gt; consumer = (m) -&gt; {
* Consumer<java.lang.reflect.Method> consumer = (m) -> {
* try { * try {
* m.invoke(o, args); * m.invoke(o, args);
* } catch (Throwable t) { * } catch (Throwable t) {
* throw Failable.rethrow(t); * throw Failable.rethrow(t);
* } * }
* }; * };
* }</pre> * </pre>
* *
* <p> * <p>
* By replacing a {@link java.util.function.Consumer Consumer&lt;O&gt;} with a {@link FailableConsumer * By replacing a {@link java.util.function.Consumer Consumer&lt;O&gt;} with a {@link FailableConsumer
@ -57,9 +56,8 @@
* </p> * </p>
* *
* <pre> * <pre>
* {@code * Functions.accept((m) -&gt; m.invoke(o, args));
* Functions.accept((m) -> m.invoke(o,args)); * </pre>
* }</pre>
* *
* <p> * <p>
* Obviously, the second version is much more concise and the spirit of Lambda expressions is met better than the second * Obviously, the second version is much more concise and the spirit of Lambda expressions is met better than the second
@ -76,12 +74,12 @@ public class Failable {
* @param consumer the consumer to consume * @param consumer the consumer to consume
* @param object1 the first object to consume by {@code consumer} * @param object1 the first object to consume by {@code consumer}
* @param object2 the second object to consume by {@code consumer} * @param object2 the second object to consume by {@code consumer}
* @param <O1> the type of the first argument the consumer accepts * @param <T> the type of the first argument the consumer accepts
* @param <O2> the type of the second argument the consumer accepts * @param <U> the type of the second argument the consumer accepts
* @param <T> the type of checked exception the consumer may throw * @param <E> the type of checked exception the consumer may throw
*/ */
public static <O1, O2, T extends Throwable> void accept(final FailableBiConsumer<O1, O2, T> consumer, public static <T, U, E extends Throwable> void accept(final FailableBiConsumer<T, U, E> consumer, final T object1,
final O1 object1, final O2 object2) { final U object2) {
run(() -> consumer.accept(object1, object2)); run(() -> consumer.accept(object1, object2));
} }
@ -90,10 +88,10 @@ public static <O1, O2, T extends Throwable> void accept(final FailableBiConsumer
* *
* @param consumer the consumer to consume * @param consumer the consumer to consume
* @param object the object to consume by {@code consumer} * @param object the object to consume by {@code consumer}
* @param <O> the type the consumer accepts * @param <T> the type the consumer accepts
* @param <T> the type of checked exception the consumer may throw * @param <E> the type of checked exception the consumer may throw
*/ */
public static <O, T extends Throwable> void accept(final FailableConsumer<O, T> consumer, final O object) { public static <T, E extends Throwable> void accept(final FailableConsumer<T, E> consumer, final T object) {
run(() -> consumer.accept(object)); run(() -> consumer.accept(object));
} }
@ -102,9 +100,9 @@ public static <O, T extends Throwable> void accept(final FailableConsumer<O, T>
* *
* @param consumer the consumer to consume * @param consumer the consumer to consume
* @param value the value to consume by {@code consumer} * @param value the value to consume by {@code consumer}
* @param <T> the type of checked exception the consumer may throw * @param <E> the type of checked exception the consumer may throw
*/ */
public static <T extends Throwable> void accept(final FailableDoubleConsumer<T> consumer, final double value) { public static <E extends Throwable> void accept(final FailableDoubleConsumer<E> consumer, final double value) {
run(() -> consumer.accept(value)); run(() -> consumer.accept(value));
} }
@ -113,9 +111,9 @@ public static <T extends Throwable> void accept(final FailableDoubleConsumer<T>
* *
* @param consumer the consumer to consume * @param consumer the consumer to consume
* @param value the value to consume by {@code consumer} * @param value the value to consume by {@code consumer}
* @param <T> the type of checked exception the consumer may throw * @param <E> the type of checked exception the consumer may throw
*/ */
public static <T extends Throwable> void accept(final FailableIntConsumer<T> consumer, final int value) { public static <E extends Throwable> void accept(final FailableIntConsumer<E> consumer, final int value) {
run(() -> consumer.accept(value)); run(() -> consumer.accept(value));
} }
@ -124,9 +122,9 @@ public static <T extends Throwable> void accept(final FailableIntConsumer<T> con
* *
* @param consumer the consumer to consume * @param consumer the consumer to consume
* @param value the value to consume by {@code consumer} * @param value the value to consume by {@code consumer}
* @param <T> the type of checked exception the consumer may throw * @param <E> the type of checked exception the consumer may throw
*/ */
public static <T extends Throwable> void accept(final FailableLongConsumer<T> consumer, final long value) { public static <E extends Throwable> void accept(final FailableLongConsumer<E> consumer, final long value) {
run(() -> consumer.accept(value)); run(() -> consumer.accept(value));
} }
@ -136,14 +134,14 @@ public static <T extends Throwable> void accept(final FailableLongConsumer<T> co
* @param function the function to apply * @param function the function to apply
* @param input1 the first input to apply {@code function} on * @param input1 the first input to apply {@code function} on
* @param input2 the second input to apply {@code function} on * @param input2 the second input to apply {@code function} on
* @param <O1> the type of the first argument the function accepts * @param <T> the type of the first argument the function accepts
* @param <O2> the type of the second argument the function accepts * @param <U> the type of the second argument the function accepts
* @param <O> the return type of the function * @param <R> the return type of the function
* @param <T> the type of checked exception the function may throw * @param <E> the type of checked exception the function may throw
* @return the value returned from the function * @return the value returned from the function
*/ */
public static <O1, O2, O, T extends Throwable> O apply(final FailableBiFunction<O1, O2, O, T> function, public static <T, U, R, E extends Throwable> R apply(final FailableBiFunction<T, U, R, E> function, final T input1,
final O1 input1, final O2 input2) { final U input2) {
return get(() -> function.apply(input1, input2)); return get(() -> function.apply(input1, input2));
} }
@ -152,12 +150,12 @@ public static <O1, O2, O, T extends Throwable> O apply(final FailableBiFunction<
* *
* @param function the function to apply * @param function the function to apply
* @param input the input to apply {@code function} on * @param input the input to apply {@code function} on
* @param <I> the type of the argument the function accepts * @param <T> the type of the argument the function accepts
* @param <O> the return type of the function * @param <R> the return type of the function
* @param <T> the type of checked exception the function may throw * @param <E> the type of checked exception the function may throw
* @return the value returned from the function * @return the value returned from the function
*/ */
public static <I, O, T extends Throwable> O apply(final FailableFunction<I, O, T> function, final I input) { public static <T, R, E extends Throwable> R apply(final FailableFunction<T, R, E> function, final T input) {
return get(() -> function.apply(input)); return get(() -> function.apply(input));
} }
@ -167,10 +165,10 @@ public static <I, O, T extends Throwable> O apply(final FailableFunction<I, O, T
* @param function the function to apply * @param function the function to apply
* @param left the first input to apply {@code function} on * @param left the first input to apply {@code function} on
* @param right the second input to apply {@code function} on * @param right the second input to apply {@code function} on
* @param <T> the type of checked exception the function may throw * @param <E> the type of checked exception the function may throw
* @return the value returned from the function * @return the value returned from the function
*/ */
public static <T extends Throwable> double applyAsDouble(final FailableDoubleBinaryOperator<T> function, public static <E extends Throwable> double applyAsDouble(final FailableDoubleBinaryOperator<E> function,
final double left, final double right) { final double left, final double right) {
return getAsDouble(() -> function.applyAsDouble(left, right)); return getAsDouble(() -> function.applyAsDouble(left, right));
} }
@ -178,82 +176,82 @@ public static <T extends Throwable> double applyAsDouble(final FailableDoubleBin
/** /**
* Converts the given {@link FailableBiConsumer} into a standard {@link BiConsumer}. * Converts the given {@link FailableBiConsumer} into a standard {@link BiConsumer}.
* *
* @param <O1> the type of the first argument of the consumers * @param <T> the type of the first argument of the consumers
* @param <O2> the type of the second argument of the consumers * @param <U> the type of the second argument of the consumers
* @param consumer a failable {@code BiConsumer} * @param consumer a failable {@code BiConsumer}
* @return a standard {@code BiConsumer} * @return a standard {@code BiConsumer}
*/ */
public static <O1, O2> BiConsumer<O1, O2> asBiConsumer(final FailableBiConsumer<O1, O2, ?> consumer) { public static <T, U> BiConsumer<T, U> asBiConsumer(final FailableBiConsumer<T, U, ?> consumer) {
return (input1, input2) -> accept(consumer, input1, input2); return (input1, input2) -> accept(consumer, input1, input2);
} }
/** /**
* Converts the given {@link FailableBiFunction} into a standard {@link BiFunction}. * Converts the given {@link FailableBiFunction} into a standard {@link BiFunction}.
* *
* @param <O1> the type of the first argument of the input of the functions * @param <T> the type of the first argument of the input of the functions
* @param <O2> the type of the second argument of the input of the functions * @param <U> the type of the second argument of the input of the functions
* @param <O> the type of the output of the functions * @param <R> the type of the output of the functions
* @param function a {@code FailableBiFunction} * @param function a {@code FailableBiFunction}
* @return a standard {@code BiFunction} * @return a standard {@code BiFunction}
*/ */
public static <O1, O2, O> BiFunction<O1, O2, O> asBiFunction(final FailableBiFunction<O1, O2, O, ?> function) { public static <T, U, R> BiFunction<T, U, R> asBiFunction(final FailableBiFunction<T, U, R, ?> function) {
return (input1, input2) -> apply(function, input1, input2); return (input1, input2) -> apply(function, input1, input2);
} }
/** /**
* Converts the given {@link FailableBiPredicate} into a standard {@link BiPredicate}. * Converts the given {@link FailableBiPredicate} into a standard {@link BiPredicate}.
* *
* @param <O1> the type of the first argument used by the predicates * @param <T> the type of the first argument used by the predicates
* @param <O2> the type of the second argument used by the predicates * @param <U> the type of the second argument used by the predicates
* @param predicate a {@code FailableBiPredicate} * @param predicate a {@code FailableBiPredicate}
* @return a standard {@code BiPredicate} * @return a standard {@code BiPredicate}
*/ */
public static <O1, O2> BiPredicate<O1, O2> asBiPredicate(final FailableBiPredicate<O1, O2, ?> predicate) { public static <T, U> BiPredicate<T, U> asBiPredicate(final FailableBiPredicate<T, U, ?> predicate) {
return (input1, input2) -> test(predicate, input1, input2); return (input1, input2) -> test(predicate, input1, input2);
} }
/** /**
* Converts the given {@link FailableCallable} into a standard {@link Callable}. * Converts the given {@link FailableCallable} into a standard {@link Callable}.
* *
* @param <O> the type used by the callables * @param <V> the type used by the callables
* @param callable a {@code FailableCallable} * @param callable a {@code FailableCallable}
* @return a standard {@code Callable} * @return a standard {@code Callable}
*/ */
public static <O> Callable<O> asCallable(final FailableCallable<O, ?> callable) { public static <V> Callable<V> asCallable(final FailableCallable<V, ?> callable) {
return () -> call(callable); return () -> call(callable);
} }
/** /**
* Converts the given {@link FailableConsumer} into a standard {@link Consumer}. * Converts the given {@link FailableConsumer} into a standard {@link Consumer}.
* *
* @param <I> the type used by the consumers * @param <T> the type used by the consumers
* @param consumer a {@code FailableConsumer} * @param consumer a {@code FailableConsumer}
* @return a standard {@code Consumer} * @return a standard {@code Consumer}
*/ */
public static <I> Consumer<I> asConsumer(final FailableConsumer<I, ?> consumer) { public static <T> Consumer<T> asConsumer(final FailableConsumer<T, ?> consumer) {
return input -> accept(consumer, input); return input -> accept(consumer, input);
} }
/** /**
* Converts the given {@link FailableFunction} into a standard {@link Function}. * Converts the given {@link FailableFunction} into a standard {@link Function}.
* *
* @param <I> the type of the input of the functions * @param <T> the type of the input of the functions
* @param <O> the type of the output of the functions * @param <R> the type of the output of the functions
* @param function a {code FailableFunction} * @param function a {code FailableFunction}
* @return a standard {@code Function} * @return a standard {@code Function}
*/ */
public static <I, O> Function<I, O> asFunction(final FailableFunction<I, O, ?> function) { public static <T, R> Function<T, R> asFunction(final FailableFunction<T, R, ?> function) {
return input -> apply(function, input); return input -> apply(function, input);
} }
/** /**
* Converts the given {@link FailablePredicate} into a standard {@link Predicate}. * Converts the given {@link FailablePredicate} into a standard {@link Predicate}.
* *
* @param <I> the type used by the predicates * @param <T> the type used by the predicates
* @param predicate a {@code FailablePredicate} * @param predicate a {@code FailablePredicate}
* @return a standard {@code Predicate} * @return a standard {@code Predicate}
*/ */
public static <I> Predicate<I> asPredicate(final FailablePredicate<I, ?> predicate) { public static <T> Predicate<T> asPredicate(final FailablePredicate<T, ?> predicate) {
return input -> test(predicate, input); return input -> test(predicate, input);
} }
@ -270,11 +268,11 @@ public static Runnable asRunnable(final FailableRunnable<?> runnable) {
/** /**
* Converts the given {@link FailableSupplier} into a standard {@link Supplier}. * Converts the given {@link FailableSupplier} into a standard {@link Supplier}.
* *
* @param <O> the type supplied by the suppliers * @param <T> the type supplied by the suppliers
* @param supplier a {@code FailableSupplier} * @param supplier a {@code FailableSupplier}
* @return a standard {@code Supplier} * @return a standard {@code Supplier}
*/ */
public static <O> Supplier<O> asSupplier(final FailableSupplier<O, ?> supplier) { public static <T> Supplier<T> asSupplier(final FailableSupplier<T, ?> supplier) {
return () -> get(supplier); return () -> get(supplier);
} }
@ -282,11 +280,11 @@ public static <O> Supplier<O> asSupplier(final FailableSupplier<O, ?> supplier)
* Calls a callable and rethrows any exception as a {@link RuntimeException}. * Calls a callable and rethrows any exception as a {@link RuntimeException}.
* *
* @param callable the callable to call * @param callable the callable to call
* @param <O> the return type of the callable * @param <V> the return type of the callable
* @param <T> the type of checked exception the callable may throw * @param <E> the type of checked exception the callable may throw
* @return the value returned from the callable * @return the value returned from the callable
*/ */
public static <O, T extends Throwable> O call(final FailableCallable<O, T> callable) { public static <V, E extends Throwable> V call(final FailableCallable<V, E> callable) {
return get(callable::call); return get(callable::call);
} }
@ -294,11 +292,11 @@ public static <O, T extends Throwable> O call(final FailableCallable<O, T> calla
* Invokes a supplier, and returns the result. * Invokes a supplier, and returns the result.
* *
* @param supplier The supplier to invoke. * @param supplier The supplier to invoke.
* @param <O> The suppliers output type. * @param <T> The suppliers output type.
* @param <T> The type of checked exception, which the supplier can throw. * @param <E> The type of checked exception, which the supplier can throw.
* @return The object, which has been created by the supplier * @return The object, which has been created by the supplier
*/ */
public static <O, T extends Throwable> O get(final FailableSupplier<O, T> supplier) { public static <T, E extends Throwable> T get(final FailableSupplier<T, E> supplier) {
try { try {
return supplier.get(); return supplier.get();
} catch (final Throwable t) { } catch (final Throwable t) {
@ -310,10 +308,10 @@ public static <O, T extends Throwable> O get(final FailableSupplier<O, T> suppli
* Invokes a boolean supplier, and returns the result. * Invokes a boolean supplier, and returns the result.
* *
* @param supplier The boolean supplier to invoke. * @param supplier The boolean supplier to invoke.
* @param <T> The type of checked exception, which the supplier can throw. * @param <E> The type of checked exception, which the supplier can throw.
* @return The boolean, which has been created by the supplier * @return The boolean, which has been created by the supplier
*/ */
public static <T extends Throwable> boolean getAsBoolean(final FailableBooleanSupplier<T> supplier) { public static <E extends Throwable> boolean getAsBoolean(final FailableBooleanSupplier<E> supplier) {
try { try {
return supplier.getAsBoolean(); return supplier.getAsBoolean();
} catch (final Throwable t) { } catch (final Throwable t) {
@ -325,10 +323,10 @@ public static <T extends Throwable> boolean getAsBoolean(final FailableBooleanSu
* Invokes a double supplier, and returns the result. * Invokes a double supplier, and returns the result.
* *
* @param supplier The double supplier to invoke. * @param supplier The double supplier to invoke.
* @param <T> The type of checked exception, which the supplier can throw. * @param <E> The type of checked exception, which the supplier can throw.
* @return The boolean, which has been created by the supplier * @return The boolean, which has been created by the supplier
*/ */
public static <T extends Throwable> double getAsDouble(final FailableDoubleSupplier<T> supplier) { public static <E extends Throwable> double getAsDouble(final FailableDoubleSupplier<E> supplier) {
try { try {
return supplier.getAsDouble(); return supplier.getAsDouble();
} catch (final Throwable t) { } catch (final Throwable t) {
@ -340,10 +338,10 @@ public static <T extends Throwable> double getAsDouble(final FailableDoubleSuppl
* Invokes an int supplier, and returns the result. * Invokes an int supplier, and returns the result.
* *
* @param supplier The int supplier to invoke. * @param supplier The int supplier to invoke.
* @param <T> The type of checked exception, which the supplier can throw. * @param <E> The type of checked exception, which the supplier can throw.
* @return The boolean, which has been created by the supplier * @return The boolean, which has been created by the supplier
*/ */
public static <T extends Throwable> int getAsInt(final FailableIntSupplier<T> supplier) { public static <E extends Throwable> int getAsInt(final FailableIntSupplier<E> supplier) {
try { try {
return supplier.getAsInt(); return supplier.getAsInt();
} catch (final Throwable t) { } catch (final Throwable t) {
@ -355,10 +353,10 @@ public static <T extends Throwable> int getAsInt(final FailableIntSupplier<T> su
* Invokes a long supplier, and returns the result. * Invokes a long supplier, and returns the result.
* *
* @param supplier The long supplier to invoke. * @param supplier The long supplier to invoke.
* @param <T> The type of checked exception, which the supplier can throw. * @param <E> The type of checked exception, which the supplier can throw.
* @return The boolean, which has been created by the supplier * @return The boolean, which has been created by the supplier
*/ */
public static <T extends Throwable> long getAsLong(final FailableLongSupplier<T> supplier) { public static <E extends Throwable> long getAsLong(final FailableLongSupplier<E> supplier) {
try { try {
return supplier.getAsLong(); return supplier.getAsLong();
} catch (final Throwable t) { } catch (final Throwable t) {
@ -410,9 +408,9 @@ public static RuntimeException rethrow(final Throwable throwable) {
* Runs a runnable and rethrows any exception as a {@link RuntimeException}. * Runs a runnable and rethrows any exception as a {@link RuntimeException}.
* *
* @param runnable The runnable to run * @param runnable The runnable to run
* @param <T> the type of checked exception the runnable may throw * @param <E> the type of checked exception the runnable may throw
*/ */
public static <T extends Throwable> void run(final FailableRunnable<T> runnable) { public static <E extends Throwable> void run(final FailableRunnable<E> runnable) {
try { try {
runnable.run(); runnable.run();
} catch (final Throwable t) { } catch (final Throwable t) {
@ -429,10 +427,10 @@ public static <T extends Throwable> void run(final FailableRunnable<T> runnable)
* </pre> * </pre>
* *
* @param collection The collection, which is being converted into a {@link FailableStream}. * @param collection The collection, which is being converted into a {@link FailableStream}.
* @param <O> The collections element type. (In turn, the result streams element type.) * @param <E> The collections element type. (In turn, the result streams element type.)
* @return The created {@link FailableStream}. * @return The created {@link FailableStream}.
*/ */
public static <O> FailableStream<O> stream(final Collection<O> collection) { public static <E> FailableStream<E> stream(final Collection<E> collection) {
return new FailableStream<>(collection.stream()); return new FailableStream<>(collection.stream());
} }
@ -443,10 +441,10 @@ public static <O> FailableStream<O> stream(final Collection<O> collection) {
* {@link Function}, {@link Consumer}, etc. * {@link Function}, {@link Consumer}, etc.
* *
* @param stream The stream, which is being converted into a {@link FailableStream}. * @param stream The stream, which is being converted into a {@link FailableStream}.
* @param <O> The streams element type. * @param <T> The streams element type.
* @return The created {@link FailableStream}. * @return The created {@link FailableStream}.
*/ */
public static <O> FailableStream<O> stream(final Stream<O> stream) { public static <T> FailableStream<T> stream(final Stream<T> stream) {
return new FailableStream<>(stream); return new FailableStream<>(stream);
} }
@ -456,14 +454,14 @@ public static <O> FailableStream<O> stream(final Stream<O> stream) {
* @param predicate the predicate to test * @param predicate the predicate to test
* @param object1 the first input to test by {@code predicate} * @param object1 the first input to test by {@code predicate}
* @param object2 the second input to test by {@code predicate} * @param object2 the second input to test by {@code predicate}
* @param <O1> the type of the first argument the predicate tests * @param <T> the type of the first argument the predicate tests
* @param <O2> the type of the second argument the predicate tests * @param <U> the type of the second argument the predicate tests
* @param <T> the type of checked exception the predicate may throw * @param <E> the type of checked exception the predicate may throw
* @return the boolean value returned by the predicate * @return the boolean value returned by the predicate
*/ */
public static <O1, O2, T extends Throwable> boolean test(final FailableBiPredicate<O1, O2, T> predicate, public static <T, U, E extends Throwable> boolean test(final FailableBiPredicate<T, U, E> predicate,
final O1 object1, final O2 object2) { final T object1, final U object2) {
return get(() -> predicate.test(object1, object2)); return getAsBoolean(() -> predicate.test(object1, object2));
} }
/** /**
@ -471,12 +469,12 @@ public static <O1, O2, T extends Throwable> boolean test(final FailableBiPredica
* *
* @param predicate the predicate to test * @param predicate the predicate to test
* @param object the input to test by {@code predicate} * @param object the input to test by {@code predicate}
* @param <O> the type of argument the predicate tests * @param <T> the type of argument the predicate tests
* @param <T> the type of checked exception the predicate may throw * @param <E> the type of checked exception the predicate may throw
* @return the boolean value returned by the predicate * @return the boolean value returned by the predicate
*/ */
public static <O, T extends Throwable> boolean test(final FailablePredicate<O, T> predicate, final O object) { public static <T, E extends Throwable> boolean test(final FailablePredicate<T, E> predicate, final T object) {
return get(() -> predicate.test(object)); return getAsBoolean(() -> predicate.test(object));
} }
/** /**
@ -487,10 +485,8 @@ public static <O, T extends Throwable> boolean test(final FailablePredicate<O, T
* {@link Throwable} is rethrown. Example use: * {@link Throwable} is rethrown. Example use:
* *
* <pre> * <pre>
* {@code
* final FileInputStream fis = new FileInputStream("my.file"); * final FileInputStream fis = new FileInputStream("my.file");
* Functions.tryWithResources(useInputStream(fis), null, () -> fis.close()); * Functions.tryWithResources(useInputStream(fis), null, () -&gt; fis.close());
* }
* </pre> * </pre>
* *
* @param action The action to execute. This object <em>will</em> always be invoked. * @param action The action to execute. This object <em>will</em> always be invoked.
@ -549,10 +545,8 @@ public static void tryWithResources(final FailableRunnable<? extends Throwable>
* {@link Throwable} is rethrown. Example use: * {@link Throwable} is rethrown. Example use:
* *
* <pre> * <pre>
* {@code
* final FileInputStream fis = new FileInputStream("my.file"); * final FileInputStream fis = new FileInputStream("my.file");
* Functions.tryWithResources(useInputStream(fis), () -> fis.close()); * Functions.tryWithResources(useInputStream(fis), () -&gt; fis.close());
* }
* </pre> * </pre>
* *
* @param action The action to execute. This object <em>will</em> always be invoked. * @param action The action to execute. This object <em>will</em> always be invoked.

View File

@ -17,25 +17,57 @@
package org.apache.commons.lang3.function; package org.apache.commons.lang3.function;
import java.util.Objects;
import java.util.function.BiConsumer; import java.util.function.BiConsumer;
/** /**
* A functional interface like {@link BiConsumer} that declares a {@code Throwable}. * A functional interface like {@link BiConsumer} that declares a {@code Throwable}.
* *
* @param <O1> Consumed type 1. * @param <T> Consumed type 1.
* @param <O2> Consumed type 2. * @param <U> Consumed type 2.
* @param <T> Thrown exception. * @param <E> Thrown exception.
* @since 3.11 * @since 3.11
*/ */
@FunctionalInterface @FunctionalInterface
public interface FailableBiConsumer<O1, O2, T extends Throwable> { public interface FailableBiConsumer<T, U, E extends Throwable> {
/** NOP singleton */
@SuppressWarnings("rawtypes")
FailableBiConsumer NOP = (t, u) -> {/* NOP */};
/**
* Returns The NOP singleton.
*
* @param <T> Consumed type 1.
* @param <U> Consumed type 2.
* @param <E> Thrown exception.
* @return The NOP singleton.
*/
static <T, U, E extends Throwable> FailableBiConsumer<T, U, E> nop() {
return NOP;
}
/** /**
* Accepts the consumer. * Accepts the consumer.
* *
* @param object1 the first parameter for the consumable to accept * @param t the first parameter for the consumable to accept
* @param object2 the second parameter for the consumable to accept * @param u the second parameter for the consumable to accept
* @throws T Thrown when the consumer fails. * @throws E Thrown when the consumer fails.
*/ */
void accept(O1 object1, O2 object2) throws T; void accept(T t, U u) throws E;
/**
* Returns a composed {@code FailableBiConsumer} like {@link BiConsumer#andThen(BiConsumer)}.
*
* @param after the operation to perform after this one.
* @return a composed {@code FailableBiConsumer} like {@link BiConsumer#andThen(BiConsumer)}.
* @throws NullPointerException when {@code after} is null.
*/
default FailableBiConsumer<T, U, E> andThen(final FailableBiConsumer<? super T, ? super U, E> after) {
Objects.requireNonNull(after);
return (t, u) -> {
accept(t, u);
after.accept(t, u);
};
}
} }

View File

@ -17,19 +17,51 @@
package org.apache.commons.lang3.function; package org.apache.commons.lang3.function;
import java.util.Objects;
import java.util.function.BiFunction; import java.util.function.BiFunction;
import java.util.function.Function;
/** /**
* A functional interface like {@link BiFunction} that declares a {@code Throwable}. * A functional interface like {@link BiFunction} that declares a {@code Throwable}.
* *
* @param <O1> Input type 1. * @param <T> Input type 1.
* @param <O2> Input type 2. * @param <U> Input type 2.
* @param <R> Return type. * @param <R> Return type.
* @param <T> Thrown exception. * @param <E> Thrown exception.
* @since 3.11 * @since 3.11
*/ */
@FunctionalInterface @FunctionalInterface
public interface FailableBiFunction<O1, O2, R, T extends Throwable> { public interface FailableBiFunction<T, U, R, E extends Throwable> {
/** NOP singleton */
@SuppressWarnings("rawtypes")
FailableBiFunction NOP = (t, u) -> null;
/**
* Returns The NOP singleton.
*
* @param <T> Consumed type 1.
* @param <U> Consumed type 2.
* @param <R> Return type.
* @param <E> Thrown exception.
* @return The NOP singleton.
*/
static <T, U, R, E extends Throwable> FailableBiFunction<T, U, R, E> nop() {
return NOP;
}
/**
* Returns a composed {@code FailableBiFunction} that like {@link BiFunction#andThen(Function)}.
*
* @param <V> the output type of the {@code after} function, and of the composed function.
* @param after the operation to perform after this one.
* @return a composed {@code FailableBiFunction} that like {@link BiFunction#andThen(Function)}.
* @throws NullPointerException when {@code after} is null.
*/
default <V> FailableBiFunction<T, U, V, E> andThen(final FailableFunction<? super R, ? extends V, E> after) {
Objects.requireNonNull(after);
return (final T t, final U u) -> after.apply(apply(t, u));
}
/** /**
* Applies this function. * Applies this function.
@ -37,7 +69,7 @@ public interface FailableBiFunction<O1, O2, R, T extends Throwable> {
* @param input1 the first input for the function * @param input1 the first input for the function
* @param input2 the second input for the function * @param input2 the second input for the function
* @return the result of the function * @return the result of the function
* @throws T Thrown when the function fails. * @throws E Thrown when the function fails.
*/ */
R apply(O1 input1, O2 input2) throws T; R apply(T input1, U input2) throws E;
} }

View File

@ -17,18 +17,84 @@
package org.apache.commons.lang3.function; package org.apache.commons.lang3.function;
import java.util.Objects;
import java.util.function.BiPredicate; import java.util.function.BiPredicate;
/** /**
* A functional interface like {@link BiPredicate} that declares a {@code Throwable}. * A functional interface like {@link BiPredicate} that declares a {@code Throwable}.
* *
* @param <O1> Predicate type 1. * @param <T> Predicate type 1.
* @param <O2> Predicate type 2. * @param <U> Predicate type 2.
* @param <T> Thrown exception. * @param <E> Thrown exception.
* @since 3.11 * @since 3.11
*/ */
@FunctionalInterface @FunctionalInterface
public interface FailableBiPredicate<O1, O2, T extends Throwable> { public interface FailableBiPredicate<T, U, E extends Throwable> {
/** FALSE singleton */
@SuppressWarnings("rawtypes")
FailableBiPredicate FALSE = (t, u) -> false;
/** TRUE singleton */
@SuppressWarnings("rawtypes")
FailableBiPredicate TRUE = (t, u) -> true;
/**
* Returns The FALSE singleton.
*
* @param <T> Consumed type 1.
* @param <U> Consumed type 2.
* @param <E> Thrown exception.
* @return The NOP singleton.
*/
static <T, U, E extends Throwable> FailableBiPredicate<T, U, E> falsePredicate() {
return FALSE;
}
/**
* Returns The FALSE TRUE.
*
* @param <T> Consumed type 1.
* @param <U> Consumed type 2.
* @param <E> Thrown exception.
* @return The NOP singleton.
*/
static <T, U, E extends Throwable> FailableBiPredicate<T, U, E> truePredicate() {
return TRUE;
}
/**
* Returns a composed {@code FailableBiPredicate} like {@link BiPredicate#and(BiPredicate)}.
*
* @param other a predicate that will be logically-ANDed with this predicate.
* @return a composed {@code FailableBiPredicate} like {@link BiPredicate#and(BiPredicate)}.
* @throws NullPointerException if other is null
*/
default FailableBiPredicate<T, U, E> and(final FailableBiPredicate<? super T, ? super U, E> other) {
Objects.requireNonNull(other);
return (final T t, final U u) -> test(t, u) && other.test(t, u);
}
/**
* Returns a predicate that negates this predicate.
*
* @return a predicate that negates this predicate.
*/
default FailableBiPredicate<T, U, E> negate() {
return (final T t, final U u) -> !test(t, u);
}
/**
* Returns a composed {@code FailableBiPredicate} like {@link BiPredicate#and(BiPredicate)}.
*
* @param other a predicate that will be logically-ORed with this predicate.
* @return a composed {@code FailableBiPredicate} like {@link BiPredicate#and(BiPredicate)}.
* @throws NullPointerException if other is null
*/
default FailableBiPredicate<T, U, E> or(final FailableBiPredicate<? super T, ? super U, E> other) {
Objects.requireNonNull(other);
return (final T t, final U u) -> test(t, u) || other.test(t, u);
}
/** /**
* Tests the predicate. * Tests the predicate.
@ -36,7 +102,7 @@ public interface FailableBiPredicate<O1, O2, T extends Throwable> {
* @param object1 the first object to test the predicate on * @param object1 the first object to test the predicate on
* @param object2 the second object to test the predicate on * @param object2 the second object to test the predicate on
* @return the predicate's evaluation * @return the predicate's evaluation
* @throws T if the predicate fails * @throws E Thrown when this predicate fails.
*/ */
boolean test(O1 object1, O2 object2) throws T; boolean test(T object1, U object2) throws E;
} }

View File

@ -22,17 +22,17 @@
/** /**
* A functional interface like {@link BooleanSupplier} that declares a {@code Throwable}. * A functional interface like {@link BooleanSupplier} that declares a {@code Throwable}.
* *
* @param <T> Thrown exception. * @param <E> Thrown exception.
* @since 3.11 * @since 3.11
*/ */
@FunctionalInterface @FunctionalInterface
public interface FailableBooleanSupplier<T extends Throwable> { public interface FailableBooleanSupplier<E extends Throwable> {
/** /**
* Supplies a boolean. * Supplies a boolean.
* *
* @return a result * @return a result
* @throws T if the supplier fails * @throws E if the supplier fails
*/ */
boolean getAsBoolean() throws T; boolean getAsBoolean() throws E;
} }

View File

@ -21,17 +21,17 @@
* A functional interface like {@link java.util.concurrent.Callable} that declares a {@code Throwable}. * A functional interface like {@link java.util.concurrent.Callable} that declares a {@code Throwable}.
* *
* @param <R> Return type. * @param <R> Return type.
* @param <T> Thrown exception. * @param <E> Thrown exception.
* @since 3.11 * @since 3.11
*/ */
@FunctionalInterface @FunctionalInterface
public interface FailableCallable<R, T extends Throwable> { public interface FailableCallable<R, E extends Throwable> {
/** /**
* Calls the callable. * Calls the callable.
* *
* @return The value returned from the callable * @return The value returned from the callable
* @throws T if the callable fails * @throws E if the callable fails
*/ */
R call() throws T; R call() throws E;
} }

View File

@ -17,23 +17,54 @@
package org.apache.commons.lang3.function; package org.apache.commons.lang3.function;
import java.util.Objects;
import java.util.function.Consumer; import java.util.function.Consumer;
/** /**
* A functional interface like {@link Consumer} that declares a {@code Throwable}. * A functional interface like {@link Consumer} that declares a {@code Throwable}.
* *
* @param <O> Consumed type 1. * @param <T> Consumed type 1.
* @param <T> Thrown exception. * @param <E> Thrown exception.
* @since 3.11 * @since 3.11
*/ */
@FunctionalInterface @FunctionalInterface
public interface FailableConsumer<O, T extends Throwable> { public interface FailableConsumer<T, E extends Throwable> {
/** NOP singleton */
@SuppressWarnings("rawtypes")
FailableConsumer NOP = t -> {/* NOP */};
/**
* Returns The NOP singleton.
*
* @param <T> Consumed type 1.
* @param <E> Thrown exception.
* @return The NOP singleton.
*/
static <T, E extends Throwable> FailableConsumer<T, E> nop() {
return NOP;
}
/** /**
* Accepts the consumer. * Accepts the consumer.
* *
* @param object the parameter for the consumable to accept * @param object the parameter for the consumable to accept
* @throws T Thrown when the consumer fails. * @throws E Thrown when the consumer fails.
*/ */
void accept(O object) throws T; void accept(T object) throws E;
/**
* Returns a composed {@code Consumer} like {@link Consumer#andThen(Consumer)}.
*
* @param after the operation to perform after this operation
* @return a composed {@code Consumer} like {@link Consumer#andThen(Consumer)}.
* @throws NullPointerException when {@code after} is null
*/
default FailableConsumer<T, E> andThen(final FailableConsumer<? super T, E> after) {
Objects.requireNonNull(after);
return (final T t) -> {
accept(t);
after.accept(t);
};
}
} }

View File

@ -22,11 +22,11 @@
/** /**
* A functional interface like {@link DoubleBinaryOperator} that declares a {@code Throwable}. * A functional interface like {@link DoubleBinaryOperator} that declares a {@code Throwable}.
* *
* @param <T> Thrown exception. * @param <E> Thrown exception.
* @since 3.11 * @since 3.11
*/ */
@FunctionalInterface @FunctionalInterface
public interface FailableDoubleBinaryOperator<T extends Throwable> { public interface FailableDoubleBinaryOperator<E extends Throwable> {
/** /**
* Applies this operator to the given operands. * Applies this operator to the given operands.
@ -34,7 +34,7 @@ public interface FailableDoubleBinaryOperator<T extends Throwable> {
* @param left the first operand * @param left the first operand
* @param right the second operand * @param right the second operand
* @return the operator result * @return the operator result
* @throws T if the operation fails * @throws E if the operation fails
*/ */
double applyAsDouble(double left, double right) throws T; double applyAsDouble(double left, double right) throws E;
} }

View File

@ -17,22 +17,52 @@
package org.apache.commons.lang3.function; package org.apache.commons.lang3.function;
import java.util.Objects;
import java.util.function.DoubleConsumer; import java.util.function.DoubleConsumer;
/** /**
* A functional interface like {@link DoubleConsumer} that declares a {@code Throwable}. * A functional interface like {@link DoubleConsumer} that declares a {@code Throwable}.
* *
* @param <T> Thrown exception. * @param <E> Thrown exception.
* @since 3.11 * @since 3.11
*/ */
@FunctionalInterface @FunctionalInterface
public interface FailableDoubleConsumer<T extends Throwable> { public interface FailableDoubleConsumer<E extends Throwable> {
/** NOP singleton */
@SuppressWarnings("rawtypes")
FailableDoubleConsumer NOP = t -> {/* NOP */};
/**
* Returns The NOP singleton.
*
* @param <E> Thrown exception.
* @return The NOP singleton.
*/
static <E extends Throwable> FailableDoubleConsumer<E> nop() {
return NOP;
}
/** /**
* Accepts the consumer. * Accepts the consumer.
* *
* @param value the parameter for the consumable to accept * @param value the parameter for the consumable to accept
* @throws T Thrown when the consumer fails. * @throws E Thrown when the consumer fails.
*/ */
void accept(double value) throws T; void accept(double value) throws E;
/**
* Returns a composed {@code FailableDoubleConsumer} like {@link DoubleConsumer#andThen(DoubleConsumer)}.
*
* @param after the operation to perform after this one.
* @return a composed {@code FailableDoubleConsumer} like {@link DoubleConsumer#andThen(DoubleConsumer)}.
* @throws NullPointerException when {@code after} is null.
*/
default FailableDoubleConsumer<E> andThen(final FailableDoubleConsumer<E> after) {
Objects.requireNonNull(after);
return (final double t) -> {
accept(t);
after.accept(t);
};
}
} }

View File

@ -23,18 +23,33 @@
* A functional interface like {@link DoubleFunction} that declares a {@code Throwable}. * A functional interface like {@link DoubleFunction} that declares a {@code Throwable}.
* *
* @param <R> Return type. * @param <R> Return type.
* @param <T> Thrown exception. * @param <E> Thrown exception.
* @since 3.11 * @since 3.11
*/ */
@FunctionalInterface @FunctionalInterface
public interface FailableDoubleFunction<R, T extends Throwable> { public interface FailableDoubleFunction<R, E extends Throwable> {
/** NOP singleton */
@SuppressWarnings("rawtypes")
FailableDoubleFunction NOP = t -> null;
/**
* Returns The NOP singleton.
*
* @param <R> Return type.
* @param <E> Thrown exception.
* @return The NOP singleton.
*/
static <R, E extends Throwable> FailableDoubleFunction<R, E> nop() {
return NOP;
}
/** /**
* Applies this function. * Applies this function.
* *
* @param input the input for the function * @param input the input for the function
* @return the result of the function * @return the result of the function
* @throws T Thrown when the function fails. * @throws E Thrown when the function fails.
*/ */
R apply(double input) throws T; R apply(double input) throws E;
} }

View File

@ -17,23 +17,85 @@
package org.apache.commons.lang3.function; package org.apache.commons.lang3.function;
import java.util.Objects;
import java.util.function.DoublePredicate; import java.util.function.DoublePredicate;
/** /**
* A functional interface like {@link DoublePredicate} that declares a {@code Throwable}. * A functional interface like {@link DoublePredicate} that declares a {@code Throwable}.
* *
* @param <T> Thrown exception. * @param <E> Thrown exception.
* @since 3.11 * @since 3.11
*/ */
@FunctionalInterface @FunctionalInterface
public interface FailableDoublePredicate<T extends Throwable> { public interface FailableDoublePredicate<E extends Throwable> {
/** FALSE singleton */
@SuppressWarnings("rawtypes")
FailableDoublePredicate FALSE = t -> false;
/** TRUE singleton */
@SuppressWarnings("rawtypes")
FailableDoublePredicate TRUE = t -> true;
/**
* Returns The FALSE singleton.
*
* @param <E> Thrown exception.
* @return The NOP singleton.
*/
static <E extends Throwable> FailableDoublePredicate<E> falsePredicate() {
return FALSE;
}
/**
* Returns The FALSE TRUE.
*
* @param <E> Thrown exception.
* @return The NOP singleton.
*/
static <E extends Throwable> FailableDoublePredicate<E> truePredicate() {
return TRUE;
}
/**
* Returns a composed {@code FailableDoublePredicate} like {@link DoublePredicate#and(DoublePredicate)}.
*
* @param other a predicate that will be logically-ANDed with this predicate.
* @return a composed {@code FailableDoublePredicate} like {@link DoublePredicate#and(DoublePredicate)}.
* @throws NullPointerException if other is null
*/
default FailableDoublePredicate<E> and(final FailableDoublePredicate<E> other) {
Objects.requireNonNull(other);
return t -> test(t) && other.test(t);
}
/**
* Returns a predicate that negates this predicate.
*
* @return a predicate that negates this predicate.
*/
default FailableDoublePredicate<E> negate() {
return t -> !test(t);
}
/**
* Returns a composed {@code FailableDoublePredicate} like {@link DoublePredicate#and(DoublePredicate)}.
*
* @param other a predicate that will be logically-ORed with this predicate.
* @return a composed {@code FailableDoublePredicate} like {@link DoublePredicate#and(DoublePredicate)}.
* @throws NullPointerException if other is null
*/
default FailableDoublePredicate<E> or(final FailableDoublePredicate<E> other) {
Objects.requireNonNull(other);
return t -> test(t) || other.test(t);
}
/** /**
* Tests the predicate. * Tests the predicate.
* *
* @param value the parameter for the predicate to accept. * @param value the parameter for the predicate to accept.
* @return {@code true} if the input argument matches the predicate, {@code false} otherwise. * @return {@code true} if the input argument matches the predicate, {@code false} otherwise.
* @throws T Thrown when the consumer fails. * @throws E Thrown when the consumer fails.
*/ */
boolean test(double value) throws T; boolean test(double value) throws E;
} }

View File

@ -22,17 +22,17 @@
/** /**
* A functional interface like {@link DoubleSupplier} that declares a {@code Throwable}. * A functional interface like {@link DoubleSupplier} that declares a {@code Throwable}.
* *
* @param <T> Thrown exception. * @param <E> Thrown exception.
* @since 3.11 * @since 3.11
*/ */
@FunctionalInterface @FunctionalInterface
public interface FailableDoubleSupplier<T extends Throwable> { public interface FailableDoubleSupplier<E extends Throwable> {
/** /**
* Supplies a double. * Supplies a double.
* *
* @return a result * @return a result
* @throws T if the supplier fails * @throws E if the supplier fails
*/ */
double getAsDouble() throws T; double getAsDouble() throws E;
} }

View File

@ -22,18 +22,32 @@
/** /**
* A functional interface like {@link DoubleToIntFunction} that declares a {@code Throwable}. * A functional interface like {@link DoubleToIntFunction} that declares a {@code Throwable}.
* *
* @param <T> Thrown exception. * @param <E> Thrown exception.
* @since 3.11 * @since 3.11
*/ */
@FunctionalInterface @FunctionalInterface
public interface FailableDoubleToIntFunction<T extends Throwable> { public interface FailableDoubleToIntFunction<E extends Throwable> {
/** NOP singleton */
@SuppressWarnings("rawtypes")
FailableDoubleToIntFunction NOP = t -> 0;
/**
* Returns The NOP singleton.
*
* @param <E> Thrown exception.
* @return The NOP singleton.
*/
static <E extends Throwable> FailableDoubleToIntFunction<E> nop() {
return NOP;
}
/** /**
* Applies this function to the given argument. * Applies this function to the given argument.
* *
* @param value the function argument * @param value the function argument
* @return the function result * @return the function result
* @throws T Thrown when the function fails. * @throws E Thrown when the function fails.
*/ */
int applyAsInt(double value) throws T; int applyAsInt(double value) throws E;
} }

View File

@ -22,18 +22,32 @@
/** /**
* A functional interface like {@link DoubleToLongFunction} that declares a {@code Throwable}. * A functional interface like {@link DoubleToLongFunction} that declares a {@code Throwable}.
* *
* @param <T> Thrown exception. * @param <E> Thrown exception.
* @since 3.11 * @since 3.11
*/ */
@FunctionalInterface @FunctionalInterface
public interface FailableDoubleToLongFunction<T extends Throwable> { public interface FailableDoubleToLongFunction<E extends Throwable> {
/** NOP singleton */
@SuppressWarnings("rawtypes")
FailableDoubleToLongFunction NOP = t -> 0;
/**
* Returns The NOP singleton.
*
* @param <E> Thrown exception.
* @return The NOP singleton.
*/
static <E extends Throwable> FailableDoubleToLongFunction<E> nop() {
return NOP;
}
/** /**
* Applies this function to the given argument. * Applies this function to the given argument.
* *
* @param value the function argument * @param value the function argument
* @return the function result * @return the function result
* @throws T if the operation fails * @throws E if the operation fails
*/ */
int applyAsLong(double value) throws T; int applyAsLong(double value) throws E;
} }

View File

@ -0,0 +1,93 @@
/*
* 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.function;
import java.util.Objects;
import java.util.function.DoubleUnaryOperator;
/**
* A functional interface like {@link DoubleUnaryOperator} that declares a {@code Throwable}.
*
* @param <E> Thrown exception.
* @since 3.11
*/
public interface FailableDoubleUnaryOperator<E extends Throwable> {
/** NOP singleton */
@SuppressWarnings("rawtypes")
FailableDoubleUnaryOperator NOP = t -> 0d;
/**
* Returns a unary operator that always returns its input argument.
*
* @param <E> Thrown exception.
* @return a unary operator that always returns its input argument
*/
static <E extends Throwable> FailableDoubleUnaryOperator<E> identity() {
return t -> t;
}
/**
* Returns The NOP singleton.
*
* @param <E> Thrown exception.
* @return The NOP singleton.
*/
static <E extends Throwable> FailableDoubleUnaryOperator<E> nop() {
return NOP;
}
/**
* Returns a composed {@code FailableDoubleUnaryOperator} like
* {@link DoubleUnaryOperator#andThen(DoubleUnaryOperator)}.
*
* @param after the operator to apply after this one.
* @return a composed {@code FailableDoubleUnaryOperator} like
* {@link DoubleUnaryOperator#andThen(DoubleUnaryOperator)}.
* @throws NullPointerException if after is null.
* @see #compose(FailableDoubleUnaryOperator)
*/
default FailableDoubleUnaryOperator<E> andThen(final FailableDoubleUnaryOperator<E> after) {
Objects.requireNonNull(after);
return (final double t) -> after.applyAsDouble(applyAsDouble(t));
}
/**
* Applies this operator to the given operand.
*
* @param operand the operand
* @return the operator result
* @throws E Thrown when a consumer fails.
*/
double applyAsDouble(double operand) throws E;
/**
* Returns a composed {@code FailableDoubleUnaryOperator} like
* {@link DoubleUnaryOperator#compose(DoubleUnaryOperator)}.
*
* @param before the operator to apply before this one.
* @return a composed {@code FailableDoubleUnaryOperator} like
* {@link DoubleUnaryOperator#compose(DoubleUnaryOperator)}.
* @throws NullPointerException if before is null.
* @see #andThen(FailableDoubleUnaryOperator)
*/
default FailableDoubleUnaryOperator<E> compose(final FailableDoubleUnaryOperator<E> before) {
Objects.requireNonNull(before);
return (final double v) -> applyAsDouble(before.applyAsDouble(v));
}
}

View File

@ -17,25 +17,80 @@
package org.apache.commons.lang3.function; package org.apache.commons.lang3.function;
import java.util.Objects;
import java.util.function.Function; import java.util.function.Function;
/** /**
* A functional interface like {@link Function} that declares a {@code Throwable}. * A functional interface like {@link Function} that declares a {@code Throwable}.
* *
* @param <I> Input type 1. * @param <T> Input type 1.
* @param <R> Return type. * @param <R> Return type.
* @param <T> Thrown exception. * @param <E> Thrown exception.
* @since 3.11 * @since 3.11
*/ */
@FunctionalInterface @FunctionalInterface
public interface FailableFunction<I, R, T extends Throwable> { public interface FailableFunction<T, R, E extends Throwable> {
/** NOP singleton */
@SuppressWarnings("rawtypes")
FailableFunction NOP = t -> null;
/**
* Returns a function that always returns its input argument.
*
* @param <T> the type of the input and output objects to the function
* @param <E> Thrown exception.
* @return a function that always returns its input argument
*/
static <T, E extends Throwable> FailableFunction<T, T, E> identity() {
return t -> t;
}
/**
* Returns The NOP singleton.
*
* @param <T> Consumed type 1.
* @param <R> Return type.
* @param <E> Thrown exception.
* @return The NOP singleton.
*/
static <T, R, E extends Throwable> FailableFunction<T, R, E> nop() {
return NOP;
}
/**
* Returns a composed {@code FailableFunction} like {@link Function#andThen(Function)}.
*
* @param <V> the output type of the {@code after} function, and of the composed function.
* @return a composed {@code FailableFunction} like {@link Function#andThen(Function)}.
* @param after the operation to perform after this one.
* @throws NullPointerException when {@code after} is null.
*/
default <V> FailableFunction<T, V, E> andThen(final FailableFunction<? super R, ? extends V, E> after) {
Objects.requireNonNull(after);
return (final T t) -> after.apply(apply(t));
}
/** /**
* Applies this function. * Applies this function.
* *
* @param input the input for the function * @param input the input for the function
* @return the result of the function * @return the result of the function
* @throws T Thrown when the function fails. * @throws E Thrown when the function fails.
*/ */
R apply(I input) throws T; R apply(T input) throws E;
/**
* Returns a composed {@code FailableFunction} like {@link Function#compose(Function)}.
*
* @param <V> the input type to the {@code before} function, and to the composed function.
* @param before the operator to apply before this one.
* @return a a composed {@code FailableFunction} like {@link Function#compose(Function)}.
* @throws NullPointerException if before is null.
* @see #andThen(FailableFunction)
*/
default <V> FailableFunction<V, R, E> compose(final FailableFunction<? super V, ? extends T, E> before) {
Objects.requireNonNull(before);
return (final V v) -> apply(before.apply(v));
}
} }

View File

@ -22,11 +22,11 @@
/** /**
* A functional interface like {@link IntBinaryOperator} that declares a {@code Throwable}. * A functional interface like {@link IntBinaryOperator} that declares a {@code Throwable}.
* *
* @param <T> Thrown exception. * @param <E> Thrown exception.
* @since 3.11 * @since 3.11
*/ */
@FunctionalInterface @FunctionalInterface
public interface FailableIntBinaryOperator<T extends Throwable> { public interface FailableIntBinaryOperator<E extends Throwable> {
/** /**
* Applies this operator to the given operands. * Applies this operator to the given operands.
@ -34,7 +34,7 @@ public interface FailableIntBinaryOperator<T extends Throwable> {
* @param left the first operand * @param left the first operand
* @param right the second operand * @param right the second operand
* @return the operator result * @return the operator result
* @throws T if the operation fails * @throws E if the operation fails
*/ */
int applyAsInt(int left, int right) throws T; int applyAsInt(int left, int right) throws E;
} }

View File

@ -17,22 +17,53 @@
package org.apache.commons.lang3.function; package org.apache.commons.lang3.function;
import java.util.Objects;
import java.util.function.IntConsumer; import java.util.function.IntConsumer;
/** /**
* A functional interface like {@link IntConsumer} that declares a {@code Throwable}. * A functional interface like {@link IntConsumer} that declares a {@code Throwable}.
* *
* @param <T> Thrown exception. * @param <E> Thrown exception.
* @since 3.11 * @since 3.11
*/ */
@FunctionalInterface @FunctionalInterface
public interface FailableIntConsumer<T extends Throwable> { public interface FailableIntConsumer<E extends Throwable> {
/** NOP singleton */
@SuppressWarnings("rawtypes")
FailableIntConsumer NOP = t -> {/* NOP */};
/**
* Returns The NOP singleton.
*
* @param <E> Thrown exception.
* @return The NOP singleton.
*/
static <E extends Throwable> FailableIntConsumer<E> nop() {
return NOP;
}
/** /**
* Accepts the consumer. * Accepts the consumer.
* *
* @param value the parameter for the consumable to accept * @param value the parameter for the consumable to accept
* @throws T Thrown when the consumer fails. * @throws E Thrown when the consumer fails.
*/ */
void accept(int value) throws T; void accept(int value) throws E;
/**
* Returns a composed {@code FailableIntConsumer} like {@link IntConsumer#andThen(IntConsumer)}.
*
* @param after the operation to perform after this one.
* @return a composed {@code FailableLongConsumer} like {@link IntConsumer#andThen(IntConsumer)}.
* @throws NullPointerException if {@code after} is null
*/
default FailableIntConsumer<E> andThen(final FailableIntConsumer<E> after) {
Objects.requireNonNull(after);
return (final int t) -> {
accept(t);
after.accept(t);
};
}
} }

View File

@ -23,18 +23,33 @@
* A functional interface like {@link IntFunction} that declares a {@code Throwable}. * A functional interface like {@link IntFunction} that declares a {@code Throwable}.
* *
* @param <R> Return type. * @param <R> Return type.
* @param <T> Thrown exception. * @param <E> Thrown exception.
* @since 3.11 * @since 3.11
*/ */
@FunctionalInterface @FunctionalInterface
public interface FailableIntFunction<R, T extends Throwable> { public interface FailableIntFunction<R, E extends Throwable> {
/** NOP singleton */
@SuppressWarnings("rawtypes")
FailableIntFunction NOP = t -> null;
/**
* Returns The NOP singleton.
*
* @param <R> Return type.
* @param <E> Thrown exception.
* @return The NOP singleton.
*/
static <R, E extends Throwable> FailableIntFunction<R, E> nop() {
return NOP;
}
/** /**
* Applies this function. * Applies this function.
* *
* @param input the input for the function * @param input the input for the function
* @return the result of the function * @return the result of the function
* @throws T Thrown when the function fails. * @throws E Thrown when the function fails.
*/ */
R apply(int input) throws T; R apply(int input) throws E;
} }

View File

@ -17,23 +17,85 @@
package org.apache.commons.lang3.function; package org.apache.commons.lang3.function;
import java.util.Objects;
import java.util.function.IntPredicate; import java.util.function.IntPredicate;
/** /**
* A functional interface like {@link IntPredicate} that declares a {@code Throwable}. * A functional interface like {@link IntPredicate} that declares a {@code Throwable}.
* *
* @param <T> Thrown exception. * @param <E> Thrown exception.
* @since 3.11 * @since 3.11
*/ */
@FunctionalInterface @FunctionalInterface
public interface FailableIntPredicate<T extends Throwable> { public interface FailableIntPredicate<E extends Throwable> {
/** FALSE singleton */
@SuppressWarnings("rawtypes")
FailableIntPredicate FALSE = t -> false;
/** TRUE singleton */
@SuppressWarnings("rawtypes")
FailableIntPredicate TRUE = t -> true;
/**
* Returns The FALSE singleton.
*
* @param <E> Thrown exception.
* @return The NOP singleton.
*/
static <E extends Throwable> FailableIntPredicate<E> falsePredicate() {
return FALSE;
}
/**
* Returns The FALSE TRUE.
*
* @param <E> Thrown exception.
* @return The NOP singleton.
*/
static <E extends Throwable> FailableIntPredicate<E> truePredicate() {
return TRUE;
}
/**
* Returns a composed {@code FailableIntPredicate} like {@link IntPredicate#and(IntPredicate)}.
*
* @param other a predicate that will be logically-ANDed with this predicate.
* @return a composed {@code FailableIntPredicate} like {@link IntPredicate#and(IntPredicate)}.
* @throws NullPointerException if other is null
*/
default FailableIntPredicate<E> and(final FailableIntPredicate<E> other) {
Objects.requireNonNull(other);
return t -> test(t) && other.test(t);
}
/**
* Returns a predicate that negates this predicate.
*
* @return a predicate that negates this predicate.
*/
default FailableIntPredicate<E> negate() {
return t -> !test(t);
}
/**
* Returns a composed {@code FailableIntPredicate} like {@link IntPredicate#and(IntPredicate)}.
*
* @param other a predicate that will be logically-ORed with this predicate.
* @return a composed {@code FailableIntPredicate} like {@link IntPredicate#and(IntPredicate)}.
* @throws NullPointerException if other is null
*/
default FailableIntPredicate<E> or(final FailableIntPredicate<E> other) {
Objects.requireNonNull(other);
return t -> test(t) || other.test(t);
}
/** /**
* Tests the predicate. * Tests the predicate.
* *
* @param value the parameter for the predicate to accept. * @param value the parameter for the predicate to accept.
* @return {@code true} if the input argument matches the predicate, {@code false} otherwise. * @return {@code true} if the input argument matches the predicate, {@code false} otherwise.
* @throws T Thrown when the consumer fails. * @throws E Thrown when the consumer fails.
*/ */
boolean test(int value) throws T; boolean test(int value) throws E;
} }

View File

@ -22,17 +22,17 @@
/** /**
* A functional interface like {@link IntSupplier} that declares a {@code Throwable}. * A functional interface like {@link IntSupplier} that declares a {@code Throwable}.
* *
* @param <T> Thrown exception. * @param <E> Thrown exception.
* @since 3.11 * @since 3.11
*/ */
@FunctionalInterface @FunctionalInterface
public interface FailableIntSupplier<T extends Throwable> { public interface FailableIntSupplier<E extends Throwable> {
/** /**
* Supplies an int. * Supplies an int.
* *
* @return a result * @return a result
* @throws T if the supplier fails * @throws E if the supplier fails
*/ */
int getAsInt() throws T; int getAsInt() throws E;
} }

View File

@ -22,18 +22,32 @@
/** /**
* A functional interface like {@link IntToDoubleFunction} that declares a {@code Throwable}. * A functional interface like {@link IntToDoubleFunction} that declares a {@code Throwable}.
* *
* @param <T> Thrown exception. * @param <E> Thrown exception.
* @since 3.11 * @since 3.11
*/ */
@FunctionalInterface @FunctionalInterface
public interface FailableIntToDoubleFunction<T extends Throwable> { public interface FailableIntToDoubleFunction<E extends Throwable> {
/** NOP singleton */
@SuppressWarnings("rawtypes")
FailableIntToDoubleFunction NOP = t -> 0d;
/**
* Returns The NOP singleton.
*
* @param <E> Thrown exception.
* @return The NOP singleton.
*/
static <E extends Throwable> FailableIntToDoubleFunction<E> nop() {
return NOP;
}
/** /**
* Applies this function to the given argument. * Applies this function to the given argument.
* *
* @param value the function argument * @param value the function argument
* @return the function result * @return the function result
* @throws T Thrown when the function fails. * @throws E Thrown when the function fails.
*/ */
double applyAsDouble(int value) throws T; double applyAsDouble(int value) throws E;
} }

View File

@ -22,18 +22,32 @@
/** /**
* A functional interface like {@link IntToLongFunction} that declares a {@code Throwable}. * A functional interface like {@link IntToLongFunction} that declares a {@code Throwable}.
* *
* @param <T> Thrown exception. * @param <E> Thrown exception.
* @since 3.11 * @since 3.11
*/ */
@FunctionalInterface @FunctionalInterface
public interface FailableIntToLongFunction<T extends Throwable> { public interface FailableIntToLongFunction<E extends Throwable> {
/** NOP singleton */
@SuppressWarnings("rawtypes")
FailableIntToLongFunction NOP = t -> 0L;
/**
* Returns The NOP singleton.
*
* @param <E> Thrown exception.
* @return The NOP singleton.
*/
static <E extends Throwable> FailableIntToLongFunction<E> nop() {
return NOP;
}
/** /**
* Applies this function to the given argument. * Applies this function to the given argument.
* *
* @param value the function argument * @param value the function argument
* @return the function result * @return the function result
* @throws T Thrown when the function fails. * @throws E Thrown when the function fails.
*/ */
long applyAsLong(int value) throws T; long applyAsLong(int value) throws E;
} }

View File

@ -0,0 +1,89 @@
/*
* 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.function;
import java.util.Objects;
import java.util.function.IntUnaryOperator;
/**
* A functional interface like {@link IntUnaryOperator} that declares a {@code Throwable}.
*
* @param <E> Thrown exception.
* @since 3.11
*/
public interface FailableIntUnaryOperator<E extends Throwable> {
/** NOP singleton */
@SuppressWarnings("rawtypes")
FailableIntUnaryOperator NOP = t -> 0;
/**
* Returns a unary operator that always returns its input argument.
*
* @param <E> Thrown exception.
* @return a unary operator that always returns its input argument
*/
static <E extends Throwable> FailableIntUnaryOperator<E> identity() {
return t -> t;
}
/**
* Returns The NOP singleton.
*
* @param <E> Thrown exception.
* @return The NOP singleton.
*/
static <E extends Throwable> FailableIntUnaryOperator<E> nop() {
return NOP;
}
/**
* Returns a composed {@code FailableDoubleUnaryOperator} like {@link IntUnaryOperator#andThen(IntUnaryOperator)}.
*
* @param after the operator to apply after this one.
* @return a composed {@code FailableIntUnaryOperator} like {@link IntUnaryOperator#andThen(IntUnaryOperator)}.
* @throws NullPointerException if after is null.
* @see #compose(FailableIntUnaryOperator)
*/
default FailableIntUnaryOperator<E> andThen(final FailableIntUnaryOperator<E> after) {
Objects.requireNonNull(after);
return (final int t) -> after.applyAsInt(applyAsInt(t));
}
/**
* Applies this operator to the given operand.
*
* @param operand the operand
* @return the operator result
* @throws E Thrown when a consumer fails.
*/
int applyAsInt(int operand) throws E;
/**
* Returns a composed {@code FailableIntUnaryOperator} like {@link IntUnaryOperator#compose(IntUnaryOperator)}.
*
* @param before the operator to apply before this one.
* @return a composed {@code FailableIntUnaryOperator} like {@link IntUnaryOperator#compose(IntUnaryOperator)}.
* @throws NullPointerException if before is null.
* @see #andThen(FailableIntUnaryOperator)
*/
default FailableIntUnaryOperator<E> compose(final FailableIntUnaryOperator<E> before) {
Objects.requireNonNull(before);
return (final int v) -> applyAsInt(before.applyAsInt(v));
}
}

View File

@ -22,11 +22,11 @@
/** /**
* A functional interface like {@link LongBinaryOperator} that declares a {@code Throwable}. * A functional interface like {@link LongBinaryOperator} that declares a {@code Throwable}.
* *
* @param <T> Thrown exception. * @param <E> Thrown exception.
* @since 3.11 * @since 3.11
*/ */
@FunctionalInterface @FunctionalInterface
public interface FailableLongBinaryOperator<T extends Throwable> { public interface FailableLongBinaryOperator<E extends Throwable> {
/** /**
* Applies this operator to the given operands. * Applies this operator to the given operands.
@ -34,7 +34,7 @@ public interface FailableLongBinaryOperator<T extends Throwable> {
* @param left the first operand * @param left the first operand
* @param right the second operand * @param right the second operand
* @return the operator result * @return the operator result
* @throws T if the operation fails * @throws E if the operation fails
*/ */
long applyAsLong(long left, long right) throws T; long applyAsLong(long left, long right) throws E;
} }

View File

@ -17,22 +17,52 @@
package org.apache.commons.lang3.function; package org.apache.commons.lang3.function;
import java.util.Objects;
import java.util.function.LongConsumer; import java.util.function.LongConsumer;
/** /**
* A functional interface like {@link LongConsumer} that declares a {@code Throwable}. * A functional interface like {@link LongConsumer} that declares a {@code Throwable}.
* *
* @param <T> Thrown exception. * @param <E> Thrown exception.
* @since 3.11 * @since 3.11
*/ */
@FunctionalInterface @FunctionalInterface
public interface FailableLongConsumer<T extends Throwable> { public interface FailableLongConsumer<E extends Throwable> {
/** NOP singleton */
@SuppressWarnings("rawtypes")
FailableLongConsumer NOP = t -> {/* NOP */};
/**
* Returns The NOP singleton.
*
* @param <E> Thrown exception.
* @return The NOP singleton.
*/
static <E extends Throwable> FailableLongConsumer<E> nop() {
return NOP;
}
/** /**
* Accepts the consumer. * Accepts the consumer.
* *
* @param object the parameter for the consumable to accept * @param object the parameter for the consumable to accept
* @throws T Thrown when the consumer fails. * @throws E Thrown when the consumer fails.
*/ */
void accept(long object) throws T; void accept(long object) throws E;
/**
* Returns a composed {@code FailableLongConsumer} like {@link LongConsumer#andThen(LongConsumer)}.
*
* @param after the operation to perform after this one.
* @return a composed {@code FailableLongConsumer} like {@link LongConsumer#andThen(LongConsumer)}.
* @throws NullPointerException if {@code after} is null
*/
default FailableLongConsumer<E> andThen(final FailableLongConsumer<E> after) {
Objects.requireNonNull(after);
return (final long t) -> {
accept(t);
after.accept(t);
};
}
} }

View File

@ -23,18 +23,33 @@
* A functional interface like {@link LongFunction} that declares a {@code Throwable}. * A functional interface like {@link LongFunction} that declares a {@code Throwable}.
* *
* @param <R> Return type. * @param <R> Return type.
* @param <T> Thrown exception. * @param <E> Thrown exception.
* @since 3.11 * @since 3.11
*/ */
@FunctionalInterface @FunctionalInterface
public interface FailableLongFunction<R, T extends Throwable> { public interface FailableLongFunction<R, E extends Throwable> {
/** NOP singleton */
@SuppressWarnings("rawtypes")
FailableLongFunction NOP = t -> null;
/**
* Returns The NOP singleton.
*
* @param <R> Return type.
* @param <E> Thrown exception.
* @return The NOP singleton.
*/
static <R, E extends Throwable> FailableLongFunction<R, E> nop() {
return NOP;
}
/** /**
* Applies this function. * Applies this function.
* *
* @param input the input for the function * @param input the input for the function
* @return the result of the function * @return the result of the function
* @throws T Thrown when the function fails. * @throws E Thrown when the function fails.
*/ */
R apply(long input) throws T; R apply(long input) throws E;
} }

View File

@ -17,23 +17,85 @@
package org.apache.commons.lang3.function; package org.apache.commons.lang3.function;
import java.util.Objects;
import java.util.function.LongPredicate; import java.util.function.LongPredicate;
/** /**
* A functional interface like {@link LongPredicate} that declares a {@code Throwable}. * A functional interface like {@link LongPredicate} that declares a {@code Throwable}.
* *
* @param <T> Thrown exception. * @param <E> Thrown exception.
* @since 3.11 * @since 3.11
*/ */
@FunctionalInterface @FunctionalInterface
public interface FailableLongPredicate<T extends Throwable> { public interface FailableLongPredicate<E extends Throwable> {
/** FALSE singleton */
@SuppressWarnings("rawtypes")
FailableLongPredicate FALSE = t -> false;
/** TRUE singleton */
@SuppressWarnings("rawtypes")
FailableLongPredicate TRUE = t -> true;
/**
* Returns The FALSE singleton.
*
* @param <E> Thrown exception.
* @return The NOP singleton.
*/
static <E extends Throwable> FailableLongPredicate<E> falsePredicate() {
return FALSE;
}
/**
* Returns The FALSE TRUE.
*
* @param <E> Thrown exception.
* @return The NOP singleton.
*/
static <E extends Throwable> FailableLongPredicate<E> truePredicate() {
return TRUE;
}
/**
* Returns a composed {@code FailableLongPredicate} like {@link LongPredicate#and(LongPredicate)}.
*
* @param other a predicate that will be logically-ANDed with this predicate.
* @return a composed {@code FailableLongPredicate} like {@link LongPredicate#and(LongPredicate)}.
* @throws NullPointerException if other is null
*/
default FailableLongPredicate<E> and(final FailableLongPredicate<E> other) {
Objects.requireNonNull(other);
return t -> test(t) && other.test(t);
}
/**
* Returns a predicate that negates this predicate.
*
* @return a predicate that negates this predicate.
*/
default FailableLongPredicate<E> negate() {
return t -> !test(t);
}
/**
* Returns a composed {@code FailableLongPredicate} like {@link LongPredicate#and(LongPredicate)}.
*
* @param other a predicate that will be logically-ORed with this predicate.
* @return a composed {@code FailableLongPredicate} like {@link LongPredicate#and(LongPredicate)}.
* @throws NullPointerException if other is null
*/
default FailableLongPredicate<E> or(final FailableLongPredicate<E> other) {
Objects.requireNonNull(other);
return t -> test(t) || other.test(t);
}
/** /**
* Tests the predicate. * Tests the predicate.
* *
* @param value the parameter for the predicate to accept. * @param value the parameter for the predicate to accept.
* @return {@code true} if the input argument matches the predicate, {@code false} otherwise. * @return {@code true} if the input argument matches the predicate, {@code false} otherwise.
* @throws T Thrown when the consumer fails. * @throws E Thrown when the consumer fails.
*/ */
boolean test(long value) throws T; boolean test(long value) throws E;
} }

View File

@ -22,17 +22,17 @@
/** /**
* A functional interface like {@link LongSupplier} that declares a {@code Throwable}. * A functional interface like {@link LongSupplier} that declares a {@code Throwable}.
* *
* @param <T> Thrown exception. * @param <E> Thrown exception.
* @since 3.11 * @since 3.11
*/ */
@FunctionalInterface @FunctionalInterface
public interface FailableLongSupplier<T extends Throwable> { public interface FailableLongSupplier<E extends Throwable> {
/** /**
* Supplies a long. * Supplies a long.
* *
* @return a result * @return a result
* @throws T if the supplier fails * @throws E if the supplier fails
*/ */
long getAsLong() throws T; long getAsLong() throws E;
} }

View File

@ -22,18 +22,32 @@
/** /**
* A functional interface like {@link LongToDoubleFunction} that declares a {@code Throwable}. * A functional interface like {@link LongToDoubleFunction} that declares a {@code Throwable}.
* *
* @param <T> Thrown exception. * @param <E> Thrown exception.
* @since 3.11 * @since 3.11
*/ */
@FunctionalInterface @FunctionalInterface
public interface FailableLongToDoubleFunction<T extends Throwable> { public interface FailableLongToDoubleFunction<E extends Throwable> {
/** NOP singleton */
@SuppressWarnings("rawtypes")
FailableLongToDoubleFunction NOP = t -> 0d;
/**
* Returns The NOP singleton.
*
* @param <E> Thrown exception.
* @return The NOP singleton.
*/
static <E extends Throwable> FailableLongToDoubleFunction<E> nop() {
return NOP;
}
/** /**
* Applies this function to the given argument. * Applies this function to the given argument.
* *
* @param value the function argument * @param value the function argument
* @return the function result * @return the function result
* @throws T Thrown when the function fails. * @throws E Thrown when the function fails.
*/ */
double applyAsDouble(long value) throws T; double applyAsDouble(long value) throws E;
} }

View File

@ -22,18 +22,32 @@
/** /**
* A functional interface like {@link LongToIntFunction} that declares a {@code Throwable}. * A functional interface like {@link LongToIntFunction} that declares a {@code Throwable}.
* *
* @param <T> Thrown exception. * @param <E> Thrown exception.
* @since 3.11 * @since 3.11
*/ */
@FunctionalInterface @FunctionalInterface
public interface FailableLongToIntFunction<T extends Throwable> { public interface FailableLongToIntFunction<E extends Throwable> {
/** NOP singleton */
@SuppressWarnings("rawtypes")
FailableLongToIntFunction NOP = t -> 0;
/**
* Returns The NOP singleton.
*
* @param <E> Thrown exception.
* @return The NOP singleton.
*/
static <E extends Throwable> FailableLongToIntFunction<E> nop() {
return NOP;
}
/** /**
* Applies this function to the given argument. * Applies this function to the given argument.
* *
* @param value the function argument * @param value the function argument
* @return the function result * @return the function result
* @throws T Thrown when the function fails. * @throws E Thrown when the function fails.
*/ */
int applyAsInt(long value) throws T; int applyAsInt(long value) throws E;
} }

View File

@ -0,0 +1,89 @@
/*
* 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.function;
import java.util.Objects;
import java.util.function.LongUnaryOperator;
/**
* A functional interface like {@link LongUnaryOperator} that declares a {@code Throwable}.
*
* @param <E> Thrown exception.
* @since 3.11
*/
public interface FailableLongUnaryOperator<E extends Throwable> {
/** NOP singleton */
@SuppressWarnings("rawtypes")
FailableLongUnaryOperator NOP = t -> 0L;
/**
* Returns a unary operator that always returns its input argument.
*
* @param <E> Thrown exception.
* @return a unary operator that always returns its input argument
*/
static <E extends Throwable> FailableLongUnaryOperator<E> identity() {
return t -> t;
}
/**
* Returns The NOP singleton.
*
* @param <E> Thrown exception.
* @return The NOP singleton.
*/
static <E extends Throwable> FailableLongUnaryOperator<E> nop() {
return NOP;
}
/**
* Returns a composed {@code FailableDoubleUnaryOperator} like {@link LongUnaryOperator#andThen(LongUnaryOperator)}.
*
* @param after the operator to apply after this one.
* @return a composed {@code FailableLongUnaryOperator} like {@link LongUnaryOperator#andThen(LongUnaryOperator)}.
* @throws NullPointerException if after is null.
* @see #compose(FailableLongUnaryOperator)
*/
default FailableLongUnaryOperator<E> andThen(final FailableLongUnaryOperator<E> after) {
Objects.requireNonNull(after);
return (final long t) -> after.applyAsLong(applyAsLong(t));
}
/**
* Applies this operator to the given operand.
*
* @param operand the operand
* @return the operator result
* @throws E Thrown when a consumer fails.
*/
long applyAsLong(long operand) throws E;
/**
* Returns a composed {@code FailableLongUnaryOperator} like {@link LongUnaryOperator#compose(LongUnaryOperator)}.
*
* @param before the operator to apply before this one.
* @return a composed {@code FailableLongUnaryOperator} like {@link LongUnaryOperator#compose(LongUnaryOperator)}.
* @throws NullPointerException if before is null.
* @see #andThen(FailableLongUnaryOperator)
*/
default FailableLongUnaryOperator<E> compose(final FailableLongUnaryOperator<E> before) {
Objects.requireNonNull(before);
return (final long v) -> applyAsLong(before.applyAsLong(v));
}
}

View File

@ -22,19 +22,34 @@
/** /**
* A functional interface like {@link ObjDoubleConsumer} that declares a {@code Throwable}. * A functional interface like {@link ObjDoubleConsumer} that declares a {@code Throwable}.
* *
* @param <O> the type of the object argument to the operation. * @param <T> the type of the object argument to the operation.
* @param <T> Thrown exception. * @param <E> Thrown exception.
* @since 3.11 * @since 3.11
*/ */
@FunctionalInterface @FunctionalInterface
public interface FailableObjDoubleConsumer<O, T extends Throwable> { public interface FailableObjDoubleConsumer<T, E extends Throwable> {
/** NOP singleton */
@SuppressWarnings("rawtypes")
FailableObjDoubleConsumer NOP = (t, u) -> {/* NOP */};
/**
* Returns The NOP singleton.
*
* @param <T> the type of the object argument to the operation.
* @param <E> Thrown exception.
* @return The NOP singleton.
*/
static <T, E extends Throwable> FailableObjDoubleConsumer<T, E> nop() {
return NOP;
}
/** /**
* Accepts the consumer. * Accepts the consumer.
* *
* @param object the object parameter for the consumable to accept. * @param object the object parameter for the consumable to accept.
* @param value the double parameter for the consumable to accept. * @param value the double parameter for the consumable to accept.
* @throws T Thrown when the consumer fails. * @throws E Thrown when the consumer fails.
*/ */
void accept(O object, double value) throws T; void accept(T object, double value) throws E;
} }

View File

@ -22,19 +22,34 @@
/** /**
* A functional interface like {@link ObjIntConsumer} that declares a {@code Throwable}. * A functional interface like {@link ObjIntConsumer} that declares a {@code Throwable}.
* *
* @param <O> the type of the object argument to the operation. * @param <T> the type of the object argument to the operation.
* @param <T> Thrown exception. * @param <E> Thrown exception.
* @since 3.11 * @since 3.11
*/ */
@FunctionalInterface @FunctionalInterface
public interface FailableObjIntConsumer<O, T extends Throwable> { public interface FailableObjIntConsumer<T, E extends Throwable> {
/** NOP singleton */
@SuppressWarnings("rawtypes")
FailableObjIntConsumer NOP = (t, u) -> {/* NOP */};
/**
* Returns The NOP singleton.
*
* @param <T> the type of the object argument to the operation.
* @param <E> Thrown exception.
* @return The NOP singleton.
*/
static <T, E extends Throwable> FailableObjIntConsumer<T, E> nop() {
return NOP;
}
/** /**
* Accepts the consumer. * Accepts the consumer.
* *
* @param object the object parameter for the consumable to accept. * @param object the object parameter for the consumable to accept.
* @param value the int parameter for the consumable to accept. * @param value the int parameter for the consumable to accept.
* @throws T Thrown when the consumer fails. * @throws E Thrown when the consumer fails.
*/ */
void accept(O object, int value) throws T; void accept(T object, int value) throws E;
} }

View File

@ -22,19 +22,34 @@
/** /**
* A functional interface like {@link ObjLongConsumer} that declares a {@code Throwable}. * A functional interface like {@link ObjLongConsumer} that declares a {@code Throwable}.
* *
* @param <O> the type of the object argument to the operation. * @param <T> the type of the object argument to the operation.
* @param <T> Thrown exception. * @param <E> Thrown exception.
* @since 3.11 * @since 3.11
*/ */
@FunctionalInterface @FunctionalInterface
public interface FailableObjLongConsumer<O, T extends Throwable> { public interface FailableObjLongConsumer<T, E extends Throwable> {
/** NOP singleton */
@SuppressWarnings("rawtypes")
FailableObjLongConsumer NOP = (t, u) -> {/* NOP */};
/**
* Returns The NOP singleton.
*
* @param <T> the type of the object argument to the operation.
* @param <E> Thrown exception.
* @return The NOP singleton.
*/
static <T, E extends Throwable> FailableObjLongConsumer<T, E> nop() {
return NOP;
}
/** /**
* Accepts the consumer. * Accepts the consumer.
* *
* @param object the object parameter for the consumable to accept. * @param object the object parameter for the consumable to accept.
* @param value the long parameter for the consumable to accept. * @param value the long parameter for the consumable to accept.
* @throws T Thrown when the consumer fails. * @throws E Thrown when the consumer fails.
*/ */
void accept(O object, long value) throws T; void accept(T object, long value) throws E;
} }

View File

@ -17,24 +17,88 @@
package org.apache.commons.lang3.function; package org.apache.commons.lang3.function;
import java.util.Objects;
import java.util.function.Predicate; import java.util.function.Predicate;
/** /**
* A functional interface like {@link Predicate} that declares a {@code Throwable}. * A functional interface like {@link Predicate} that declares a {@code Throwable}.
* *
* @param <I> Predicate type 1. * @param <T> Predicate type.
* @param <T> Thrown exception. * @param <E> Thrown exception.
* @since 3.11 * @since 3.11
*/ */
@FunctionalInterface @FunctionalInterface
public interface FailablePredicate<I, T extends Throwable> { public interface FailablePredicate<T, E extends Throwable> {
/** FALSE singleton */
@SuppressWarnings("rawtypes")
FailablePredicate FALSE = t -> false;
/** TRUE singleton */
@SuppressWarnings("rawtypes")
FailablePredicate TRUE = t -> true;
/**
* Returns The FALSE singleton.
*
* @param <T> Predicate type.
* @param <E> Thrown exception.
* @return The NOP singleton.
*/
static <T, E extends Throwable> FailablePredicate<T, E> falsePredicate() {
return FALSE;
}
/**
* Returns The FALSE TRUE.
*
* @param <T> Predicate type.
* @param <E> Thrown exception.
* @return The NOP singleton.
*/
static <T, E extends Throwable> FailablePredicate<T, E> truePredicate() {
return TRUE;
}
/**
* Returns a composed {@code FailablePredicate} like {@link Predicate#and(Predicate)}.
*
* @param other a predicate that will be logically-ANDed with this predicate.
* @return a composed {@code FailablePredicate} like {@link Predicate#and(Predicate)}.
* @throws NullPointerException if other is null
*/
default FailablePredicate<T, E> and(final FailablePredicate<? super T, E> other) {
Objects.requireNonNull(other);
return t -> test(t) && other.test(t);
}
/**
* Returns a predicate that negates this predicate.
*
* @return a predicate that negates this predicate.
*/
default FailablePredicate<T, E> negate() {
return t -> !test(t);
}
/**
* Returns a composed {@code FailablePredicate} like {@link Predicate#and(Predicate)}.
*
* @param other a predicate that will be logically-ORed with this predicate.
* @return a composed {@code FailablePredicate} like {@link Predicate#and(Predicate)}.
* @throws NullPointerException if other is null
*/
default FailablePredicate<T, E> or(final FailablePredicate<? super T, E> other) {
Objects.requireNonNull(other);
return t -> test(t) || other.test(t);
}
/** /**
* Tests the predicate. * Tests the predicate.
* *
* @param object the object to test the predicate on * @param object the object to test the predicate on
* @return the predicate's evaluation * @return the predicate's evaluation
* @throws T if the predicate fails * @throws E if the predicate fails
*/ */
boolean test(I object) throws T; boolean test(T object) throws E;
} }

View File

@ -20,16 +20,16 @@
/** /**
* A functional interface like {@link Runnable} that declares a {@code Throwable}. * A functional interface like {@link Runnable} that declares a {@code Throwable}.
* *
* @param <T> Thrown exception. * @param <E> Thrown exception.
* @since 3.11 * @since 3.11
*/ */
@FunctionalInterface @FunctionalInterface
public interface FailableRunnable<T extends Throwable> { public interface FailableRunnable<E extends Throwable> {
/** /**
* Runs the function. * Runs the function.
* *
* @throws T Thrown when the function fails. * @throws E Thrown when the function fails.
*/ */
void run() throws T; void run() throws E;
} }

View File

@ -23,17 +23,17 @@
* A functional interface like {@link Supplier} that declares a {@code Throwable}. * A functional interface like {@link Supplier} that declares a {@code Throwable}.
* *
* @param <R> Return type. * @param <R> Return type.
* @param <T> Thrown exception. * @param <E> Thrown exception.
* @since 3.11 * @since 3.11
*/ */
@FunctionalInterface @FunctionalInterface
public interface FailableSupplier<R, T extends Throwable> { public interface FailableSupplier<R, E extends Throwable> {
/** /**
* Supplies an object * Supplies an object
* *
* @return a result * @return a result
* @throws T if the supplier fails * @throws E if the supplier fails
*/ */
R get() throws T; R get() throws E;
} }

View File

@ -22,13 +22,29 @@
/** /**
* A functional interface like {@link ToDoubleBiFunction} that declares a {@code Throwable}. * A functional interface like {@link ToDoubleBiFunction} that declares a {@code Throwable}.
* *
* @param <O1> the type of the first argument to the function * @param <T> the type of the first argument to the function
* @param <O2> the type of the second argument to the function * @param <U> the type of the second argument to the function
* @param <T> Thrown exception. * @param <E> Thrown exception.
* @since 3.11 * @since 3.11
*/ */
@FunctionalInterface @FunctionalInterface
public interface FailableToDoubleBiFunction<O1, O2, T extends Throwable> { public interface FailableToDoubleBiFunction<T, U, E extends Throwable> {
/** NOP singleton */
@SuppressWarnings("rawtypes")
FailableToDoubleBiFunction NOP = (t, u) -> 0d;
/**
* Returns The NOP singleton.
*
* @param <T> the type of the first argument to the function
* @param <U> the type of the second argument to the function
* @param <E> Thrown exception.
* @return The NOP singleton.
*/
static <T, U, E extends Throwable> FailableToDoubleBiFunction<T, U, E> nop() {
return NOP;
}
/** /**
* Applies this function to the given arguments. * Applies this function to the given arguments.
@ -36,7 +52,7 @@ public interface FailableToDoubleBiFunction<O1, O2, T extends Throwable> {
* @param t the first function argument * @param t the first function argument
* @param u the second function argument * @param u the second function argument
* @return the function result * @return the function result
* @throws T Thrown when the function fails. * @throws E Thrown when the function fails.
*/ */
double applyAsDouble(O1 t, O2 u) throws T; double applyAsDouble(T t, U u) throws E;
} }

View File

@ -22,19 +22,34 @@
/** /**
* A functional interface like {@link ToDoubleFunction} that declares a {@code Throwable}. * A functional interface like {@link ToDoubleFunction} that declares a {@code Throwable}.
* *
* @param <I> the type of the first argument to the function * @param <T> the type of the argument to the function
* @param <T> Thrown exception. * @param <E> Thrown exception.
* @since 3.11 * @since 3.11
*/ */
@FunctionalInterface @FunctionalInterface
public interface FailableToDoubleFunction<I, T extends Throwable> { public interface FailableToDoubleFunction<T, E extends Throwable> {
/** NOP singleton */
@SuppressWarnings("rawtypes")
FailableToDoubleFunction NOP = t -> 0d;
/**
* Returns The NOP singleton.
*
* @param <T> the type of the argument to the function
* @param <E> Thrown exception.
* @return The NOP singleton.
*/
static <T, E extends Throwable> FailableToDoubleFunction<T, E> nop() {
return NOP;
}
/** /**
* Applies this function to the given arguments. * Applies this function to the given arguments.
* *
* @param t the first function argument * @param t the first function argument
* @return the function result * @return the function result
* @throws T Thrown when the function fails. * @throws E Thrown when the function fails.
*/ */
double applyAsDouble(I t) throws T; double applyAsDouble(T t) throws E;
} }

View File

@ -22,13 +22,29 @@
/** /**
* A functional interface like {@link ToIntBiFunction} that declares a {@code Throwable}. * A functional interface like {@link ToIntBiFunction} that declares a {@code Throwable}.
* *
* @param <O1> the type of the first argument to the function * @param <T> the type of the first argument to the function
* @param <O2> the type of the second argument to the function * @param <U> the type of the second argument to the function
* @param <T> Thrown exception. * @param <E> Thrown exception.
* @since 3.11 * @since 3.11
*/ */
@FunctionalInterface @FunctionalInterface
public interface FailableToIntBiFunction<O1, O2, T extends Throwable> { public interface FailableToIntBiFunction<T, U, E extends Throwable> {
/** NOP singleton */
@SuppressWarnings("rawtypes")
FailableToIntBiFunction NOP = (t, u) -> 0;
/**
* Returns The NOP singleton.
*
* @param <T> the type of the first argument to the function
* @param <U> the type of the second argument to the function
* @param <E> Thrown exception.
* @return The NOP singleton.
*/
static <T, U, E extends Throwable> FailableToIntBiFunction<T, U, E> nop() {
return NOP;
}
/** /**
* Applies this function to the given arguments. * Applies this function to the given arguments.
@ -36,7 +52,7 @@ public interface FailableToIntBiFunction<O1, O2, T extends Throwable> {
* @param t the first function argument * @param t the first function argument
* @param u the second function argument * @param u the second function argument
* @return the function result * @return the function result
* @throws T Thrown when the function fails. * @throws E Thrown when the function fails.
*/ */
int applyAsInt(O1 t, O2 u) throws T; int applyAsInt(T t, U u) throws E;
} }

View File

@ -22,19 +22,34 @@
/** /**
* A functional interface like {@link ToIntFunction} that declares a {@code Throwable}. * A functional interface like {@link ToIntFunction} that declares a {@code Throwable}.
* *
* @param <I> the type of the first argument to the function * @param <T> the type of the argument to the function
* @param <T> Thrown exception. * @param <E> Thrown exception.
* @since 3.11 * @since 3.11
*/ */
@FunctionalInterface @FunctionalInterface
public interface FailableToIntFunction<I, T extends Throwable> { public interface FailableToIntFunction<T, E extends Throwable> {
/** NOP singleton */
@SuppressWarnings("rawtypes")
FailableToIntFunction NOP = t -> 0;
/**
* Returns The NOP singleton.
*
* @param <T> the type of the argument to the function
* @param <E> Thrown exception.
* @return The NOP singleton.
*/
static <T, E extends Throwable> FailableToIntFunction<T, E> nop() {
return NOP;
}
/** /**
* Applies this function to the given arguments. * Applies this function to the given arguments.
* *
* @param t the first function argument * @param t the first function argument
* @return the function result * @return the function result
* @throws T Thrown when the function fails. * @throws E Thrown when the function fails.
*/ */
int applyAsInt(I t) throws T; int applyAsInt(T t) throws E;
} }

View File

@ -22,13 +22,29 @@
/** /**
* A functional interface like {@link ToLongBiFunction} that declares a {@code Throwable}. * A functional interface like {@link ToLongBiFunction} that declares a {@code Throwable}.
* *
* @param <O1> the type of the first argument to the function * @param <T> the type of the first argument to the function
* @param <O2> the type of the second argument to the function * @param <U> the type of the second argument to the function
* @param <T> Thrown exception. * @param <E> Thrown exception.
* @since 3.11 * @since 3.11
*/ */
@FunctionalInterface @FunctionalInterface
public interface FailableToLongBiFunction<O1, O2, T extends Throwable> { public interface FailableToLongBiFunction<T, U, E extends Throwable> {
/** NOP singleton */
@SuppressWarnings("rawtypes")
FailableToLongBiFunction NOP = (t, u) -> 0;
/**
* Returns The NOP singleton.
*
* @param <T> the type of the first argument to the function
* @param <U> the type of the second argument to the function
* @param <E> Thrown exception.
* @return The NOP singleton.
*/
static <T, U, E extends Throwable> FailableToLongBiFunction<T, U, E> nop() {
return NOP;
}
/** /**
* Applies this function to the given arguments. * Applies this function to the given arguments.
@ -36,7 +52,7 @@ public interface FailableToLongBiFunction<O1, O2, T extends Throwable> {
* @param t the first function argument * @param t the first function argument
* @param u the second function argument * @param u the second function argument
* @return the function result * @return the function result
* @throws T Thrown when the function fails. * @throws E Thrown when the function fails.
*/ */
long applyAsLong(O1 t, O2 u) throws T; long applyAsLong(T t, U u) throws E;
} }

View File

@ -22,19 +22,34 @@
/** /**
* A functional interface like {@link ToLongFunction} that declares a {@code Throwable}. * A functional interface like {@link ToLongFunction} that declares a {@code Throwable}.
* *
* @param <I> the type of the first argument to the function * @param <T> the type of the first argument to the function
* @param <T> Thrown exception. * @param <E> Thrown exception.
* @since 3.11 * @since 3.11
*/ */
@FunctionalInterface @FunctionalInterface
public interface FailableToLongFunction<I, T extends Throwable> { public interface FailableToLongFunction<T, E extends Throwable> {
/** NOP singleton */
@SuppressWarnings("rawtypes")
FailableToLongFunction NOP = t -> 0L;
/**
* Returns The NOP singleton.
*
* @param <T> the type of the argument to the function
* @param <E> Thrown exception.
* @return The NOP singleton.
*/
static <T, E extends Throwable> FailableToLongFunction<T, E> nop() {
return NOP;
}
/** /**
* Applies this function to the given arguments. * Applies this function to the given arguments.
* *
* @param t the first function argument * @param t the first function argument
* @return the function result * @return the function result
* @throws T Thrown when the function fails. * @throws E Thrown when the function fails.
*/ */
long applyAsLong(I t) throws T; long applyAsLong(T t) throws E;
} }

View File

@ -15,13 +15,13 @@
* limitations under the License. * limitations under the License.
*/ */
/** /**
* Provides functional interfaces to complement those in {@code java.lang.function} and utilities * Provides functional interfaces to complement those in {@code java.lang.function} and utilities for working with Java
* for working with Java 8 lambdas. * 8 lambdas.
* *
* <p>Contains failable functional interfaces that address the fact that lambdas are supposed not to * <p>
* throw Exceptions, at least not checked Exceptions, AKA instances of {@link java.lang.Exception}. * Contains failable functional interfaces that address the fact that lambdas are supposed not to throw Exceptions, at
* A failable functional interface declares a type of Exception that may be raised if the function * least not checked Exceptions, A.K.A. instances of {@link java.lang.Exception}. A failable functional interface
* fails. * declares a type of Exception that may be raised if the function fails.
* *
* @since 3.11 * @since 3.11
*/ */

View File

@ -19,8 +19,8 @@
import java.lang.reflect.Array; import java.lang.reflect.Array;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.BigInteger; import java.math.BigInteger;
import java.math.RoundingMode; import java.math.RoundingMode;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate; import org.apache.commons.lang3.Validate;

View File

@ -29,8 +29,9 @@
* If these do not suffice, you can subclass and implement your own matcher. * If these do not suffice, you can subclass and implement your own matcher.
* <p> * <p>
* For example, it would be possible to implement a lookup that used the * For example, it would be possible to implement a lookup that used the
* key as a primary key, and looked up the value on demand from the database * key as a primary key, and looked up the value on demand from the database.
* *
* @param <V> Unused.
* @since 2.2 * @since 2.2
* @deprecated as of 3.6, use commons-text * @deprecated as of 3.6, use commons-text
* <a href="https://commons.apache.org/proper/commons-text/javadocs/api-release/org/apache/commons/text/StringLookupFactory.html"> * <a href="https://commons.apache.org/proper/commons-text/javadocs/api-release/org/apache/commons/text/StringLookupFactory.html">

View File

@ -361,14 +361,14 @@ public static Date parseDateStrictly(final String str, final Locale locale, fina
* @throws ParseException if none of the date patterns were suitable * @throws ParseException if none of the date patterns were suitable
* @see java.util.Calendar#isLenient() * @see java.util.Calendar#isLenient()
*/ */
private static Date parseDateWithLeniency( private static Date parseDateWithLeniency(final String str, final Locale locale, final String[] parsePatterns,
final String str, final Locale locale, final String[] parsePatterns, final boolean lenient) throws ParseException { final boolean lenient) throws ParseException {
if (str == null || parsePatterns == null) { if (str == null || parsePatterns == null) {
throw new IllegalArgumentException("Date and Patterns must not be null"); throw new IllegalArgumentException("Date and Patterns must not be null");
} }
final TimeZone tz = TimeZone.getDefault(); final TimeZone tz = TimeZone.getDefault();
final Locale lcl = locale==null ?Locale.getDefault() : locale; final Locale lcl = locale == null ? Locale.getDefault() : locale;
final ParsePosition pos = new ParsePosition(0); final ParsePosition pos = new ParsePosition(0);
final Calendar calendar = Calendar.getInstance(tz, lcl); final Calendar calendar = Calendar.getInstance(tz, lcl);
calendar.setLenient(lenient); calendar.setLenient(lenient);
@ -377,10 +377,10 @@ private static Date parseDateWithLeniency(
final FastDateParser fdp = new FastDateParser(parsePattern, tz, lcl); final FastDateParser fdp = new FastDateParser(parsePattern, tz, lcl);
calendar.clear(); calendar.clear();
try { try {
if (fdp.parse(str, pos, calendar) && pos.getIndex()==str.length()) { if (fdp.parse(str, pos, calendar) && pos.getIndex() == str.length()) {
return calendar.getTime(); return calendar.getTime();
} }
} catch(final IllegalArgumentException ignore) { } catch (final IllegalArgumentException ignore) {
// leniency is preventing calendar from being set // leniency is preventing calendar from being set
} }
pos.setIndex(0); pos.setIndex(0);

View File

@ -69,6 +69,22 @@ public static <L, R> ImmutablePair<L, R>[] emptyArray() {
return (ImmutablePair<L, R>[]) EMPTY_ARRAY; return (ImmutablePair<L, R>[]) EMPTY_ARRAY;
} }
/**
* <p>Creates an immutable pair of two objects inferring the generic types.</p>
*
* <p>This factory allows the pair to be created using inference to
* obtain the generic types.</p>
*
* @param <L> the left element type
* @param <R> the right element type
* @param left the left element, may be null
* @return a pair formed from the two parameters, not null
* @since 3.11
*/
public static <L, R> Pair<L, R> left(final L left) {
return ImmutablePair.of(left, null);
}
/** /**
* Returns an immutable pair of nulls. * Returns an immutable pair of nulls.
* *
@ -122,6 +138,22 @@ public static <L, R> ImmutablePair<L, R> of(final Map.Entry<L, R> pair) {
return new ImmutablePair<>(left, right); return new ImmutablePair<>(left, right);
} }
/**
* <p>Creates an immutable pair of two objects inferring the generic types.</p>
*
* <p>This factory allows the pair to be created using inference to
* obtain the generic types.</p>
*
* @param <L> the left element type
* @param <R> the right element type
* @param right the right element, may be null
* @return a pair formed from the two parameters, not null
* @since 3.11
*/
public static <L, R> Pair<L, R> right(final R right) {
return ImmutablePair.of(null, right);
}
/** Left object */ /** Left object */
public final L left; public final L left;

View File

@ -40,8 +40,8 @@
import java.util.Collection; import java.util.Collection;
import java.util.Map; import java.util.Map;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
/** /**
*/ */

View File

@ -16,17 +16,17 @@
*/ */
package org.apache.commons.lang3; package org.apache.commons.lang3;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.apache.commons.lang3.arch.Processor; import org.apache.commons.lang3.arch.Processor;
import org.apache.commons.lang3.arch.Processor.Arch; import org.apache.commons.lang3.arch.Processor.Arch;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
/** /**
* Test class for {@link ArchUtils}. * Test class for {@link ArchUtils}.
*/ */

View File

@ -30,8 +30,8 @@
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections;
import java.util.BitSet; import java.util.BitSet;
import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.Date; import java.util.Date;
import java.util.Map; import java.util.Map;

View File

@ -21,10 +21,10 @@
import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import org.junit.jupiter.api.Test;
/** /**
* Tests CharEncoding. * Tests CharEncoding.
* *

View File

@ -16,13 +16,15 @@
*/ */
package org.apache.commons.lang3; package org.apache.commons.lang3;
import org.apache.commons.lang3.ClassUtils.Interfaces; import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import org.apache.commons.lang3.reflect.testbed.GenericConsumer; import static org.junit.jupiter.api.Assertions.assertEquals;
import org.apache.commons.lang3.reflect.testbed.GenericParent; import static org.junit.jupiter.api.Assertions.assertFalse;
import org.apache.commons.lang3.reflect.testbed.StringParameterizedChild; import static org.junit.jupiter.api.Assertions.assertNotNull;
import org.junit.jupiter.api.Assertions; import static org.junit.jupiter.api.Assertions.assertNotSame;
import org.junit.jupiter.api.DisplayName; import static org.junit.jupiter.api.Assertions.assertNull;
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.lang.reflect.Method; import java.lang.reflect.Method;
@ -35,15 +37,13 @@
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import static org.junit.jupiter.api.Assertions.assertArrayEquals; import org.apache.commons.lang3.ClassUtils.Interfaces;
import static org.junit.jupiter.api.Assertions.assertEquals; import org.apache.commons.lang3.reflect.testbed.GenericConsumer;
import static org.junit.jupiter.api.Assertions.assertFalse; import org.apache.commons.lang3.reflect.testbed.GenericParent;
import static org.junit.jupiter.api.Assertions.assertNotNull; import org.apache.commons.lang3.reflect.testbed.StringParameterizedChild;
import static org.junit.jupiter.api.Assertions.assertNotSame; import org.junit.jupiter.api.Assertions;
import static org.junit.jupiter.api.Assertions.assertNull; import org.junit.jupiter.api.DisplayName;
import static org.junit.jupiter.api.Assertions.assertSame; import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
/** /**
* Unit tests {@link org.apache.commons.lang3.ClassUtils}. * Unit tests {@link org.apache.commons.lang3.ClassUtils}.

View File

@ -17,7 +17,6 @@
package org.apache.commons.lang3; package org.apache.commons.lang3;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertSame;
@ -77,17 +76,17 @@ static boolean failingBool() throws SomeException {
return true; return true;
} }
static boolean testDouble(double value) throws SomeException { static boolean testDouble(final double value) throws SomeException {
throwOnOdd(); throwOnOdd();
return true; return true;
} }
static boolean testInt(int value) throws SomeException { static boolean testInt(final int value) throws SomeException {
throwOnOdd(); throwOnOdd();
return true; return true;
} }
static boolean testLong(long value) throws SomeException { static boolean testLong(final long value) throws SomeException {
throwOnOdd(); throwOnOdd();
return true; return true;
} }
@ -160,7 +159,7 @@ public void test() throws Throwable {
test(throwable); test(throwable);
} }
public Object test(Object input1, Object input2) throws Throwable { public Object test(final Object input1, final Object input2) throws Throwable {
test(throwable); test(throwable);
return acceptedObject; return acceptedObject;
} }
@ -226,41 +225,41 @@ public long testAsLongPrimitive(final Throwable throwable) throws Throwable {
return 0; return 0;
} }
public void testDouble(double i) throws Throwable { public void testDouble(final double i) throws Throwable {
test(throwable); test(throwable);
acceptedPrimitiveObject1 = (P) ((Double) i); acceptedPrimitiveObject1 = (P) ((Double) i);
} }
public double testDoubleDouble(double i, double j) throws Throwable { public double testDoubleDouble(final double i, final double j) throws Throwable {
test(throwable); test(throwable);
acceptedPrimitiveObject1 = (P) ((Double) i); acceptedPrimitiveObject1 = (P) ((Double) i);
acceptedPrimitiveObject2 = (P) ((Double) j); acceptedPrimitiveObject2 = (P) ((Double) j);
return 3d; return 3d;
} }
public void testInt(int i) throws Throwable { public void testInt(final int i) throws Throwable {
test(throwable); test(throwable);
acceptedPrimitiveObject1 = (P) ((Integer) i); acceptedPrimitiveObject1 = (P) ((Integer) i);
} }
public void testLong(long i) throws Throwable { public void testLong(final long i) throws Throwable {
test(throwable); test(throwable);
acceptedPrimitiveObject1 = (P) ((Long) i); acceptedPrimitiveObject1 = (P) ((Long) i);
} }
public void testObjDouble(T object, double i) throws Throwable { public void testObjDouble(final T object, final double i) throws Throwable {
test(throwable); test(throwable);
acceptedObject = object; acceptedObject = object;
acceptedPrimitiveObject1 = (P) ((Double) i); acceptedPrimitiveObject1 = (P) ((Double) i);
} }
public void testObjInt(T object, int i) throws Throwable { public void testObjInt(final T object, final int i) throws Throwable {
test(throwable); test(throwable);
acceptedObject = object; acceptedObject = object;
acceptedPrimitiveObject1 = (P) ((Integer) i); acceptedPrimitiveObject1 = (P) ((Integer) i);
} }
public void testObjLong(T object, long i) throws Throwable { public void testObjLong(final T object, final long i) throws Throwable {
test(throwable); test(throwable);
acceptedObject = object; acceptedObject = object;
acceptedPrimitiveObject1 = (P) ((Long) i); acceptedPrimitiveObject1 = (P) ((Long) i);
@ -510,19 +509,6 @@ public void testApplyBiFunction() {
assertEquals(0, i.intValue()); assertEquals(0, i.intValue());
} }
@Test
public void testApplyDoubleBinaryOperator() {
final IllegalStateException ise = new IllegalStateException();
final Testable<?, Double> testable = new Testable<>(ise);
Throwable e = assertThrows(IllegalStateException.class,
() -> Functions.applyAsDouble(testable::testDoubleDouble, 1d, 2d));
assertSame(ise, e);
final Testable<?, Double> testable2 = new Testable<>(null);
final double i = Functions.applyAsDouble(testable2::testDoubleDouble, 1d, 2d);
assertEquals(3d, i);
}
@Test @Test
public void testApplyFunction() { public void testApplyFunction() {
final IllegalStateException ise = new IllegalStateException(); final IllegalStateException ise = new IllegalStateException();
@ -736,103 +722,6 @@ public void testFunction() {
assertEquals(0, function.apply(null).intValue()); assertEquals(0, function.apply(null).intValue());
} }
@Test
public void testGetAsBooleanSupplier() {
final IllegalStateException ise = new IllegalStateException();
final Testable<?, ?> testable = new Testable<>(ise);
Throwable e = assertThrows(IllegalStateException.class,
() -> Functions.getAsBoolean(testable::testAsBooleanPrimitive));
assertSame(ise, e);
final Error error = new OutOfMemoryError();
testable.setThrowable(error);
e = assertThrows(OutOfMemoryError.class, () -> Functions.getAsBoolean(testable::testAsBooleanPrimitive));
assertSame(error, e);
final IOException ioe = new IOException("Unknown I/O error");
testable.setThrowable(ioe);
e = assertThrows(UncheckedIOException.class, () -> Functions.getAsBoolean(testable::testAsBooleanPrimitive));
final Throwable t = e.getCause();
assertNotNull(t);
assertSame(ioe, t);
testable.setThrowable(null);
assertFalse(Functions.getAsBoolean(testable::testAsBooleanPrimitive));
}
@Test
public void testGetAsDoubleSupplier() {
final IllegalStateException ise = new IllegalStateException();
final Testable<?, ?> testable = new Testable<>(ise);
Throwable e = assertThrows(IllegalStateException.class,
() -> Functions.getAsDouble(testable::testAsDoublePrimitive));
assertSame(ise, e);
final Error error = new OutOfMemoryError();
testable.setThrowable(error);
e = assertThrows(OutOfMemoryError.class, () -> Functions.getAsDouble(testable::testAsDoublePrimitive));
assertSame(error, e);
final IOException ioe = new IOException("Unknown I/O error");
testable.setThrowable(ioe);
e = assertThrows(UncheckedIOException.class, () -> Functions.getAsDouble(testable::testAsDoublePrimitive));
final Throwable t = e.getCause();
assertNotNull(t);
assertSame(ioe, t);
testable.setThrowable(null);
assertEquals(0, Functions.getAsDouble(testable::testAsDoublePrimitive));
}
@Test
public void testGetAsIntSupplier() {
final IllegalStateException ise = new IllegalStateException();
final Testable<?, ?> testable = new Testable<>(ise);
Throwable e = assertThrows(IllegalStateException.class, () -> Functions.getAsInt(testable::testAsIntPrimitive));
assertSame(ise, e);
final Error error = new OutOfMemoryError();
testable.setThrowable(error);
e = assertThrows(OutOfMemoryError.class, () -> Functions.getAsInt(testable::testAsIntPrimitive));
assertSame(error, e);
final IOException ioe = new IOException("Unknown I/O error");
testable.setThrowable(ioe);
e = assertThrows(UncheckedIOException.class, () -> Functions.getAsInt(testable::testAsIntPrimitive));
final Throwable t = e.getCause();
assertNotNull(t);
assertSame(ioe, t);
testable.setThrowable(null);
final int i = Functions.getAsInt(testable::testAsInteger);
assertEquals(0, i);
}
@Test
public void testGetAsLongSupplier() {
final IllegalStateException ise = new IllegalStateException();
final Testable<?, ?> testable = new Testable<>(ise);
Throwable e = assertThrows(IllegalStateException.class,
() -> Functions.getAsLong(testable::testAsLongPrimitive));
assertSame(ise, e);
final Error error = new OutOfMemoryError();
testable.setThrowable(error);
e = assertThrows(OutOfMemoryError.class, () -> Functions.getAsLong(testable::testAsLongPrimitive));
assertSame(error, e);
final IOException ioe = new IOException("Unknown I/O error");
testable.setThrowable(ioe);
e = assertThrows(UncheckedIOException.class, () -> Functions.getAsLong(testable::testAsLongPrimitive));
final Throwable t = e.getCause();
assertNotNull(t);
assertSame(ioe, t);
testable.setThrowable(null);
final long i = Functions.getAsLong(testable::testAsLongPrimitive);
assertEquals(0, i);
}
@Test @Test
public void testGetFromSupplier() { public void testGetFromSupplier() {
FailureOnOddInvocations.invocations = 0; FailureOnOddInvocations.invocations = 0;
@ -911,7 +800,7 @@ public void testThrows_FailableBiConsumer_Object_Throwable() {
new Functions.FailableBiConsumer<Object, Object, Throwable>() { new Functions.FailableBiConsumer<Object, Object, Throwable>() {
@Override @Override
public void accept(Object object1, Object object2) throws Throwable { public void accept(final Object object1, final Object object2) throws Throwable {
throw new IOException("test"); throw new IOException("test");
} }
}; };
@ -926,7 +815,7 @@ public void testThrows_FailableBiConsumer_String_IOException() {
new Functions.FailableBiConsumer<String, String, IOException>() { new Functions.FailableBiConsumer<String, String, IOException>() {
@Override @Override
public void accept(String object1, String object2) throws IOException { public void accept(final String object1, final String object2) throws IOException {
throw new IOException("test"); throw new IOException("test");
} }
@ -942,7 +831,7 @@ public void testThrows_FailableBiFunction_Object_Throwable() {
new Functions.FailableBiFunction<Object, Object, Object, Throwable>() { new Functions.FailableBiFunction<Object, Object, Object, Throwable>() {
@Override @Override
public Object apply(Object input1, Object input2) throws Throwable { public Object apply(final Object input1, final Object input2) throws Throwable {
throw new IOException("test"); throw new IOException("test");
} }
}; };
@ -957,7 +846,7 @@ public void testThrows_FailableBiFunction_String_IOException() {
new Functions.FailableBiFunction<String, String, String, IOException>() { new Functions.FailableBiFunction<String, String, String, IOException>() {
@Override @Override
public String apply(String input1, String input2) throws IOException { public String apply(final String input1, final String input2) throws IOException {
throw new IOException("test"); throw new IOException("test");
} }
}; };
@ -972,7 +861,7 @@ public void testThrows_FailableBiPredicate_Object_Throwable() {
new Functions.FailableBiPredicate<Object, Object, Throwable>() { new Functions.FailableBiPredicate<Object, Object, Throwable>() {
@Override @Override
public boolean test(Object object1, Object object2) throws Throwable { public boolean test(final Object object1, final Object object2) throws Throwable {
throw new IOException("test"); throw new IOException("test");
} }
}; };
@ -987,7 +876,7 @@ public void testThrows_FailableBiPredicate_String_IOException() {
new Functions.FailableBiPredicate<String, String, IOException>() { new Functions.FailableBiPredicate<String, String, IOException>() {
@Override @Override
public boolean test(String object1, String object2) throws IOException { public boolean test(final String object1, final String object2) throws IOException {
throw new IOException("test"); throw new IOException("test");
} }
}; };
@ -1032,7 +921,7 @@ public void testThrows_FailableConsumer_Object_Throwable() {
new Functions.FailableConsumer<Object, Throwable>() { new Functions.FailableConsumer<Object, Throwable>() {
@Override @Override
public void accept(Object object) throws Throwable { public void accept(final Object object) throws Throwable {
throw new IOException("test"); throw new IOException("test");
} }
@ -1048,7 +937,7 @@ public void testThrows_FailableConsumer_String_IOException() {
new Functions.FailableConsumer<String, IOException>() { new Functions.FailableConsumer<String, IOException>() {
@Override @Override
public void accept(String object) throws IOException { public void accept(final String object) throws IOException {
throw new IOException("test"); throw new IOException("test");
} }
@ -1064,7 +953,7 @@ public void testThrows_FailableFunction_Object_Throwable() {
new Functions.FailableFunction<Object, Object, Throwable>() { new Functions.FailableFunction<Object, Object, Throwable>() {
@Override @Override
public Object apply(Object input) throws Throwable { public Object apply(final Object input) throws Throwable {
throw new IOException("test"); throw new IOException("test");
} }
}; };
@ -1079,7 +968,7 @@ public void testThrows_FailableFunction_String_IOException() {
new Functions.FailableFunction<String, String, IOException>() { new Functions.FailableFunction<String, String, IOException>() {
@Override @Override
public String apply(String input) throws IOException { public String apply(final String input) throws IOException {
throw new IOException("test"); throw new IOException("test");
} }
}; };
@ -1094,7 +983,7 @@ public void testThrows_FailablePredicate_Object_Throwable() {
new Functions.FailablePredicate<Object, Throwable>() { new Functions.FailablePredicate<Object, Throwable>() {
@Override @Override
public boolean test(Object object) throws Throwable { public boolean test(final Object object) throws Throwable {
throw new IOException("test"); throw new IOException("test");
} }
}; };
@ -1109,7 +998,7 @@ public void testThrows_FailablePredicate_String_IOException() {
new Functions.FailablePredicate<String, IOException>() { new Functions.FailablePredicate<String, IOException>() {
@Override @Override
public boolean test(String object) throws IOException { public boolean test(final String object) throws IOException {
throw new IOException("test"); throw new IOException("test");
} }
}; };

View File

@ -18,8 +18,6 @@
*/ */
package org.apache.commons.lang3; package org.apache.commons.lang3;
import org.junit.jupiter.api.Test;
import static org.apache.commons.lang3.JavaVersion.JAVA_0_9; import static org.apache.commons.lang3.JavaVersion.JAVA_0_9;
import static org.apache.commons.lang3.JavaVersion.JAVA_10; import static org.apache.commons.lang3.JavaVersion.JAVA_10;
import static org.apache.commons.lang3.JavaVersion.JAVA_11; import static org.apache.commons.lang3.JavaVersion.JAVA_11;
@ -27,6 +25,7 @@
import static org.apache.commons.lang3.JavaVersion.JAVA_13; import static org.apache.commons.lang3.JavaVersion.JAVA_13;
import static org.apache.commons.lang3.JavaVersion.JAVA_14; import static org.apache.commons.lang3.JavaVersion.JAVA_14;
import static org.apache.commons.lang3.JavaVersion.JAVA_15; import static org.apache.commons.lang3.JavaVersion.JAVA_15;
import static org.apache.commons.lang3.JavaVersion.JAVA_16;
import static org.apache.commons.lang3.JavaVersion.JAVA_1_1; import static org.apache.commons.lang3.JavaVersion.JAVA_1_1;
import static org.apache.commons.lang3.JavaVersion.JAVA_1_2; import static org.apache.commons.lang3.JavaVersion.JAVA_1_2;
import static org.apache.commons.lang3.JavaVersion.JAVA_1_3; import static org.apache.commons.lang3.JavaVersion.JAVA_1_3;
@ -43,6 +42,8 @@
import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
/** /**
* Unit tests {@link org.apache.commons.lang3.JavaVersion}. * Unit tests {@link org.apache.commons.lang3.JavaVersion}.
*/ */
@ -66,10 +67,11 @@ public void testGetJavaVersion() {
assertEquals(JAVA_13, get("13"), "13 failed"); assertEquals(JAVA_13, get("13"), "13 failed");
assertEquals(JAVA_14, get("14"), "14 failed"); assertEquals(JAVA_14, get("14"), "14 failed");
assertEquals(JAVA_15, get("15"), "15 failed"); assertEquals(JAVA_15, get("15"), "15 failed");
assertEquals(JAVA_16, get("16"), "16 failed");
assertEquals(JAVA_RECENT, get("1.10"), "1.10 failed"); assertEquals(JAVA_RECENT, get("1.10"), "1.10 failed");
// assertNull("2.10 unexpectedly worked", get("2.10")); // assertNull("2.10 unexpectedly worked", get("2.10"));
assertEquals(get("1.5"), getJavaVersion("1.5"), "Wrapper method failed"); assertEquals(get("1.5"), getJavaVersion("1.5"), "Wrapper method failed");
assertEquals(JAVA_RECENT, get("16"), "Unhandled"); // LANG-1384 assertEquals(JAVA_RECENT, get("17"), "Unhandled"); // LANG-1384
} }
@Test @Test

View File

@ -16,10 +16,11 @@
*/ */
package org.apache.commons.lang3; package org.apache.commons.lang3;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNotNull;
import org.junit.jupiter.api.Test;
/** /**
* Unit tests {@link org.apache.commons.lang3.NotImplementedException}. * Unit tests {@link org.apache.commons.lang3.NotImplementedException}.
*/ */

View File

@ -361,6 +361,15 @@ public void testToString_ObjectString() {
assertEquals(Boolean.TRUE.toString(), ObjectUtils.toString(Boolean.TRUE, BAR) ); assertEquals(Boolean.TRUE.toString(), ObjectUtils.toString(Boolean.TRUE, BAR) );
} }
@Test
public void testToString_SupplierString() {
assertEquals(null, ObjectUtils.toString(null, (Supplier<String>) null));
assertEquals(null, ObjectUtils.toString(null, () -> null));
// Pretend computing BAR is expensive.
assertEquals(BAR, ObjectUtils.toString(null, () -> BAR));
assertEquals(Boolean.TRUE.toString(), ObjectUtils.toString(Boolean.TRUE, () -> BAR));
}
@SuppressWarnings("cast") // 1 OK, because we are checking for code change @SuppressWarnings("cast") // 1 OK, because we are checking for code change
@Test @Test
public void testNull() { public void testNull() {

View File

@ -23,11 +23,11 @@
import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import org.junit.jupiter.api.Test;
/** /**
* Tests for {@link RandomUtils} * Tests for {@link RandomUtils}
*/ */

View File

@ -232,17 +232,10 @@ public void testContainsAny_StringStringArray() {
public void testContainsIgnoreCase_LocaleIndependence() { public void testContainsIgnoreCase_LocaleIndependence() {
final Locale[] locales = { Locale.ENGLISH, new Locale("tr"), Locale.getDefault() }; final Locale[] locales = { Locale.ENGLISH, new Locale("tr"), Locale.getDefault() };
final String[][] tdata = { final String[][] tdata = { { "i", "I" }, { "I", "i" }, { "\u03C2", "\u03C3" }, { "\u03A3", "\u03C2" },
{ "i", "I" }, { "\u03A3", "\u03C3" }, };
{ "I", "i" },
{ "\u03C2", "\u03C3" },
{ "\u03A3", "\u03C2" },
{ "\u03A3", "\u03C3" },
};
final String[][] fdata = { final String[][] fdata = { { "\u00DF", "SS" }, };
{ "\u00DF", "SS" },
};
for (final Locale testLocale : locales) { for (final Locale testLocale : locales) {
Locale.setDefault(testLocale); Locale.setDefault(testLocale);

View File

@ -16,11 +16,11 @@
*/ */
package org.apache.commons.lang3; package org.apache.commons.lang3;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
/** /**
* Unit tests {@link org.apache.commons.lang3.StringUtils} - StartsWith/EndsWith methods * Unit tests {@link org.apache.commons.lang3.StringUtils} - StartsWith/EndsWith methods

View File

@ -60,11 +60,11 @@ public class StringUtilsTest {
static final String NON_TRIMMABLE; static final String NON_TRIMMABLE;
static { static {
StringBuilder ws = new StringBuilder(); final StringBuilder ws = new StringBuilder();
StringBuilder nws = new StringBuilder(); final StringBuilder nws = new StringBuilder();
final String hs = String.valueOf(((char) 160)); final String hs = String.valueOf(((char) 160));
StringBuilder tr = new StringBuilder(); final StringBuilder tr = new StringBuilder();
StringBuilder ntr = new StringBuilder(); final StringBuilder ntr = new StringBuilder();
for (int i = 0; i < Character.MAX_VALUE; i++) { for (int i = 0; i < Character.MAX_VALUE; i++) {
if (Character.isWhitespace((char) i)) { if (Character.isWhitespace((char) i)) {
ws.append(String.valueOf((char) i)); ws.append(String.valueOf((char) i));
@ -3279,12 +3279,12 @@ public void testToRootLowerCase() {
assertEquals("title", "TITLE".toLowerCase(Locale.ROOT)); assertEquals("title", "TITLE".toLowerCase(Locale.ROOT));
assertEquals("title", StringUtils.toRootLowerCase("TITLE")); assertEquals("title", StringUtils.toRootLowerCase("TITLE"));
// Make sure we are not using the default Locale: // Make sure we are not using the default Locale:
final Locale defaultLocales = Locale.getDefault(); final Locale defaultLocale = Locale.getDefault();
try { try {
Locale.setDefault(TURKISH); Locale.setDefault(TURKISH);
assertEquals("title", StringUtils.toRootLowerCase("TITLE")); assertEquals("title", StringUtils.toRootLowerCase("TITLE"));
} finally { } finally {
Locale.setDefault(defaultLocales); Locale.setDefault(defaultLocale);
} }
} }
@ -3299,36 +3299,36 @@ public void testToRootUpperCase() {
assertEquals("TITLE", "title".toUpperCase(Locale.ROOT)); assertEquals("TITLE", "title".toUpperCase(Locale.ROOT));
assertEquals("TITLE", StringUtils.toRootUpperCase("title")); assertEquals("TITLE", StringUtils.toRootUpperCase("title"));
// Make sure we are not using the default Locale: // Make sure we are not using the default Locale:
final Locale defaultLocales = Locale.getDefault(); final Locale defaultLocale = Locale.getDefault();
try { try {
Locale.setDefault(TURKISH); Locale.setDefault(TURKISH);
assertEquals("TITLE", StringUtils.toRootUpperCase("title")); assertEquals("TITLE", StringUtils.toRootUpperCase("title"));
} finally { } finally {
Locale.setDefault(defaultLocales); Locale.setDefault(defaultLocale);
} }
} }
@Test @Test
public void testGeorgianSample() { public void testGeorgianSample() {
char[] arrayI = new char[]{ final char[] arrayI = new char[]{
//Latin Small Letter dotless I //Latin Small Letter dotless I
(char) 0x0131, (char) 0x0131,
//Greek Capital Letter Theta //Greek Capital Letter Theta
(char) 0x03F4 (char) 0x03F4
}; };
char[] arrayJ = new char[]{ final char[] arrayJ = new char[]{
//Latin Capital Letter I with dot above //Latin Capital Letter I with dot above
(char) 0x0130, (char) 0x0130,
//Greek Theta Symbol //Greek Theta Symbol
(char) 0x03D1 (char) 0x03D1
}; };
for (char i : arrayI) { for (final char i : arrayI) {
for (char j : arrayJ) { for (final char j : arrayJ) {
String si = String.valueOf(i); final String si = String.valueOf(i);
String sj = String.valueOf(j); final String sj = String.valueOf(j);
boolean res1 = si.equalsIgnoreCase(sj); final boolean res1 = si.equalsIgnoreCase(sj);
CharSequence ci = new StringBuilder(si); final CharSequence ci = new StringBuilder(si);
CharSequence cj = new StringBuilder(sj); final CharSequence cj = new StringBuilder(sj);
boolean res2 = StringUtils.startsWithIgnoreCase(ci, cj); boolean res2 = StringUtils.startsWithIgnoreCase(ci, cj);
assertEquals(res1, res2, "si : " + si + " sj : " + sj); assertEquals(res1, res2, "si : " + si + " sj : " + sj);
res2 = StringUtils.endsWithIgnoreCase(ci, cj); res2 = StringUtils.endsWithIgnoreCase(ci, cj);

View File

@ -19,6 +19,10 @@
package org.apache.commons.lang3; package org.apache.commons.lang3;
import static org.apache.commons.lang3.JavaVersion.JAVA_10;
import static org.apache.commons.lang3.JavaVersion.JAVA_11;
import static org.apache.commons.lang3.JavaVersion.JAVA_12;
import static org.apache.commons.lang3.JavaVersion.JAVA_13;
import static org.apache.commons.lang3.JavaVersion.JAVA_1_1; import static org.apache.commons.lang3.JavaVersion.JAVA_1_1;
import static org.apache.commons.lang3.JavaVersion.JAVA_1_2; import static org.apache.commons.lang3.JavaVersion.JAVA_1_2;
import static org.apache.commons.lang3.JavaVersion.JAVA_1_3; import static org.apache.commons.lang3.JavaVersion.JAVA_1_3;
@ -28,10 +32,6 @@
import static org.apache.commons.lang3.JavaVersion.JAVA_1_7; import static org.apache.commons.lang3.JavaVersion.JAVA_1_7;
import static org.apache.commons.lang3.JavaVersion.JAVA_1_8; import static org.apache.commons.lang3.JavaVersion.JAVA_1_8;
import static org.apache.commons.lang3.JavaVersion.JAVA_9; import static org.apache.commons.lang3.JavaVersion.JAVA_9;
import static org.apache.commons.lang3.JavaVersion.JAVA_10;
import static org.apache.commons.lang3.JavaVersion.JAVA_11;
import static org.apache.commons.lang3.JavaVersion.JAVA_12;
import static org.apache.commons.lang3.JavaVersion.JAVA_13;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNotNull;

View File

@ -323,7 +323,7 @@ public void testComplexThreadGroups() throws Exception {
assertEquals(1, ThreadUtils.findThreadsByName(t4.getName(), threadGroup3.getName()).size()); assertEquals(1, ThreadUtils.findThreadsByName(t4.getName(), threadGroup3.getName()).size());
assertEquals(0, ThreadUtils.findThreadsByName(t4.getName(), threadGroup2.getName()).size()); assertEquals(0, ThreadUtils.findThreadsByName(t4.getName(), threadGroup2.getName()).size());
assertEquals(2, ThreadUtils.findThreadsByName(t11.getName(), threadGroup7.getName()).size()); assertEquals(2, ThreadUtils.findThreadsByName(t11.getName(), threadGroup7.getName()).size());
}finally { } finally {
for (final Thread thread : threads) { for (final Thread thread : threads) {
thread.interrupt(); thread.interrupt();
thread.join(); thread.join();

View File

@ -18,8 +18,12 @@
*/ */
package org.apache.commons.lang3; package org.apache.commons.lang3;
import org.junit.jupiter.api.Nested; import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
@ -29,12 +33,8 @@
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.Nested;
import static org.junit.jupiter.api.Assertions.assertFalse; import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
/** /**
* Unit tests {@link org.apache.commons.lang3.Validate}. * Unit tests {@link org.apache.commons.lang3.Validate}.

View File

@ -20,8 +20,11 @@
import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertThrows;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import org.apache.commons.lang3.builder.ToStringStyleTest.Person; import org.apache.commons.lang3.builder.ToStringStyleTest.Person;
import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.AfterEach;
@ -181,6 +184,73 @@ public void testObject() {
.toString()); .toString());
} }
@Test
public void testList() {
final Student student = new Student();
final ArrayList<Hobby> objects = new ArrayList<>();
objects.add(Hobby.BOOK);
objects.add(Hobby.SPORT);
objects.add(Hobby.MUSIC);
student.setHobbies(objects);
assertEquals(student.toString(), "{\"hobbies\":[\"BOOK\",\"SPORT\",\"MUSIC\"]}");
student.setHobbies(new ArrayList<>());
assertEquals(student.toString(), "{\"hobbies\":[]}");
student.setHobbies(null);
assertEquals(student.toString(), "{\"hobbies\":null}");
}
@Test
public void testArrayEnum() {
final Teacher teacher = new Teacher();
final Hobby[] hobbies = new Hobby[3];
hobbies[0] = Hobby.BOOK;
hobbies[1] = Hobby.SPORT;
hobbies[2] = Hobby.MUSIC;
teacher.setHobbies(hobbies);
assertEquals(teacher.toString(), "{\"hobbies\":[\"BOOK\",\"SPORT\",\"MUSIC\"]}");
teacher.setHobbies(new Hobby[0]);
assertEquals(teacher.toString(), "{\"hobbies\":[]}");
teacher.setHobbies(null);
assertEquals(teacher.toString(), "{\"hobbies\":null}");
}
@Test
public void testCombineListAndEnum() {
final Teacher teacher = new Teacher();
final Hobby[] teacherHobbies = new Hobby[3];
teacherHobbies[0] = Hobby.BOOK;
teacherHobbies[1] = Hobby.SPORT;
teacherHobbies[2] = Hobby.MUSIC;
teacher.setHobbies(teacherHobbies);
final Student john = new Student();
john.setHobbies(Arrays.asList(Hobby.BOOK, Hobby.MUSIC));
final Student alice = new Student();
alice.setHobbies(new ArrayList<>());
final Student bob = new Student();
bob.setHobbies(Collections.singletonList(Hobby.BOOK));
final ArrayList<Student> students = new ArrayList<>();
students.add(john);
students.add(alice);
students.add(bob);
final AcademyClass academyClass = new AcademyClass();
academyClass.setStudents(students);
academyClass.setTeacher(teacher);
assertEquals(academyClass.toString(), "{\"students\":[{\"hobbies\":[\"BOOK\",\"MUSIC\"]},{\"hobbies\":[]},{\"hobbies\":[\"BOOK\"]}],\"teacher\":{\"hobbies\":[\"BOOK\",\"SPORT\",\"MUSIC\"]}}");
}
@Test @Test
public void testPerson() { public void testPerson() {
final Person p = new Person(); final Person p = new Person();
@ -477,4 +547,73 @@ static class NestingPerson {
*/ */
Person person; Person person;
} }
enum Hobby {
SPORT,
BOOK,
MUSIC
}
enum EmptyEnum {
}
static class Student {
List<Hobby> hobbies;
public List<Hobby> getHobbies() {
return hobbies;
}
public void setHobbies(final List<Hobby> hobbies) {
this.hobbies = hobbies;
}
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this);
}
}
static class Teacher {
Hobby[] hobbies;
public Hobby[] getHobbies() {
return hobbies;
}
public void setHobbies(final Hobby[] hobbies) {
this.hobbies = hobbies;
}
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this);
}
}
static class AcademyClass {
Teacher teacher;
List<Student> students;
public void setTeacher(final Teacher teacher) {
this.teacher = teacher;
}
public void setStudents(final List<Student> students) {
this.students = students;
}
public Teacher getTeacher() {
return teacher;
}
public List<Student> getStudents() {
return students;
}
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this);
}
}
} }

View File

@ -16,15 +16,15 @@
*/ */
package org.apache.commons.lang3.builder; package org.apache.commons.lang3.builder;
import org.apache.commons.lang3.builder.ToStringStyleTest.Person; import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import static org.junit.jupiter.api.Assertions.assertEquals; import org.apache.commons.lang3.builder.ToStringStyleTest.Person;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
/** /**
* Unit tests {@link ToStringStyle#NO_CLASS_NAME_STYLE}. * Unit tests {@link ToStringStyle#NO_CLASS_NAME_STYLE}.

View File

@ -17,9 +17,9 @@
package org.apache.commons.lang3.builder; package org.apache.commons.lang3.builder;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.not; import static org.hamcrest.Matchers.not;
import static org.hamcrest.MatcherAssert.assertThat;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;

View File

@ -16,10 +16,10 @@
*/ */
package org.apache.commons.lang3.builder; package org.apache.commons.lang3.builder;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
public class ReflectionToStringBuilderSummaryTest { public class ReflectionToStringBuilderSummaryTest {
@SuppressWarnings("unused") @SuppressWarnings("unused")

View File

@ -16,10 +16,10 @@
*/ */
package org.apache.commons.lang3.builder; package org.apache.commons.lang3.builder;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.Test;
public class ReflectionToStringBuilderTest { public class ReflectionToStringBuilderTest {
@Test @Test

View File

@ -16,8 +16,6 @@
*/ */
package org.apache.commons.lang3.concurrent; package org.apache.commons.lang3.concurrent;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNotNull;
@ -32,6 +30,8 @@
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;
import org.junit.jupiter.api.Test;
public class BackgroundInitializerTest { public class BackgroundInitializerTest {
/** /**
* Helper method for checking whether the initialize() method was correctly * Helper method for checking whether the initialize() method was correctly

View File

@ -16,14 +16,14 @@
*/ */
package org.apache.commons.lang3.concurrent; package org.apache.commons.lang3.concurrent;
import org.apache.commons.lang3.exception.AbstractExceptionTest;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertThrows;
import org.apache.commons.lang3.exception.AbstractExceptionTest;
import org.junit.jupiter.api.Test;
/** /**
* JUnit tests for {@link CircuitBreakingException}. * JUnit tests for {@link CircuitBreakingException}.

View File

@ -18,8 +18,8 @@
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;

View File

@ -44,7 +44,7 @@ public void testWriteLock() throws Exception {
runTest(DELAY, true, l -> assertTrue(l >= NUMBER_OF_THREADS*DELAY)); runTest(DELAY, true, l -> assertTrue(l >= NUMBER_OF_THREADS*DELAY));
} }
private void runTest(long delay, boolean exclusiveLock, LongConsumer runTimeCheck) throws InterruptedException { private void runTest(final long delay, final boolean exclusiveLock, final LongConsumer runTimeCheck) throws InterruptedException {
final boolean[] booleanValues = new boolean[10]; final boolean[] booleanValues = new boolean[10];
final Lock<boolean[]> lock = Locks.lock(booleanValues); final Lock<boolean[]> lock = Locks.lock(booleanValues);
final boolean[] runningValues = new boolean[10]; final boolean[] runningValues = new boolean[10];
@ -60,9 +60,9 @@ private void runTest(long delay, boolean exclusiveLock, LongConsumer runTimeChec
}; };
final Thread t = new Thread(() -> { final Thread t = new Thread(() -> {
if (exclusiveLock) { if (exclusiveLock) {
lock.runWriteLocked(consumer); lock.acceptWriteLocked(consumer);
} else { } else {
lock.runReadLocked(consumer); lock.acceptReadLocked(consumer);
} }
}); });
modify(runningValues, i, true); modify(runningValues, i, true);
@ -78,13 +78,13 @@ private void runTest(long delay, boolean exclusiveLock, LongConsumer runTimeChec
runTimeCheck.accept(endTime-startTime); runTimeCheck.accept(endTime-startTime);
} }
protected void modify(boolean[] booleanArray, int offset, boolean value) { protected void modify(final boolean[] booleanArray, final int offset, final boolean value) {
synchronized(booleanArray) { synchronized(booleanArray) {
booleanArray[offset] = value; booleanArray[offset] = value;
} }
} }
protected boolean someValueIsTrue(boolean[] booleanArray) { protected boolean someValueIsTrue(final boolean[] booleanArray) {
synchronized(booleanArray) { synchronized(booleanArray) {
for (int i = 0; i < booleanArray.length; i++) { for (int i = 0; i < booleanArray.length; i++) {
if (booleanArray[i]) { if (booleanArray[i]) {

View File

@ -16,15 +16,15 @@
*/ */
package org.apache.commons.lang3.concurrent; package org.apache.commons.lang3.concurrent;
import org.easymock.EasyMock;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.easymock.EasyMock.expect; import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.replay; import static org.easymock.EasyMock.replay;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertThrows;
import org.easymock.EasyMock;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class MemoizerTest { public class MemoizerTest {
private Computable<Integer, Integer> computable; private Computable<Integer, Integer> computable;

View File

@ -16,9 +16,6 @@
*/ */
package org.apache.commons.lang3.exception; package org.apache.commons.lang3.exception;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.BeforeEach;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertNull;
@ -33,6 +30,8 @@
import org.apache.commons.lang3.SerializationUtils; import org.apache.commons.lang3.SerializationUtils;
import org.apache.commons.lang3.tuple.Pair; import org.apache.commons.lang3.tuple.Pair;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
/** /**

View File

@ -16,13 +16,13 @@
*/ */
package org.apache.commons.lang3.exception; package org.apache.commons.lang3.exception;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.Test;
/** /**
* JUnit tests for {@link CloneFailedExceptionTest}. * JUnit tests for {@link CloneFailedExceptionTest}.
*/ */

View File

@ -27,8 +27,8 @@
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.BigInteger; import java.math.BigInteger;
import java.math.RoundingMode; import java.math.RoundingMode;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
/** /**

View File

@ -17,14 +17,14 @@
package org.apache.commons.lang3.mutable; package org.apache.commons.lang3.mutable;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
/** /**
* JUnit tests. * JUnit tests.
* *

View File

@ -16,12 +16,12 @@
*/ */
package org.apache.commons.lang3.mutable; package org.apache.commons.lang3.mutable;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.Test;
/** /**
* JUnit tests. * JUnit tests.
* *

View File

@ -16,13 +16,13 @@
*/ */
package org.apache.commons.lang3.mutable; package org.apache.commons.lang3.mutable;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
/** /**
* JUnit tests. * JUnit tests.
* *

View File

@ -16,13 +16,13 @@
*/ */
package org.apache.commons.lang3.mutable; package org.apache.commons.lang3.mutable;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
/** /**
* JUnit tests. * JUnit tests.
* *

View File

@ -16,12 +16,12 @@
*/ */
package org.apache.commons.lang3.mutable; package org.apache.commons.lang3.mutable;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.Test;
/** /**
* JUnit tests. * JUnit tests.
* *

View File

@ -16,12 +16,12 @@
*/ */
package org.apache.commons.lang3.mutable; package org.apache.commons.lang3.mutable;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.Test;
/** /**
* JUnit tests. * JUnit tests.
* *

View File

@ -16,12 +16,12 @@
*/ */
package org.apache.commons.lang3.mutable; package org.apache.commons.lang3.mutable;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.Test;
/** /**
* JUnit tests. * JUnit tests.
* *

Some files were not shown because too many files have changed in this diff Show More