diff --git a/Java/commons-lang-BackgroundInitializer_231/Dockerfile b/Java/commons-lang-BackgroundInitializer_231/Dockerfile new file mode 100644 index 000000000..7b7fbe349 --- /dev/null +++ b/Java/commons-lang-BackgroundInitializer_231/Dockerfile @@ -0,0 +1,18 @@ +FROM ghcr.io/kupl/starlab-benchmarks/java-base:commons-lang + +ENV TZ=Asia/Seoul + +COPY ./metadata.json . +COPY ./npe.json . +COPY ./buggy.java /tmp/buggy.java +RUN export BUGGY_PATH=$(cat metadata.json | jq -r ".npe.filepath") \ + && export BUGGY_LINE=$(cat metadata.json | jq -r ".npe.line") \ + && export BUGGY_MTHD=$(cat metadata.json | jq -r ".npe.npe_method") \ + && mv /tmp/buggy.java $BUGGY_PATH \ + && echo "[{\"filepath\": \"$BUGGY_PATH\", \"line\": $BUGGY_LINE, \"method_name\": \"$BUGGY_MTHD\"}]" | jq . > traces.json + +RUN git init . && git add -A + +RUN $(cat metadata.json | jq -r ".buildCommand") + +RUN $(cat metadata.json | jq -r ".testCommand"); if [ $? -eq 0 ]; then exit 1; fi diff --git a/Java/commons-lang-BackgroundInitializer_231/buggy.java b/Java/commons-lang-BackgroundInitializer_231/buggy.java new file mode 100644 index 000000000..b0c8929c0 --- /dev/null +++ b/Java/commons-lang-BackgroundInitializer_231/buggy.java @@ -0,0 +1,341 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.lang3.concurrent; + +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; + +/** + *
+ * A class that allows complex initialization operations in a background task. + *
+ *+ * Applications often have to do some expensive initialization steps when they + * are started, e.g. constructing a connection to a database, reading a + * configuration file, etc. Doing these things in parallel can enhance + * performance as the CPU load can be improved. However, when access to the + * resources initialized in a background thread is actually required, + * synchronization has to be performed to ensure that their initialization is + * complete. + *
+ *+ * This abstract base class provides support for this use case. A concrete + * subclass must implement the {@link #initialize()} method. Here an arbitrary + * initialization can be implemented, and a result object can be returned. With + * this method in place the basic usage of this class is as follows (where + * {@code MyBackgroundInitializer} is a concrete subclass): + *
+ * + *+ * MyBackgroundInitializer initializer = new MyBackgroundInitializer(); + * initializer.start(); + * // Now do some other things. Initialization runs in a parallel thread + * ... + * // Wait for the end of initialization and access the result object + * Object result = initializer.get(); + *+ * + *
+ * After the construction of a {@code BackgroundInitializer} object its + * {@link #start()} method has to be called. This starts the background + * processing. The application can now continue to do other things. When it + * needs access to the object produced by the {@code BackgroundInitializer} it + * calls its {@link #get()} method. If initialization is already complete, + * {@link #get()} returns the result object immediately. Otherwise it blocks + * until the result object is fully constructed. + *
+ *+ * {@code BackgroundInitializer} is a thin wrapper around a {@code Future} + * object and uses an {@code ExecutorService} for running the background + * initialization task. It is possible to pass in an {@code ExecutorService} at + * construction time or set one using {@code setExternalExecutor()} before + * {@code start()} was called. Then this object is used to spawn the background + * task. If no {@code ExecutorService} has been provided, {@code + * BackgroundInitializer} creates a temporary {@code ExecutorService} and + * destroys it when initialization is complete. + *
+ *+ * The methods provided by {@code BackgroundInitializer} provide for minimal + * interaction with the wrapped {@code Future} object. It is also possible to + * obtain the {@code Future} object directly. Then the enhanced functionality + * offered by {@code Future} can be used, e.g. to check whether the background + * operation is complete or to cancel the operation. + *
+ * + * @since 3.0 + * @param+ * A class that allows complex initialization operations in a background task. + *
+ *+ * Applications often have to do some expensive initialization steps when they + * are started, e.g. constructing a connection to a database, reading a + * configuration file, etc. Doing these things in parallel can enhance + * performance as the CPU load can be improved. However, when access to the + * resources initialized in a background thread is actually required, + * synchronization has to be performed to ensure that their initialization is + * complete. + *
+ *+ * This abstract base class provides support for this use case. A concrete + * subclass must implement the {@link #initialize()} method. Here an arbitrary + * initialization can be implemented, and a result object can be returned. With + * this method in place the basic usage of this class is as follows (where + * {@code MyBackgroundInitializer} is a concrete subclass): + *
+ * + *+ * MyBackgroundInitializer initializer = new MyBackgroundInitializer(); + * initializer.start(); + * // Now do some other things. Initialization runs in a parallel thread + * ... + * // Wait for the end of initialization and access the result object + * Object result = initializer.get(); + *+ * + *
+ * After the construction of a {@code BackgroundInitializer} object its + * {@link #start()} method has to be called. This starts the background + * processing. The application can now continue to do other things. When it + * needs access to the object produced by the {@code BackgroundInitializer} it + * calls its {@link #get()} method. If initialization is already complete, + * {@link #get()} returns the result object immediately. Otherwise it blocks + * until the result object is fully constructed. + *
+ *+ * {@code BackgroundInitializer} is a thin wrapper around a {@code Future} + * object and uses an {@code ExecutorService} for running the background + * initialization task. It is possible to pass in an {@code ExecutorService} at + * construction time or set one using {@code setExternalExecutor()} before + * {@code start()} was called. Then this object is used to spawn the background + * task. If no {@code ExecutorService} has been provided, {@code + * BackgroundInitializer} creates a temporary {@code ExecutorService} and + * destroys it when initialization is complete. + *
+ *+ * The methods provided by {@code BackgroundInitializer} provide for minimal + * interaction with the wrapped {@code Future} object. It is also possible to + * obtain the {@code Future} object directly. Then the enhanced functionality + * offered by {@code Future} can be used, e.g. to check whether the background + * operation is complete or to cancel the operation. + *
+ * + * @since 3.0 + * @param+ * An implementation of the {@code ThreadFactory} interface that provides some + * configuration options for the threads it creates. + *
+ *+ * A {@code ThreadFactory} is used for instance by an {@code ExecutorService} to + * create the threads it uses for executing tasks. In many cases users do not + * have to care about a {@code ThreadFactory} because the default one used by an + * {@code ExecutorService} will do. However, if there are special requirements + * for the threads, a custom {@code ThreadFactory} has to be created. + *
+ *+ * This class provides some frequently needed configuration options for the + * threads it creates. These are the following: + *
+ *+ * {@code BasicThreadFactory} wraps another thread factory which actually + * creates new threads. The configuration options are set on the threads created + * by the wrapped thread factory. On construction time the factory to be wrapped + * can be specified. If none is provided, a default {@code ThreadFactory} is + * used. + *
+ *+ * Instances of {@code BasicThreadFactory} are not created directly, but the + * nested {@code Builder} class is used for this purpose. Using the builder only + * the configuration options an application is interested in need to be set. The + * following example shows how a {@code BasicThreadFactory} is created and + * installed in an {@code ExecutorService}: + *
+ * + *+ * // Create a factory that produces daemon threads with a naming pattern and + * // a priority + * BasicThreadFactory factory = new BasicThreadFactory.Builder() + * .namingPattern("workerthread-%d") + * .daemon(true) + * .priority(Thread.MAX_PRIORITY) + * .build(); + * // Create an executor service for single-threaded execution + * ExecutorService exec = Executors.newSingleThreadExecutor(factory); + *+ * + * @since 3.0 + */ +public class BasicThreadFactory implements ThreadFactory { + /** A counter for the threads created by this factory. */ + private final AtomicLong threadCounter; + + /** Stores the wrapped factory. */ + private final ThreadFactory wrappedFactory; + + /** Stores the uncaught exception handler. */ + private final Thread.UncaughtExceptionHandler uncaughtExceptionHandler; + + /** Stores the naming pattern for newly created threads. */ + private final String namingPattern; + + /** Stores the priority. */ + private final Integer priority; + + /** Stores the daemon status flag. */ + private final Boolean daemonFlag; + + /** + * Creates a new instance of {@code ThreadFactoryImpl} and configures it + * from the specified {@code Builder} object. + * + * @param builder the {@code Builder} object + */ + private BasicThreadFactory(final Builder builder) { + if (builder.wrappedFactory == null) { + wrappedFactory = Executors.defaultThreadFactory(); + } else { + wrappedFactory = builder.wrappedFactory; + } + + namingPattern = builder.namingPattern; + priority = builder.priority; + daemonFlag = builder.daemonFlag; + uncaughtExceptionHandler = builder.exceptionHandler; + + threadCounter = new AtomicLong(); + } + + /** + * Returns the wrapped {@code ThreadFactory}. This factory is used for + * actually creating threads. This method never returns null. If no + * {@code ThreadFactory} was passed when this object was created, a default + * thread factory is returned. + * + * @return the wrapped {@code ThreadFactory} + */ + public final ThreadFactory getWrappedFactory() { + return wrappedFactory; + } + + /** + * Returns the naming pattern for naming newly created threads. Result can + * be null if no naming pattern was provided. + * + * @return the naming pattern + */ + public final String getNamingPattern() { + return namingPattern; + } + + /** + * Returns the daemon flag. This flag determines whether newly created + * threads should be daemon threads. If true, this factory object + * calls {@code setDaemon(true)} on the newly created threads. Result can be + * null if no daemon flag was provided at creation time. + * + * @return the daemon flag + */ + public final Boolean getDaemonFlag() { + return daemonFlag; + } + + /** + * Returns the priority of the threads created by this factory. Result can + * be null if no priority was specified. + * + * @return the priority for newly created threads + */ + public final Integer getPriority() { + return priority; + } + + /** + * Returns the {@code UncaughtExceptionHandler} for the threads created by + * this factory. Result can be null if no handler was provided. + * + * @return the {@code UncaughtExceptionHandler} + */ + public final Thread.UncaughtExceptionHandler getUncaughtExceptionHandler() { + return uncaughtExceptionHandler; + } + + /** + * Returns the number of threads this factory has already created. This + * class maintains an internal counter that is incremented each time the + * {@link #newThread(Runnable)} method is invoked. + * + * @return the number of threads created by this factory + */ + public long getThreadCount() { + return threadCounter.get(); + } + + /** + * Creates a new thread. This implementation delegates to the wrapped + * factory for creating the thread. Then, on the newly created thread the + * corresponding configuration options are set. + * + * @param r the {@code Runnable} to be executed by the new thread + * @return the newly created thread + */ + @Override + public Thread newThread(final Runnable r) { + final Thread t = getWrappedFactory().newThread(r); + initializeThread(t); + + return t; + } + + /** + * Initializes the specified thread. This method is called by + * {@link #newThread(Runnable)} after a new thread has been obtained from + * the wrapped thread factory. It initializes the thread according to the + * options set for this factory. + * + * @param t the thread to be initialized + */ +/** + * Initializes the specified thread. This method is called by + * {@link #newThread(Runnable)} after a new thread has been obtained from + * the wrapped thread factory. It initializes the thread according to the + * options set for this factory. + * + * @param t + * the thread to be initialized + */ +private void initializeThread(final java.lang.Thread t) { + { + final java.lang.Long count = java.lang.Long.valueOf(threadCounter.incrementAndGet()); + t.setName(java.lang.String.format(/* NPEX_NULL_EXP */ + getNamingPattern(), count)); + } + if (getUncaughtExceptionHandler() != null) { + t.setUncaughtExceptionHandler(getUncaughtExceptionHandler()); + } + if (getPriority() != null) { + t.setPriority(getPriority().intValue()); + } + if (getDaemonFlag() != null) { + t.setDaemon(getDaemonFlag().booleanValue()); + } +} + + /** + *
+ * A builder class for creating instances of {@code + * BasicThreadFactory}. + *
+ *+ * Using this builder class instances of {@code BasicThreadFactory} can be + * created and initialized. The class provides methods that correspond to + * the configuration options supported by {@code BasicThreadFactory}. Method + * chaining is supported. Refer to the documentation of {@code + * BasicThreadFactory} for a usage example. + *
+ * + */ + public static class Builder + implements org.apache.commons.lang3.builder.Builder+ * An implementation of the {@code ThreadFactory} interface that provides some + * configuration options for the threads it creates. + *
+ *+ * A {@code ThreadFactory} is used for instance by an {@code ExecutorService} to + * create the threads it uses for executing tasks. In many cases users do not + * have to care about a {@code ThreadFactory} because the default one used by an + * {@code ExecutorService} will do. However, if there are special requirements + * for the threads, a custom {@code ThreadFactory} has to be created. + *
+ *+ * This class provides some frequently needed configuration options for the + * threads it creates. These are the following: + *
+ *+ * {@code BasicThreadFactory} wraps another thread factory which actually + * creates new threads. The configuration options are set on the threads created + * by the wrapped thread factory. On construction time the factory to be wrapped + * can be specified. If none is provided, a default {@code ThreadFactory} is + * used. + *
+ *+ * Instances of {@code BasicThreadFactory} are not created directly, but the + * nested {@code Builder} class is used for this purpose. Using the builder only + * the configuration options an application is interested in need to be set. The + * following example shows how a {@code BasicThreadFactory} is created and + * installed in an {@code ExecutorService}: + *
+ * + *+ * // Create a factory that produces daemon threads with a naming pattern and + * // a priority + * BasicThreadFactory factory = new BasicThreadFactory.Builder() + * .namingPattern("workerthread-%d") + * .daemon(true) + * .priority(Thread.MAX_PRIORITY) + * .build(); + * // Create an executor service for single-threaded execution + * ExecutorService exec = Executors.newSingleThreadExecutor(factory); + *+ * + * @since 3.0 + */ +public class BasicThreadFactory implements ThreadFactory { + /** A counter for the threads created by this factory. */ + private final AtomicLong threadCounter; + + /** Stores the wrapped factory. */ + private final ThreadFactory wrappedFactory; + + /** Stores the uncaught exception handler. */ + private final Thread.UncaughtExceptionHandler uncaughtExceptionHandler; + + /** Stores the naming pattern for newly created threads. */ + private final String namingPattern; + + /** Stores the priority. */ + private final Integer priority; + + /** Stores the daemon status flag. */ + private final Boolean daemonFlag; + + /** + * Creates a new instance of {@code ThreadFactoryImpl} and configures it + * from the specified {@code Builder} object. + * + * @param builder the {@code Builder} object + */ + private BasicThreadFactory(final Builder builder) { + if (builder.wrappedFactory == null) { + wrappedFactory = Executors.defaultThreadFactory(); + } else { + wrappedFactory = builder.wrappedFactory; + } + + namingPattern = builder.namingPattern; + priority = builder.priority; + daemonFlag = builder.daemonFlag; + uncaughtExceptionHandler = builder.exceptionHandler; + + threadCounter = new AtomicLong(); + } + + /** + * Returns the wrapped {@code ThreadFactory}. This factory is used for + * actually creating threads. This method never returns null. If no + * {@code ThreadFactory} was passed when this object was created, a default + * thread factory is returned. + * + * @return the wrapped {@code ThreadFactory} + */ + public final ThreadFactory getWrappedFactory() { + return wrappedFactory; + } + + /** + * Returns the naming pattern for naming newly created threads. Result can + * be null if no naming pattern was provided. + * + * @return the naming pattern + */ + public final String getNamingPattern() { + return namingPattern; + } + + /** + * Returns the daemon flag. This flag determines whether newly created + * threads should be daemon threads. If true, this factory object + * calls {@code setDaemon(true)} on the newly created threads. Result can be + * null if no daemon flag was provided at creation time. + * + * @return the daemon flag + */ + public final Boolean getDaemonFlag() { + return daemonFlag; + } + + /** + * Returns the priority of the threads created by this factory. Result can + * be null if no priority was specified. + * + * @return the priority for newly created threads + */ + public final Integer getPriority() { + return priority; + } + + /** + * Returns the {@code UncaughtExceptionHandler} for the threads created by + * this factory. Result can be null if no handler was provided. + * + * @return the {@code UncaughtExceptionHandler} + */ + public final Thread.UncaughtExceptionHandler getUncaughtExceptionHandler() { + return uncaughtExceptionHandler; + } + + /** + * Returns the number of threads this factory has already created. This + * class maintains an internal counter that is incremented each time the + * {@link #newThread(Runnable)} method is invoked. + * + * @return the number of threads created by this factory + */ + public long getThreadCount() { + return threadCounter.get(); + } + + /** + * Creates a new thread. This implementation delegates to the wrapped + * factory for creating the thread. Then, on the newly created thread the + * corresponding configuration options are set. + * + * @param r the {@code Runnable} to be executed by the new thread + * @return the newly created thread + */ + @Override + public Thread newThread(final Runnable r) { + final Thread t = getWrappedFactory().newThread(r); + initializeThread(t); + + return t; + } + + /** + * Initializes the specified thread. This method is called by + * {@link #newThread(Runnable)} after a new thread has been obtained from + * the wrapped thread factory. It initializes the thread according to the + * options set for this factory. + * + * @param t the thread to be initialized + */ +/** + * Initializes the specified thread. This method is called by + * {@link #newThread(Runnable)} after a new thread has been obtained from + * the wrapped thread factory. It initializes the thread according to the + * options set for this factory. + * + * @param t + * the thread to be initialized + */ +private void initializeThread(final java.lang.Thread t) { + if (getNamingPattern() != null) { + final java.lang.Long count = java.lang.Long.valueOf(threadCounter.incrementAndGet()); + t.setName(java.lang.String.format(getNamingPattern(), count)); + } + { + t.setUncaughtExceptionHandler(/* NPEX_NULL_EXP */ + getUncaughtExceptionHandler()); + } + if (getPriority() != null) { + t.setPriority(getPriority().intValue()); + } + if (getDaemonFlag() != null) { + t.setDaemon(getDaemonFlag().booleanValue()); + } +} + + /** + *
+ * A builder class for creating instances of {@code + * BasicThreadFactory}. + *
+ *+ * Using this builder class instances of {@code BasicThreadFactory} can be + * created and initialized. The class provides methods that correspond to + * the configuration options supported by {@code BasicThreadFactory}. Method + * chaining is supported. Refer to the documentation of {@code + * BasicThreadFactory} for a usage example. + *
+ * + */ + public static class Builder + implements org.apache.commons.lang3.builder.Builder+ * An implementation of the {@code ThreadFactory} interface that provides some + * configuration options for the threads it creates. + *
+ *+ * A {@code ThreadFactory} is used for instance by an {@code ExecutorService} to + * create the threads it uses for executing tasks. In many cases users do not + * have to care about a {@code ThreadFactory} because the default one used by an + * {@code ExecutorService} will do. However, if there are special requirements + * for the threads, a custom {@code ThreadFactory} has to be created. + *
+ *+ * This class provides some frequently needed configuration options for the + * threads it creates. These are the following: + *
+ *+ * {@code BasicThreadFactory} wraps another thread factory which actually + * creates new threads. The configuration options are set on the threads created + * by the wrapped thread factory. On construction time the factory to be wrapped + * can be specified. If none is provided, a default {@code ThreadFactory} is + * used. + *
+ *+ * Instances of {@code BasicThreadFactory} are not created directly, but the + * nested {@code Builder} class is used for this purpose. Using the builder only + * the configuration options an application is interested in need to be set. The + * following example shows how a {@code BasicThreadFactory} is created and + * installed in an {@code ExecutorService}: + *
+ * + *+ * // Create a factory that produces daemon threads with a naming pattern and + * // a priority + * BasicThreadFactory factory = new BasicThreadFactory.Builder() + * .namingPattern("workerthread-%d") + * .daemon(true) + * .priority(Thread.MAX_PRIORITY) + * .build(); + * // Create an executor service for single-threaded execution + * ExecutorService exec = Executors.newSingleThreadExecutor(factory); + *+ * + * @since 3.0 + */ +public class BasicThreadFactory implements ThreadFactory { + /** A counter for the threads created by this factory. */ + private final AtomicLong threadCounter; + + /** Stores the wrapped factory. */ + private final ThreadFactory wrappedFactory; + + /** Stores the uncaught exception handler. */ + private final Thread.UncaughtExceptionHandler uncaughtExceptionHandler; + + /** Stores the naming pattern for newly created threads. */ + private final String namingPattern; + + /** Stores the priority. */ + private final Integer priority; + + /** Stores the daemon status flag. */ + private final Boolean daemonFlag; + + /** + * Creates a new instance of {@code ThreadFactoryImpl} and configures it + * from the specified {@code Builder} object. + * + * @param builder the {@code Builder} object + */ + private BasicThreadFactory(final Builder builder) { + if (builder.wrappedFactory == null) { + wrappedFactory = Executors.defaultThreadFactory(); + } else { + wrappedFactory = builder.wrappedFactory; + } + + namingPattern = builder.namingPattern; + priority = builder.priority; + daemonFlag = builder.daemonFlag; + uncaughtExceptionHandler = builder.exceptionHandler; + + threadCounter = new AtomicLong(); + } + + /** + * Returns the wrapped {@code ThreadFactory}. This factory is used for + * actually creating threads. This method never returns null. If no + * {@code ThreadFactory} was passed when this object was created, a default + * thread factory is returned. + * + * @return the wrapped {@code ThreadFactory} + */ + public final ThreadFactory getWrappedFactory() { + return wrappedFactory; + } + + /** + * Returns the naming pattern for naming newly created threads. Result can + * be null if no naming pattern was provided. + * + * @return the naming pattern + */ + public final String getNamingPattern() { + return namingPattern; + } + + /** + * Returns the daemon flag. This flag determines whether newly created + * threads should be daemon threads. If true, this factory object + * calls {@code setDaemon(true)} on the newly created threads. Result can be + * null if no daemon flag was provided at creation time. + * + * @return the daemon flag + */ + public final Boolean getDaemonFlag() { + return daemonFlag; + } + + /** + * Returns the priority of the threads created by this factory. Result can + * be null if no priority was specified. + * + * @return the priority for newly created threads + */ + public final Integer getPriority() { + return priority; + } + + /** + * Returns the {@code UncaughtExceptionHandler} for the threads created by + * this factory. Result can be null if no handler was provided. + * + * @return the {@code UncaughtExceptionHandler} + */ + public final Thread.UncaughtExceptionHandler getUncaughtExceptionHandler() { + return uncaughtExceptionHandler; + } + + /** + * Returns the number of threads this factory has already created. This + * class maintains an internal counter that is incremented each time the + * {@link #newThread(Runnable)} method is invoked. + * + * @return the number of threads created by this factory + */ + public long getThreadCount() { + return threadCounter.get(); + } + + /** + * Creates a new thread. This implementation delegates to the wrapped + * factory for creating the thread. Then, on the newly created thread the + * corresponding configuration options are set. + * + * @param r the {@code Runnable} to be executed by the new thread + * @return the newly created thread + */ + @Override + public Thread newThread(final Runnable r) { + final Thread t = getWrappedFactory().newThread(r); + initializeThread(t); + + return t; + } + + /** + * Initializes the specified thread. This method is called by + * {@link #newThread(Runnable)} after a new thread has been obtained from + * the wrapped thread factory. It initializes the thread according to the + * options set for this factory. + * + * @param t the thread to be initialized + */ +/** + * Initializes the specified thread. This method is called by + * {@link #newThread(Runnable)} after a new thread has been obtained from + * the wrapped thread factory. It initializes the thread according to the + * options set for this factory. + * + * @param t + * the thread to be initialized + */ +private void initializeThread(final java.lang.Thread t) { + if (getNamingPattern() != null) { + final java.lang.Long count = java.lang.Long.valueOf(threadCounter.incrementAndGet()); + t.setName(java.lang.String.format(getNamingPattern(), count)); + } + if (getUncaughtExceptionHandler() != null) { + t.setUncaughtExceptionHandler(getUncaughtExceptionHandler()); + } + { + t.setPriority(/* NPEX_NULL_EXP */ + getPriority().intValue()); + } + if (getDaemonFlag() != null) { + t.setDaemon(getDaemonFlag().booleanValue()); + } +} + + /** + *
+ * A builder class for creating instances of {@code + * BasicThreadFactory}. + *
+ *+ * Using this builder class instances of {@code BasicThreadFactory} can be + * created and initialized. The class provides methods that correspond to + * the configuration options supported by {@code BasicThreadFactory}. Method + * chaining is supported. Refer to the documentation of {@code + * BasicThreadFactory} for a usage example. + *
+ * + */ + public static class Builder + implements org.apache.commons.lang3.builder.Builder+ * An implementation of the {@code ThreadFactory} interface that provides some + * configuration options for the threads it creates. + *
+ *+ * A {@code ThreadFactory} is used for instance by an {@code ExecutorService} to + * create the threads it uses for executing tasks. In many cases users do not + * have to care about a {@code ThreadFactory} because the default one used by an + * {@code ExecutorService} will do. However, if there are special requirements + * for the threads, a custom {@code ThreadFactory} has to be created. + *
+ *+ * This class provides some frequently needed configuration options for the + * threads it creates. These are the following: + *
+ *+ * {@code BasicThreadFactory} wraps another thread factory which actually + * creates new threads. The configuration options are set on the threads created + * by the wrapped thread factory. On construction time the factory to be wrapped + * can be specified. If none is provided, a default {@code ThreadFactory} is + * used. + *
+ *+ * Instances of {@code BasicThreadFactory} are not created directly, but the + * nested {@code Builder} class is used for this purpose. Using the builder only + * the configuration options an application is interested in need to be set. The + * following example shows how a {@code BasicThreadFactory} is created and + * installed in an {@code ExecutorService}: + *
+ * + *+ * // Create a factory that produces daemon threads with a naming pattern and + * // a priority + * BasicThreadFactory factory = new BasicThreadFactory.Builder() + * .namingPattern("workerthread-%d") + * .daemon(true) + * .priority(Thread.MAX_PRIORITY) + * .build(); + * // Create an executor service for single-threaded execution + * ExecutorService exec = Executors.newSingleThreadExecutor(factory); + *+ * + * @since 3.0 + */ +public class BasicThreadFactory implements ThreadFactory { + /** A counter for the threads created by this factory. */ + private final AtomicLong threadCounter; + + /** Stores the wrapped factory. */ + private final ThreadFactory wrappedFactory; + + /** Stores the uncaught exception handler. */ + private final Thread.UncaughtExceptionHandler uncaughtExceptionHandler; + + /** Stores the naming pattern for newly created threads. */ + private final String namingPattern; + + /** Stores the priority. */ + private final Integer priority; + + /** Stores the daemon status flag. */ + private final Boolean daemonFlag; + + /** + * Creates a new instance of {@code ThreadFactoryImpl} and configures it + * from the specified {@code Builder} object. + * + * @param builder the {@code Builder} object + */ + private BasicThreadFactory(final Builder builder) { + if (builder.wrappedFactory == null) { + wrappedFactory = Executors.defaultThreadFactory(); + } else { + wrappedFactory = builder.wrappedFactory; + } + + namingPattern = builder.namingPattern; + priority = builder.priority; + daemonFlag = builder.daemonFlag; + uncaughtExceptionHandler = builder.exceptionHandler; + + threadCounter = new AtomicLong(); + } + + /** + * Returns the wrapped {@code ThreadFactory}. This factory is used for + * actually creating threads. This method never returns null. If no + * {@code ThreadFactory} was passed when this object was created, a default + * thread factory is returned. + * + * @return the wrapped {@code ThreadFactory} + */ + public final ThreadFactory getWrappedFactory() { + return wrappedFactory; + } + + /** + * Returns the naming pattern for naming newly created threads. Result can + * be null if no naming pattern was provided. + * + * @return the naming pattern + */ + public final String getNamingPattern() { + return namingPattern; + } + + /** + * Returns the daemon flag. This flag determines whether newly created + * threads should be daemon threads. If true, this factory object + * calls {@code setDaemon(true)} on the newly created threads. Result can be + * null if no daemon flag was provided at creation time. + * + * @return the daemon flag + */ + public final Boolean getDaemonFlag() { + return daemonFlag; + } + + /** + * Returns the priority of the threads created by this factory. Result can + * be null if no priority was specified. + * + * @return the priority for newly created threads + */ + public final Integer getPriority() { + return priority; + } + + /** + * Returns the {@code UncaughtExceptionHandler} for the threads created by + * this factory. Result can be null if no handler was provided. + * + * @return the {@code UncaughtExceptionHandler} + */ + public final Thread.UncaughtExceptionHandler getUncaughtExceptionHandler() { + return uncaughtExceptionHandler; + } + + /** + * Returns the number of threads this factory has already created. This + * class maintains an internal counter that is incremented each time the + * {@link #newThread(Runnable)} method is invoked. + * + * @return the number of threads created by this factory + */ + public long getThreadCount() { + return threadCounter.get(); + } + + /** + * Creates a new thread. This implementation delegates to the wrapped + * factory for creating the thread. Then, on the newly created thread the + * corresponding configuration options are set. + * + * @param r the {@code Runnable} to be executed by the new thread + * @return the newly created thread + */ + @Override + public Thread newThread(final Runnable r) { + final Thread t = getWrappedFactory().newThread(r); + initializeThread(t); + + return t; + } + + /** + * Initializes the specified thread. This method is called by + * {@link #newThread(Runnable)} after a new thread has been obtained from + * the wrapped thread factory. It initializes the thread according to the + * options set for this factory. + * + * @param t the thread to be initialized + */ +/** + * Initializes the specified thread. This method is called by + * {@link #newThread(Runnable)} after a new thread has been obtained from + * the wrapped thread factory. It initializes the thread according to the + * options set for this factory. + * + * @param t + * the thread to be initialized + */ +private void initializeThread(final java.lang.Thread t) { + if (getNamingPattern() != null) { + final java.lang.Long count = java.lang.Long.valueOf(threadCounter.incrementAndGet()); + t.setName(java.lang.String.format(getNamingPattern(), count)); + } + if (getUncaughtExceptionHandler() != null) { + t.setUncaughtExceptionHandler(getUncaughtExceptionHandler()); + } + if (getPriority() != null) { + t.setPriority(getPriority().intValue()); + } + { + t.setDaemon(/* NPEX_NULL_EXP */ + getDaemonFlag().booleanValue()); + } +} + + /** + *
+ * A builder class for creating instances of {@code + * BasicThreadFactory}. + *
+ *+ * Using this builder class instances of {@code BasicThreadFactory} can be + * created and initialized. The class provides methods that correspond to + * the configuration options supported by {@code BasicThreadFactory}. Method + * chaining is supported. Refer to the documentation of {@code + * BasicThreadFactory} for a usage example. + *
+ * + */ + public static class Builder + implements org.apache.commons.lang3.builder.Builder+ * An implementation of the {@code ThreadFactory} interface that provides some + * configuration options for the threads it creates. + *
+ *+ * A {@code ThreadFactory} is used for instance by an {@code ExecutorService} to + * create the threads it uses for executing tasks. In many cases users do not + * have to care about a {@code ThreadFactory} because the default one used by an + * {@code ExecutorService} will do. However, if there are special requirements + * for the threads, a custom {@code ThreadFactory} has to be created. + *
+ *+ * This class provides some frequently needed configuration options for the + * threads it creates. These are the following: + *
+ *+ * {@code BasicThreadFactory} wraps another thread factory which actually + * creates new threads. The configuration options are set on the threads created + * by the wrapped thread factory. On construction time the factory to be wrapped + * can be specified. If none is provided, a default {@code ThreadFactory} is + * used. + *
+ *+ * Instances of {@code BasicThreadFactory} are not created directly, but the + * nested {@code Builder} class is used for this purpose. Using the builder only + * the configuration options an application is interested in need to be set. The + * following example shows how a {@code BasicThreadFactory} is created and + * installed in an {@code ExecutorService}: + *
+ * + *+ * // Create a factory that produces daemon threads with a naming pattern and + * // a priority + * BasicThreadFactory factory = new BasicThreadFactory.Builder() + * .namingPattern("workerthread-%d") + * .daemon(true) + * .priority(Thread.MAX_PRIORITY) + * .build(); + * // Create an executor service for single-threaded execution + * ExecutorService exec = Executors.newSingleThreadExecutor(factory); + *+ * + * @since 3.0 + */ +public class BasicThreadFactory implements ThreadFactory { + /** A counter for the threads created by this factory. */ + private final AtomicLong threadCounter; + + /** Stores the wrapped factory. */ + private final ThreadFactory wrappedFactory; + + /** Stores the uncaught exception handler. */ + private final Thread.UncaughtExceptionHandler uncaughtExceptionHandler; + + /** Stores the naming pattern for newly created threads. */ + private final String namingPattern; + + /** Stores the priority. */ + private final Integer priority; + + /** Stores the daemon status flag. */ + private final Boolean daemonFlag; + + /** + * Creates a new instance of {@code ThreadFactoryImpl} and configures it + * from the specified {@code Builder} object. + * + * @param builder the {@code Builder} object + */ + private BasicThreadFactory(final Builder builder) { + if (builder.wrappedFactory == null) { + wrappedFactory = Executors.defaultThreadFactory(); + } else { + wrappedFactory = builder.wrappedFactory; + } + + namingPattern = builder.namingPattern; + priority = builder.priority; + daemonFlag = builder.daemonFlag; + uncaughtExceptionHandler = builder.exceptionHandler; + + threadCounter = new AtomicLong(); + } + + /** + * Returns the wrapped {@code ThreadFactory}. This factory is used for + * actually creating threads. This method never returns null. If no + * {@code ThreadFactory} was passed when this object was created, a default + * thread factory is returned. + * + * @return the wrapped {@code ThreadFactory} + */ + public final ThreadFactory getWrappedFactory() { + return wrappedFactory; + } + + /** + * Returns the naming pattern for naming newly created threads. Result can + * be null if no naming pattern was provided. + * + * @return the naming pattern + */ + public final String getNamingPattern() { + return namingPattern; + } + + /** + * Returns the daemon flag. This flag determines whether newly created + * threads should be daemon threads. If true, this factory object + * calls {@code setDaemon(true)} on the newly created threads. Result can be + * null if no daemon flag was provided at creation time. + * + * @return the daemon flag + */ + public final Boolean getDaemonFlag() { + return daemonFlag; + } + + /** + * Returns the priority of the threads created by this factory. Result can + * be null if no priority was specified. + * + * @return the priority for newly created threads + */ + public final Integer getPriority() { + return priority; + } + + /** + * Returns the {@code UncaughtExceptionHandler} for the threads created by + * this factory. Result can be null if no handler was provided. + * + * @return the {@code UncaughtExceptionHandler} + */ + public final Thread.UncaughtExceptionHandler getUncaughtExceptionHandler() { + return uncaughtExceptionHandler; + } + + /** + * Returns the number of threads this factory has already created. This + * class maintains an internal counter that is incremented each time the + * {@link #newThread(Runnable)} method is invoked. + * + * @return the number of threads created by this factory + */ + public long getThreadCount() { + return threadCounter.get(); + } + + /** + * Creates a new thread. This implementation delegates to the wrapped + * factory for creating the thread. Then, on the newly created thread the + * corresponding configuration options are set. + * + * @param r the {@code Runnable} to be executed by the new thread + * @return the newly created thread + */ + @Override + public Thread newThread(final Runnable r) { + final Thread t = getWrappedFactory().newThread(r); + initializeThread(t); + + return t; + } + + /** + * Initializes the specified thread. This method is called by + * {@link #newThread(Runnable)} after a new thread has been obtained from + * the wrapped thread factory. It initializes the thread according to the + * options set for this factory. + * + * @param t the thread to be initialized + */ + private void initializeThread(final Thread t) { + + if (getNamingPattern() != null) { + final Long count = Long.valueOf(threadCounter.incrementAndGet()); + t.setName(String.format(getNamingPattern(), count)); + } + + if (getUncaughtExceptionHandler() != null) { + t.setUncaughtExceptionHandler(getUncaughtExceptionHandler()); + } + + if (getPriority() != null) { + t.setPriority(getPriority().intValue()); + } + + if (getDaemonFlag() != null) { + t.setDaemon(getDaemonFlag().booleanValue()); + } + } + + /** + *
+ * A builder class for creating instances of {@code + * BasicThreadFactory}. + *
+ *+ * Using this builder class instances of {@code BasicThreadFactory} can be + * created and initialized. The class provides methods that correspond to + * the configuration options supported by {@code BasicThreadFactory}. Method + * chaining is supported. Refer to the documentation of {@code + * BasicThreadFactory} for a usage example. + *
+ * + */ + public static class Builder + implements org.apache.commons.lang3.builder.Builder+ * An implementation of the {@code ThreadFactory} interface that provides some + * configuration options for the threads it creates. + *
+ *+ * A {@code ThreadFactory} is used for instance by an {@code ExecutorService} to + * create the threads it uses for executing tasks. In many cases users do not + * have to care about a {@code ThreadFactory} because the default one used by an + * {@code ExecutorService} will do. However, if there are special requirements + * for the threads, a custom {@code ThreadFactory} has to be created. + *
+ *+ * This class provides some frequently needed configuration options for the + * threads it creates. These are the following: + *
+ *+ * {@code BasicThreadFactory} wraps another thread factory which actually + * creates new threads. The configuration options are set on the threads created + * by the wrapped thread factory. On construction time the factory to be wrapped + * can be specified. If none is provided, a default {@code ThreadFactory} is + * used. + *
+ *+ * Instances of {@code BasicThreadFactory} are not created directly, but the + * nested {@code Builder} class is used for this purpose. Using the builder only + * the configuration options an application is interested in need to be set. The + * following example shows how a {@code BasicThreadFactory} is created and + * installed in an {@code ExecutorService}: + *
+ * + *+ * // Create a factory that produces daemon threads with a naming pattern and + * // a priority + * BasicThreadFactory factory = new BasicThreadFactory.Builder() + * .namingPattern("workerthread-%d") + * .daemon(true) + * .priority(Thread.MAX_PRIORITY) + * .build(); + * // Create an executor service for single-threaded execution + * ExecutorService exec = Executors.newSingleThreadExecutor(factory); + *+ * + * @since 3.0 + */ +public class BasicThreadFactory implements ThreadFactory { + /** A counter for the threads created by this factory. */ + private final AtomicLong threadCounter; + + /** Stores the wrapped factory. */ + private final ThreadFactory wrappedFactory; + + /** Stores the uncaught exception handler. */ + private final Thread.UncaughtExceptionHandler uncaughtExceptionHandler; + + /** Stores the naming pattern for newly created threads. */ + private final String namingPattern; + + /** Stores the priority. */ + private final Integer priority; + + /** Stores the daemon status flag. */ + private final Boolean daemonFlag; + + /** + * Creates a new instance of {@code ThreadFactoryImpl} and configures it + * from the specified {@code Builder} object. + * + * @param builder the {@code Builder} object + */ + private BasicThreadFactory(final Builder builder) { + if (builder.wrappedFactory == null) { + wrappedFactory = Executors.defaultThreadFactory(); + } else { + wrappedFactory = builder.wrappedFactory; + } + + namingPattern = builder.namingPattern; + priority = builder.priority; + daemonFlag = builder.daemonFlag; + uncaughtExceptionHandler = builder.exceptionHandler; + + threadCounter = new AtomicLong(); + } + + /** + * Returns the wrapped {@code ThreadFactory}. This factory is used for + * actually creating threads. This method never returns null. If no + * {@code ThreadFactory} was passed when this object was created, a default + * thread factory is returned. + * + * @return the wrapped {@code ThreadFactory} + */ + public final ThreadFactory getWrappedFactory() { + return wrappedFactory; + } + + /** + * Returns the naming pattern for naming newly created threads. Result can + * be null if no naming pattern was provided. + * + * @return the naming pattern + */ + public final String getNamingPattern() { + return namingPattern; + } + + /** + * Returns the daemon flag. This flag determines whether newly created + * threads should be daemon threads. If true, this factory object + * calls {@code setDaemon(true)} on the newly created threads. Result can be + * null if no daemon flag was provided at creation time. + * + * @return the daemon flag + */ + public final Boolean getDaemonFlag() { + return daemonFlag; + } + + /** + * Returns the priority of the threads created by this factory. Result can + * be null if no priority was specified. + * + * @return the priority for newly created threads + */ + public final Integer getPriority() { + return priority; + } + + /** + * Returns the {@code UncaughtExceptionHandler} for the threads created by + * this factory. Result can be null if no handler was provided. + * + * @return the {@code UncaughtExceptionHandler} + */ + public final Thread.UncaughtExceptionHandler getUncaughtExceptionHandler() { + return uncaughtExceptionHandler; + } + + /** + * Returns the number of threads this factory has already created. This + * class maintains an internal counter that is incremented each time the + * {@link #newThread(Runnable)} method is invoked. + * + * @return the number of threads created by this factory + */ + public long getThreadCount() { + return threadCounter.get(); + } + + /** + * Creates a new thread. This implementation delegates to the wrapped + * factory for creating the thread. Then, on the newly created thread the + * corresponding configuration options are set. + * + * @param r the {@code Runnable} to be executed by the new thread + * @return the newly created thread + */ + @Override + public Thread newThread(final Runnable r) { + final Thread t = getWrappedFactory().newThread(r); + initializeThread(t); + + return t; + } + + /** + * Initializes the specified thread. This method is called by + * {@link #newThread(Runnable)} after a new thread has been obtained from + * the wrapped thread factory. It initializes the thread according to the + * options set for this factory. + * + * @param t the thread to be initialized + */ + private void initializeThread(final Thread t) { + + if (getNamingPattern() != null) { + final Long count = Long.valueOf(threadCounter.incrementAndGet()); + t.setName(String.format(getNamingPattern(), count)); + } + + if (getUncaughtExceptionHandler() != null) { + t.setUncaughtExceptionHandler(getUncaughtExceptionHandler()); + } + + if (getPriority() != null) { + t.setPriority(getPriority().intValue()); + } + + if (getDaemonFlag() != null) { + t.setDaemon(getDaemonFlag().booleanValue()); + } + } + + /** + *
+ * A builder class for creating instances of {@code + * BasicThreadFactory}. + *
+ *+ * Using this builder class instances of {@code BasicThreadFactory} can be + * created and initialized. The class provides methods that correspond to + * the configuration options supported by {@code BasicThreadFactory}. Method + * chaining is supported. Refer to the documentation of {@code + * BasicThreadFactory} for a usage example. + *
+ * + */ + public static class Builder + implements org.apache.commons.lang3.builder.Builder+ * An implementation of the {@code ThreadFactory} interface that provides some + * configuration options for the threads it creates. + *
+ *+ * A {@code ThreadFactory} is used for instance by an {@code ExecutorService} to + * create the threads it uses for executing tasks. In many cases users do not + * have to care about a {@code ThreadFactory} because the default one used by an + * {@code ExecutorService} will do. However, if there are special requirements + * for the threads, a custom {@code ThreadFactory} has to be created. + *
+ *+ * This class provides some frequently needed configuration options for the + * threads it creates. These are the following: + *
+ *+ * {@code BasicThreadFactory} wraps another thread factory which actually + * creates new threads. The configuration options are set on the threads created + * by the wrapped thread factory. On construction time the factory to be wrapped + * can be specified. If none is provided, a default {@code ThreadFactory} is + * used. + *
+ *+ * Instances of {@code BasicThreadFactory} are not created directly, but the + * nested {@code Builder} class is used for this purpose. Using the builder only + * the configuration options an application is interested in need to be set. The + * following example shows how a {@code BasicThreadFactory} is created and + * installed in an {@code ExecutorService}: + *
+ * + *+ * // Create a factory that produces daemon threads with a naming pattern and + * // a priority + * BasicThreadFactory factory = new BasicThreadFactory.Builder() + * .namingPattern("workerthread-%d") + * .daemon(true) + * .priority(Thread.MAX_PRIORITY) + * .build(); + * // Create an executor service for single-threaded execution + * ExecutorService exec = Executors.newSingleThreadExecutor(factory); + *+ * + * @since 3.0 + */ +public class BasicThreadFactory implements ThreadFactory { + /** A counter for the threads created by this factory. */ + private final AtomicLong threadCounter; + + /** Stores the wrapped factory. */ + private final ThreadFactory wrappedFactory; + + /** Stores the uncaught exception handler. */ + private final Thread.UncaughtExceptionHandler uncaughtExceptionHandler; + + /** Stores the naming pattern for newly created threads. */ + private final String namingPattern; + + /** Stores the priority. */ + private final Integer priority; + + /** Stores the daemon status flag. */ + private final Boolean daemonFlag; + + /** + * Creates a new instance of {@code ThreadFactoryImpl} and configures it + * from the specified {@code Builder} object. + * + * @param builder the {@code Builder} object + */ + private BasicThreadFactory(final Builder builder) { + if (builder.wrappedFactory == null) { + wrappedFactory = Executors.defaultThreadFactory(); + } else { + wrappedFactory = builder.wrappedFactory; + } + + namingPattern = builder.namingPattern; + priority = builder.priority; + daemonFlag = builder.daemonFlag; + uncaughtExceptionHandler = builder.exceptionHandler; + + threadCounter = new AtomicLong(); + } + + /** + * Returns the wrapped {@code ThreadFactory}. This factory is used for + * actually creating threads. This method never returns null. If no + * {@code ThreadFactory} was passed when this object was created, a default + * thread factory is returned. + * + * @return the wrapped {@code ThreadFactory} + */ + public final ThreadFactory getWrappedFactory() { + return wrappedFactory; + } + + /** + * Returns the naming pattern for naming newly created threads. Result can + * be null if no naming pattern was provided. + * + * @return the naming pattern + */ + public final String getNamingPattern() { + return namingPattern; + } + + /** + * Returns the daemon flag. This flag determines whether newly created + * threads should be daemon threads. If true, this factory object + * calls {@code setDaemon(true)} on the newly created threads. Result can be + * null if no daemon flag was provided at creation time. + * + * @return the daemon flag + */ + public final Boolean getDaemonFlag() { + return daemonFlag; + } + + /** + * Returns the priority of the threads created by this factory. Result can + * be null if no priority was specified. + * + * @return the priority for newly created threads + */ + public final Integer getPriority() { + return priority; + } + + /** + * Returns the {@code UncaughtExceptionHandler} for the threads created by + * this factory. Result can be null if no handler was provided. + * + * @return the {@code UncaughtExceptionHandler} + */ + public final Thread.UncaughtExceptionHandler getUncaughtExceptionHandler() { + return uncaughtExceptionHandler; + } + + /** + * Returns the number of threads this factory has already created. This + * class maintains an internal counter that is incremented each time the + * {@link #newThread(Runnable)} method is invoked. + * + * @return the number of threads created by this factory + */ + public long getThreadCount() { + return threadCounter.get(); + } + + /** + * Creates a new thread. This implementation delegates to the wrapped + * factory for creating the thread. Then, on the newly created thread the + * corresponding configuration options are set. + * + * @param r the {@code Runnable} to be executed by the new thread + * @return the newly created thread + */ + @Override + public Thread newThread(final Runnable r) { + final Thread t = getWrappedFactory().newThread(r); + initializeThread(t); + + return t; + } + + /** + * Initializes the specified thread. This method is called by + * {@link #newThread(Runnable)} after a new thread has been obtained from + * the wrapped thread factory. It initializes the thread according to the + * options set for this factory. + * + * @param t the thread to be initialized + */ + private void initializeThread(final Thread t) { + + if (getNamingPattern() != null) { + final Long count = Long.valueOf(threadCounter.incrementAndGet()); + t.setName(String.format(getNamingPattern(), count)); + } + + if (getUncaughtExceptionHandler() != null) { + t.setUncaughtExceptionHandler(getUncaughtExceptionHandler()); + } + + if (getPriority() != null) { + t.setPriority(getPriority().intValue()); + } + + if (getDaemonFlag() != null) { + t.setDaemon(getDaemonFlag().booleanValue()); + } + } + + /** + *
+ * A builder class for creating instances of {@code + * BasicThreadFactory}. + *
+ *+ * Using this builder class instances of {@code BasicThreadFactory} can be + * created and initialized. The class provides methods that correspond to + * the configuration options supported by {@code BasicThreadFactory}. Method + * chaining is supported. Refer to the documentation of {@code + * BasicThreadFactory} for a usage example. + *
+ * + */ + public static class Builder + implements org.apache.commons.lang3.builder.BuilderOperations on boolean primitives and Boolean objects.
+ * + *This class tries to handle {@code null} input gracefully. + * An exception will not be thrown for a {@code null} input. + * Each method documents its behaviour in more detail.
+ * + *#ThreadSafe#
+ * @since 2.0 + */ +public class BooleanUtils { + + /** + *{@code BooleanUtils} instances should NOT be constructed in standard programming. + * Instead, the class should be used as {@code BooleanUtils.negate(true);}.
+ * + *This constructor is public to permit tools that require a JavaBean instance + * to operate.
+ */ + public BooleanUtils() { + super(); + } + + // Boolean utilities + //-------------------------------------------------------------------------- + /** + *Negates the specified boolean.
+ * + *If {@code null} is passed in, {@code null} will be returned.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.negate(Boolean.TRUE) = Boolean.FALSE; + * BooleanUtils.negate(Boolean.FALSE) = Boolean.TRUE; + * BooleanUtils.negate(null) = null; + *+ * + * @param bool the Boolean to negate, may be null + * @return the negated Boolean, or {@code null} if {@code null} input + */ + public static Boolean negate(final Boolean bool) { + if (bool == null) { + return null; + } + return bool.booleanValue() ? Boolean.FALSE : Boolean.TRUE; + } + + // boolean Boolean methods + //----------------------------------------------------------------------- + /** + *
Checks if a {@code Boolean} value is {@code true}, + * handling {@code null} by returning {@code false}.
+ * + *+ * BooleanUtils.isTrue(Boolean.TRUE) = true + * BooleanUtils.isTrue(Boolean.FALSE) = false + * BooleanUtils.isTrue(null) = false + *+ * + * @param bool the boolean to check, null returns {@code false} + * @return {@code true} only if the input is non-null and true + * @since 2.1 + */ + public static boolean isTrue(final Boolean bool) { + return Boolean.TRUE.equals(bool); + } + + /** + *
Checks if a {@code Boolean} value is not {@code true}, + * handling {@code null} by returning {@code true}.
+ * + *+ * BooleanUtils.isNotTrue(Boolean.TRUE) = false + * BooleanUtils.isNotTrue(Boolean.FALSE) = true + * BooleanUtils.isNotTrue(null) = true + *+ * + * @param bool the boolean to check, null returns {@code true} + * @return {@code true} if the input is null or false + * @since 2.3 + */ + public static boolean isNotTrue(final Boolean bool) { + return !isTrue(bool); + } + + /** + *
Checks if a {@code Boolean} value is {@code false}, + * handling {@code null} by returning {@code false}.
+ * + *+ * BooleanUtils.isFalse(Boolean.TRUE) = false + * BooleanUtils.isFalse(Boolean.FALSE) = true + * BooleanUtils.isFalse(null) = false + *+ * + * @param bool the boolean to check, null returns {@code false} + * @return {@code true} only if the input is non-null and false + * @since 2.1 + */ + public static boolean isFalse(final Boolean bool) { + return Boolean.FALSE.equals(bool); + } + + /** + *
Checks if a {@code Boolean} value is not {@code false}, + * handling {@code null} by returning {@code true}.
+ * + *+ * BooleanUtils.isNotFalse(Boolean.TRUE) = true + * BooleanUtils.isNotFalse(Boolean.FALSE) = false + * BooleanUtils.isNotFalse(null) = true + *+ * + * @param bool the boolean to check, null returns {@code true} + * @return {@code true} if the input is null or true + * @since 2.3 + */ + public static boolean isNotFalse(final Boolean bool) { + return !isFalse(bool); + } + + //----------------------------------------------------------------------- + /** + *
Converts a Boolean to a boolean handling {@code null} + * by returning {@code false}.
+ * + *+ * BooleanUtils.toBoolean(Boolean.TRUE) = true + * BooleanUtils.toBoolean(Boolean.FALSE) = false + * BooleanUtils.toBoolean(null) = false + *+ * + * @param bool the boolean to convert + * @return {@code true} or {@code false}, {@code null} returns {@code false} + */ + public static boolean toBoolean(final Boolean bool) { + return bool != null && bool.booleanValue(); + } + + /** + *
Converts a Boolean to a boolean handling {@code null}.
+ * + *+ * BooleanUtils.toBooleanDefaultIfNull(Boolean.TRUE, false) = true + * BooleanUtils.toBooleanDefaultIfNull(Boolean.FALSE, true) = false + * BooleanUtils.toBooleanDefaultIfNull(null, true) = true + *+ * + * @param bool the boolean to convert + * @param valueIfNull the boolean value to return if {@code null} + * @return {@code true} or {@code false} + */ + public static boolean toBooleanDefaultIfNull(final Boolean bool, final boolean valueIfNull) { + if (bool == null) { + return valueIfNull; + } + return bool.booleanValue(); + } + + // Integer to Boolean methods + //----------------------------------------------------------------------- + /** + *
Converts an int to a boolean using the convention that {@code zero} + * is {@code false}.
+ * + *+ * BooleanUtils.toBoolean(0) = false + * BooleanUtils.toBoolean(1) = true + * BooleanUtils.toBoolean(2) = true + *+ * + * @param value the int to convert + * @return {@code true} if non-zero, {@code false} + * if zero + */ + public static boolean toBoolean(final int value) { + return value != 0; + } + + /** + *
Converts an int to a Boolean using the convention that {@code zero} + * is {@code false}.
+ * + *+ * BooleanUtils.toBoolean(0) = Boolean.FALSE + * BooleanUtils.toBoolean(1) = Boolean.TRUE + * BooleanUtils.toBoolean(2) = Boolean.TRUE + *+ * + * @param value the int to convert + * @return Boolean.TRUE if non-zero, Boolean.FALSE if zero, + * {@code null} if {@code null} + */ + public static Boolean toBooleanObject(final int value) { + return value == 0 ? Boolean.FALSE : Boolean.TRUE; + } + + /** + *
Converts an Integer to a Boolean using the convention that {@code zero} + * is {@code false}.
+ * + *{@code null} will be converted to {@code null}.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.toBoolean(Integer.valueOf(0)) = Boolean.FALSE + * BooleanUtils.toBoolean(Integer.valueOf(1)) = Boolean.TRUE + * BooleanUtils.toBoolean(Integer.valueOf(null)) = null + *+ * + * @param value the Integer to convert + * @return Boolean.TRUE if non-zero, Boolean.FALSE if zero, + * {@code null} if {@code null} input + */ + public static Boolean toBooleanObject(final Integer value) { + if (value == null) { + return null; + } + return value.intValue() == 0 ? Boolean.FALSE : Boolean.TRUE; + } + + /** + *
Converts an int to a boolean specifying the conversion values.
+ * + *+ * BooleanUtils.toBoolean(0, 1, 0) = false + * BooleanUtils.toBoolean(1, 1, 0) = true + * BooleanUtils.toBoolean(2, 1, 2) = false + * BooleanUtils.toBoolean(2, 2, 0) = true + *+ * + * @param value the Integer to convert + * @param trueValue the value to match for {@code true} + * @param falseValue the value to match for {@code false} + * @return {@code true} or {@code false} + * @throws IllegalArgumentException if no match + */ + public static boolean toBoolean(final int value, final int trueValue, final int falseValue) { + if (value == trueValue) { + return true; + } + if (value == falseValue) { + return false; + } + // no match + throw new IllegalArgumentException("The Integer did not match either specified value"); + } + + /** + *
Converts an Integer to a boolean specifying the conversion values.
+ * + *+ * BooleanUtils.toBoolean(Integer.valueOf(0), Integer.valueOf(1), Integer.valueOf(0)) = false + * BooleanUtils.toBoolean(Integer.valueOf(1), Integer.valueOf(1), Integer.valueOf(0)) = true + * BooleanUtils.toBoolean(Integer.valueOf(2), Integer.valueOf(1), Integer.valueOf(2)) = false + * BooleanUtils.toBoolean(Integer.valueOf(2), Integer.valueOf(2), Integer.valueOf(0)) = true + * BooleanUtils.toBoolean(null, null, Integer.valueOf(0)) = true + *+ * + * @param value the Integer to convert + * @param trueValue the value to match for {@code true}, may be {@code null} + * @param falseValue the value to match for {@code false}, may be {@code null} + * @return {@code true} or {@code false} + * @throws IllegalArgumentException if no match + */ + public static boolean toBoolean(final Integer value, final Integer trueValue, final Integer falseValue) { + if (value == null) { + if (trueValue == null) { + return true; + } + if (falseValue == null) { + return false; + } + } else if (value.equals(trueValue)) { + return true; + } else if (value.equals(falseValue)) { + return false; + } + // no match + throw new IllegalArgumentException("The Integer did not match either specified value"); + } + + /** + *
Converts an int to a Boolean specifying the conversion values.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.toBooleanObject(0, 0, 2, 3) = Boolean.TRUE + * BooleanUtils.toBooleanObject(2, 1, 2, 3) = Boolean.FALSE + * BooleanUtils.toBooleanObject(3, 1, 2, 3) = null + *+ * + * @param value the Integer to convert + * @param trueValue the value to match for {@code true} + * @param falseValue the value to match for {@code false} + * @param nullValue the value to to match for {@code null} + * @return Boolean.TRUE, Boolean.FALSE, or {@code null} + * @throws IllegalArgumentException if no match + */ + public static Boolean toBooleanObject(final int value, final int trueValue, final int falseValue, final int nullValue) { + if (value == trueValue) { + return Boolean.TRUE; + } + if (value == falseValue) { + return Boolean.FALSE; + } + if (value == nullValue) { + return null; + } + // no match + throw new IllegalArgumentException("The Integer did not match any specified value"); + } + + /** + *
Converts an Integer to a Boolean specifying the conversion values.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.toBooleanObject(Integer.valueOf(0), Integer.valueOf(0), Integer.valueOf(2), Integer.valueOf(3)) = Boolean.TRUE + * BooleanUtils.toBooleanObject(Integer.valueOf(2), Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(3)) = Boolean.FALSE + * BooleanUtils.toBooleanObject(Integer.valueOf(3), Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(3)) = null + *+ * + * @param value the Integer to convert + * @param trueValue the value to match for {@code true}, may be {@code null} + * @param falseValue the value to match for {@code false}, may be {@code null} + * @param nullValue the value to to match for {@code null}, may be {@code null} + * @return Boolean.TRUE, Boolean.FALSE, or {@code null} + * @throws IllegalArgumentException if no match + */ + public static Boolean toBooleanObject(final Integer value, final Integer trueValue, final Integer falseValue, final Integer nullValue) { + if (value == null) { + if (trueValue == null) { + return Boolean.TRUE; + } + if (falseValue == null) { + return Boolean.FALSE; + } + if (nullValue == null) { + return null; + } + } else if (value.equals(trueValue)) { + return Boolean.TRUE; + } else if (value.equals(falseValue)) { + return Boolean.FALSE; + } else if (value.equals(nullValue)) { + return null; + } + // no match + throw new IllegalArgumentException("The Integer did not match any specified value"); + } + + // Boolean to Integer methods + //----------------------------------------------------------------------- + /** + *
Converts a boolean to an int using the convention that + * {@code zero} is {@code false}.
+ * + *+ * BooleanUtils.toInteger(true) = 1 + * BooleanUtils.toInteger(false) = 0 + *+ * + * @param bool the boolean to convert + * @return one if {@code true}, zero if {@code false} + */ + public static int toInteger(final boolean bool) { + return bool ? 1 : 0; + } + + /** + *
Converts a boolean to an Integer using the convention that + * {@code zero} is {@code false}.
+ * + *+ * BooleanUtils.toIntegerObject(true) = Integer.valueOf(1) + * BooleanUtils.toIntegerObject(false) = Integer.valueOf(0) + *+ * + * @param bool the boolean to convert + * @return one if {@code true}, zero if {@code false} + */ + public static Integer toIntegerObject(final boolean bool) { + return bool ? NumberUtils.INTEGER_ONE : NumberUtils.INTEGER_ZERO; + } + + /** + *
Converts a Boolean to a Integer using the convention that + * {@code zero} is {@code false}.
+ * + *{@code null} will be converted to {@code null}.
+ * + *+ * BooleanUtils.toIntegerObject(Boolean.TRUE) = Integer.valueOf(1) + * BooleanUtils.toIntegerObject(Boolean.FALSE) = Integer.valueOf(0) + *+ * + * @param bool the Boolean to convert + * @return one if Boolean.TRUE, zero if Boolean.FALSE, {@code null} if {@code null} + */ + public static Integer toIntegerObject(final Boolean bool) { + if (bool == null) { + return null; + } + return bool.booleanValue() ? NumberUtils.INTEGER_ONE : NumberUtils.INTEGER_ZERO; + } + + /** + *
Converts a boolean to an int specifying the conversion values.
+ * + *+ * BooleanUtils.toInteger(true, 1, 0) = 1 + * BooleanUtils.toInteger(false, 1, 0) = 0 + *+ * + * @param bool the to convert + * @param trueValue the value to return if {@code true} + * @param falseValue the value to return if {@code false} + * @return the appropriate value + */ + public static int toInteger(final boolean bool, final int trueValue, final int falseValue) { + return bool ? trueValue : falseValue; + } + + /** + *
Converts a Boolean to an int specifying the conversion values.
+ * + *+ * BooleanUtils.toInteger(Boolean.TRUE, 1, 0, 2) = 1 + * BooleanUtils.toInteger(Boolean.FALSE, 1, 0, 2) = 0 + * BooleanUtils.toInteger(null, 1, 0, 2) = 2 + *+ * + * @param bool the Boolean to convert + * @param trueValue the value to return if {@code true} + * @param falseValue the value to return if {@code false} + * @param nullValue the value to return if {@code null} + * @return the appropriate value + */ + public static int toInteger(final Boolean bool, final int trueValue, final int falseValue, final int nullValue) { + if (bool == null) { + return nullValue; + } + return bool.booleanValue() ? trueValue : falseValue; + } + + /** + *
Converts a boolean to an Integer specifying the conversion values.
+ * + *+ * BooleanUtils.toIntegerObject(true, Integer.valueOf(1), Integer.valueOf(0)) = Integer.valueOf(1) + * BooleanUtils.toIntegerObject(false, Integer.valueOf(1), Integer.valueOf(0)) = Integer.valueOf(0) + *+ * + * @param bool the to convert + * @param trueValue the value to return if {@code true}, may be {@code null} + * @param falseValue the value to return if {@code false}, may be {@code null} + * @return the appropriate value + */ + public static Integer toIntegerObject(final boolean bool, final Integer trueValue, final Integer falseValue) { + return bool ? trueValue : falseValue; + } + + /** + *
Converts a Boolean to an Integer specifying the conversion values.
+ * + *+ * BooleanUtils.toIntegerObject(Boolean.TRUE, Integer.valueOf(1), Integer.valueOf(0), Integer.valueOf(2)) = Integer.valueOf(1) + * BooleanUtils.toIntegerObject(Boolean.FALSE, Integer.valueOf(1), Integer.valueOf(0), Integer.valueOf(2)) = Integer.valueOf(0) + * BooleanUtils.toIntegerObject(null, Integer.valueOf(1), Integer.valueOf(0), Integer.valueOf(2)) = Integer.valueOf(2) + *+ * + * @param bool the Boolean to convert + * @param trueValue the value to return if {@code true}, may be {@code null} + * @param falseValue the value to return if {@code false}, may be {@code null} + * @param nullValue the value to return if {@code null}, may be {@code null} + * @return the appropriate value + */ + public static Integer toIntegerObject(final Boolean bool, final Integer trueValue, final Integer falseValue, final Integer nullValue) { + if (bool == null) { + return nullValue; + } + return bool.booleanValue() ? trueValue : falseValue; + } + + // String to Boolean methods + //----------------------------------------------------------------------- + /** + *
Converts a String to a Boolean.
+ * + *{@code 'true'}, {@code 'on'}, {@code 'y'}, {@code 't'} or {@code 'yes'} + * (case insensitive) will return {@code true}. + * {@code 'false'}, {@code 'off'}, {@code 'n'}, {@code 'f'} or {@code 'no'} + * (case insensitive) will return {@code false}. + * Otherwise, {@code null} is returned.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * // N.B. case is not significant + * BooleanUtils.toBooleanObject(null) = null + * BooleanUtils.toBooleanObject("true") = Boolean.TRUE + * BooleanUtils.toBooleanObject("T") = Boolean.TRUE // i.e. T[RUE] + * BooleanUtils.toBooleanObject("false") = Boolean.FALSE + * BooleanUtils.toBooleanObject("f") = Boolean.FALSE // i.e. f[alse] + * BooleanUtils.toBooleanObject("No") = Boolean.FALSE + * BooleanUtils.toBooleanObject("n") = Boolean.FALSE // i.e. n[o] + * BooleanUtils.toBooleanObject("on") = Boolean.TRUE + * BooleanUtils.toBooleanObject("ON") = Boolean.TRUE + * BooleanUtils.toBooleanObject("off") = Boolean.FALSE + * BooleanUtils.toBooleanObject("oFf") = Boolean.FALSE + * BooleanUtils.toBooleanObject("yes") = Boolean.TRUE + * BooleanUtils.toBooleanObject("Y") = Boolean.TRUE // i.e. Y[ES] + * BooleanUtils.toBooleanObject("blue") = null + * BooleanUtils.toBooleanObject("true ") = null // trailing space (too long) + * BooleanUtils.toBooleanObject("ono") = null // does not match on or no + *+ * + * @param str the String to check; upper and lower case are treated as the same + * @return the Boolean value of the string, {@code null} if no match or {@code null} input + */ + public static Boolean toBooleanObject(final String str) { + // Previously used equalsIgnoreCase, which was fast for interned 'true'. + // Non interned 'true' matched 15 times slower. + // + // Optimisation provides same performance as before for interned 'true'. + // Similar performance for null, 'false', and other strings not length 2/3/4. + // 'true'/'TRUE' match 4 times slower, 'tRUE'/'True' 7 times slower. + if (str == "true") { + return Boolean.TRUE; + } + if (str == null) { + return null; + } + switch (str.length()) { + case 1: { + final char ch0 = str.charAt(0); + if (ch0 == 'y' || ch0 == 'Y' || + ch0 == 't' || ch0 == 'T') { + return Boolean.TRUE; + } + if (ch0 == 'n' || ch0 == 'N' || + ch0 == 'f' || ch0 == 'F') { + return Boolean.FALSE; + } + break; + } + case 2: { + final char ch0 = str.charAt(0); + final char ch1 = str.charAt(1); + if ((ch0 == 'o' || ch0 == 'O') && + (ch1 == 'n' || ch1 == 'N') ) { + return Boolean.TRUE; + } + if ((ch0 == 'n' || ch0 == 'N') && + (ch1 == 'o' || ch1 == 'O') ) { + return Boolean.FALSE; + } + break; + } + case 3: { + final char ch0 = str.charAt(0); + final char ch1 = str.charAt(1); + final char ch2 = str.charAt(2); + if ((ch0 == 'y' || ch0 == 'Y') && + (ch1 == 'e' || ch1 == 'E') && + (ch2 == 's' || ch2 == 'S') ) { + return Boolean.TRUE; + } + if ((ch0 == 'o' || ch0 == 'O') && + (ch1 == 'f' || ch1 == 'F') && + (ch2 == 'f' || ch2 == 'F') ) { + return Boolean.FALSE; + } + break; + } + case 4: { + final char ch0 = str.charAt(0); + final char ch1 = str.charAt(1); + final char ch2 = str.charAt(2); + final char ch3 = str.charAt(3); + if ((ch0 == 't' || ch0 == 'T') && + (ch1 == 'r' || ch1 == 'R') && + (ch2 == 'u' || ch2 == 'U') && + (ch3 == 'e' || ch3 == 'E') ) { + return Boolean.TRUE; + } + break; + } + case 5: { + final char ch0 = str.charAt(0); + final char ch1 = str.charAt(1); + final char ch2 = str.charAt(2); + final char ch3 = str.charAt(3); + final char ch4 = str.charAt(4); + if ((ch0 == 'f' || ch0 == 'F') && + (ch1 == 'a' || ch1 == 'A') && + (ch2 == 'l' || ch2 == 'L') && + (ch3 == 's' || ch3 == 'S') && + (ch4 == 'e' || ch4 == 'E') ) { + return Boolean.FALSE; + } + break; + } + default: + break; + } + + return null; + } + + /** + *
Converts a String to a Boolean throwing an exception if no match.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.toBooleanObject("true", "true", "false", "null") = Boolean.TRUE + * BooleanUtils.toBooleanObject("false", "true", "false", "null") = Boolean.FALSE + * BooleanUtils.toBooleanObject("null", "true", "false", "null") = null + *+ * + * @param str the String to check + * @param trueString the String to match for {@code true} (case sensitive), may be {@code null} + * @param falseString the String to match for {@code false} (case sensitive), may be {@code null} + * @param nullString the String to match for {@code null} (case sensitive), may be {@code null} + * @return the Boolean value of the string, {@code null} if either the String matches {@code nullString} + * or if {@code null} input and {@code nullString} is {@code null} + * @throws IllegalArgumentException if the String doesn't match + */ + public static Boolean toBooleanObject(final String str, final String trueString, final String falseString, final String nullString) { + if (str == null) { + if (trueString == null) { + return Boolean.TRUE; + } + if (falseString == null) { + return Boolean.FALSE; + } + if (nullString == null) { + return null; + } + } else if (str.equals(trueString)) { + return Boolean.TRUE; + } else if (str.equals(falseString)) { + return Boolean.FALSE; + } else if (str.equals(nullString)) { + return null; + } + // no match + throw new IllegalArgumentException("The String did not match any specified value"); + } + + // String to boolean methods + //----------------------------------------------------------------------- + /** + *
Converts a String to a boolean (optimised for performance).
+ * + *{@code 'true'}, {@code 'on'}, {@code 'y'}, {@code 't'} or {@code 'yes'} + * (case insensitive) will return {@code true}. Otherwise, + * {@code false} is returned.
+ * + *This method performs 4 times faster (JDK1.4) than + * {@code Boolean.valueOf(String)}. However, this method accepts + * 'on' and 'yes', 't', 'y' as true values. + * + *
+ * BooleanUtils.toBoolean(null) = false + * BooleanUtils.toBoolean("true") = true + * BooleanUtils.toBoolean("TRUE") = true + * BooleanUtils.toBoolean("tRUe") = true + * BooleanUtils.toBoolean("on") = true + * BooleanUtils.toBoolean("yes") = true + * BooleanUtils.toBoolean("false") = false + * BooleanUtils.toBoolean("x gti") = false + * BooleanUtils.toBooleanObject("y") = true + * BooleanUtils.toBooleanObject("n") = false + * BooleanUtils.toBooleanObject("t") = true + * BooleanUtils.toBooleanObject("f") = false + *+ * + * @param str the String to check + * @return the boolean value of the string, {@code false} if no match or the String is null + */ + public static boolean toBoolean(final String str) { + return toBooleanObject(str) == Boolean.TRUE; + } + + /** + *
Converts a String to a Boolean throwing an exception if no match found.
+ * + *+ * BooleanUtils.toBoolean("true", "true", "false") = true + * BooleanUtils.toBoolean("false", "true", "false") = false + *+ * + * @param str the String to check + * @param trueString the String to match for {@code true} (case sensitive), may be {@code null} + * @param falseString the String to match for {@code false} (case sensitive), may be {@code null} + * @return the boolean value of the string + * @throws IllegalArgumentException if the String doesn't match + */ + public static boolean toBoolean(final String str, final String trueString, final String falseString) { + if (str == trueString) { + return true; + } else if (str == falseString) { + return false; + } else if (str != null) { + if (str.equals(trueString)) { + return true; + } else if (str.equals(falseString)) { + return false; + } + } + // no match + throw new IllegalArgumentException("The String did not match either specified value"); + } + + // Boolean to String methods + //----------------------------------------------------------------------- + /** + *
Converts a Boolean to a String returning {@code 'true'}, + * {@code 'false'}, or {@code null}.
+ * + *+ * BooleanUtils.toStringTrueFalse(Boolean.TRUE) = "true" + * BooleanUtils.toStringTrueFalse(Boolean.FALSE) = "false" + * BooleanUtils.toStringTrueFalse(null) = null; + *+ * + * @param bool the Boolean to check + * @return {@code 'true'}, {@code 'false'}, or {@code null} + */ + public static String toStringTrueFalse(final Boolean bool) { + return toString(bool, "true", "false", null); + } + + /** + *
Converts a Boolean to a String returning {@code 'on'}, + * {@code 'off'}, or {@code null}.
+ * + *+ * BooleanUtils.toStringOnOff(Boolean.TRUE) = "on" + * BooleanUtils.toStringOnOff(Boolean.FALSE) = "off" + * BooleanUtils.toStringOnOff(null) = null; + *+ * + * @param bool the Boolean to check + * @return {@code 'on'}, {@code 'off'}, or {@code null} + */ + public static String toStringOnOff(final Boolean bool) { + return toString(bool, "on", "off", null); + } + + /** + *
Converts a Boolean to a String returning {@code 'yes'}, + * {@code 'no'}, or {@code null}.
+ * + *+ * BooleanUtils.toStringYesNo(Boolean.TRUE) = "yes" + * BooleanUtils.toStringYesNo(Boolean.FALSE) = "no" + * BooleanUtils.toStringYesNo(null) = null; + *+ * + * @param bool the Boolean to check + * @return {@code 'yes'}, {@code 'no'}, or {@code null} + */ + public static String toStringYesNo(final Boolean bool) { + return toString(bool, "yes", "no", null); + } + + /** + *
Converts a Boolean to a String returning one of the input Strings.
+ * + *+ * BooleanUtils.toString(Boolean.TRUE, "true", "false", null) = "true" + * BooleanUtils.toString(Boolean.FALSE, "true", "false", null) = "false" + * BooleanUtils.toString(null, "true", "false", null) = null; + *+ * + * @param bool the Boolean to check + * @param trueString the String to return if {@code true}, may be {@code null} + * @param falseString the String to return if {@code false}, may be {@code null} + * @param nullString the String to return if {@code null}, may be {@code null} + * @return one of the three input Strings + */ + public static String toString(final Boolean bool, final String trueString, final String falseString, final String nullString) { + if (bool == null) { + return nullString; + } + return bool.booleanValue() ? trueString : falseString; + } + + // boolean to String methods + //----------------------------------------------------------------------- + /** + *
Converts a boolean to a String returning {@code 'true'} + * or {@code 'false'}.
+ * + *+ * BooleanUtils.toStringTrueFalse(true) = "true" + * BooleanUtils.toStringTrueFalse(false) = "false" + *+ * + * @param bool the Boolean to check + * @return {@code 'true'}, {@code 'false'}, or {@code null} + */ + public static String toStringTrueFalse(final boolean bool) { + return toString(bool, "true", "false"); + } + + /** + *
Converts a boolean to a String returning {@code 'on'} + * or {@code 'off'}.
+ * + *+ * BooleanUtils.toStringOnOff(true) = "on" + * BooleanUtils.toStringOnOff(false) = "off" + *+ * + * @param bool the Boolean to check + * @return {@code 'on'}, {@code 'off'}, or {@code null} + */ + public static String toStringOnOff(final boolean bool) { + return toString(bool, "on", "off"); + } + + /** + *
Converts a boolean to a String returning {@code 'yes'} + * or {@code 'no'}.
+ * + *+ * BooleanUtils.toStringYesNo(true) = "yes" + * BooleanUtils.toStringYesNo(false) = "no" + *+ * + * @param bool the Boolean to check + * @return {@code 'yes'}, {@code 'no'}, or {@code null} + */ + public static String toStringYesNo(final boolean bool) { + return toString(bool, "yes", "no"); + } + + /** + *
Converts a boolean to a String returning one of the input Strings.
+ * + *+ * BooleanUtils.toString(true, "true", "false") = "true" + * BooleanUtils.toString(false, "true", "false") = "false" + *+ * + * @param bool the Boolean to check + * @param trueString the String to return if {@code true}, may be {@code null} + * @param falseString the String to return if {@code false}, may be {@code null} + * @return one of the two input Strings + */ + public static String toString(final boolean bool, final String trueString, final String falseString) { + return bool ? trueString : falseString; + } + + // logical operations + // ---------------------------------------------------------------------- + /** + *
Performs an and on a set of booleans.
+ * + *+ * BooleanUtils.and(true, true) = true + * BooleanUtils.and(false, false) = false + * BooleanUtils.and(true, false) = false + * BooleanUtils.and(true, true, false) = false + * BooleanUtils.and(true, true, true) = true + *+ * + * @param array an array of {@code boolean}s + * @return {@code true} if the and is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + * @since 3.0.1 + */ + public static boolean and(final boolean... array) { + // Validates input + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + for (final boolean element : array) { + if (!element) { + return false; + } + } + return true; + } + + /** + *
Performs an and on an array of Booleans.
+ * + *+ * BooleanUtils.and(Boolean.TRUE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.and(Boolean.FALSE, Boolean.FALSE) = Boolean.FALSE + * BooleanUtils.and(Boolean.TRUE, Boolean.FALSE) = Boolean.FALSE + * BooleanUtils.and(Boolean.TRUE, Boolean.TRUE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.and(Boolean.FALSE, Boolean.FALSE, Boolean.TRUE) = Boolean.FALSE + * BooleanUtils.and(Boolean.TRUE, Boolean.FALSE, Boolean.TRUE) = Boolean.FALSE + *+ * + * @param array an array of {@code Boolean}s + * @return {@code true} if the and is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + * @throws IllegalArgumentException if {@code array} contains a {@code null} + * @since 3.0.1 + */ + public static Boolean and(final Boolean... array) { + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + try { + final boolean[] primitive = ArrayUtils.toPrimitive(array); + return and(primitive) ? Boolean.TRUE : Boolean.FALSE; + } catch (final NullPointerException ex) { + throw new IllegalArgumentException("The array must not contain any null elements"); + } + } + + /** + *
Performs an or on a set of booleans.
+ * + *+ * BooleanUtils.or(true, true) = true + * BooleanUtils.or(false, false) = false + * BooleanUtils.or(true, false) = true + * BooleanUtils.or(true, true, false) = true + * BooleanUtils.or(true, true, true) = true + * BooleanUtils.or(false, false, false) = false + *+ * + * @param array an array of {@code boolean}s + * @return {@code true} if the or is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + * @since 3.0.1 + */ + public static boolean or(final boolean... array) { + // Validates input + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + for (final boolean element : array) { + if (element) { + return true; + } + } + return false; + } + + /** + *
Performs an or on an array of Booleans.
+ * + *+ * BooleanUtils.or(Boolean.TRUE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.or(Boolean.FALSE, Boolean.FALSE) = Boolean.FALSE + * BooleanUtils.or(Boolean.TRUE, Boolean.FALSE) = Boolean.TRUE + * BooleanUtils.or(Boolean.TRUE, Boolean.TRUE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.or(Boolean.FALSE, Boolean.FALSE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.or(Boolean.TRUE, Boolean.FALSE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.or(Boolean.FALSE, Boolean.FALSE, Boolean.FALSE) = Boolean.FALSE + *+ * + * @param array an array of {@code Boolean}s + * @return {@code true} if the or is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + * @throws IllegalArgumentException if {@code array} contains a {@code null} + * @since 3.0.1 + */ +/** + *
Performs an or on an array of Booleans.
+ * + *+ * BooleanUtils.or(Boolean.TRUE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.or(Boolean.FALSE, Boolean.FALSE) = Boolean.FALSE + * BooleanUtils.or(Boolean.TRUE, Boolean.FALSE) = Boolean.TRUE + * BooleanUtils.or(Boolean.TRUE, Boolean.TRUE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.or(Boolean.FALSE, Boolean.FALSE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.or(Boolean.TRUE, Boolean.FALSE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.or(Boolean.FALSE, Boolean.FALSE, Boolean.FALSE) = Boolean.FALSE + *+ * + * @param array + * an array of {@code Boolean}s + * @return {@code true} if the or is successful. + * @throws IllegalArgumentException + * if {@code array} is {@code null} + * @throws IllegalArgumentException + * if {@code array} is empty. + * @throws IllegalArgumentException + * if {@code array} contains a {@code null} + * @since 3.0.1 + */ +public static java.lang.Boolean or(final java.lang.Boolean... array) { + { + if (/* NPEX_NULL_EXP */ + array.length == 0) { + throw new java.lang.IllegalArgumentException("Array is empty"); + } + try { + final boolean[] primitive = org.apache.commons.lang3.ArrayUtils.toPrimitive(array); + return org.apache.commons.lang3.BooleanUtils.or(primitive) ? java.lang.Boolean.TRUE : java.lang.Boolean.FALSE; + } catch (final java.lang.NullPointerException ex) { + throw new java.lang.IllegalArgumentException("The array must not contain any null elements"); + } + } +} + + /** + *
Performs an xor on a set of booleans.
+ * + *+ * BooleanUtils.xor(true, true) = false + * BooleanUtils.xor(false, false) = false + * BooleanUtils.xor(true, false) = true + * BooleanUtils.xor(true, true) = false + * BooleanUtils.xor(false, false) = false + * BooleanUtils.xor(true, false) = true + *+ * + * @param array an array of {@code boolean}s + * @return {@code true} if the xor is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + */ + public static boolean xor(final boolean... array) { + // Validates input + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + + // false if the neutral element of the xor operator + boolean result = false; + for (final boolean element : array) { + result ^= element; + } + + return result; + } + + /** + *
Performs an xor on an array of Booleans.
+ * + *+ * BooleanUtils.xor(new Boolean[] { Boolean.TRUE, Boolean.TRUE }) = Boolean.FALSE + * BooleanUtils.xor(new Boolean[] { Boolean.FALSE, Boolean.FALSE }) = Boolean.FALSE + * BooleanUtils.xor(new Boolean[] { Boolean.TRUE, Boolean.FALSE }) = Boolean.TRUE + *+ * + * @param array an array of {@code Boolean}s + * @return {@code true} if the xor is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + * @throws IllegalArgumentException if {@code array} contains a {@code null} + */ + public static Boolean xor(final Boolean... array) { + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + try { + final boolean[] primitive = ArrayUtils.toPrimitive(array); + return xor(primitive) ? Boolean.TRUE : Boolean.FALSE; + } catch (final NullPointerException ex) { + throw new IllegalArgumentException("The array must not contain any null elements"); + } + } + + /** + *
Compares two {@code boolean} values. This is the same functionality as provided in Java 7.
+ * + * @param x the first {@code boolean} to compare + * @param y the second {@code boolean} to compare + * @return the value {@code 0} if {@code x == y}; + * a value less than {@code 0} if {@code !x && y}; and + * a value greater than {@code 0} if {@code x && !y} + * @since 3.4 + */ + public static int compare(boolean x, boolean y) { + if (x == y) { + return 0; + } + return x ? 1 : -1; + } + +} diff --git a/Java/commons-lang-BooleanUtils_1008/metadata.json b/Java/commons-lang-BooleanUtils_1008/metadata.json new file mode 100644 index 000000000..5e4d9a9bf --- /dev/null +++ b/Java/commons-lang-BooleanUtils_1008/metadata.json @@ -0,0 +1,21 @@ +{ + "language": "java", + "id": "commons-lang-BooleanUtils_1008", + "buggyPath": ".", + "referencePath": null, + "buildCommand": "mvn package -V -B -Denforcer.skip=true -Dcheckstyle.skip=true -Dcobertura.skip=true -Drat.skip=true -Dlicense.skip=true -Dfindbugs.skip=true -Dgpg.skip=true -Dskip.npm=true -Dskip.gulp=true -Dskip.bower=true -Drat.numUnapprovedLicenses=100 -DskipTests=true -DskipITs=true -Dtest=None -DfailIfNoTests=false", + "testCommand": "mvn test -V -B -Denforcer.skip=true -Dcheckstyle.skip=true -Dcobertura.skip=true -Drat.skip=true -Dlicense.skip=true -Dfindbugs.skip=true -Dgpg.skip=true -Dskip.npm=true -Dskip.gulp=true -Dskip.bower=true -Drat.numUnapprovedLicenses=100", + "categories": [ + "safety", + "npe" + ], + "npe": { + "filepath": "src/main/java/org/apache/commons/lang3/BooleanUtils.java", + "line": 1034, + "npe_method": "or", + "deref_field": "array", + "npe_class": "BooleanUtils", + "repo": "commons-lang", + "bug_id": "BooleanUtils_1008" + } +} diff --git a/Java/commons-lang-BooleanUtils_1008/npe.json b/Java/commons-lang-BooleanUtils_1008/npe.json new file mode 100644 index 000000000..b5c41b794 --- /dev/null +++ b/Java/commons-lang-BooleanUtils_1008/npe.json @@ -0,0 +1,7 @@ +{ + "filepath": "src/main/java/org/apache/commons/lang3/BooleanUtils.java", + "line": 1034, + "npe_method": "or", + "deref_field": "array", + "npe_class": "BooleanUtils" +} \ No newline at end of file diff --git a/Java/commons-lang-BooleanUtils_1041/Dockerfile b/Java/commons-lang-BooleanUtils_1041/Dockerfile new file mode 100644 index 000000000..7b7fbe349 --- /dev/null +++ b/Java/commons-lang-BooleanUtils_1041/Dockerfile @@ -0,0 +1,18 @@ +FROM ghcr.io/kupl/starlab-benchmarks/java-base:commons-lang + +ENV TZ=Asia/Seoul + +COPY ./metadata.json . +COPY ./npe.json . +COPY ./buggy.java /tmp/buggy.java +RUN export BUGGY_PATH=$(cat metadata.json | jq -r ".npe.filepath") \ + && export BUGGY_LINE=$(cat metadata.json | jq -r ".npe.line") \ + && export BUGGY_MTHD=$(cat metadata.json | jq -r ".npe.npe_method") \ + && mv /tmp/buggy.java $BUGGY_PATH \ + && echo "[{\"filepath\": \"$BUGGY_PATH\", \"line\": $BUGGY_LINE, \"method_name\": \"$BUGGY_MTHD\"}]" | jq . > traces.json + +RUN git init . && git add -A + +RUN $(cat metadata.json | jq -r ".buildCommand") + +RUN $(cat metadata.json | jq -r ".testCommand"); if [ $? -eq 0 ]; then exit 1; fi diff --git a/Java/commons-lang-BooleanUtils_1041/buggy.java b/Java/commons-lang-BooleanUtils_1041/buggy.java new file mode 100644 index 000000000..52aa1d9d6 --- /dev/null +++ b/Java/commons-lang-BooleanUtils_1041/buggy.java @@ -0,0 +1,1121 @@ +/* + * 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; + +import org.apache.commons.lang3.math.NumberUtils; + +/** + *Operations on boolean primitives and Boolean objects.
+ * + *This class tries to handle {@code null} input gracefully. + * An exception will not be thrown for a {@code null} input. + * Each method documents its behaviour in more detail.
+ * + *#ThreadSafe#
+ * @since 2.0 + */ +public class BooleanUtils { + + /** + *{@code BooleanUtils} instances should NOT be constructed in standard programming. + * Instead, the class should be used as {@code BooleanUtils.negate(true);}.
+ * + *This constructor is public to permit tools that require a JavaBean instance + * to operate.
+ */ + public BooleanUtils() { + super(); + } + + // Boolean utilities + //-------------------------------------------------------------------------- + /** + *Negates the specified boolean.
+ * + *If {@code null} is passed in, {@code null} will be returned.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.negate(Boolean.TRUE) = Boolean.FALSE; + * BooleanUtils.negate(Boolean.FALSE) = Boolean.TRUE; + * BooleanUtils.negate(null) = null; + *+ * + * @param bool the Boolean to negate, may be null + * @return the negated Boolean, or {@code null} if {@code null} input + */ + public static Boolean negate(final Boolean bool) { + if (bool == null) { + return null; + } + return bool.booleanValue() ? Boolean.FALSE : Boolean.TRUE; + } + + // boolean Boolean methods + //----------------------------------------------------------------------- + /** + *
Checks if a {@code Boolean} value is {@code true}, + * handling {@code null} by returning {@code false}.
+ * + *+ * BooleanUtils.isTrue(Boolean.TRUE) = true + * BooleanUtils.isTrue(Boolean.FALSE) = false + * BooleanUtils.isTrue(null) = false + *+ * + * @param bool the boolean to check, null returns {@code false} + * @return {@code true} only if the input is non-null and true + * @since 2.1 + */ + public static boolean isTrue(final Boolean bool) { + return Boolean.TRUE.equals(bool); + } + + /** + *
Checks if a {@code Boolean} value is not {@code true}, + * handling {@code null} by returning {@code true}.
+ * + *+ * BooleanUtils.isNotTrue(Boolean.TRUE) = false + * BooleanUtils.isNotTrue(Boolean.FALSE) = true + * BooleanUtils.isNotTrue(null) = true + *+ * + * @param bool the boolean to check, null returns {@code true} + * @return {@code true} if the input is null or false + * @since 2.3 + */ + public static boolean isNotTrue(final Boolean bool) { + return !isTrue(bool); + } + + /** + *
Checks if a {@code Boolean} value is {@code false}, + * handling {@code null} by returning {@code false}.
+ * + *+ * BooleanUtils.isFalse(Boolean.TRUE) = false + * BooleanUtils.isFalse(Boolean.FALSE) = true + * BooleanUtils.isFalse(null) = false + *+ * + * @param bool the boolean to check, null returns {@code false} + * @return {@code true} only if the input is non-null and false + * @since 2.1 + */ + public static boolean isFalse(final Boolean bool) { + return Boolean.FALSE.equals(bool); + } + + /** + *
Checks if a {@code Boolean} value is not {@code false}, + * handling {@code null} by returning {@code true}.
+ * + *+ * BooleanUtils.isNotFalse(Boolean.TRUE) = true + * BooleanUtils.isNotFalse(Boolean.FALSE) = false + * BooleanUtils.isNotFalse(null) = true + *+ * + * @param bool the boolean to check, null returns {@code true} + * @return {@code true} if the input is null or true + * @since 2.3 + */ + public static boolean isNotFalse(final Boolean bool) { + return !isFalse(bool); + } + + //----------------------------------------------------------------------- + /** + *
Converts a Boolean to a boolean handling {@code null} + * by returning {@code false}.
+ * + *+ * BooleanUtils.toBoolean(Boolean.TRUE) = true + * BooleanUtils.toBoolean(Boolean.FALSE) = false + * BooleanUtils.toBoolean(null) = false + *+ * + * @param bool the boolean to convert + * @return {@code true} or {@code false}, {@code null} returns {@code false} + */ + public static boolean toBoolean(final Boolean bool) { + return bool != null && bool.booleanValue(); + } + + /** + *
Converts a Boolean to a boolean handling {@code null}.
+ * + *+ * BooleanUtils.toBooleanDefaultIfNull(Boolean.TRUE, false) = true + * BooleanUtils.toBooleanDefaultIfNull(Boolean.FALSE, true) = false + * BooleanUtils.toBooleanDefaultIfNull(null, true) = true + *+ * + * @param bool the boolean to convert + * @param valueIfNull the boolean value to return if {@code null} + * @return {@code true} or {@code false} + */ + public static boolean toBooleanDefaultIfNull(final Boolean bool, final boolean valueIfNull) { + if (bool == null) { + return valueIfNull; + } + return bool.booleanValue(); + } + + // Integer to Boolean methods + //----------------------------------------------------------------------- + /** + *
Converts an int to a boolean using the convention that {@code zero} + * is {@code false}.
+ * + *+ * BooleanUtils.toBoolean(0) = false + * BooleanUtils.toBoolean(1) = true + * BooleanUtils.toBoolean(2) = true + *+ * + * @param value the int to convert + * @return {@code true} if non-zero, {@code false} + * if zero + */ + public static boolean toBoolean(final int value) { + return value != 0; + } + + /** + *
Converts an int to a Boolean using the convention that {@code zero} + * is {@code false}.
+ * + *+ * BooleanUtils.toBoolean(0) = Boolean.FALSE + * BooleanUtils.toBoolean(1) = Boolean.TRUE + * BooleanUtils.toBoolean(2) = Boolean.TRUE + *+ * + * @param value the int to convert + * @return Boolean.TRUE if non-zero, Boolean.FALSE if zero, + * {@code null} if {@code null} + */ + public static Boolean toBooleanObject(final int value) { + return value == 0 ? Boolean.FALSE : Boolean.TRUE; + } + + /** + *
Converts an Integer to a Boolean using the convention that {@code zero} + * is {@code false}.
+ * + *{@code null} will be converted to {@code null}.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.toBoolean(Integer.valueOf(0)) = Boolean.FALSE + * BooleanUtils.toBoolean(Integer.valueOf(1)) = Boolean.TRUE + * BooleanUtils.toBoolean(Integer.valueOf(null)) = null + *+ * + * @param value the Integer to convert + * @return Boolean.TRUE if non-zero, Boolean.FALSE if zero, + * {@code null} if {@code null} input + */ + public static Boolean toBooleanObject(final Integer value) { + if (value == null) { + return null; + } + return value.intValue() == 0 ? Boolean.FALSE : Boolean.TRUE; + } + + /** + *
Converts an int to a boolean specifying the conversion values.
+ * + *+ * BooleanUtils.toBoolean(0, 1, 0) = false + * BooleanUtils.toBoolean(1, 1, 0) = true + * BooleanUtils.toBoolean(2, 1, 2) = false + * BooleanUtils.toBoolean(2, 2, 0) = true + *+ * + * @param value the Integer to convert + * @param trueValue the value to match for {@code true} + * @param falseValue the value to match for {@code false} + * @return {@code true} or {@code false} + * @throws IllegalArgumentException if no match + */ + public static boolean toBoolean(final int value, final int trueValue, final int falseValue) { + if (value == trueValue) { + return true; + } + if (value == falseValue) { + return false; + } + // no match + throw new IllegalArgumentException("The Integer did not match either specified value"); + } + + /** + *
Converts an Integer to a boolean specifying the conversion values.
+ * + *+ * BooleanUtils.toBoolean(Integer.valueOf(0), Integer.valueOf(1), Integer.valueOf(0)) = false + * BooleanUtils.toBoolean(Integer.valueOf(1), Integer.valueOf(1), Integer.valueOf(0)) = true + * BooleanUtils.toBoolean(Integer.valueOf(2), Integer.valueOf(1), Integer.valueOf(2)) = false + * BooleanUtils.toBoolean(Integer.valueOf(2), Integer.valueOf(2), Integer.valueOf(0)) = true + * BooleanUtils.toBoolean(null, null, Integer.valueOf(0)) = true + *+ * + * @param value the Integer to convert + * @param trueValue the value to match for {@code true}, may be {@code null} + * @param falseValue the value to match for {@code false}, may be {@code null} + * @return {@code true} or {@code false} + * @throws IllegalArgumentException if no match + */ + public static boolean toBoolean(final Integer value, final Integer trueValue, final Integer falseValue) { + if (value == null) { + if (trueValue == null) { + return true; + } + if (falseValue == null) { + return false; + } + } else if (value.equals(trueValue)) { + return true; + } else if (value.equals(falseValue)) { + return false; + } + // no match + throw new IllegalArgumentException("The Integer did not match either specified value"); + } + + /** + *
Converts an int to a Boolean specifying the conversion values.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.toBooleanObject(0, 0, 2, 3) = Boolean.TRUE + * BooleanUtils.toBooleanObject(2, 1, 2, 3) = Boolean.FALSE + * BooleanUtils.toBooleanObject(3, 1, 2, 3) = null + *+ * + * @param value the Integer to convert + * @param trueValue the value to match for {@code true} + * @param falseValue the value to match for {@code false} + * @param nullValue the value to to match for {@code null} + * @return Boolean.TRUE, Boolean.FALSE, or {@code null} + * @throws IllegalArgumentException if no match + */ + public static Boolean toBooleanObject(final int value, final int trueValue, final int falseValue, final int nullValue) { + if (value == trueValue) { + return Boolean.TRUE; + } + if (value == falseValue) { + return Boolean.FALSE; + } + if (value == nullValue) { + return null; + } + // no match + throw new IllegalArgumentException("The Integer did not match any specified value"); + } + + /** + *
Converts an Integer to a Boolean specifying the conversion values.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.toBooleanObject(Integer.valueOf(0), Integer.valueOf(0), Integer.valueOf(2), Integer.valueOf(3)) = Boolean.TRUE + * BooleanUtils.toBooleanObject(Integer.valueOf(2), Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(3)) = Boolean.FALSE + * BooleanUtils.toBooleanObject(Integer.valueOf(3), Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(3)) = null + *+ * + * @param value the Integer to convert + * @param trueValue the value to match for {@code true}, may be {@code null} + * @param falseValue the value to match for {@code false}, may be {@code null} + * @param nullValue the value to to match for {@code null}, may be {@code null} + * @return Boolean.TRUE, Boolean.FALSE, or {@code null} + * @throws IllegalArgumentException if no match + */ + public static Boolean toBooleanObject(final Integer value, final Integer trueValue, final Integer falseValue, final Integer nullValue) { + if (value == null) { + if (trueValue == null) { + return Boolean.TRUE; + } + if (falseValue == null) { + return Boolean.FALSE; + } + if (nullValue == null) { + return null; + } + } else if (value.equals(trueValue)) { + return Boolean.TRUE; + } else if (value.equals(falseValue)) { + return Boolean.FALSE; + } else if (value.equals(nullValue)) { + return null; + } + // no match + throw new IllegalArgumentException("The Integer did not match any specified value"); + } + + // Boolean to Integer methods + //----------------------------------------------------------------------- + /** + *
Converts a boolean to an int using the convention that + * {@code zero} is {@code false}.
+ * + *+ * BooleanUtils.toInteger(true) = 1 + * BooleanUtils.toInteger(false) = 0 + *+ * + * @param bool the boolean to convert + * @return one if {@code true}, zero if {@code false} + */ + public static int toInteger(final boolean bool) { + return bool ? 1 : 0; + } + + /** + *
Converts a boolean to an Integer using the convention that + * {@code zero} is {@code false}.
+ * + *+ * BooleanUtils.toIntegerObject(true) = Integer.valueOf(1) + * BooleanUtils.toIntegerObject(false) = Integer.valueOf(0) + *+ * + * @param bool the boolean to convert + * @return one if {@code true}, zero if {@code false} + */ + public static Integer toIntegerObject(final boolean bool) { + return bool ? NumberUtils.INTEGER_ONE : NumberUtils.INTEGER_ZERO; + } + + /** + *
Converts a Boolean to a Integer using the convention that + * {@code zero} is {@code false}.
+ * + *{@code null} will be converted to {@code null}.
+ * + *+ * BooleanUtils.toIntegerObject(Boolean.TRUE) = Integer.valueOf(1) + * BooleanUtils.toIntegerObject(Boolean.FALSE) = Integer.valueOf(0) + *+ * + * @param bool the Boolean to convert + * @return one if Boolean.TRUE, zero if Boolean.FALSE, {@code null} if {@code null} + */ + public static Integer toIntegerObject(final Boolean bool) { + if (bool == null) { + return null; + } + return bool.booleanValue() ? NumberUtils.INTEGER_ONE : NumberUtils.INTEGER_ZERO; + } + + /** + *
Converts a boolean to an int specifying the conversion values.
+ * + *+ * BooleanUtils.toInteger(true, 1, 0) = 1 + * BooleanUtils.toInteger(false, 1, 0) = 0 + *+ * + * @param bool the to convert + * @param trueValue the value to return if {@code true} + * @param falseValue the value to return if {@code false} + * @return the appropriate value + */ + public static int toInteger(final boolean bool, final int trueValue, final int falseValue) { + return bool ? trueValue : falseValue; + } + + /** + *
Converts a Boolean to an int specifying the conversion values.
+ * + *+ * BooleanUtils.toInteger(Boolean.TRUE, 1, 0, 2) = 1 + * BooleanUtils.toInteger(Boolean.FALSE, 1, 0, 2) = 0 + * BooleanUtils.toInteger(null, 1, 0, 2) = 2 + *+ * + * @param bool the Boolean to convert + * @param trueValue the value to return if {@code true} + * @param falseValue the value to return if {@code false} + * @param nullValue the value to return if {@code null} + * @return the appropriate value + */ + public static int toInteger(final Boolean bool, final int trueValue, final int falseValue, final int nullValue) { + if (bool == null) { + return nullValue; + } + return bool.booleanValue() ? trueValue : falseValue; + } + + /** + *
Converts a boolean to an Integer specifying the conversion values.
+ * + *+ * BooleanUtils.toIntegerObject(true, Integer.valueOf(1), Integer.valueOf(0)) = Integer.valueOf(1) + * BooleanUtils.toIntegerObject(false, Integer.valueOf(1), Integer.valueOf(0)) = Integer.valueOf(0) + *+ * + * @param bool the to convert + * @param trueValue the value to return if {@code true}, may be {@code null} + * @param falseValue the value to return if {@code false}, may be {@code null} + * @return the appropriate value + */ + public static Integer toIntegerObject(final boolean bool, final Integer trueValue, final Integer falseValue) { + return bool ? trueValue : falseValue; + } + + /** + *
Converts a Boolean to an Integer specifying the conversion values.
+ * + *+ * BooleanUtils.toIntegerObject(Boolean.TRUE, Integer.valueOf(1), Integer.valueOf(0), Integer.valueOf(2)) = Integer.valueOf(1) + * BooleanUtils.toIntegerObject(Boolean.FALSE, Integer.valueOf(1), Integer.valueOf(0), Integer.valueOf(2)) = Integer.valueOf(0) + * BooleanUtils.toIntegerObject(null, Integer.valueOf(1), Integer.valueOf(0), Integer.valueOf(2)) = Integer.valueOf(2) + *+ * + * @param bool the Boolean to convert + * @param trueValue the value to return if {@code true}, may be {@code null} + * @param falseValue the value to return if {@code false}, may be {@code null} + * @param nullValue the value to return if {@code null}, may be {@code null} + * @return the appropriate value + */ + public static Integer toIntegerObject(final Boolean bool, final Integer trueValue, final Integer falseValue, final Integer nullValue) { + if (bool == null) { + return nullValue; + } + return bool.booleanValue() ? trueValue : falseValue; + } + + // String to Boolean methods + //----------------------------------------------------------------------- + /** + *
Converts a String to a Boolean.
+ * + *{@code 'true'}, {@code 'on'}, {@code 'y'}, {@code 't'} or {@code 'yes'} + * (case insensitive) will return {@code true}. + * {@code 'false'}, {@code 'off'}, {@code 'n'}, {@code 'f'} or {@code 'no'} + * (case insensitive) will return {@code false}. + * Otherwise, {@code null} is returned.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * // N.B. case is not significant + * BooleanUtils.toBooleanObject(null) = null + * BooleanUtils.toBooleanObject("true") = Boolean.TRUE + * BooleanUtils.toBooleanObject("T") = Boolean.TRUE // i.e. T[RUE] + * BooleanUtils.toBooleanObject("false") = Boolean.FALSE + * BooleanUtils.toBooleanObject("f") = Boolean.FALSE // i.e. f[alse] + * BooleanUtils.toBooleanObject("No") = Boolean.FALSE + * BooleanUtils.toBooleanObject("n") = Boolean.FALSE // i.e. n[o] + * BooleanUtils.toBooleanObject("on") = Boolean.TRUE + * BooleanUtils.toBooleanObject("ON") = Boolean.TRUE + * BooleanUtils.toBooleanObject("off") = Boolean.FALSE + * BooleanUtils.toBooleanObject("oFf") = Boolean.FALSE + * BooleanUtils.toBooleanObject("yes") = Boolean.TRUE + * BooleanUtils.toBooleanObject("Y") = Boolean.TRUE // i.e. Y[ES] + * BooleanUtils.toBooleanObject("blue") = null + * BooleanUtils.toBooleanObject("true ") = null // trailing space (too long) + * BooleanUtils.toBooleanObject("ono") = null // does not match on or no + *+ * + * @param str the String to check; upper and lower case are treated as the same + * @return the Boolean value of the string, {@code null} if no match or {@code null} input + */ + public static Boolean toBooleanObject(final String str) { + // Previously used equalsIgnoreCase, which was fast for interned 'true'. + // Non interned 'true' matched 15 times slower. + // + // Optimisation provides same performance as before for interned 'true'. + // Similar performance for null, 'false', and other strings not length 2/3/4. + // 'true'/'TRUE' match 4 times slower, 'tRUE'/'True' 7 times slower. + if (str == "true") { + return Boolean.TRUE; + } + if (str == null) { + return null; + } + switch (str.length()) { + case 1: { + final char ch0 = str.charAt(0); + if (ch0 == 'y' || ch0 == 'Y' || + ch0 == 't' || ch0 == 'T') { + return Boolean.TRUE; + } + if (ch0 == 'n' || ch0 == 'N' || + ch0 == 'f' || ch0 == 'F') { + return Boolean.FALSE; + } + break; + } + case 2: { + final char ch0 = str.charAt(0); + final char ch1 = str.charAt(1); + if ((ch0 == 'o' || ch0 == 'O') && + (ch1 == 'n' || ch1 == 'N') ) { + return Boolean.TRUE; + } + if ((ch0 == 'n' || ch0 == 'N') && + (ch1 == 'o' || ch1 == 'O') ) { + return Boolean.FALSE; + } + break; + } + case 3: { + final char ch0 = str.charAt(0); + final char ch1 = str.charAt(1); + final char ch2 = str.charAt(2); + if ((ch0 == 'y' || ch0 == 'Y') && + (ch1 == 'e' || ch1 == 'E') && + (ch2 == 's' || ch2 == 'S') ) { + return Boolean.TRUE; + } + if ((ch0 == 'o' || ch0 == 'O') && + (ch1 == 'f' || ch1 == 'F') && + (ch2 == 'f' || ch2 == 'F') ) { + return Boolean.FALSE; + } + break; + } + case 4: { + final char ch0 = str.charAt(0); + final char ch1 = str.charAt(1); + final char ch2 = str.charAt(2); + final char ch3 = str.charAt(3); + if ((ch0 == 't' || ch0 == 'T') && + (ch1 == 'r' || ch1 == 'R') && + (ch2 == 'u' || ch2 == 'U') && + (ch3 == 'e' || ch3 == 'E') ) { + return Boolean.TRUE; + } + break; + } + case 5: { + final char ch0 = str.charAt(0); + final char ch1 = str.charAt(1); + final char ch2 = str.charAt(2); + final char ch3 = str.charAt(3); + final char ch4 = str.charAt(4); + if ((ch0 == 'f' || ch0 == 'F') && + (ch1 == 'a' || ch1 == 'A') && + (ch2 == 'l' || ch2 == 'L') && + (ch3 == 's' || ch3 == 'S') && + (ch4 == 'e' || ch4 == 'E') ) { + return Boolean.FALSE; + } + break; + } + default: + break; + } + + return null; + } + + /** + *
Converts a String to a Boolean throwing an exception if no match.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.toBooleanObject("true", "true", "false", "null") = Boolean.TRUE + * BooleanUtils.toBooleanObject("false", "true", "false", "null") = Boolean.FALSE + * BooleanUtils.toBooleanObject("null", "true", "false", "null") = null + *+ * + * @param str the String to check + * @param trueString the String to match for {@code true} (case sensitive), may be {@code null} + * @param falseString the String to match for {@code false} (case sensitive), may be {@code null} + * @param nullString the String to match for {@code null} (case sensitive), may be {@code null} + * @return the Boolean value of the string, {@code null} if either the String matches {@code nullString} + * or if {@code null} input and {@code nullString} is {@code null} + * @throws IllegalArgumentException if the String doesn't match + */ + public static Boolean toBooleanObject(final String str, final String trueString, final String falseString, final String nullString) { + if (str == null) { + if (trueString == null) { + return Boolean.TRUE; + } + if (falseString == null) { + return Boolean.FALSE; + } + if (nullString == null) { + return null; + } + } else if (str.equals(trueString)) { + return Boolean.TRUE; + } else if (str.equals(falseString)) { + return Boolean.FALSE; + } else if (str.equals(nullString)) { + return null; + } + // no match + throw new IllegalArgumentException("The String did not match any specified value"); + } + + // String to boolean methods + //----------------------------------------------------------------------- + /** + *
Converts a String to a boolean (optimised for performance).
+ * + *{@code 'true'}, {@code 'on'}, {@code 'y'}, {@code 't'} or {@code 'yes'} + * (case insensitive) will return {@code true}. Otherwise, + * {@code false} is returned.
+ * + *This method performs 4 times faster (JDK1.4) than + * {@code Boolean.valueOf(String)}. However, this method accepts + * 'on' and 'yes', 't', 'y' as true values. + * + *
+ * BooleanUtils.toBoolean(null) = false + * BooleanUtils.toBoolean("true") = true + * BooleanUtils.toBoolean("TRUE") = true + * BooleanUtils.toBoolean("tRUe") = true + * BooleanUtils.toBoolean("on") = true + * BooleanUtils.toBoolean("yes") = true + * BooleanUtils.toBoolean("false") = false + * BooleanUtils.toBoolean("x gti") = false + * BooleanUtils.toBooleanObject("y") = true + * BooleanUtils.toBooleanObject("n") = false + * BooleanUtils.toBooleanObject("t") = true + * BooleanUtils.toBooleanObject("f") = false + *+ * + * @param str the String to check + * @return the boolean value of the string, {@code false} if no match or the String is null + */ + public static boolean toBoolean(final String str) { + return toBooleanObject(str) == Boolean.TRUE; + } + + /** + *
Converts a String to a Boolean throwing an exception if no match found.
+ * + *+ * BooleanUtils.toBoolean("true", "true", "false") = true + * BooleanUtils.toBoolean("false", "true", "false") = false + *+ * + * @param str the String to check + * @param trueString the String to match for {@code true} (case sensitive), may be {@code null} + * @param falseString the String to match for {@code false} (case sensitive), may be {@code null} + * @return the boolean value of the string + * @throws IllegalArgumentException if the String doesn't match + */ + public static boolean toBoolean(final String str, final String trueString, final String falseString) { + if (str == trueString) { + return true; + } else if (str == falseString) { + return false; + } else if (str != null) { + if (str.equals(trueString)) { + return true; + } else if (str.equals(falseString)) { + return false; + } + } + // no match + throw new IllegalArgumentException("The String did not match either specified value"); + } + + // Boolean to String methods + //----------------------------------------------------------------------- + /** + *
Converts a Boolean to a String returning {@code 'true'}, + * {@code 'false'}, or {@code null}.
+ * + *+ * BooleanUtils.toStringTrueFalse(Boolean.TRUE) = "true" + * BooleanUtils.toStringTrueFalse(Boolean.FALSE) = "false" + * BooleanUtils.toStringTrueFalse(null) = null; + *+ * + * @param bool the Boolean to check + * @return {@code 'true'}, {@code 'false'}, or {@code null} + */ + public static String toStringTrueFalse(final Boolean bool) { + return toString(bool, "true", "false", null); + } + + /** + *
Converts a Boolean to a String returning {@code 'on'}, + * {@code 'off'}, or {@code null}.
+ * + *+ * BooleanUtils.toStringOnOff(Boolean.TRUE) = "on" + * BooleanUtils.toStringOnOff(Boolean.FALSE) = "off" + * BooleanUtils.toStringOnOff(null) = null; + *+ * + * @param bool the Boolean to check + * @return {@code 'on'}, {@code 'off'}, or {@code null} + */ + public static String toStringOnOff(final Boolean bool) { + return toString(bool, "on", "off", null); + } + + /** + *
Converts a Boolean to a String returning {@code 'yes'}, + * {@code 'no'}, or {@code null}.
+ * + *+ * BooleanUtils.toStringYesNo(Boolean.TRUE) = "yes" + * BooleanUtils.toStringYesNo(Boolean.FALSE) = "no" + * BooleanUtils.toStringYesNo(null) = null; + *+ * + * @param bool the Boolean to check + * @return {@code 'yes'}, {@code 'no'}, or {@code null} + */ + public static String toStringYesNo(final Boolean bool) { + return toString(bool, "yes", "no", null); + } + + /** + *
Converts a Boolean to a String returning one of the input Strings.
+ * + *+ * BooleanUtils.toString(Boolean.TRUE, "true", "false", null) = "true" + * BooleanUtils.toString(Boolean.FALSE, "true", "false", null) = "false" + * BooleanUtils.toString(null, "true", "false", null) = null; + *+ * + * @param bool the Boolean to check + * @param trueString the String to return if {@code true}, may be {@code null} + * @param falseString the String to return if {@code false}, may be {@code null} + * @param nullString the String to return if {@code null}, may be {@code null} + * @return one of the three input Strings + */ + public static String toString(final Boolean bool, final String trueString, final String falseString, final String nullString) { + if (bool == null) { + return nullString; + } + return bool.booleanValue() ? trueString : falseString; + } + + // boolean to String methods + //----------------------------------------------------------------------- + /** + *
Converts a boolean to a String returning {@code 'true'} + * or {@code 'false'}.
+ * + *+ * BooleanUtils.toStringTrueFalse(true) = "true" + * BooleanUtils.toStringTrueFalse(false) = "false" + *+ * + * @param bool the Boolean to check + * @return {@code 'true'}, {@code 'false'}, or {@code null} + */ + public static String toStringTrueFalse(final boolean bool) { + return toString(bool, "true", "false"); + } + + /** + *
Converts a boolean to a String returning {@code 'on'} + * or {@code 'off'}.
+ * + *+ * BooleanUtils.toStringOnOff(true) = "on" + * BooleanUtils.toStringOnOff(false) = "off" + *+ * + * @param bool the Boolean to check + * @return {@code 'on'}, {@code 'off'}, or {@code null} + */ + public static String toStringOnOff(final boolean bool) { + return toString(bool, "on", "off"); + } + + /** + *
Converts a boolean to a String returning {@code 'yes'} + * or {@code 'no'}.
+ * + *+ * BooleanUtils.toStringYesNo(true) = "yes" + * BooleanUtils.toStringYesNo(false) = "no" + *+ * + * @param bool the Boolean to check + * @return {@code 'yes'}, {@code 'no'}, or {@code null} + */ + public static String toStringYesNo(final boolean bool) { + return toString(bool, "yes", "no"); + } + + /** + *
Converts a boolean to a String returning one of the input Strings.
+ * + *+ * BooleanUtils.toString(true, "true", "false") = "true" + * BooleanUtils.toString(false, "true", "false") = "false" + *+ * + * @param bool the Boolean to check + * @param trueString the String to return if {@code true}, may be {@code null} + * @param falseString the String to return if {@code false}, may be {@code null} + * @return one of the two input Strings + */ + public static String toString(final boolean bool, final String trueString, final String falseString) { + return bool ? trueString : falseString; + } + + // logical operations + // ---------------------------------------------------------------------- + /** + *
Performs an and on a set of booleans.
+ * + *+ * BooleanUtils.and(true, true) = true + * BooleanUtils.and(false, false) = false + * BooleanUtils.and(true, false) = false + * BooleanUtils.and(true, true, false) = false + * BooleanUtils.and(true, true, true) = true + *+ * + * @param array an array of {@code boolean}s + * @return {@code true} if the and is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + * @since 3.0.1 + */ + public static boolean and(final boolean... array) { + // Validates input + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + for (final boolean element : array) { + if (!element) { + return false; + } + } + return true; + } + + /** + *
Performs an and on an array of Booleans.
+ * + *+ * BooleanUtils.and(Boolean.TRUE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.and(Boolean.FALSE, Boolean.FALSE) = Boolean.FALSE + * BooleanUtils.and(Boolean.TRUE, Boolean.FALSE) = Boolean.FALSE + * BooleanUtils.and(Boolean.TRUE, Boolean.TRUE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.and(Boolean.FALSE, Boolean.FALSE, Boolean.TRUE) = Boolean.FALSE + * BooleanUtils.and(Boolean.TRUE, Boolean.FALSE, Boolean.TRUE) = Boolean.FALSE + *+ * + * @param array an array of {@code Boolean}s + * @return {@code true} if the and is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + * @throws IllegalArgumentException if {@code array} contains a {@code null} + * @since 3.0.1 + */ + public static Boolean and(final Boolean... array) { + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + try { + final boolean[] primitive = ArrayUtils.toPrimitive(array); + return and(primitive) ? Boolean.TRUE : Boolean.FALSE; + } catch (final NullPointerException ex) { + throw new IllegalArgumentException("The array must not contain any null elements"); + } + } + + /** + *
Performs an or on a set of booleans.
+ * + *+ * BooleanUtils.or(true, true) = true + * BooleanUtils.or(false, false) = false + * BooleanUtils.or(true, false) = true + * BooleanUtils.or(true, true, false) = true + * BooleanUtils.or(true, true, true) = true + * BooleanUtils.or(false, false, false) = false + *+ * + * @param array an array of {@code boolean}s + * @return {@code true} if the or is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + * @since 3.0.1 + */ + public static boolean or(final boolean... array) { + // Validates input + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + for (final boolean element : array) { + if (element) { + return true; + } + } + return false; + } + + /** + *
Performs an or on an array of Booleans.
+ * + *+ * BooleanUtils.or(Boolean.TRUE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.or(Boolean.FALSE, Boolean.FALSE) = Boolean.FALSE + * BooleanUtils.or(Boolean.TRUE, Boolean.FALSE) = Boolean.TRUE + * BooleanUtils.or(Boolean.TRUE, Boolean.TRUE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.or(Boolean.FALSE, Boolean.FALSE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.or(Boolean.TRUE, Boolean.FALSE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.or(Boolean.FALSE, Boolean.FALSE, Boolean.FALSE) = Boolean.FALSE + *+ * + * @param array an array of {@code Boolean}s + * @return {@code true} if the or is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + * @throws IllegalArgumentException if {@code array} contains a {@code null} + * @since 3.0.1 + */ + public static Boolean or(final Boolean... array) { + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + try { + final boolean[] primitive = ArrayUtils.toPrimitive(array); + return or(primitive) ? Boolean.TRUE : Boolean.FALSE; + } catch (final NullPointerException ex) { + throw new IllegalArgumentException("The array must not contain any null elements"); + } + } + + /** + *
Performs an xor on a set of booleans.
+ * + *+ * BooleanUtils.xor(true, true) = false + * BooleanUtils.xor(false, false) = false + * BooleanUtils.xor(true, false) = true + * BooleanUtils.xor(true, true) = false + * BooleanUtils.xor(false, false) = false + * BooleanUtils.xor(true, false) = true + *+ * + * @param array an array of {@code boolean}s + * @return {@code true} if the xor is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + */ +/** + *
Performs an xor on a set of booleans.
+ * + *+ * BooleanUtils.xor(true, true) = false + * BooleanUtils.xor(false, false) = false + * BooleanUtils.xor(true, false) = true + * BooleanUtils.xor(true, true) = false + * BooleanUtils.xor(false, false) = false + * BooleanUtils.xor(true, false) = true + *+ * + * @param array + * an array of {@code boolean}s + * @return {@code true} if the xor is successful. + * @throws IllegalArgumentException + * if {@code array} is {@code null} + * @throws IllegalArgumentException + * if {@code array} is empty. + */ +public static boolean xor(final boolean... array) { + { + if (/* NPEX_NULL_EXP */ + array.length == 0) { + throw new java.lang.IllegalArgumentException("Array is empty"); + } + // false if the neutral element of the xor operator + boolean result = false; + for (final boolean element : array) { + result ^= element; + } + return result; + } +} + + /** + *
Performs an xor on an array of Booleans.
+ * + *+ * BooleanUtils.xor(new Boolean[] { Boolean.TRUE, Boolean.TRUE }) = Boolean.FALSE + * BooleanUtils.xor(new Boolean[] { Boolean.FALSE, Boolean.FALSE }) = Boolean.FALSE + * BooleanUtils.xor(new Boolean[] { Boolean.TRUE, Boolean.FALSE }) = Boolean.TRUE + *+ * + * @param array an array of {@code Boolean}s + * @return {@code true} if the xor is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + * @throws IllegalArgumentException if {@code array} contains a {@code null} + */ + public static Boolean xor(final Boolean... array) { + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + try { + final boolean[] primitive = ArrayUtils.toPrimitive(array); + return xor(primitive) ? Boolean.TRUE : Boolean.FALSE; + } catch (final NullPointerException ex) { + throw new IllegalArgumentException("The array must not contain any null elements"); + } + } + + /** + *
Compares two {@code boolean} values. This is the same functionality as provided in Java 7.
+ * + * @param x the first {@code boolean} to compare + * @param y the second {@code boolean} to compare + * @return the value {@code 0} if {@code x == y}; + * a value less than {@code 0} if {@code !x && y}; and + * a value greater than {@code 0} if {@code x && !y} + * @since 3.4 + */ + public static int compare(boolean x, boolean y) { + if (x == y) { + return 0; + } + return x ? 1 : -1; + } + +} diff --git a/Java/commons-lang-BooleanUtils_1041/metadata.json b/Java/commons-lang-BooleanUtils_1041/metadata.json new file mode 100644 index 000000000..ab66a1a15 --- /dev/null +++ b/Java/commons-lang-BooleanUtils_1041/metadata.json @@ -0,0 +1,21 @@ +{ + "language": "java", + "id": "commons-lang-BooleanUtils_1041", + "buggyPath": ".", + "referencePath": null, + "buildCommand": "mvn package -V -B -Denforcer.skip=true -Dcheckstyle.skip=true -Dcobertura.skip=true -Drat.skip=true -Dlicense.skip=true -Dfindbugs.skip=true -Dgpg.skip=true -Dskip.npm=true -Dskip.gulp=true -Dskip.bower=true -Drat.numUnapprovedLicenses=100 -DskipTests=true -DskipITs=true -Dtest=None -DfailIfNoTests=false", + "testCommand": "mvn test -V -B -Denforcer.skip=true -Dcheckstyle.skip=true -Dcobertura.skip=true -Drat.skip=true -Dlicense.skip=true -Dfindbugs.skip=true -Dgpg.skip=true -Dskip.npm=true -Dskip.gulp=true -Dskip.bower=true -Drat.numUnapprovedLicenses=100", + "categories": [ + "safety", + "npe" + ], + "npe": { + "filepath": "src/main/java/org/apache/commons/lang3/BooleanUtils.java", + "line": 1062, + "npe_method": "xor", + "deref_field": "array", + "npe_class": "BooleanUtils", + "repo": "commons-lang", + "bug_id": "BooleanUtils_1041" + } +} diff --git a/Java/commons-lang-BooleanUtils_1041/npe.json b/Java/commons-lang-BooleanUtils_1041/npe.json new file mode 100644 index 000000000..85905dfc4 --- /dev/null +++ b/Java/commons-lang-BooleanUtils_1041/npe.json @@ -0,0 +1,7 @@ +{ + "filepath": "src/main/java/org/apache/commons/lang3/BooleanUtils.java", + "line": 1062, + "npe_method": "xor", + "deref_field": "array", + "npe_class": "BooleanUtils" +} \ No newline at end of file diff --git a/Java/commons-lang-BooleanUtils_1073/Dockerfile b/Java/commons-lang-BooleanUtils_1073/Dockerfile new file mode 100644 index 000000000..7b7fbe349 --- /dev/null +++ b/Java/commons-lang-BooleanUtils_1073/Dockerfile @@ -0,0 +1,18 @@ +FROM ghcr.io/kupl/starlab-benchmarks/java-base:commons-lang + +ENV TZ=Asia/Seoul + +COPY ./metadata.json . +COPY ./npe.json . +COPY ./buggy.java /tmp/buggy.java +RUN export BUGGY_PATH=$(cat metadata.json | jq -r ".npe.filepath") \ + && export BUGGY_LINE=$(cat metadata.json | jq -r ".npe.line") \ + && export BUGGY_MTHD=$(cat metadata.json | jq -r ".npe.npe_method") \ + && mv /tmp/buggy.java $BUGGY_PATH \ + && echo "[{\"filepath\": \"$BUGGY_PATH\", \"line\": $BUGGY_LINE, \"method_name\": \"$BUGGY_MTHD\"}]" | jq . > traces.json + +RUN git init . && git add -A + +RUN $(cat metadata.json | jq -r ".buildCommand") + +RUN $(cat metadata.json | jq -r ".testCommand"); if [ $? -eq 0 ]; then exit 1; fi diff --git a/Java/commons-lang-BooleanUtils_1073/buggy.java b/Java/commons-lang-BooleanUtils_1073/buggy.java new file mode 100644 index 000000000..5caa7a13e --- /dev/null +++ b/Java/commons-lang-BooleanUtils_1073/buggy.java @@ -0,0 +1,1123 @@ +/* + * 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; + +import org.apache.commons.lang3.math.NumberUtils; + +/** + *Operations on boolean primitives and Boolean objects.
+ * + *This class tries to handle {@code null} input gracefully. + * An exception will not be thrown for a {@code null} input. + * Each method documents its behaviour in more detail.
+ * + *#ThreadSafe#
+ * @since 2.0 + */ +public class BooleanUtils { + + /** + *{@code BooleanUtils} instances should NOT be constructed in standard programming. + * Instead, the class should be used as {@code BooleanUtils.negate(true);}.
+ * + *This constructor is public to permit tools that require a JavaBean instance + * to operate.
+ */ + public BooleanUtils() { + super(); + } + + // Boolean utilities + //-------------------------------------------------------------------------- + /** + *Negates the specified boolean.
+ * + *If {@code null} is passed in, {@code null} will be returned.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.negate(Boolean.TRUE) = Boolean.FALSE; + * BooleanUtils.negate(Boolean.FALSE) = Boolean.TRUE; + * BooleanUtils.negate(null) = null; + *+ * + * @param bool the Boolean to negate, may be null + * @return the negated Boolean, or {@code null} if {@code null} input + */ + public static Boolean negate(final Boolean bool) { + if (bool == null) { + return null; + } + return bool.booleanValue() ? Boolean.FALSE : Boolean.TRUE; + } + + // boolean Boolean methods + //----------------------------------------------------------------------- + /** + *
Checks if a {@code Boolean} value is {@code true}, + * handling {@code null} by returning {@code false}.
+ * + *+ * BooleanUtils.isTrue(Boolean.TRUE) = true + * BooleanUtils.isTrue(Boolean.FALSE) = false + * BooleanUtils.isTrue(null) = false + *+ * + * @param bool the boolean to check, null returns {@code false} + * @return {@code true} only if the input is non-null and true + * @since 2.1 + */ + public static boolean isTrue(final Boolean bool) { + return Boolean.TRUE.equals(bool); + } + + /** + *
Checks if a {@code Boolean} value is not {@code true}, + * handling {@code null} by returning {@code true}.
+ * + *+ * BooleanUtils.isNotTrue(Boolean.TRUE) = false + * BooleanUtils.isNotTrue(Boolean.FALSE) = true + * BooleanUtils.isNotTrue(null) = true + *+ * + * @param bool the boolean to check, null returns {@code true} + * @return {@code true} if the input is null or false + * @since 2.3 + */ + public static boolean isNotTrue(final Boolean bool) { + return !isTrue(bool); + } + + /** + *
Checks if a {@code Boolean} value is {@code false}, + * handling {@code null} by returning {@code false}.
+ * + *+ * BooleanUtils.isFalse(Boolean.TRUE) = false + * BooleanUtils.isFalse(Boolean.FALSE) = true + * BooleanUtils.isFalse(null) = false + *+ * + * @param bool the boolean to check, null returns {@code false} + * @return {@code true} only if the input is non-null and false + * @since 2.1 + */ + public static boolean isFalse(final Boolean bool) { + return Boolean.FALSE.equals(bool); + } + + /** + *
Checks if a {@code Boolean} value is not {@code false}, + * handling {@code null} by returning {@code true}.
+ * + *+ * BooleanUtils.isNotFalse(Boolean.TRUE) = true + * BooleanUtils.isNotFalse(Boolean.FALSE) = false + * BooleanUtils.isNotFalse(null) = true + *+ * + * @param bool the boolean to check, null returns {@code true} + * @return {@code true} if the input is null or true + * @since 2.3 + */ + public static boolean isNotFalse(final Boolean bool) { + return !isFalse(bool); + } + + //----------------------------------------------------------------------- + /** + *
Converts a Boolean to a boolean handling {@code null} + * by returning {@code false}.
+ * + *+ * BooleanUtils.toBoolean(Boolean.TRUE) = true + * BooleanUtils.toBoolean(Boolean.FALSE) = false + * BooleanUtils.toBoolean(null) = false + *+ * + * @param bool the boolean to convert + * @return {@code true} or {@code false}, {@code null} returns {@code false} + */ + public static boolean toBoolean(final Boolean bool) { + return bool != null && bool.booleanValue(); + } + + /** + *
Converts a Boolean to a boolean handling {@code null}.
+ * + *+ * BooleanUtils.toBooleanDefaultIfNull(Boolean.TRUE, false) = true + * BooleanUtils.toBooleanDefaultIfNull(Boolean.FALSE, true) = false + * BooleanUtils.toBooleanDefaultIfNull(null, true) = true + *+ * + * @param bool the boolean to convert + * @param valueIfNull the boolean value to return if {@code null} + * @return {@code true} or {@code false} + */ + public static boolean toBooleanDefaultIfNull(final Boolean bool, final boolean valueIfNull) { + if (bool == null) { + return valueIfNull; + } + return bool.booleanValue(); + } + + // Integer to Boolean methods + //----------------------------------------------------------------------- + /** + *
Converts an int to a boolean using the convention that {@code zero} + * is {@code false}.
+ * + *+ * BooleanUtils.toBoolean(0) = false + * BooleanUtils.toBoolean(1) = true + * BooleanUtils.toBoolean(2) = true + *+ * + * @param value the int to convert + * @return {@code true} if non-zero, {@code false} + * if zero + */ + public static boolean toBoolean(final int value) { + return value != 0; + } + + /** + *
Converts an int to a Boolean using the convention that {@code zero} + * is {@code false}.
+ * + *+ * BooleanUtils.toBoolean(0) = Boolean.FALSE + * BooleanUtils.toBoolean(1) = Boolean.TRUE + * BooleanUtils.toBoolean(2) = Boolean.TRUE + *+ * + * @param value the int to convert + * @return Boolean.TRUE if non-zero, Boolean.FALSE if zero, + * {@code null} if {@code null} + */ + public static Boolean toBooleanObject(final int value) { + return value == 0 ? Boolean.FALSE : Boolean.TRUE; + } + + /** + *
Converts an Integer to a Boolean using the convention that {@code zero} + * is {@code false}.
+ * + *{@code null} will be converted to {@code null}.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.toBoolean(Integer.valueOf(0)) = Boolean.FALSE + * BooleanUtils.toBoolean(Integer.valueOf(1)) = Boolean.TRUE + * BooleanUtils.toBoolean(Integer.valueOf(null)) = null + *+ * + * @param value the Integer to convert + * @return Boolean.TRUE if non-zero, Boolean.FALSE if zero, + * {@code null} if {@code null} input + */ + public static Boolean toBooleanObject(final Integer value) { + if (value == null) { + return null; + } + return value.intValue() == 0 ? Boolean.FALSE : Boolean.TRUE; + } + + /** + *
Converts an int to a boolean specifying the conversion values.
+ * + *+ * BooleanUtils.toBoolean(0, 1, 0) = false + * BooleanUtils.toBoolean(1, 1, 0) = true + * BooleanUtils.toBoolean(2, 1, 2) = false + * BooleanUtils.toBoolean(2, 2, 0) = true + *+ * + * @param value the Integer to convert + * @param trueValue the value to match for {@code true} + * @param falseValue the value to match for {@code false} + * @return {@code true} or {@code false} + * @throws IllegalArgumentException if no match + */ + public static boolean toBoolean(final int value, final int trueValue, final int falseValue) { + if (value == trueValue) { + return true; + } + if (value == falseValue) { + return false; + } + // no match + throw new IllegalArgumentException("The Integer did not match either specified value"); + } + + /** + *
Converts an Integer to a boolean specifying the conversion values.
+ * + *+ * BooleanUtils.toBoolean(Integer.valueOf(0), Integer.valueOf(1), Integer.valueOf(0)) = false + * BooleanUtils.toBoolean(Integer.valueOf(1), Integer.valueOf(1), Integer.valueOf(0)) = true + * BooleanUtils.toBoolean(Integer.valueOf(2), Integer.valueOf(1), Integer.valueOf(2)) = false + * BooleanUtils.toBoolean(Integer.valueOf(2), Integer.valueOf(2), Integer.valueOf(0)) = true + * BooleanUtils.toBoolean(null, null, Integer.valueOf(0)) = true + *+ * + * @param value the Integer to convert + * @param trueValue the value to match for {@code true}, may be {@code null} + * @param falseValue the value to match for {@code false}, may be {@code null} + * @return {@code true} or {@code false} + * @throws IllegalArgumentException if no match + */ + public static boolean toBoolean(final Integer value, final Integer trueValue, final Integer falseValue) { + if (value == null) { + if (trueValue == null) { + return true; + } + if (falseValue == null) { + return false; + } + } else if (value.equals(trueValue)) { + return true; + } else if (value.equals(falseValue)) { + return false; + } + // no match + throw new IllegalArgumentException("The Integer did not match either specified value"); + } + + /** + *
Converts an int to a Boolean specifying the conversion values.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.toBooleanObject(0, 0, 2, 3) = Boolean.TRUE + * BooleanUtils.toBooleanObject(2, 1, 2, 3) = Boolean.FALSE + * BooleanUtils.toBooleanObject(3, 1, 2, 3) = null + *+ * + * @param value the Integer to convert + * @param trueValue the value to match for {@code true} + * @param falseValue the value to match for {@code false} + * @param nullValue the value to to match for {@code null} + * @return Boolean.TRUE, Boolean.FALSE, or {@code null} + * @throws IllegalArgumentException if no match + */ + public static Boolean toBooleanObject(final int value, final int trueValue, final int falseValue, final int nullValue) { + if (value == trueValue) { + return Boolean.TRUE; + } + if (value == falseValue) { + return Boolean.FALSE; + } + if (value == nullValue) { + return null; + } + // no match + throw new IllegalArgumentException("The Integer did not match any specified value"); + } + + /** + *
Converts an Integer to a Boolean specifying the conversion values.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.toBooleanObject(Integer.valueOf(0), Integer.valueOf(0), Integer.valueOf(2), Integer.valueOf(3)) = Boolean.TRUE + * BooleanUtils.toBooleanObject(Integer.valueOf(2), Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(3)) = Boolean.FALSE + * BooleanUtils.toBooleanObject(Integer.valueOf(3), Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(3)) = null + *+ * + * @param value the Integer to convert + * @param trueValue the value to match for {@code true}, may be {@code null} + * @param falseValue the value to match for {@code false}, may be {@code null} + * @param nullValue the value to to match for {@code null}, may be {@code null} + * @return Boolean.TRUE, Boolean.FALSE, or {@code null} + * @throws IllegalArgumentException if no match + */ + public static Boolean toBooleanObject(final Integer value, final Integer trueValue, final Integer falseValue, final Integer nullValue) { + if (value == null) { + if (trueValue == null) { + return Boolean.TRUE; + } + if (falseValue == null) { + return Boolean.FALSE; + } + if (nullValue == null) { + return null; + } + } else if (value.equals(trueValue)) { + return Boolean.TRUE; + } else if (value.equals(falseValue)) { + return Boolean.FALSE; + } else if (value.equals(nullValue)) { + return null; + } + // no match + throw new IllegalArgumentException("The Integer did not match any specified value"); + } + + // Boolean to Integer methods + //----------------------------------------------------------------------- + /** + *
Converts a boolean to an int using the convention that + * {@code zero} is {@code false}.
+ * + *+ * BooleanUtils.toInteger(true) = 1 + * BooleanUtils.toInteger(false) = 0 + *+ * + * @param bool the boolean to convert + * @return one if {@code true}, zero if {@code false} + */ + public static int toInteger(final boolean bool) { + return bool ? 1 : 0; + } + + /** + *
Converts a boolean to an Integer using the convention that + * {@code zero} is {@code false}.
+ * + *+ * BooleanUtils.toIntegerObject(true) = Integer.valueOf(1) + * BooleanUtils.toIntegerObject(false) = Integer.valueOf(0) + *+ * + * @param bool the boolean to convert + * @return one if {@code true}, zero if {@code false} + */ + public static Integer toIntegerObject(final boolean bool) { + return bool ? NumberUtils.INTEGER_ONE : NumberUtils.INTEGER_ZERO; + } + + /** + *
Converts a Boolean to a Integer using the convention that + * {@code zero} is {@code false}.
+ * + *{@code null} will be converted to {@code null}.
+ * + *+ * BooleanUtils.toIntegerObject(Boolean.TRUE) = Integer.valueOf(1) + * BooleanUtils.toIntegerObject(Boolean.FALSE) = Integer.valueOf(0) + *+ * + * @param bool the Boolean to convert + * @return one if Boolean.TRUE, zero if Boolean.FALSE, {@code null} if {@code null} + */ + public static Integer toIntegerObject(final Boolean bool) { + if (bool == null) { + return null; + } + return bool.booleanValue() ? NumberUtils.INTEGER_ONE : NumberUtils.INTEGER_ZERO; + } + + /** + *
Converts a boolean to an int specifying the conversion values.
+ * + *+ * BooleanUtils.toInteger(true, 1, 0) = 1 + * BooleanUtils.toInteger(false, 1, 0) = 0 + *+ * + * @param bool the to convert + * @param trueValue the value to return if {@code true} + * @param falseValue the value to return if {@code false} + * @return the appropriate value + */ + public static int toInteger(final boolean bool, final int trueValue, final int falseValue) { + return bool ? trueValue : falseValue; + } + + /** + *
Converts a Boolean to an int specifying the conversion values.
+ * + *+ * BooleanUtils.toInteger(Boolean.TRUE, 1, 0, 2) = 1 + * BooleanUtils.toInteger(Boolean.FALSE, 1, 0, 2) = 0 + * BooleanUtils.toInteger(null, 1, 0, 2) = 2 + *+ * + * @param bool the Boolean to convert + * @param trueValue the value to return if {@code true} + * @param falseValue the value to return if {@code false} + * @param nullValue the value to return if {@code null} + * @return the appropriate value + */ + public static int toInteger(final Boolean bool, final int trueValue, final int falseValue, final int nullValue) { + if (bool == null) { + return nullValue; + } + return bool.booleanValue() ? trueValue : falseValue; + } + + /** + *
Converts a boolean to an Integer specifying the conversion values.
+ * + *+ * BooleanUtils.toIntegerObject(true, Integer.valueOf(1), Integer.valueOf(0)) = Integer.valueOf(1) + * BooleanUtils.toIntegerObject(false, Integer.valueOf(1), Integer.valueOf(0)) = Integer.valueOf(0) + *+ * + * @param bool the to convert + * @param trueValue the value to return if {@code true}, may be {@code null} + * @param falseValue the value to return if {@code false}, may be {@code null} + * @return the appropriate value + */ + public static Integer toIntegerObject(final boolean bool, final Integer trueValue, final Integer falseValue) { + return bool ? trueValue : falseValue; + } + + /** + *
Converts a Boolean to an Integer specifying the conversion values.
+ * + *+ * BooleanUtils.toIntegerObject(Boolean.TRUE, Integer.valueOf(1), Integer.valueOf(0), Integer.valueOf(2)) = Integer.valueOf(1) + * BooleanUtils.toIntegerObject(Boolean.FALSE, Integer.valueOf(1), Integer.valueOf(0), Integer.valueOf(2)) = Integer.valueOf(0) + * BooleanUtils.toIntegerObject(null, Integer.valueOf(1), Integer.valueOf(0), Integer.valueOf(2)) = Integer.valueOf(2) + *+ * + * @param bool the Boolean to convert + * @param trueValue the value to return if {@code true}, may be {@code null} + * @param falseValue the value to return if {@code false}, may be {@code null} + * @param nullValue the value to return if {@code null}, may be {@code null} + * @return the appropriate value + */ + public static Integer toIntegerObject(final Boolean bool, final Integer trueValue, final Integer falseValue, final Integer nullValue) { + if (bool == null) { + return nullValue; + } + return bool.booleanValue() ? trueValue : falseValue; + } + + // String to Boolean methods + //----------------------------------------------------------------------- + /** + *
Converts a String to a Boolean.
+ * + *{@code 'true'}, {@code 'on'}, {@code 'y'}, {@code 't'} or {@code 'yes'} + * (case insensitive) will return {@code true}. + * {@code 'false'}, {@code 'off'}, {@code 'n'}, {@code 'f'} or {@code 'no'} + * (case insensitive) will return {@code false}. + * Otherwise, {@code null} is returned.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * // N.B. case is not significant + * BooleanUtils.toBooleanObject(null) = null + * BooleanUtils.toBooleanObject("true") = Boolean.TRUE + * BooleanUtils.toBooleanObject("T") = Boolean.TRUE // i.e. T[RUE] + * BooleanUtils.toBooleanObject("false") = Boolean.FALSE + * BooleanUtils.toBooleanObject("f") = Boolean.FALSE // i.e. f[alse] + * BooleanUtils.toBooleanObject("No") = Boolean.FALSE + * BooleanUtils.toBooleanObject("n") = Boolean.FALSE // i.e. n[o] + * BooleanUtils.toBooleanObject("on") = Boolean.TRUE + * BooleanUtils.toBooleanObject("ON") = Boolean.TRUE + * BooleanUtils.toBooleanObject("off") = Boolean.FALSE + * BooleanUtils.toBooleanObject("oFf") = Boolean.FALSE + * BooleanUtils.toBooleanObject("yes") = Boolean.TRUE + * BooleanUtils.toBooleanObject("Y") = Boolean.TRUE // i.e. Y[ES] + * BooleanUtils.toBooleanObject("blue") = null + * BooleanUtils.toBooleanObject("true ") = null // trailing space (too long) + * BooleanUtils.toBooleanObject("ono") = null // does not match on or no + *+ * + * @param str the String to check; upper and lower case are treated as the same + * @return the Boolean value of the string, {@code null} if no match or {@code null} input + */ + public static Boolean toBooleanObject(final String str) { + // Previously used equalsIgnoreCase, which was fast for interned 'true'. + // Non interned 'true' matched 15 times slower. + // + // Optimisation provides same performance as before for interned 'true'. + // Similar performance for null, 'false', and other strings not length 2/3/4. + // 'true'/'TRUE' match 4 times slower, 'tRUE'/'True' 7 times slower. + if (str == "true") { + return Boolean.TRUE; + } + if (str == null) { + return null; + } + switch (str.length()) { + case 1: { + final char ch0 = str.charAt(0); + if (ch0 == 'y' || ch0 == 'Y' || + ch0 == 't' || ch0 == 'T') { + return Boolean.TRUE; + } + if (ch0 == 'n' || ch0 == 'N' || + ch0 == 'f' || ch0 == 'F') { + return Boolean.FALSE; + } + break; + } + case 2: { + final char ch0 = str.charAt(0); + final char ch1 = str.charAt(1); + if ((ch0 == 'o' || ch0 == 'O') && + (ch1 == 'n' || ch1 == 'N') ) { + return Boolean.TRUE; + } + if ((ch0 == 'n' || ch0 == 'N') && + (ch1 == 'o' || ch1 == 'O') ) { + return Boolean.FALSE; + } + break; + } + case 3: { + final char ch0 = str.charAt(0); + final char ch1 = str.charAt(1); + final char ch2 = str.charAt(2); + if ((ch0 == 'y' || ch0 == 'Y') && + (ch1 == 'e' || ch1 == 'E') && + (ch2 == 's' || ch2 == 'S') ) { + return Boolean.TRUE; + } + if ((ch0 == 'o' || ch0 == 'O') && + (ch1 == 'f' || ch1 == 'F') && + (ch2 == 'f' || ch2 == 'F') ) { + return Boolean.FALSE; + } + break; + } + case 4: { + final char ch0 = str.charAt(0); + final char ch1 = str.charAt(1); + final char ch2 = str.charAt(2); + final char ch3 = str.charAt(3); + if ((ch0 == 't' || ch0 == 'T') && + (ch1 == 'r' || ch1 == 'R') && + (ch2 == 'u' || ch2 == 'U') && + (ch3 == 'e' || ch3 == 'E') ) { + return Boolean.TRUE; + } + break; + } + case 5: { + final char ch0 = str.charAt(0); + final char ch1 = str.charAt(1); + final char ch2 = str.charAt(2); + final char ch3 = str.charAt(3); + final char ch4 = str.charAt(4); + if ((ch0 == 'f' || ch0 == 'F') && + (ch1 == 'a' || ch1 == 'A') && + (ch2 == 'l' || ch2 == 'L') && + (ch3 == 's' || ch3 == 'S') && + (ch4 == 'e' || ch4 == 'E') ) { + return Boolean.FALSE; + } + break; + } + default: + break; + } + + return null; + } + + /** + *
Converts a String to a Boolean throwing an exception if no match.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.toBooleanObject("true", "true", "false", "null") = Boolean.TRUE + * BooleanUtils.toBooleanObject("false", "true", "false", "null") = Boolean.FALSE + * BooleanUtils.toBooleanObject("null", "true", "false", "null") = null + *+ * + * @param str the String to check + * @param trueString the String to match for {@code true} (case sensitive), may be {@code null} + * @param falseString the String to match for {@code false} (case sensitive), may be {@code null} + * @param nullString the String to match for {@code null} (case sensitive), may be {@code null} + * @return the Boolean value of the string, {@code null} if either the String matches {@code nullString} + * or if {@code null} input and {@code nullString} is {@code null} + * @throws IllegalArgumentException if the String doesn't match + */ + public static Boolean toBooleanObject(final String str, final String trueString, final String falseString, final String nullString) { + if (str == null) { + if (trueString == null) { + return Boolean.TRUE; + } + if (falseString == null) { + return Boolean.FALSE; + } + if (nullString == null) { + return null; + } + } else if (str.equals(trueString)) { + return Boolean.TRUE; + } else if (str.equals(falseString)) { + return Boolean.FALSE; + } else if (str.equals(nullString)) { + return null; + } + // no match + throw new IllegalArgumentException("The String did not match any specified value"); + } + + // String to boolean methods + //----------------------------------------------------------------------- + /** + *
Converts a String to a boolean (optimised for performance).
+ * + *{@code 'true'}, {@code 'on'}, {@code 'y'}, {@code 't'} or {@code 'yes'} + * (case insensitive) will return {@code true}. Otherwise, + * {@code false} is returned.
+ * + *This method performs 4 times faster (JDK1.4) than + * {@code Boolean.valueOf(String)}. However, this method accepts + * 'on' and 'yes', 't', 'y' as true values. + * + *
+ * BooleanUtils.toBoolean(null) = false + * BooleanUtils.toBoolean("true") = true + * BooleanUtils.toBoolean("TRUE") = true + * BooleanUtils.toBoolean("tRUe") = true + * BooleanUtils.toBoolean("on") = true + * BooleanUtils.toBoolean("yes") = true + * BooleanUtils.toBoolean("false") = false + * BooleanUtils.toBoolean("x gti") = false + * BooleanUtils.toBooleanObject("y") = true + * BooleanUtils.toBooleanObject("n") = false + * BooleanUtils.toBooleanObject("t") = true + * BooleanUtils.toBooleanObject("f") = false + *+ * + * @param str the String to check + * @return the boolean value of the string, {@code false} if no match or the String is null + */ + public static boolean toBoolean(final String str) { + return toBooleanObject(str) == Boolean.TRUE; + } + + /** + *
Converts a String to a Boolean throwing an exception if no match found.
+ * + *+ * BooleanUtils.toBoolean("true", "true", "false") = true + * BooleanUtils.toBoolean("false", "true", "false") = false + *+ * + * @param str the String to check + * @param trueString the String to match for {@code true} (case sensitive), may be {@code null} + * @param falseString the String to match for {@code false} (case sensitive), may be {@code null} + * @return the boolean value of the string + * @throws IllegalArgumentException if the String doesn't match + */ + public static boolean toBoolean(final String str, final String trueString, final String falseString) { + if (str == trueString) { + return true; + } else if (str == falseString) { + return false; + } else if (str != null) { + if (str.equals(trueString)) { + return true; + } else if (str.equals(falseString)) { + return false; + } + } + // no match + throw new IllegalArgumentException("The String did not match either specified value"); + } + + // Boolean to String methods + //----------------------------------------------------------------------- + /** + *
Converts a Boolean to a String returning {@code 'true'}, + * {@code 'false'}, or {@code null}.
+ * + *+ * BooleanUtils.toStringTrueFalse(Boolean.TRUE) = "true" + * BooleanUtils.toStringTrueFalse(Boolean.FALSE) = "false" + * BooleanUtils.toStringTrueFalse(null) = null; + *+ * + * @param bool the Boolean to check + * @return {@code 'true'}, {@code 'false'}, or {@code null} + */ + public static String toStringTrueFalse(final Boolean bool) { + return toString(bool, "true", "false", null); + } + + /** + *
Converts a Boolean to a String returning {@code 'on'}, + * {@code 'off'}, or {@code null}.
+ * + *+ * BooleanUtils.toStringOnOff(Boolean.TRUE) = "on" + * BooleanUtils.toStringOnOff(Boolean.FALSE) = "off" + * BooleanUtils.toStringOnOff(null) = null; + *+ * + * @param bool the Boolean to check + * @return {@code 'on'}, {@code 'off'}, or {@code null} + */ + public static String toStringOnOff(final Boolean bool) { + return toString(bool, "on", "off", null); + } + + /** + *
Converts a Boolean to a String returning {@code 'yes'}, + * {@code 'no'}, or {@code null}.
+ * + *+ * BooleanUtils.toStringYesNo(Boolean.TRUE) = "yes" + * BooleanUtils.toStringYesNo(Boolean.FALSE) = "no" + * BooleanUtils.toStringYesNo(null) = null; + *+ * + * @param bool the Boolean to check + * @return {@code 'yes'}, {@code 'no'}, or {@code null} + */ + public static String toStringYesNo(final Boolean bool) { + return toString(bool, "yes", "no", null); + } + + /** + *
Converts a Boolean to a String returning one of the input Strings.
+ * + *+ * BooleanUtils.toString(Boolean.TRUE, "true", "false", null) = "true" + * BooleanUtils.toString(Boolean.FALSE, "true", "false", null) = "false" + * BooleanUtils.toString(null, "true", "false", null) = null; + *+ * + * @param bool the Boolean to check + * @param trueString the String to return if {@code true}, may be {@code null} + * @param falseString the String to return if {@code false}, may be {@code null} + * @param nullString the String to return if {@code null}, may be {@code null} + * @return one of the three input Strings + */ + public static String toString(final Boolean bool, final String trueString, final String falseString, final String nullString) { + if (bool == null) { + return nullString; + } + return bool.booleanValue() ? trueString : falseString; + } + + // boolean to String methods + //----------------------------------------------------------------------- + /** + *
Converts a boolean to a String returning {@code 'true'} + * or {@code 'false'}.
+ * + *+ * BooleanUtils.toStringTrueFalse(true) = "true" + * BooleanUtils.toStringTrueFalse(false) = "false" + *+ * + * @param bool the Boolean to check + * @return {@code 'true'}, {@code 'false'}, or {@code null} + */ + public static String toStringTrueFalse(final boolean bool) { + return toString(bool, "true", "false"); + } + + /** + *
Converts a boolean to a String returning {@code 'on'} + * or {@code 'off'}.
+ * + *+ * BooleanUtils.toStringOnOff(true) = "on" + * BooleanUtils.toStringOnOff(false) = "off" + *+ * + * @param bool the Boolean to check + * @return {@code 'on'}, {@code 'off'}, or {@code null} + */ + public static String toStringOnOff(final boolean bool) { + return toString(bool, "on", "off"); + } + + /** + *
Converts a boolean to a String returning {@code 'yes'} + * or {@code 'no'}.
+ * + *+ * BooleanUtils.toStringYesNo(true) = "yes" + * BooleanUtils.toStringYesNo(false) = "no" + *+ * + * @param bool the Boolean to check + * @return {@code 'yes'}, {@code 'no'}, or {@code null} + */ + public static String toStringYesNo(final boolean bool) { + return toString(bool, "yes", "no"); + } + + /** + *
Converts a boolean to a String returning one of the input Strings.
+ * + *+ * BooleanUtils.toString(true, "true", "false") = "true" + * BooleanUtils.toString(false, "true", "false") = "false" + *+ * + * @param bool the Boolean to check + * @param trueString the String to return if {@code true}, may be {@code null} + * @param falseString the String to return if {@code false}, may be {@code null} + * @return one of the two input Strings + */ + public static String toString(final boolean bool, final String trueString, final String falseString) { + return bool ? trueString : falseString; + } + + // logical operations + // ---------------------------------------------------------------------- + /** + *
Performs an and on a set of booleans.
+ * + *+ * BooleanUtils.and(true, true) = true + * BooleanUtils.and(false, false) = false + * BooleanUtils.and(true, false) = false + * BooleanUtils.and(true, true, false) = false + * BooleanUtils.and(true, true, true) = true + *+ * + * @param array an array of {@code boolean}s + * @return {@code true} if the and is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + * @since 3.0.1 + */ + public static boolean and(final boolean... array) { + // Validates input + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + for (final boolean element : array) { + if (!element) { + return false; + } + } + return true; + } + + /** + *
Performs an and on an array of Booleans.
+ * + *+ * BooleanUtils.and(Boolean.TRUE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.and(Boolean.FALSE, Boolean.FALSE) = Boolean.FALSE + * BooleanUtils.and(Boolean.TRUE, Boolean.FALSE) = Boolean.FALSE + * BooleanUtils.and(Boolean.TRUE, Boolean.TRUE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.and(Boolean.FALSE, Boolean.FALSE, Boolean.TRUE) = Boolean.FALSE + * BooleanUtils.and(Boolean.TRUE, Boolean.FALSE, Boolean.TRUE) = Boolean.FALSE + *+ * + * @param array an array of {@code Boolean}s + * @return {@code true} if the and is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + * @throws IllegalArgumentException if {@code array} contains a {@code null} + * @since 3.0.1 + */ + public static Boolean and(final Boolean... array) { + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + try { + final boolean[] primitive = ArrayUtils.toPrimitive(array); + return and(primitive) ? Boolean.TRUE : Boolean.FALSE; + } catch (final NullPointerException ex) { + throw new IllegalArgumentException("The array must not contain any null elements"); + } + } + + /** + *
Performs an or on a set of booleans.
+ * + *+ * BooleanUtils.or(true, true) = true + * BooleanUtils.or(false, false) = false + * BooleanUtils.or(true, false) = true + * BooleanUtils.or(true, true, false) = true + * BooleanUtils.or(true, true, true) = true + * BooleanUtils.or(false, false, false) = false + *+ * + * @param array an array of {@code boolean}s + * @return {@code true} if the or is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + * @since 3.0.1 + */ + public static boolean or(final boolean... array) { + // Validates input + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + for (final boolean element : array) { + if (element) { + return true; + } + } + return false; + } + + /** + *
Performs an or on an array of Booleans.
+ * + *+ * BooleanUtils.or(Boolean.TRUE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.or(Boolean.FALSE, Boolean.FALSE) = Boolean.FALSE + * BooleanUtils.or(Boolean.TRUE, Boolean.FALSE) = Boolean.TRUE + * BooleanUtils.or(Boolean.TRUE, Boolean.TRUE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.or(Boolean.FALSE, Boolean.FALSE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.or(Boolean.TRUE, Boolean.FALSE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.or(Boolean.FALSE, Boolean.FALSE, Boolean.FALSE) = Boolean.FALSE + *+ * + * @param array an array of {@code Boolean}s + * @return {@code true} if the or is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + * @throws IllegalArgumentException if {@code array} contains a {@code null} + * @since 3.0.1 + */ + public static Boolean or(final Boolean... array) { + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + try { + final boolean[] primitive = ArrayUtils.toPrimitive(array); + return or(primitive) ? Boolean.TRUE : Boolean.FALSE; + } catch (final NullPointerException ex) { + throw new IllegalArgumentException("The array must not contain any null elements"); + } + } + + /** + *
Performs an xor on a set of booleans.
+ * + *+ * BooleanUtils.xor(true, true) = false + * BooleanUtils.xor(false, false) = false + * BooleanUtils.xor(true, false) = true + * BooleanUtils.xor(true, true) = false + * BooleanUtils.xor(false, false) = false + * BooleanUtils.xor(true, false) = true + *+ * + * @param array an array of {@code boolean}s + * @return {@code true} if the xor is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + */ + public static boolean xor(final boolean... array) { + // Validates input + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + + // false if the neutral element of the xor operator + boolean result = false; + for (final boolean element : array) { + result ^= element; + } + + return result; + } + + /** + *
Performs an xor on an array of Booleans.
+ * + *+ * BooleanUtils.xor(new Boolean[] { Boolean.TRUE, Boolean.TRUE }) = Boolean.FALSE + * BooleanUtils.xor(new Boolean[] { Boolean.FALSE, Boolean.FALSE }) = Boolean.FALSE + * BooleanUtils.xor(new Boolean[] { Boolean.TRUE, Boolean.FALSE }) = Boolean.TRUE + *+ * + * @param array an array of {@code Boolean}s + * @return {@code true} if the xor is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + * @throws IllegalArgumentException if {@code array} contains a {@code null} + */ +/** + *
Performs an xor on an array of Booleans.
+ * + *+ * BooleanUtils.xor(new Boolean[] { Boolean.TRUE, Boolean.TRUE }) = Boolean.FALSE + * BooleanUtils.xor(new Boolean[] { Boolean.FALSE, Boolean.FALSE }) = Boolean.FALSE + * BooleanUtils.xor(new Boolean[] { Boolean.TRUE, Boolean.FALSE }) = Boolean.TRUE + *+ * + * @param array + * an array of {@code Boolean}s + * @return {@code true} if the xor is successful. + * @throws IllegalArgumentException + * if {@code array} is {@code null} + * @throws IllegalArgumentException + * if {@code array} is empty. + * @throws IllegalArgumentException + * if {@code array} contains a {@code null} + */ +public static java.lang.Boolean xor(final java.lang.Boolean... array) { + { + if (/* NPEX_NULL_EXP */ + array.length == 0) { + throw new java.lang.IllegalArgumentException("Array is empty"); + } + try { + final boolean[] primitive = org.apache.commons.lang3.ArrayUtils.toPrimitive(array); + return org.apache.commons.lang3.BooleanUtils.xor(primitive) ? java.lang.Boolean.TRUE : java.lang.Boolean.FALSE; + } catch (final java.lang.NullPointerException ex) { + throw new java.lang.IllegalArgumentException("The array must not contain any null elements"); + } + } +} + + /** + *
Compares two {@code boolean} values. This is the same functionality as provided in Java 7.
+ * + * @param x the first {@code boolean} to compare + * @param y the second {@code boolean} to compare + * @return the value {@code 0} if {@code x == y}; + * a value less than {@code 0} if {@code !x && y}; and + * a value greater than {@code 0} if {@code x && !y} + * @since 3.4 + */ + public static int compare(boolean x, boolean y) { + if (x == y) { + return 0; + } + return x ? 1 : -1; + } + +} diff --git a/Java/commons-lang-BooleanUtils_1073/metadata.json b/Java/commons-lang-BooleanUtils_1073/metadata.json new file mode 100644 index 000000000..538a14ead --- /dev/null +++ b/Java/commons-lang-BooleanUtils_1073/metadata.json @@ -0,0 +1,21 @@ +{ + "language": "java", + "id": "commons-lang-BooleanUtils_1073", + "buggyPath": ".", + "referencePath": null, + "buildCommand": "mvn package -V -B -Denforcer.skip=true -Dcheckstyle.skip=true -Dcobertura.skip=true -Drat.skip=true -Dlicense.skip=true -Dfindbugs.skip=true -Dgpg.skip=true -Dskip.npm=true -Dskip.gulp=true -Dskip.bower=true -Drat.numUnapprovedLicenses=100 -DskipTests=true -DskipITs=true -Dtest=None -DfailIfNoTests=false", + "testCommand": "mvn test -V -B -Denforcer.skip=true -Dcheckstyle.skip=true -Dcobertura.skip=true -Drat.skip=true -Dlicense.skip=true -Dfindbugs.skip=true -Dgpg.skip=true -Dskip.npm=true -Dskip.gulp=true -Dskip.bower=true -Drat.numUnapprovedLicenses=100", + "categories": [ + "safety", + "npe" + ], + "npe": { + "filepath": "src/main/java/org/apache/commons/lang3/BooleanUtils.java", + "line": 1094, + "npe_method": "xor", + "deref_field": "array", + "npe_class": "BooleanUtils", + "repo": "commons-lang", + "bug_id": "BooleanUtils_1073" + } +} diff --git a/Java/commons-lang-BooleanUtils_1073/npe.json b/Java/commons-lang-BooleanUtils_1073/npe.json new file mode 100644 index 000000000..eb4c89eb1 --- /dev/null +++ b/Java/commons-lang-BooleanUtils_1073/npe.json @@ -0,0 +1,7 @@ +{ + "filepath": "src/main/java/org/apache/commons/lang3/BooleanUtils.java", + "line": 1094, + "npe_method": "xor", + "deref_field": "array", + "npe_class": "BooleanUtils" +} \ No newline at end of file diff --git a/Java/commons-lang-BooleanUtils_175/Dockerfile b/Java/commons-lang-BooleanUtils_175/Dockerfile new file mode 100644 index 000000000..7b7fbe349 --- /dev/null +++ b/Java/commons-lang-BooleanUtils_175/Dockerfile @@ -0,0 +1,18 @@ +FROM ghcr.io/kupl/starlab-benchmarks/java-base:commons-lang + +ENV TZ=Asia/Seoul + +COPY ./metadata.json . +COPY ./npe.json . +COPY ./buggy.java /tmp/buggy.java +RUN export BUGGY_PATH=$(cat metadata.json | jq -r ".npe.filepath") \ + && export BUGGY_LINE=$(cat metadata.json | jq -r ".npe.line") \ + && export BUGGY_MTHD=$(cat metadata.json | jq -r ".npe.npe_method") \ + && mv /tmp/buggy.java $BUGGY_PATH \ + && echo "[{\"filepath\": \"$BUGGY_PATH\", \"line\": $BUGGY_LINE, \"method_name\": \"$BUGGY_MTHD\"}]" | jq . > traces.json + +RUN git init . && git add -A + +RUN $(cat metadata.json | jq -r ".buildCommand") + +RUN $(cat metadata.json | jq -r ".testCommand"); if [ $? -eq 0 ]; then exit 1; fi diff --git a/Java/commons-lang-BooleanUtils_175/buggy.java b/Java/commons-lang-BooleanUtils_175/buggy.java new file mode 100644 index 000000000..da314415c --- /dev/null +++ b/Java/commons-lang-BooleanUtils_175/buggy.java @@ -0,0 +1,1119 @@ +/* + * 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; + +import org.apache.commons.lang3.math.NumberUtils; + +/** + *Operations on boolean primitives and Boolean objects.
+ * + *This class tries to handle {@code null} input gracefully. + * An exception will not be thrown for a {@code null} input. + * Each method documents its behaviour in more detail.
+ * + *#ThreadSafe#
+ * @since 2.0 + */ +public class BooleanUtils { + + /** + *{@code BooleanUtils} instances should NOT be constructed in standard programming. + * Instead, the class should be used as {@code BooleanUtils.negate(true);}.
+ * + *This constructor is public to permit tools that require a JavaBean instance + * to operate.
+ */ + public BooleanUtils() { + super(); + } + + // Boolean utilities + //-------------------------------------------------------------------------- + /** + *Negates the specified boolean.
+ * + *If {@code null} is passed in, {@code null} will be returned.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.negate(Boolean.TRUE) = Boolean.FALSE; + * BooleanUtils.negate(Boolean.FALSE) = Boolean.TRUE; + * BooleanUtils.negate(null) = null; + *+ * + * @param bool the Boolean to negate, may be null + * @return the negated Boolean, or {@code null} if {@code null} input + */ + public static Boolean negate(final Boolean bool) { + if (bool == null) { + return null; + } + return bool.booleanValue() ? Boolean.FALSE : Boolean.TRUE; + } + + // boolean Boolean methods + //----------------------------------------------------------------------- + /** + *
Checks if a {@code Boolean} value is {@code true}, + * handling {@code null} by returning {@code false}.
+ * + *+ * BooleanUtils.isTrue(Boolean.TRUE) = true + * BooleanUtils.isTrue(Boolean.FALSE) = false + * BooleanUtils.isTrue(null) = false + *+ * + * @param bool the boolean to check, null returns {@code false} + * @return {@code true} only if the input is non-null and true + * @since 2.1 + */ + public static boolean isTrue(final Boolean bool) { + return Boolean.TRUE.equals(bool); + } + + /** + *
Checks if a {@code Boolean} value is not {@code true}, + * handling {@code null} by returning {@code true}.
+ * + *+ * BooleanUtils.isNotTrue(Boolean.TRUE) = false + * BooleanUtils.isNotTrue(Boolean.FALSE) = true + * BooleanUtils.isNotTrue(null) = true + *+ * + * @param bool the boolean to check, null returns {@code true} + * @return {@code true} if the input is null or false + * @since 2.3 + */ + public static boolean isNotTrue(final Boolean bool) { + return !isTrue(bool); + } + + /** + *
Checks if a {@code Boolean} value is {@code false}, + * handling {@code null} by returning {@code false}.
+ * + *+ * BooleanUtils.isFalse(Boolean.TRUE) = false + * BooleanUtils.isFalse(Boolean.FALSE) = true + * BooleanUtils.isFalse(null) = false + *+ * + * @param bool the boolean to check, null returns {@code false} + * @return {@code true} only if the input is non-null and false + * @since 2.1 + */ + public static boolean isFalse(final Boolean bool) { + return Boolean.FALSE.equals(bool); + } + + /** + *
Checks if a {@code Boolean} value is not {@code false}, + * handling {@code null} by returning {@code true}.
+ * + *+ * BooleanUtils.isNotFalse(Boolean.TRUE) = true + * BooleanUtils.isNotFalse(Boolean.FALSE) = false + * BooleanUtils.isNotFalse(null) = true + *+ * + * @param bool the boolean to check, null returns {@code true} + * @return {@code true} if the input is null or true + * @since 2.3 + */ + public static boolean isNotFalse(final Boolean bool) { + return !isFalse(bool); + } + + //----------------------------------------------------------------------- + /** + *
Converts a Boolean to a boolean handling {@code null} + * by returning {@code false}.
+ * + *+ * BooleanUtils.toBoolean(Boolean.TRUE) = true + * BooleanUtils.toBoolean(Boolean.FALSE) = false + * BooleanUtils.toBoolean(null) = false + *+ * + * @param bool the boolean to convert + * @return {@code true} or {@code false}, {@code null} returns {@code false} + */ + public static boolean toBoolean(final Boolean bool) { + return bool != null && bool.booleanValue(); + } + + /** + *
Converts a Boolean to a boolean handling {@code null}.
+ * + *+ * BooleanUtils.toBooleanDefaultIfNull(Boolean.TRUE, false) = true + * BooleanUtils.toBooleanDefaultIfNull(Boolean.FALSE, true) = false + * BooleanUtils.toBooleanDefaultIfNull(null, true) = true + *+ * + * @param bool the boolean to convert + * @param valueIfNull the boolean value to return if {@code null} + * @return {@code true} or {@code false} + */ +/** + *
Converts a Boolean to a boolean handling {@code null}.
+ * + *+ * BooleanUtils.toBooleanDefaultIfNull(Boolean.TRUE, false) = true + * BooleanUtils.toBooleanDefaultIfNull(Boolean.FALSE, true) = false + * BooleanUtils.toBooleanDefaultIfNull(null, true) = true + *+ * + * @param bool + * the boolean to convert + * @param valueIfNull + * the boolean value to return if {@code null} + * @return {@code true} or {@code false} + */ +public static boolean toBooleanDefaultIfNull(final java.lang.Boolean bool, final boolean valueIfNull) { + { + return /* NPEX_NULL_EXP */ + bool.booleanValue(); + } +} + + // Integer to Boolean methods + //----------------------------------------------------------------------- + /** + *
Converts an int to a boolean using the convention that {@code zero} + * is {@code false}.
+ * + *+ * BooleanUtils.toBoolean(0) = false + * BooleanUtils.toBoolean(1) = true + * BooleanUtils.toBoolean(2) = true + *+ * + * @param value the int to convert + * @return {@code true} if non-zero, {@code false} + * if zero + */ + public static boolean toBoolean(final int value) { + return value != 0; + } + + /** + *
Converts an int to a Boolean using the convention that {@code zero} + * is {@code false}.
+ * + *+ * BooleanUtils.toBoolean(0) = Boolean.FALSE + * BooleanUtils.toBoolean(1) = Boolean.TRUE + * BooleanUtils.toBoolean(2) = Boolean.TRUE + *+ * + * @param value the int to convert + * @return Boolean.TRUE if non-zero, Boolean.FALSE if zero, + * {@code null} if {@code null} + */ + public static Boolean toBooleanObject(final int value) { + return value == 0 ? Boolean.FALSE : Boolean.TRUE; + } + + /** + *
Converts an Integer to a Boolean using the convention that {@code zero} + * is {@code false}.
+ * + *{@code null} will be converted to {@code null}.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.toBoolean(Integer.valueOf(0)) = Boolean.FALSE + * BooleanUtils.toBoolean(Integer.valueOf(1)) = Boolean.TRUE + * BooleanUtils.toBoolean(Integer.valueOf(null)) = null + *+ * + * @param value the Integer to convert + * @return Boolean.TRUE if non-zero, Boolean.FALSE if zero, + * {@code null} if {@code null} input + */ + public static Boolean toBooleanObject(final Integer value) { + if (value == null) { + return null; + } + return value.intValue() == 0 ? Boolean.FALSE : Boolean.TRUE; + } + + /** + *
Converts an int to a boolean specifying the conversion values.
+ * + *+ * BooleanUtils.toBoolean(0, 1, 0) = false + * BooleanUtils.toBoolean(1, 1, 0) = true + * BooleanUtils.toBoolean(2, 1, 2) = false + * BooleanUtils.toBoolean(2, 2, 0) = true + *+ * + * @param value the Integer to convert + * @param trueValue the value to match for {@code true} + * @param falseValue the value to match for {@code false} + * @return {@code true} or {@code false} + * @throws IllegalArgumentException if no match + */ + public static boolean toBoolean(final int value, final int trueValue, final int falseValue) { + if (value == trueValue) { + return true; + } + if (value == falseValue) { + return false; + } + // no match + throw new IllegalArgumentException("The Integer did not match either specified value"); + } + + /** + *
Converts an Integer to a boolean specifying the conversion values.
+ * + *+ * BooleanUtils.toBoolean(Integer.valueOf(0), Integer.valueOf(1), Integer.valueOf(0)) = false + * BooleanUtils.toBoolean(Integer.valueOf(1), Integer.valueOf(1), Integer.valueOf(0)) = true + * BooleanUtils.toBoolean(Integer.valueOf(2), Integer.valueOf(1), Integer.valueOf(2)) = false + * BooleanUtils.toBoolean(Integer.valueOf(2), Integer.valueOf(2), Integer.valueOf(0)) = true + * BooleanUtils.toBoolean(null, null, Integer.valueOf(0)) = true + *+ * + * @param value the Integer to convert + * @param trueValue the value to match for {@code true}, may be {@code null} + * @param falseValue the value to match for {@code false}, may be {@code null} + * @return {@code true} or {@code false} + * @throws IllegalArgumentException if no match + */ + public static boolean toBoolean(final Integer value, final Integer trueValue, final Integer falseValue) { + if (value == null) { + if (trueValue == null) { + return true; + } + if (falseValue == null) { + return false; + } + } else if (value.equals(trueValue)) { + return true; + } else if (value.equals(falseValue)) { + return false; + } + // no match + throw new IllegalArgumentException("The Integer did not match either specified value"); + } + + /** + *
Converts an int to a Boolean specifying the conversion values.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.toBooleanObject(0, 0, 2, 3) = Boolean.TRUE + * BooleanUtils.toBooleanObject(2, 1, 2, 3) = Boolean.FALSE + * BooleanUtils.toBooleanObject(3, 1, 2, 3) = null + *+ * + * @param value the Integer to convert + * @param trueValue the value to match for {@code true} + * @param falseValue the value to match for {@code false} + * @param nullValue the value to to match for {@code null} + * @return Boolean.TRUE, Boolean.FALSE, or {@code null} + * @throws IllegalArgumentException if no match + */ + public static Boolean toBooleanObject(final int value, final int trueValue, final int falseValue, final int nullValue) { + if (value == trueValue) { + return Boolean.TRUE; + } + if (value == falseValue) { + return Boolean.FALSE; + } + if (value == nullValue) { + return null; + } + // no match + throw new IllegalArgumentException("The Integer did not match any specified value"); + } + + /** + *
Converts an Integer to a Boolean specifying the conversion values.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.toBooleanObject(Integer.valueOf(0), Integer.valueOf(0), Integer.valueOf(2), Integer.valueOf(3)) = Boolean.TRUE + * BooleanUtils.toBooleanObject(Integer.valueOf(2), Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(3)) = Boolean.FALSE + * BooleanUtils.toBooleanObject(Integer.valueOf(3), Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(3)) = null + *+ * + * @param value the Integer to convert + * @param trueValue the value to match for {@code true}, may be {@code null} + * @param falseValue the value to match for {@code false}, may be {@code null} + * @param nullValue the value to to match for {@code null}, may be {@code null} + * @return Boolean.TRUE, Boolean.FALSE, or {@code null} + * @throws IllegalArgumentException if no match + */ + public static Boolean toBooleanObject(final Integer value, final Integer trueValue, final Integer falseValue, final Integer nullValue) { + if (value == null) { + if (trueValue == null) { + return Boolean.TRUE; + } + if (falseValue == null) { + return Boolean.FALSE; + } + if (nullValue == null) { + return null; + } + } else if (value.equals(trueValue)) { + return Boolean.TRUE; + } else if (value.equals(falseValue)) { + return Boolean.FALSE; + } else if (value.equals(nullValue)) { + return null; + } + // no match + throw new IllegalArgumentException("The Integer did not match any specified value"); + } + + // Boolean to Integer methods + //----------------------------------------------------------------------- + /** + *
Converts a boolean to an int using the convention that + * {@code zero} is {@code false}.
+ * + *+ * BooleanUtils.toInteger(true) = 1 + * BooleanUtils.toInteger(false) = 0 + *+ * + * @param bool the boolean to convert + * @return one if {@code true}, zero if {@code false} + */ + public static int toInteger(final boolean bool) { + return bool ? 1 : 0; + } + + /** + *
Converts a boolean to an Integer using the convention that + * {@code zero} is {@code false}.
+ * + *+ * BooleanUtils.toIntegerObject(true) = Integer.valueOf(1) + * BooleanUtils.toIntegerObject(false) = Integer.valueOf(0) + *+ * + * @param bool the boolean to convert + * @return one if {@code true}, zero if {@code false} + */ + public static Integer toIntegerObject(final boolean bool) { + return bool ? NumberUtils.INTEGER_ONE : NumberUtils.INTEGER_ZERO; + } + + /** + *
Converts a Boolean to a Integer using the convention that + * {@code zero} is {@code false}.
+ * + *{@code null} will be converted to {@code null}.
+ * + *+ * BooleanUtils.toIntegerObject(Boolean.TRUE) = Integer.valueOf(1) + * BooleanUtils.toIntegerObject(Boolean.FALSE) = Integer.valueOf(0) + *+ * + * @param bool the Boolean to convert + * @return one if Boolean.TRUE, zero if Boolean.FALSE, {@code null} if {@code null} + */ + public static Integer toIntegerObject(final Boolean bool) { + if (bool == null) { + return null; + } + return bool.booleanValue() ? NumberUtils.INTEGER_ONE : NumberUtils.INTEGER_ZERO; + } + + /** + *
Converts a boolean to an int specifying the conversion values.
+ * + *+ * BooleanUtils.toInteger(true, 1, 0) = 1 + * BooleanUtils.toInteger(false, 1, 0) = 0 + *+ * + * @param bool the to convert + * @param trueValue the value to return if {@code true} + * @param falseValue the value to return if {@code false} + * @return the appropriate value + */ + public static int toInteger(final boolean bool, final int trueValue, final int falseValue) { + return bool ? trueValue : falseValue; + } + + /** + *
Converts a Boolean to an int specifying the conversion values.
+ * + *+ * BooleanUtils.toInteger(Boolean.TRUE, 1, 0, 2) = 1 + * BooleanUtils.toInteger(Boolean.FALSE, 1, 0, 2) = 0 + * BooleanUtils.toInteger(null, 1, 0, 2) = 2 + *+ * + * @param bool the Boolean to convert + * @param trueValue the value to return if {@code true} + * @param falseValue the value to return if {@code false} + * @param nullValue the value to return if {@code null} + * @return the appropriate value + */ + public static int toInteger(final Boolean bool, final int trueValue, final int falseValue, final int nullValue) { + if (bool == null) { + return nullValue; + } + return bool.booleanValue() ? trueValue : falseValue; + } + + /** + *
Converts a boolean to an Integer specifying the conversion values.
+ * + *+ * BooleanUtils.toIntegerObject(true, Integer.valueOf(1), Integer.valueOf(0)) = Integer.valueOf(1) + * BooleanUtils.toIntegerObject(false, Integer.valueOf(1), Integer.valueOf(0)) = Integer.valueOf(0) + *+ * + * @param bool the to convert + * @param trueValue the value to return if {@code true}, may be {@code null} + * @param falseValue the value to return if {@code false}, may be {@code null} + * @return the appropriate value + */ + public static Integer toIntegerObject(final boolean bool, final Integer trueValue, final Integer falseValue) { + return bool ? trueValue : falseValue; + } + + /** + *
Converts a Boolean to an Integer specifying the conversion values.
+ * + *+ * BooleanUtils.toIntegerObject(Boolean.TRUE, Integer.valueOf(1), Integer.valueOf(0), Integer.valueOf(2)) = Integer.valueOf(1) + * BooleanUtils.toIntegerObject(Boolean.FALSE, Integer.valueOf(1), Integer.valueOf(0), Integer.valueOf(2)) = Integer.valueOf(0) + * BooleanUtils.toIntegerObject(null, Integer.valueOf(1), Integer.valueOf(0), Integer.valueOf(2)) = Integer.valueOf(2) + *+ * + * @param bool the Boolean to convert + * @param trueValue the value to return if {@code true}, may be {@code null} + * @param falseValue the value to return if {@code false}, may be {@code null} + * @param nullValue the value to return if {@code null}, may be {@code null} + * @return the appropriate value + */ + public static Integer toIntegerObject(final Boolean bool, final Integer trueValue, final Integer falseValue, final Integer nullValue) { + if (bool == null) { + return nullValue; + } + return bool.booleanValue() ? trueValue : falseValue; + } + + // String to Boolean methods + //----------------------------------------------------------------------- + /** + *
Converts a String to a Boolean.
+ * + *{@code 'true'}, {@code 'on'}, {@code 'y'}, {@code 't'} or {@code 'yes'} + * (case insensitive) will return {@code true}. + * {@code 'false'}, {@code 'off'}, {@code 'n'}, {@code 'f'} or {@code 'no'} + * (case insensitive) will return {@code false}. + * Otherwise, {@code null} is returned.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * // N.B. case is not significant + * BooleanUtils.toBooleanObject(null) = null + * BooleanUtils.toBooleanObject("true") = Boolean.TRUE + * BooleanUtils.toBooleanObject("T") = Boolean.TRUE // i.e. T[RUE] + * BooleanUtils.toBooleanObject("false") = Boolean.FALSE + * BooleanUtils.toBooleanObject("f") = Boolean.FALSE // i.e. f[alse] + * BooleanUtils.toBooleanObject("No") = Boolean.FALSE + * BooleanUtils.toBooleanObject("n") = Boolean.FALSE // i.e. n[o] + * BooleanUtils.toBooleanObject("on") = Boolean.TRUE + * BooleanUtils.toBooleanObject("ON") = Boolean.TRUE + * BooleanUtils.toBooleanObject("off") = Boolean.FALSE + * BooleanUtils.toBooleanObject("oFf") = Boolean.FALSE + * BooleanUtils.toBooleanObject("yes") = Boolean.TRUE + * BooleanUtils.toBooleanObject("Y") = Boolean.TRUE // i.e. Y[ES] + * BooleanUtils.toBooleanObject("blue") = null + * BooleanUtils.toBooleanObject("true ") = null // trailing space (too long) + * BooleanUtils.toBooleanObject("ono") = null // does not match on or no + *+ * + * @param str the String to check; upper and lower case are treated as the same + * @return the Boolean value of the string, {@code null} if no match or {@code null} input + */ + public static Boolean toBooleanObject(final String str) { + // Previously used equalsIgnoreCase, which was fast for interned 'true'. + // Non interned 'true' matched 15 times slower. + // + // Optimisation provides same performance as before for interned 'true'. + // Similar performance for null, 'false', and other strings not length 2/3/4. + // 'true'/'TRUE' match 4 times slower, 'tRUE'/'True' 7 times slower. + if (str == "true") { + return Boolean.TRUE; + } + if (str == null) { + return null; + } + switch (str.length()) { + case 1: { + final char ch0 = str.charAt(0); + if (ch0 == 'y' || ch0 == 'Y' || + ch0 == 't' || ch0 == 'T') { + return Boolean.TRUE; + } + if (ch0 == 'n' || ch0 == 'N' || + ch0 == 'f' || ch0 == 'F') { + return Boolean.FALSE; + } + break; + } + case 2: { + final char ch0 = str.charAt(0); + final char ch1 = str.charAt(1); + if ((ch0 == 'o' || ch0 == 'O') && + (ch1 == 'n' || ch1 == 'N') ) { + return Boolean.TRUE; + } + if ((ch0 == 'n' || ch0 == 'N') && + (ch1 == 'o' || ch1 == 'O') ) { + return Boolean.FALSE; + } + break; + } + case 3: { + final char ch0 = str.charAt(0); + final char ch1 = str.charAt(1); + final char ch2 = str.charAt(2); + if ((ch0 == 'y' || ch0 == 'Y') && + (ch1 == 'e' || ch1 == 'E') && + (ch2 == 's' || ch2 == 'S') ) { + return Boolean.TRUE; + } + if ((ch0 == 'o' || ch0 == 'O') && + (ch1 == 'f' || ch1 == 'F') && + (ch2 == 'f' || ch2 == 'F') ) { + return Boolean.FALSE; + } + break; + } + case 4: { + final char ch0 = str.charAt(0); + final char ch1 = str.charAt(1); + final char ch2 = str.charAt(2); + final char ch3 = str.charAt(3); + if ((ch0 == 't' || ch0 == 'T') && + (ch1 == 'r' || ch1 == 'R') && + (ch2 == 'u' || ch2 == 'U') && + (ch3 == 'e' || ch3 == 'E') ) { + return Boolean.TRUE; + } + break; + } + case 5: { + final char ch0 = str.charAt(0); + final char ch1 = str.charAt(1); + final char ch2 = str.charAt(2); + final char ch3 = str.charAt(3); + final char ch4 = str.charAt(4); + if ((ch0 == 'f' || ch0 == 'F') && + (ch1 == 'a' || ch1 == 'A') && + (ch2 == 'l' || ch2 == 'L') && + (ch3 == 's' || ch3 == 'S') && + (ch4 == 'e' || ch4 == 'E') ) { + return Boolean.FALSE; + } + break; + } + default: + break; + } + + return null; + } + + /** + *
Converts a String to a Boolean throwing an exception if no match.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.toBooleanObject("true", "true", "false", "null") = Boolean.TRUE + * BooleanUtils.toBooleanObject("false", "true", "false", "null") = Boolean.FALSE + * BooleanUtils.toBooleanObject("null", "true", "false", "null") = null + *+ * + * @param str the String to check + * @param trueString the String to match for {@code true} (case sensitive), may be {@code null} + * @param falseString the String to match for {@code false} (case sensitive), may be {@code null} + * @param nullString the String to match for {@code null} (case sensitive), may be {@code null} + * @return the Boolean value of the string, {@code null} if either the String matches {@code nullString} + * or if {@code null} input and {@code nullString} is {@code null} + * @throws IllegalArgumentException if the String doesn't match + */ + public static Boolean toBooleanObject(final String str, final String trueString, final String falseString, final String nullString) { + if (str == null) { + if (trueString == null) { + return Boolean.TRUE; + } + if (falseString == null) { + return Boolean.FALSE; + } + if (nullString == null) { + return null; + } + } else if (str.equals(trueString)) { + return Boolean.TRUE; + } else if (str.equals(falseString)) { + return Boolean.FALSE; + } else if (str.equals(nullString)) { + return null; + } + // no match + throw new IllegalArgumentException("The String did not match any specified value"); + } + + // String to boolean methods + //----------------------------------------------------------------------- + /** + *
Converts a String to a boolean (optimised for performance).
+ * + *{@code 'true'}, {@code 'on'}, {@code 'y'}, {@code 't'} or {@code 'yes'} + * (case insensitive) will return {@code true}. Otherwise, + * {@code false} is returned.
+ * + *This method performs 4 times faster (JDK1.4) than + * {@code Boolean.valueOf(String)}. However, this method accepts + * 'on' and 'yes', 't', 'y' as true values. + * + *
+ * BooleanUtils.toBoolean(null) = false + * BooleanUtils.toBoolean("true") = true + * BooleanUtils.toBoolean("TRUE") = true + * BooleanUtils.toBoolean("tRUe") = true + * BooleanUtils.toBoolean("on") = true + * BooleanUtils.toBoolean("yes") = true + * BooleanUtils.toBoolean("false") = false + * BooleanUtils.toBoolean("x gti") = false + * BooleanUtils.toBooleanObject("y") = true + * BooleanUtils.toBooleanObject("n") = false + * BooleanUtils.toBooleanObject("t") = true + * BooleanUtils.toBooleanObject("f") = false + *+ * + * @param str the String to check + * @return the boolean value of the string, {@code false} if no match or the String is null + */ + public static boolean toBoolean(final String str) { + return toBooleanObject(str) == Boolean.TRUE; + } + + /** + *
Converts a String to a Boolean throwing an exception if no match found.
+ * + *+ * BooleanUtils.toBoolean("true", "true", "false") = true + * BooleanUtils.toBoolean("false", "true", "false") = false + *+ * + * @param str the String to check + * @param trueString the String to match for {@code true} (case sensitive), may be {@code null} + * @param falseString the String to match for {@code false} (case sensitive), may be {@code null} + * @return the boolean value of the string + * @throws IllegalArgumentException if the String doesn't match + */ + public static boolean toBoolean(final String str, final String trueString, final String falseString) { + if (str == trueString) { + return true; + } else if (str == falseString) { + return false; + } else if (str != null) { + if (str.equals(trueString)) { + return true; + } else if (str.equals(falseString)) { + return false; + } + } + // no match + throw new IllegalArgumentException("The String did not match either specified value"); + } + + // Boolean to String methods + //----------------------------------------------------------------------- + /** + *
Converts a Boolean to a String returning {@code 'true'}, + * {@code 'false'}, or {@code null}.
+ * + *+ * BooleanUtils.toStringTrueFalse(Boolean.TRUE) = "true" + * BooleanUtils.toStringTrueFalse(Boolean.FALSE) = "false" + * BooleanUtils.toStringTrueFalse(null) = null; + *+ * + * @param bool the Boolean to check + * @return {@code 'true'}, {@code 'false'}, or {@code null} + */ + public static String toStringTrueFalse(final Boolean bool) { + return toString(bool, "true", "false", null); + } + + /** + *
Converts a Boolean to a String returning {@code 'on'}, + * {@code 'off'}, or {@code null}.
+ * + *+ * BooleanUtils.toStringOnOff(Boolean.TRUE) = "on" + * BooleanUtils.toStringOnOff(Boolean.FALSE) = "off" + * BooleanUtils.toStringOnOff(null) = null; + *+ * + * @param bool the Boolean to check + * @return {@code 'on'}, {@code 'off'}, or {@code null} + */ + public static String toStringOnOff(final Boolean bool) { + return toString(bool, "on", "off", null); + } + + /** + *
Converts a Boolean to a String returning {@code 'yes'}, + * {@code 'no'}, or {@code null}.
+ * + *+ * BooleanUtils.toStringYesNo(Boolean.TRUE) = "yes" + * BooleanUtils.toStringYesNo(Boolean.FALSE) = "no" + * BooleanUtils.toStringYesNo(null) = null; + *+ * + * @param bool the Boolean to check + * @return {@code 'yes'}, {@code 'no'}, or {@code null} + */ + public static String toStringYesNo(final Boolean bool) { + return toString(bool, "yes", "no", null); + } + + /** + *
Converts a Boolean to a String returning one of the input Strings.
+ * + *+ * BooleanUtils.toString(Boolean.TRUE, "true", "false", null) = "true" + * BooleanUtils.toString(Boolean.FALSE, "true", "false", null) = "false" + * BooleanUtils.toString(null, "true", "false", null) = null; + *+ * + * @param bool the Boolean to check + * @param trueString the String to return if {@code true}, may be {@code null} + * @param falseString the String to return if {@code false}, may be {@code null} + * @param nullString the String to return if {@code null}, may be {@code null} + * @return one of the three input Strings + */ + public static String toString(final Boolean bool, final String trueString, final String falseString, final String nullString) { + if (bool == null) { + return nullString; + } + return bool.booleanValue() ? trueString : falseString; + } + + // boolean to String methods + //----------------------------------------------------------------------- + /** + *
Converts a boolean to a String returning {@code 'true'} + * or {@code 'false'}.
+ * + *+ * BooleanUtils.toStringTrueFalse(true) = "true" + * BooleanUtils.toStringTrueFalse(false) = "false" + *+ * + * @param bool the Boolean to check + * @return {@code 'true'}, {@code 'false'}, or {@code null} + */ + public static String toStringTrueFalse(final boolean bool) { + return toString(bool, "true", "false"); + } + + /** + *
Converts a boolean to a String returning {@code 'on'} + * or {@code 'off'}.
+ * + *+ * BooleanUtils.toStringOnOff(true) = "on" + * BooleanUtils.toStringOnOff(false) = "off" + *+ * + * @param bool the Boolean to check + * @return {@code 'on'}, {@code 'off'}, or {@code null} + */ + public static String toStringOnOff(final boolean bool) { + return toString(bool, "on", "off"); + } + + /** + *
Converts a boolean to a String returning {@code 'yes'} + * or {@code 'no'}.
+ * + *+ * BooleanUtils.toStringYesNo(true) = "yes" + * BooleanUtils.toStringYesNo(false) = "no" + *+ * + * @param bool the Boolean to check + * @return {@code 'yes'}, {@code 'no'}, or {@code null} + */ + public static String toStringYesNo(final boolean bool) { + return toString(bool, "yes", "no"); + } + + /** + *
Converts a boolean to a String returning one of the input Strings.
+ * + *+ * BooleanUtils.toString(true, "true", "false") = "true" + * BooleanUtils.toString(false, "true", "false") = "false" + *+ * + * @param bool the Boolean to check + * @param trueString the String to return if {@code true}, may be {@code null} + * @param falseString the String to return if {@code false}, may be {@code null} + * @return one of the two input Strings + */ + public static String toString(final boolean bool, final String trueString, final String falseString) { + return bool ? trueString : falseString; + } + + // logical operations + // ---------------------------------------------------------------------- + /** + *
Performs an and on a set of booleans.
+ * + *+ * BooleanUtils.and(true, true) = true + * BooleanUtils.and(false, false) = false + * BooleanUtils.and(true, false) = false + * BooleanUtils.and(true, true, false) = false + * BooleanUtils.and(true, true, true) = true + *+ * + * @param array an array of {@code boolean}s + * @return {@code true} if the and is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + * @since 3.0.1 + */ + public static boolean and(final boolean... array) { + // Validates input + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + for (final boolean element : array) { + if (!element) { + return false; + } + } + return true; + } + + /** + *
Performs an and on an array of Booleans.
+ * + *+ * BooleanUtils.and(Boolean.TRUE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.and(Boolean.FALSE, Boolean.FALSE) = Boolean.FALSE + * BooleanUtils.and(Boolean.TRUE, Boolean.FALSE) = Boolean.FALSE + * BooleanUtils.and(Boolean.TRUE, Boolean.TRUE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.and(Boolean.FALSE, Boolean.FALSE, Boolean.TRUE) = Boolean.FALSE + * BooleanUtils.and(Boolean.TRUE, Boolean.FALSE, Boolean.TRUE) = Boolean.FALSE + *+ * + * @param array an array of {@code Boolean}s + * @return {@code true} if the and is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + * @throws IllegalArgumentException if {@code array} contains a {@code null} + * @since 3.0.1 + */ + public static Boolean and(final Boolean... array) { + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + try { + final boolean[] primitive = ArrayUtils.toPrimitive(array); + return and(primitive) ? Boolean.TRUE : Boolean.FALSE; + } catch (final NullPointerException ex) { + throw new IllegalArgumentException("The array must not contain any null elements"); + } + } + + /** + *
Performs an or on a set of booleans.
+ * + *+ * BooleanUtils.or(true, true) = true + * BooleanUtils.or(false, false) = false + * BooleanUtils.or(true, false) = true + * BooleanUtils.or(true, true, false) = true + * BooleanUtils.or(true, true, true) = true + * BooleanUtils.or(false, false, false) = false + *+ * + * @param array an array of {@code boolean}s + * @return {@code true} if the or is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + * @since 3.0.1 + */ + public static boolean or(final boolean... array) { + // Validates input + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + for (final boolean element : array) { + if (element) { + return true; + } + } + return false; + } + + /** + *
Performs an or on an array of Booleans.
+ * + *+ * BooleanUtils.or(Boolean.TRUE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.or(Boolean.FALSE, Boolean.FALSE) = Boolean.FALSE + * BooleanUtils.or(Boolean.TRUE, Boolean.FALSE) = Boolean.TRUE + * BooleanUtils.or(Boolean.TRUE, Boolean.TRUE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.or(Boolean.FALSE, Boolean.FALSE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.or(Boolean.TRUE, Boolean.FALSE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.or(Boolean.FALSE, Boolean.FALSE, Boolean.FALSE) = Boolean.FALSE + *+ * + * @param array an array of {@code Boolean}s + * @return {@code true} if the or is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + * @throws IllegalArgumentException if {@code array} contains a {@code null} + * @since 3.0.1 + */ + public static Boolean or(final Boolean... array) { + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + try { + final boolean[] primitive = ArrayUtils.toPrimitive(array); + return or(primitive) ? Boolean.TRUE : Boolean.FALSE; + } catch (final NullPointerException ex) { + throw new IllegalArgumentException("The array must not contain any null elements"); + } + } + + /** + *
Performs an xor on a set of booleans.
+ * + *+ * BooleanUtils.xor(true, true) = false + * BooleanUtils.xor(false, false) = false + * BooleanUtils.xor(true, false) = true + * BooleanUtils.xor(true, true) = false + * BooleanUtils.xor(false, false) = false + * BooleanUtils.xor(true, false) = true + *+ * + * @param array an array of {@code boolean}s + * @return {@code true} if the xor is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + */ + public static boolean xor(final boolean... array) { + // Validates input + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + + // false if the neutral element of the xor operator + boolean result = false; + for (final boolean element : array) { + result ^= element; + } + + return result; + } + + /** + *
Performs an xor on an array of Booleans.
+ * + *+ * BooleanUtils.xor(new Boolean[] { Boolean.TRUE, Boolean.TRUE }) = Boolean.FALSE + * BooleanUtils.xor(new Boolean[] { Boolean.FALSE, Boolean.FALSE }) = Boolean.FALSE + * BooleanUtils.xor(new Boolean[] { Boolean.TRUE, Boolean.FALSE }) = Boolean.TRUE + *+ * + * @param array an array of {@code Boolean}s + * @return {@code true} if the xor is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + * @throws IllegalArgumentException if {@code array} contains a {@code null} + */ + public static Boolean xor(final Boolean... array) { + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + try { + final boolean[] primitive = ArrayUtils.toPrimitive(array); + return xor(primitive) ? Boolean.TRUE : Boolean.FALSE; + } catch (final NullPointerException ex) { + throw new IllegalArgumentException("The array must not contain any null elements"); + } + } + + /** + *
Compares two {@code boolean} values. This is the same functionality as provided in Java 7.
+ * + * @param x the first {@code boolean} to compare + * @param y the second {@code boolean} to compare + * @return the value {@code 0} if {@code x == y}; + * a value less than {@code 0} if {@code !x && y}; and + * a value greater than {@code 0} if {@code x && !y} + * @since 3.4 + */ + public static int compare(boolean x, boolean y) { + if (x == y) { + return 0; + } + return x ? 1 : -1; + } + +} diff --git a/Java/commons-lang-BooleanUtils_175/metadata.json b/Java/commons-lang-BooleanUtils_175/metadata.json new file mode 100644 index 000000000..ce7d05b27 --- /dev/null +++ b/Java/commons-lang-BooleanUtils_175/metadata.json @@ -0,0 +1,21 @@ +{ + "language": "java", + "id": "commons-lang-BooleanUtils_175", + "buggyPath": ".", + "referencePath": null, + "buildCommand": "mvn package -V -B -Denforcer.skip=true -Dcheckstyle.skip=true -Dcobertura.skip=true -Drat.skip=true -Dlicense.skip=true -Dfindbugs.skip=true -Dgpg.skip=true -Dskip.npm=true -Dskip.gulp=true -Dskip.bower=true -Drat.numUnapprovedLicenses=100 -DskipTests=true -DskipITs=true -Dtest=None -DfailIfNoTests=false", + "testCommand": "mvn test -V -B -Denforcer.skip=true -Dcheckstyle.skip=true -Dcobertura.skip=true -Drat.skip=true -Dlicense.skip=true -Dfindbugs.skip=true -Dgpg.skip=true -Dskip.npm=true -Dskip.gulp=true -Dskip.bower=true -Drat.numUnapprovedLicenses=100", + "categories": [ + "safety", + "npe" + ], + "npe": { + "filepath": "src/main/java/org/apache/commons/lang3/BooleanUtils.java", + "line": 192, + "npe_method": "toBooleanDefaultIfNull", + "deref_field": "bool", + "npe_class": "BooleanUtils", + "repo": "commons-lang", + "bug_id": "BooleanUtils_175" + } +} diff --git a/Java/commons-lang-BooleanUtils_175/npe.json b/Java/commons-lang-BooleanUtils_175/npe.json new file mode 100644 index 000000000..ff13ba81c --- /dev/null +++ b/Java/commons-lang-BooleanUtils_175/npe.json @@ -0,0 +1,7 @@ +{ + "filepath": "src/main/java/org/apache/commons/lang3/BooleanUtils.java", + "line": 192, + "npe_method": "toBooleanDefaultIfNull", + "deref_field": "bool", + "npe_class": "BooleanUtils" +} \ No newline at end of file diff --git a/Java/commons-lang-BooleanUtils_238/Dockerfile b/Java/commons-lang-BooleanUtils_238/Dockerfile new file mode 100644 index 000000000..7b7fbe349 --- /dev/null +++ b/Java/commons-lang-BooleanUtils_238/Dockerfile @@ -0,0 +1,18 @@ +FROM ghcr.io/kupl/starlab-benchmarks/java-base:commons-lang + +ENV TZ=Asia/Seoul + +COPY ./metadata.json . +COPY ./npe.json . +COPY ./buggy.java /tmp/buggy.java +RUN export BUGGY_PATH=$(cat metadata.json | jq -r ".npe.filepath") \ + && export BUGGY_LINE=$(cat metadata.json | jq -r ".npe.line") \ + && export BUGGY_MTHD=$(cat metadata.json | jq -r ".npe.npe_method") \ + && mv /tmp/buggy.java $BUGGY_PATH \ + && echo "[{\"filepath\": \"$BUGGY_PATH\", \"line\": $BUGGY_LINE, \"method_name\": \"$BUGGY_MTHD\"}]" | jq . > traces.json + +RUN git init . && git add -A + +RUN $(cat metadata.json | jq -r ".buildCommand") + +RUN $(cat metadata.json | jq -r ".testCommand"); if [ $? -eq 0 ]; then exit 1; fi diff --git a/Java/commons-lang-BooleanUtils_238/buggy.java b/Java/commons-lang-BooleanUtils_238/buggy.java new file mode 100644 index 000000000..6d458faf3 --- /dev/null +++ b/Java/commons-lang-BooleanUtils_238/buggy.java @@ -0,0 +1,1123 @@ +/* + * 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; + +import org.apache.commons.lang3.math.NumberUtils; + +/** + *Operations on boolean primitives and Boolean objects.
+ * + *This class tries to handle {@code null} input gracefully. + * An exception will not be thrown for a {@code null} input. + * Each method documents its behaviour in more detail.
+ * + *#ThreadSafe#
+ * @since 2.0 + */ +public class BooleanUtils { + + /** + *{@code BooleanUtils} instances should NOT be constructed in standard programming. + * Instead, the class should be used as {@code BooleanUtils.negate(true);}.
+ * + *This constructor is public to permit tools that require a JavaBean instance + * to operate.
+ */ + public BooleanUtils() { + super(); + } + + // Boolean utilities + //-------------------------------------------------------------------------- + /** + *Negates the specified boolean.
+ * + *If {@code null} is passed in, {@code null} will be returned.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.negate(Boolean.TRUE) = Boolean.FALSE; + * BooleanUtils.negate(Boolean.FALSE) = Boolean.TRUE; + * BooleanUtils.negate(null) = null; + *+ * + * @param bool the Boolean to negate, may be null + * @return the negated Boolean, or {@code null} if {@code null} input + */ + public static Boolean negate(final Boolean bool) { + if (bool == null) { + return null; + } + return bool.booleanValue() ? Boolean.FALSE : Boolean.TRUE; + } + + // boolean Boolean methods + //----------------------------------------------------------------------- + /** + *
Checks if a {@code Boolean} value is {@code true}, + * handling {@code null} by returning {@code false}.
+ * + *+ * BooleanUtils.isTrue(Boolean.TRUE) = true + * BooleanUtils.isTrue(Boolean.FALSE) = false + * BooleanUtils.isTrue(null) = false + *+ * + * @param bool the boolean to check, null returns {@code false} + * @return {@code true} only if the input is non-null and true + * @since 2.1 + */ + public static boolean isTrue(final Boolean bool) { + return Boolean.TRUE.equals(bool); + } + + /** + *
Checks if a {@code Boolean} value is not {@code true}, + * handling {@code null} by returning {@code true}.
+ * + *+ * BooleanUtils.isNotTrue(Boolean.TRUE) = false + * BooleanUtils.isNotTrue(Boolean.FALSE) = true + * BooleanUtils.isNotTrue(null) = true + *+ * + * @param bool the boolean to check, null returns {@code true} + * @return {@code true} if the input is null or false + * @since 2.3 + */ + public static boolean isNotTrue(final Boolean bool) { + return !isTrue(bool); + } + + /** + *
Checks if a {@code Boolean} value is {@code false}, + * handling {@code null} by returning {@code false}.
+ * + *+ * BooleanUtils.isFalse(Boolean.TRUE) = false + * BooleanUtils.isFalse(Boolean.FALSE) = true + * BooleanUtils.isFalse(null) = false + *+ * + * @param bool the boolean to check, null returns {@code false} + * @return {@code true} only if the input is non-null and false + * @since 2.1 + */ + public static boolean isFalse(final Boolean bool) { + return Boolean.FALSE.equals(bool); + } + + /** + *
Checks if a {@code Boolean} value is not {@code false}, + * handling {@code null} by returning {@code true}.
+ * + *+ * BooleanUtils.isNotFalse(Boolean.TRUE) = true + * BooleanUtils.isNotFalse(Boolean.FALSE) = false + * BooleanUtils.isNotFalse(null) = true + *+ * + * @param bool the boolean to check, null returns {@code true} + * @return {@code true} if the input is null or true + * @since 2.3 + */ + public static boolean isNotFalse(final Boolean bool) { + return !isFalse(bool); + } + + //----------------------------------------------------------------------- + /** + *
Converts a Boolean to a boolean handling {@code null} + * by returning {@code false}.
+ * + *+ * BooleanUtils.toBoolean(Boolean.TRUE) = true + * BooleanUtils.toBoolean(Boolean.FALSE) = false + * BooleanUtils.toBoolean(null) = false + *+ * + * @param bool the boolean to convert + * @return {@code true} or {@code false}, {@code null} returns {@code false} + */ + public static boolean toBoolean(final Boolean bool) { + return bool != null && bool.booleanValue(); + } + + /** + *
Converts a Boolean to a boolean handling {@code null}.
+ * + *+ * BooleanUtils.toBooleanDefaultIfNull(Boolean.TRUE, false) = true + * BooleanUtils.toBooleanDefaultIfNull(Boolean.FALSE, true) = false + * BooleanUtils.toBooleanDefaultIfNull(null, true) = true + *+ * + * @param bool the boolean to convert + * @param valueIfNull the boolean value to return if {@code null} + * @return {@code true} or {@code false} + */ + public static boolean toBooleanDefaultIfNull(final Boolean bool, final boolean valueIfNull) { + if (bool == null) { + return valueIfNull; + } + return bool.booleanValue(); + } + + // Integer to Boolean methods + //----------------------------------------------------------------------- + /** + *
Converts an int to a boolean using the convention that {@code zero} + * is {@code false}.
+ * + *+ * BooleanUtils.toBoolean(0) = false + * BooleanUtils.toBoolean(1) = true + * BooleanUtils.toBoolean(2) = true + *+ * + * @param value the int to convert + * @return {@code true} if non-zero, {@code false} + * if zero + */ + public static boolean toBoolean(final int value) { + return value != 0; + } + + /** + *
Converts an int to a Boolean using the convention that {@code zero} + * is {@code false}.
+ * + *+ * BooleanUtils.toBoolean(0) = Boolean.FALSE + * BooleanUtils.toBoolean(1) = Boolean.TRUE + * BooleanUtils.toBoolean(2) = Boolean.TRUE + *+ * + * @param value the int to convert + * @return Boolean.TRUE if non-zero, Boolean.FALSE if zero, + * {@code null} if {@code null} + */ + public static Boolean toBooleanObject(final int value) { + return value == 0 ? Boolean.FALSE : Boolean.TRUE; + } + + /** + *
Converts an Integer to a Boolean using the convention that {@code zero} + * is {@code false}.
+ * + *{@code null} will be converted to {@code null}.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.toBoolean(Integer.valueOf(0)) = Boolean.FALSE + * BooleanUtils.toBoolean(Integer.valueOf(1)) = Boolean.TRUE + * BooleanUtils.toBoolean(Integer.valueOf(null)) = null + *+ * + * @param value the Integer to convert + * @return Boolean.TRUE if non-zero, Boolean.FALSE if zero, + * {@code null} if {@code null} input + */ +/** + *
Converts an Integer to a Boolean using the convention that {@code zero} + * is {@code false}.
+ * + *{@code null} will be converted to {@code null}.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.toBoolean(Integer.valueOf(0)) = Boolean.FALSE + * BooleanUtils.toBoolean(Integer.valueOf(1)) = Boolean.TRUE + * BooleanUtils.toBoolean(Integer.valueOf(null)) = null + *+ * + * @param value + * the Integer to convert + * @return Boolean.TRUE if non-zero, Boolean.FALSE if zero, +{@code null} if {@code null} input + */ +public static java.lang.Boolean toBooleanObject(final java.lang.Integer value) { + { + return /* NPEX_NULL_EXP */ + value.intValue() == 0 ? java.lang.Boolean.FALSE : java.lang.Boolean.TRUE; + } +} + + /** + *
Converts an int to a boolean specifying the conversion values.
+ * + *+ * BooleanUtils.toBoolean(0, 1, 0) = false + * BooleanUtils.toBoolean(1, 1, 0) = true + * BooleanUtils.toBoolean(2, 1, 2) = false + * BooleanUtils.toBoolean(2, 2, 0) = true + *+ * + * @param value the Integer to convert + * @param trueValue the value to match for {@code true} + * @param falseValue the value to match for {@code false} + * @return {@code true} or {@code false} + * @throws IllegalArgumentException if no match + */ + public static boolean toBoolean(final int value, final int trueValue, final int falseValue) { + if (value == trueValue) { + return true; + } + if (value == falseValue) { + return false; + } + // no match + throw new IllegalArgumentException("The Integer did not match either specified value"); + } + + /** + *
Converts an Integer to a boolean specifying the conversion values.
+ * + *+ * BooleanUtils.toBoolean(Integer.valueOf(0), Integer.valueOf(1), Integer.valueOf(0)) = false + * BooleanUtils.toBoolean(Integer.valueOf(1), Integer.valueOf(1), Integer.valueOf(0)) = true + * BooleanUtils.toBoolean(Integer.valueOf(2), Integer.valueOf(1), Integer.valueOf(2)) = false + * BooleanUtils.toBoolean(Integer.valueOf(2), Integer.valueOf(2), Integer.valueOf(0)) = true + * BooleanUtils.toBoolean(null, null, Integer.valueOf(0)) = true + *+ * + * @param value the Integer to convert + * @param trueValue the value to match for {@code true}, may be {@code null} + * @param falseValue the value to match for {@code false}, may be {@code null} + * @return {@code true} or {@code false} + * @throws IllegalArgumentException if no match + */ + public static boolean toBoolean(final Integer value, final Integer trueValue, final Integer falseValue) { + if (value == null) { + if (trueValue == null) { + return true; + } + if (falseValue == null) { + return false; + } + } else if (value.equals(trueValue)) { + return true; + } else if (value.equals(falseValue)) { + return false; + } + // no match + throw new IllegalArgumentException("The Integer did not match either specified value"); + } + + /** + *
Converts an int to a Boolean specifying the conversion values.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.toBooleanObject(0, 0, 2, 3) = Boolean.TRUE + * BooleanUtils.toBooleanObject(2, 1, 2, 3) = Boolean.FALSE + * BooleanUtils.toBooleanObject(3, 1, 2, 3) = null + *+ * + * @param value the Integer to convert + * @param trueValue the value to match for {@code true} + * @param falseValue the value to match for {@code false} + * @param nullValue the value to to match for {@code null} + * @return Boolean.TRUE, Boolean.FALSE, or {@code null} + * @throws IllegalArgumentException if no match + */ + public static Boolean toBooleanObject(final int value, final int trueValue, final int falseValue, final int nullValue) { + if (value == trueValue) { + return Boolean.TRUE; + } + if (value == falseValue) { + return Boolean.FALSE; + } + if (value == nullValue) { + return null; + } + // no match + throw new IllegalArgumentException("The Integer did not match any specified value"); + } + + /** + *
Converts an Integer to a Boolean specifying the conversion values.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.toBooleanObject(Integer.valueOf(0), Integer.valueOf(0), Integer.valueOf(2), Integer.valueOf(3)) = Boolean.TRUE + * BooleanUtils.toBooleanObject(Integer.valueOf(2), Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(3)) = Boolean.FALSE + * BooleanUtils.toBooleanObject(Integer.valueOf(3), Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(3)) = null + *+ * + * @param value the Integer to convert + * @param trueValue the value to match for {@code true}, may be {@code null} + * @param falseValue the value to match for {@code false}, may be {@code null} + * @param nullValue the value to to match for {@code null}, may be {@code null} + * @return Boolean.TRUE, Boolean.FALSE, or {@code null} + * @throws IllegalArgumentException if no match + */ + public static Boolean toBooleanObject(final Integer value, final Integer trueValue, final Integer falseValue, final Integer nullValue) { + if (value == null) { + if (trueValue == null) { + return Boolean.TRUE; + } + if (falseValue == null) { + return Boolean.FALSE; + } + if (nullValue == null) { + return null; + } + } else if (value.equals(trueValue)) { + return Boolean.TRUE; + } else if (value.equals(falseValue)) { + return Boolean.FALSE; + } else if (value.equals(nullValue)) { + return null; + } + // no match + throw new IllegalArgumentException("The Integer did not match any specified value"); + } + + // Boolean to Integer methods + //----------------------------------------------------------------------- + /** + *
Converts a boolean to an int using the convention that + * {@code zero} is {@code false}.
+ * + *+ * BooleanUtils.toInteger(true) = 1 + * BooleanUtils.toInteger(false) = 0 + *+ * + * @param bool the boolean to convert + * @return one if {@code true}, zero if {@code false} + */ + public static int toInteger(final boolean bool) { + return bool ? 1 : 0; + } + + /** + *
Converts a boolean to an Integer using the convention that + * {@code zero} is {@code false}.
+ * + *+ * BooleanUtils.toIntegerObject(true) = Integer.valueOf(1) + * BooleanUtils.toIntegerObject(false) = Integer.valueOf(0) + *+ * + * @param bool the boolean to convert + * @return one if {@code true}, zero if {@code false} + */ + public static Integer toIntegerObject(final boolean bool) { + return bool ? NumberUtils.INTEGER_ONE : NumberUtils.INTEGER_ZERO; + } + + /** + *
Converts a Boolean to a Integer using the convention that + * {@code zero} is {@code false}.
+ * + *{@code null} will be converted to {@code null}.
+ * + *+ * BooleanUtils.toIntegerObject(Boolean.TRUE) = Integer.valueOf(1) + * BooleanUtils.toIntegerObject(Boolean.FALSE) = Integer.valueOf(0) + *+ * + * @param bool the Boolean to convert + * @return one if Boolean.TRUE, zero if Boolean.FALSE, {@code null} if {@code null} + */ + public static Integer toIntegerObject(final Boolean bool) { + if (bool == null) { + return null; + } + return bool.booleanValue() ? NumberUtils.INTEGER_ONE : NumberUtils.INTEGER_ZERO; + } + + /** + *
Converts a boolean to an int specifying the conversion values.
+ * + *+ * BooleanUtils.toInteger(true, 1, 0) = 1 + * BooleanUtils.toInteger(false, 1, 0) = 0 + *+ * + * @param bool the to convert + * @param trueValue the value to return if {@code true} + * @param falseValue the value to return if {@code false} + * @return the appropriate value + */ + public static int toInteger(final boolean bool, final int trueValue, final int falseValue) { + return bool ? trueValue : falseValue; + } + + /** + *
Converts a Boolean to an int specifying the conversion values.
+ * + *+ * BooleanUtils.toInteger(Boolean.TRUE, 1, 0, 2) = 1 + * BooleanUtils.toInteger(Boolean.FALSE, 1, 0, 2) = 0 + * BooleanUtils.toInteger(null, 1, 0, 2) = 2 + *+ * + * @param bool the Boolean to convert + * @param trueValue the value to return if {@code true} + * @param falseValue the value to return if {@code false} + * @param nullValue the value to return if {@code null} + * @return the appropriate value + */ + public static int toInteger(final Boolean bool, final int trueValue, final int falseValue, final int nullValue) { + if (bool == null) { + return nullValue; + } + return bool.booleanValue() ? trueValue : falseValue; + } + + /** + *
Converts a boolean to an Integer specifying the conversion values.
+ * + *+ * BooleanUtils.toIntegerObject(true, Integer.valueOf(1), Integer.valueOf(0)) = Integer.valueOf(1) + * BooleanUtils.toIntegerObject(false, Integer.valueOf(1), Integer.valueOf(0)) = Integer.valueOf(0) + *+ * + * @param bool the to convert + * @param trueValue the value to return if {@code true}, may be {@code null} + * @param falseValue the value to return if {@code false}, may be {@code null} + * @return the appropriate value + */ + public static Integer toIntegerObject(final boolean bool, final Integer trueValue, final Integer falseValue) { + return bool ? trueValue : falseValue; + } + + /** + *
Converts a Boolean to an Integer specifying the conversion values.
+ * + *+ * BooleanUtils.toIntegerObject(Boolean.TRUE, Integer.valueOf(1), Integer.valueOf(0), Integer.valueOf(2)) = Integer.valueOf(1) + * BooleanUtils.toIntegerObject(Boolean.FALSE, Integer.valueOf(1), Integer.valueOf(0), Integer.valueOf(2)) = Integer.valueOf(0) + * BooleanUtils.toIntegerObject(null, Integer.valueOf(1), Integer.valueOf(0), Integer.valueOf(2)) = Integer.valueOf(2) + *+ * + * @param bool the Boolean to convert + * @param trueValue the value to return if {@code true}, may be {@code null} + * @param falseValue the value to return if {@code false}, may be {@code null} + * @param nullValue the value to return if {@code null}, may be {@code null} + * @return the appropriate value + */ + public static Integer toIntegerObject(final Boolean bool, final Integer trueValue, final Integer falseValue, final Integer nullValue) { + if (bool == null) { + return nullValue; + } + return bool.booleanValue() ? trueValue : falseValue; + } + + // String to Boolean methods + //----------------------------------------------------------------------- + /** + *
Converts a String to a Boolean.
+ * + *{@code 'true'}, {@code 'on'}, {@code 'y'}, {@code 't'} or {@code 'yes'} + * (case insensitive) will return {@code true}. + * {@code 'false'}, {@code 'off'}, {@code 'n'}, {@code 'f'} or {@code 'no'} + * (case insensitive) will return {@code false}. + * Otherwise, {@code null} is returned.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * // N.B. case is not significant + * BooleanUtils.toBooleanObject(null) = null + * BooleanUtils.toBooleanObject("true") = Boolean.TRUE + * BooleanUtils.toBooleanObject("T") = Boolean.TRUE // i.e. T[RUE] + * BooleanUtils.toBooleanObject("false") = Boolean.FALSE + * BooleanUtils.toBooleanObject("f") = Boolean.FALSE // i.e. f[alse] + * BooleanUtils.toBooleanObject("No") = Boolean.FALSE + * BooleanUtils.toBooleanObject("n") = Boolean.FALSE // i.e. n[o] + * BooleanUtils.toBooleanObject("on") = Boolean.TRUE + * BooleanUtils.toBooleanObject("ON") = Boolean.TRUE + * BooleanUtils.toBooleanObject("off") = Boolean.FALSE + * BooleanUtils.toBooleanObject("oFf") = Boolean.FALSE + * BooleanUtils.toBooleanObject("yes") = Boolean.TRUE + * BooleanUtils.toBooleanObject("Y") = Boolean.TRUE // i.e. Y[ES] + * BooleanUtils.toBooleanObject("blue") = null + * BooleanUtils.toBooleanObject("true ") = null // trailing space (too long) + * BooleanUtils.toBooleanObject("ono") = null // does not match on or no + *+ * + * @param str the String to check; upper and lower case are treated as the same + * @return the Boolean value of the string, {@code null} if no match or {@code null} input + */ + public static Boolean toBooleanObject(final String str) { + // Previously used equalsIgnoreCase, which was fast for interned 'true'. + // Non interned 'true' matched 15 times slower. + // + // Optimisation provides same performance as before for interned 'true'. + // Similar performance for null, 'false', and other strings not length 2/3/4. + // 'true'/'TRUE' match 4 times slower, 'tRUE'/'True' 7 times slower. + if (str == "true") { + return Boolean.TRUE; + } + if (str == null) { + return null; + } + switch (str.length()) { + case 1: { + final char ch0 = str.charAt(0); + if (ch0 == 'y' || ch0 == 'Y' || + ch0 == 't' || ch0 == 'T') { + return Boolean.TRUE; + } + if (ch0 == 'n' || ch0 == 'N' || + ch0 == 'f' || ch0 == 'F') { + return Boolean.FALSE; + } + break; + } + case 2: { + final char ch0 = str.charAt(0); + final char ch1 = str.charAt(1); + if ((ch0 == 'o' || ch0 == 'O') && + (ch1 == 'n' || ch1 == 'N') ) { + return Boolean.TRUE; + } + if ((ch0 == 'n' || ch0 == 'N') && + (ch1 == 'o' || ch1 == 'O') ) { + return Boolean.FALSE; + } + break; + } + case 3: { + final char ch0 = str.charAt(0); + final char ch1 = str.charAt(1); + final char ch2 = str.charAt(2); + if ((ch0 == 'y' || ch0 == 'Y') && + (ch1 == 'e' || ch1 == 'E') && + (ch2 == 's' || ch2 == 'S') ) { + return Boolean.TRUE; + } + if ((ch0 == 'o' || ch0 == 'O') && + (ch1 == 'f' || ch1 == 'F') && + (ch2 == 'f' || ch2 == 'F') ) { + return Boolean.FALSE; + } + break; + } + case 4: { + final char ch0 = str.charAt(0); + final char ch1 = str.charAt(1); + final char ch2 = str.charAt(2); + final char ch3 = str.charAt(3); + if ((ch0 == 't' || ch0 == 'T') && + (ch1 == 'r' || ch1 == 'R') && + (ch2 == 'u' || ch2 == 'U') && + (ch3 == 'e' || ch3 == 'E') ) { + return Boolean.TRUE; + } + break; + } + case 5: { + final char ch0 = str.charAt(0); + final char ch1 = str.charAt(1); + final char ch2 = str.charAt(2); + final char ch3 = str.charAt(3); + final char ch4 = str.charAt(4); + if ((ch0 == 'f' || ch0 == 'F') && + (ch1 == 'a' || ch1 == 'A') && + (ch2 == 'l' || ch2 == 'L') && + (ch3 == 's' || ch3 == 'S') && + (ch4 == 'e' || ch4 == 'E') ) { + return Boolean.FALSE; + } + break; + } + default: + break; + } + + return null; + } + + /** + *
Converts a String to a Boolean throwing an exception if no match.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.toBooleanObject("true", "true", "false", "null") = Boolean.TRUE + * BooleanUtils.toBooleanObject("false", "true", "false", "null") = Boolean.FALSE + * BooleanUtils.toBooleanObject("null", "true", "false", "null") = null + *+ * + * @param str the String to check + * @param trueString the String to match for {@code true} (case sensitive), may be {@code null} + * @param falseString the String to match for {@code false} (case sensitive), may be {@code null} + * @param nullString the String to match for {@code null} (case sensitive), may be {@code null} + * @return the Boolean value of the string, {@code null} if either the String matches {@code nullString} + * or if {@code null} input and {@code nullString} is {@code null} + * @throws IllegalArgumentException if the String doesn't match + */ + public static Boolean toBooleanObject(final String str, final String trueString, final String falseString, final String nullString) { + if (str == null) { + if (trueString == null) { + return Boolean.TRUE; + } + if (falseString == null) { + return Boolean.FALSE; + } + if (nullString == null) { + return null; + } + } else if (str.equals(trueString)) { + return Boolean.TRUE; + } else if (str.equals(falseString)) { + return Boolean.FALSE; + } else if (str.equals(nullString)) { + return null; + } + // no match + throw new IllegalArgumentException("The String did not match any specified value"); + } + + // String to boolean methods + //----------------------------------------------------------------------- + /** + *
Converts a String to a boolean (optimised for performance).
+ * + *{@code 'true'}, {@code 'on'}, {@code 'y'}, {@code 't'} or {@code 'yes'} + * (case insensitive) will return {@code true}. Otherwise, + * {@code false} is returned.
+ * + *This method performs 4 times faster (JDK1.4) than + * {@code Boolean.valueOf(String)}. However, this method accepts + * 'on' and 'yes', 't', 'y' as true values. + * + *
+ * BooleanUtils.toBoolean(null) = false + * BooleanUtils.toBoolean("true") = true + * BooleanUtils.toBoolean("TRUE") = true + * BooleanUtils.toBoolean("tRUe") = true + * BooleanUtils.toBoolean("on") = true + * BooleanUtils.toBoolean("yes") = true + * BooleanUtils.toBoolean("false") = false + * BooleanUtils.toBoolean("x gti") = false + * BooleanUtils.toBooleanObject("y") = true + * BooleanUtils.toBooleanObject("n") = false + * BooleanUtils.toBooleanObject("t") = true + * BooleanUtils.toBooleanObject("f") = false + *+ * + * @param str the String to check + * @return the boolean value of the string, {@code false} if no match or the String is null + */ + public static boolean toBoolean(final String str) { + return toBooleanObject(str) == Boolean.TRUE; + } + + /** + *
Converts a String to a Boolean throwing an exception if no match found.
+ * + *+ * BooleanUtils.toBoolean("true", "true", "false") = true + * BooleanUtils.toBoolean("false", "true", "false") = false + *+ * + * @param str the String to check + * @param trueString the String to match for {@code true} (case sensitive), may be {@code null} + * @param falseString the String to match for {@code false} (case sensitive), may be {@code null} + * @return the boolean value of the string + * @throws IllegalArgumentException if the String doesn't match + */ + public static boolean toBoolean(final String str, final String trueString, final String falseString) { + if (str == trueString) { + return true; + } else if (str == falseString) { + return false; + } else if (str != null) { + if (str.equals(trueString)) { + return true; + } else if (str.equals(falseString)) { + return false; + } + } + // no match + throw new IllegalArgumentException("The String did not match either specified value"); + } + + // Boolean to String methods + //----------------------------------------------------------------------- + /** + *
Converts a Boolean to a String returning {@code 'true'}, + * {@code 'false'}, or {@code null}.
+ * + *+ * BooleanUtils.toStringTrueFalse(Boolean.TRUE) = "true" + * BooleanUtils.toStringTrueFalse(Boolean.FALSE) = "false" + * BooleanUtils.toStringTrueFalse(null) = null; + *+ * + * @param bool the Boolean to check + * @return {@code 'true'}, {@code 'false'}, or {@code null} + */ + public static String toStringTrueFalse(final Boolean bool) { + return toString(bool, "true", "false", null); + } + + /** + *
Converts a Boolean to a String returning {@code 'on'}, + * {@code 'off'}, or {@code null}.
+ * + *+ * BooleanUtils.toStringOnOff(Boolean.TRUE) = "on" + * BooleanUtils.toStringOnOff(Boolean.FALSE) = "off" + * BooleanUtils.toStringOnOff(null) = null; + *+ * + * @param bool the Boolean to check + * @return {@code 'on'}, {@code 'off'}, or {@code null} + */ + public static String toStringOnOff(final Boolean bool) { + return toString(bool, "on", "off", null); + } + + /** + *
Converts a Boolean to a String returning {@code 'yes'}, + * {@code 'no'}, or {@code null}.
+ * + *+ * BooleanUtils.toStringYesNo(Boolean.TRUE) = "yes" + * BooleanUtils.toStringYesNo(Boolean.FALSE) = "no" + * BooleanUtils.toStringYesNo(null) = null; + *+ * + * @param bool the Boolean to check + * @return {@code 'yes'}, {@code 'no'}, or {@code null} + */ + public static String toStringYesNo(final Boolean bool) { + return toString(bool, "yes", "no", null); + } + + /** + *
Converts a Boolean to a String returning one of the input Strings.
+ * + *+ * BooleanUtils.toString(Boolean.TRUE, "true", "false", null) = "true" + * BooleanUtils.toString(Boolean.FALSE, "true", "false", null) = "false" + * BooleanUtils.toString(null, "true", "false", null) = null; + *+ * + * @param bool the Boolean to check + * @param trueString the String to return if {@code true}, may be {@code null} + * @param falseString the String to return if {@code false}, may be {@code null} + * @param nullString the String to return if {@code null}, may be {@code null} + * @return one of the three input Strings + */ + public static String toString(final Boolean bool, final String trueString, final String falseString, final String nullString) { + if (bool == null) { + return nullString; + } + return bool.booleanValue() ? trueString : falseString; + } + + // boolean to String methods + //----------------------------------------------------------------------- + /** + *
Converts a boolean to a String returning {@code 'true'} + * or {@code 'false'}.
+ * + *+ * BooleanUtils.toStringTrueFalse(true) = "true" + * BooleanUtils.toStringTrueFalse(false) = "false" + *+ * + * @param bool the Boolean to check + * @return {@code 'true'}, {@code 'false'}, or {@code null} + */ + public static String toStringTrueFalse(final boolean bool) { + return toString(bool, "true", "false"); + } + + /** + *
Converts a boolean to a String returning {@code 'on'} + * or {@code 'off'}.
+ * + *+ * BooleanUtils.toStringOnOff(true) = "on" + * BooleanUtils.toStringOnOff(false) = "off" + *+ * + * @param bool the Boolean to check + * @return {@code 'on'}, {@code 'off'}, or {@code null} + */ + public static String toStringOnOff(final boolean bool) { + return toString(bool, "on", "off"); + } + + /** + *
Converts a boolean to a String returning {@code 'yes'} + * or {@code 'no'}.
+ * + *+ * BooleanUtils.toStringYesNo(true) = "yes" + * BooleanUtils.toStringYesNo(false) = "no" + *+ * + * @param bool the Boolean to check + * @return {@code 'yes'}, {@code 'no'}, or {@code null} + */ + public static String toStringYesNo(final boolean bool) { + return toString(bool, "yes", "no"); + } + + /** + *
Converts a boolean to a String returning one of the input Strings.
+ * + *+ * BooleanUtils.toString(true, "true", "false") = "true" + * BooleanUtils.toString(false, "true", "false") = "false" + *+ * + * @param bool the Boolean to check + * @param trueString the String to return if {@code true}, may be {@code null} + * @param falseString the String to return if {@code false}, may be {@code null} + * @return one of the two input Strings + */ + public static String toString(final boolean bool, final String trueString, final String falseString) { + return bool ? trueString : falseString; + } + + // logical operations + // ---------------------------------------------------------------------- + /** + *
Performs an and on a set of booleans.
+ * + *+ * BooleanUtils.and(true, true) = true + * BooleanUtils.and(false, false) = false + * BooleanUtils.and(true, false) = false + * BooleanUtils.and(true, true, false) = false + * BooleanUtils.and(true, true, true) = true + *+ * + * @param array an array of {@code boolean}s + * @return {@code true} if the and is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + * @since 3.0.1 + */ + public static boolean and(final boolean... array) { + // Validates input + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + for (final boolean element : array) { + if (!element) { + return false; + } + } + return true; + } + + /** + *
Performs an and on an array of Booleans.
+ * + *+ * BooleanUtils.and(Boolean.TRUE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.and(Boolean.FALSE, Boolean.FALSE) = Boolean.FALSE + * BooleanUtils.and(Boolean.TRUE, Boolean.FALSE) = Boolean.FALSE + * BooleanUtils.and(Boolean.TRUE, Boolean.TRUE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.and(Boolean.FALSE, Boolean.FALSE, Boolean.TRUE) = Boolean.FALSE + * BooleanUtils.and(Boolean.TRUE, Boolean.FALSE, Boolean.TRUE) = Boolean.FALSE + *+ * + * @param array an array of {@code Boolean}s + * @return {@code true} if the and is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + * @throws IllegalArgumentException if {@code array} contains a {@code null} + * @since 3.0.1 + */ + public static Boolean and(final Boolean... array) { + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + try { + final boolean[] primitive = ArrayUtils.toPrimitive(array); + return and(primitive) ? Boolean.TRUE : Boolean.FALSE; + } catch (final NullPointerException ex) { + throw new IllegalArgumentException("The array must not contain any null elements"); + } + } + + /** + *
Performs an or on a set of booleans.
+ * + *+ * BooleanUtils.or(true, true) = true + * BooleanUtils.or(false, false) = false + * BooleanUtils.or(true, false) = true + * BooleanUtils.or(true, true, false) = true + * BooleanUtils.or(true, true, true) = true + * BooleanUtils.or(false, false, false) = false + *+ * + * @param array an array of {@code boolean}s + * @return {@code true} if the or is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + * @since 3.0.1 + */ + public static boolean or(final boolean... array) { + // Validates input + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + for (final boolean element : array) { + if (element) { + return true; + } + } + return false; + } + + /** + *
Performs an or on an array of Booleans.
+ * + *+ * BooleanUtils.or(Boolean.TRUE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.or(Boolean.FALSE, Boolean.FALSE) = Boolean.FALSE + * BooleanUtils.or(Boolean.TRUE, Boolean.FALSE) = Boolean.TRUE + * BooleanUtils.or(Boolean.TRUE, Boolean.TRUE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.or(Boolean.FALSE, Boolean.FALSE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.or(Boolean.TRUE, Boolean.FALSE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.or(Boolean.FALSE, Boolean.FALSE, Boolean.FALSE) = Boolean.FALSE + *+ * + * @param array an array of {@code Boolean}s + * @return {@code true} if the or is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + * @throws IllegalArgumentException if {@code array} contains a {@code null} + * @since 3.0.1 + */ + public static Boolean or(final Boolean... array) { + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + try { + final boolean[] primitive = ArrayUtils.toPrimitive(array); + return or(primitive) ? Boolean.TRUE : Boolean.FALSE; + } catch (final NullPointerException ex) { + throw new IllegalArgumentException("The array must not contain any null elements"); + } + } + + /** + *
Performs an xor on a set of booleans.
+ * + *+ * BooleanUtils.xor(true, true) = false + * BooleanUtils.xor(false, false) = false + * BooleanUtils.xor(true, false) = true + * BooleanUtils.xor(true, true) = false + * BooleanUtils.xor(false, false) = false + * BooleanUtils.xor(true, false) = true + *+ * + * @param array an array of {@code boolean}s + * @return {@code true} if the xor is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + */ + public static boolean xor(final boolean... array) { + // Validates input + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + + // false if the neutral element of the xor operator + boolean result = false; + for (final boolean element : array) { + result ^= element; + } + + return result; + } + + /** + *
Performs an xor on an array of Booleans.
+ * + *+ * BooleanUtils.xor(new Boolean[] { Boolean.TRUE, Boolean.TRUE }) = Boolean.FALSE + * BooleanUtils.xor(new Boolean[] { Boolean.FALSE, Boolean.FALSE }) = Boolean.FALSE + * BooleanUtils.xor(new Boolean[] { Boolean.TRUE, Boolean.FALSE }) = Boolean.TRUE + *+ * + * @param array an array of {@code Boolean}s + * @return {@code true} if the xor is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + * @throws IllegalArgumentException if {@code array} contains a {@code null} + */ + public static Boolean xor(final Boolean... array) { + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + try { + final boolean[] primitive = ArrayUtils.toPrimitive(array); + return xor(primitive) ? Boolean.TRUE : Boolean.FALSE; + } catch (final NullPointerException ex) { + throw new IllegalArgumentException("The array must not contain any null elements"); + } + } + + /** + *
Compares two {@code boolean} values. This is the same functionality as provided in Java 7.
+ * + * @param x the first {@code boolean} to compare + * @param y the second {@code boolean} to compare + * @return the value {@code 0} if {@code x == y}; + * a value less than {@code 0} if {@code !x && y}; and + * a value greater than {@code 0} if {@code x && !y} + * @since 3.4 + */ + public static int compare(boolean x, boolean y) { + if (x == y) { + return 0; + } + return x ? 1 : -1; + } + +} diff --git a/Java/commons-lang-BooleanUtils_238/metadata.json b/Java/commons-lang-BooleanUtils_238/metadata.json new file mode 100644 index 000000000..e3d80e1d2 --- /dev/null +++ b/Java/commons-lang-BooleanUtils_238/metadata.json @@ -0,0 +1,21 @@ +{ + "language": "java", + "id": "commons-lang-BooleanUtils_238", + "buggyPath": ".", + "referencePath": null, + "buildCommand": "mvn package -V -B -Denforcer.skip=true -Dcheckstyle.skip=true -Dcobertura.skip=true -Drat.skip=true -Dlicense.skip=true -Dfindbugs.skip=true -Dgpg.skip=true -Dskip.npm=true -Dskip.gulp=true -Dskip.bower=true -Drat.numUnapprovedLicenses=100 -DskipTests=true -DskipITs=true -Dtest=None -DfailIfNoTests=false", + "testCommand": "mvn test -V -B -Denforcer.skip=true -Dcheckstyle.skip=true -Dcobertura.skip=true -Drat.skip=true -Dlicense.skip=true -Dfindbugs.skip=true -Dgpg.skip=true -Dskip.npm=true -Dskip.gulp=true -Dskip.bower=true -Drat.numUnapprovedLicenses=100", + "categories": [ + "safety", + "npe" + ], + "npe": { + "filepath": "src/main/java/org/apache/commons/lang3/BooleanUtils.java", + "line": 259, + "npe_method": "toBooleanObject", + "deref_field": "value", + "npe_class": "BooleanUtils", + "repo": "commons-lang", + "bug_id": "BooleanUtils_238" + } +} diff --git a/Java/commons-lang-BooleanUtils_238/npe.json b/Java/commons-lang-BooleanUtils_238/npe.json new file mode 100644 index 000000000..29947022d --- /dev/null +++ b/Java/commons-lang-BooleanUtils_238/npe.json @@ -0,0 +1,7 @@ +{ + "filepath": "src/main/java/org/apache/commons/lang3/BooleanUtils.java", + "line": 259, + "npe_method": "toBooleanObject", + "deref_field": "value", + "npe_class": "BooleanUtils" +} \ No newline at end of file diff --git a/Java/commons-lang-BooleanUtils_289/Dockerfile b/Java/commons-lang-BooleanUtils_289/Dockerfile new file mode 100644 index 000000000..7b7fbe349 --- /dev/null +++ b/Java/commons-lang-BooleanUtils_289/Dockerfile @@ -0,0 +1,18 @@ +FROM ghcr.io/kupl/starlab-benchmarks/java-base:commons-lang + +ENV TZ=Asia/Seoul + +COPY ./metadata.json . +COPY ./npe.json . +COPY ./buggy.java /tmp/buggy.java +RUN export BUGGY_PATH=$(cat metadata.json | jq -r ".npe.filepath") \ + && export BUGGY_LINE=$(cat metadata.json | jq -r ".npe.line") \ + && export BUGGY_MTHD=$(cat metadata.json | jq -r ".npe.npe_method") \ + && mv /tmp/buggy.java $BUGGY_PATH \ + && echo "[{\"filepath\": \"$BUGGY_PATH\", \"line\": $BUGGY_LINE, \"method_name\": \"$BUGGY_MTHD\"}]" | jq . > traces.json + +RUN git init . && git add -A + +RUN $(cat metadata.json | jq -r ".buildCommand") + +RUN $(cat metadata.json | jq -r ".testCommand"); if [ $? -eq 0 ]; then exit 1; fi diff --git a/Java/commons-lang-BooleanUtils_289/buggy.java b/Java/commons-lang-BooleanUtils_289/buggy.java new file mode 100644 index 000000000..4cd32dc30 --- /dev/null +++ b/Java/commons-lang-BooleanUtils_289/buggy.java @@ -0,0 +1,1121 @@ +/* + * 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; + +import org.apache.commons.lang3.math.NumberUtils; + +/** + *Operations on boolean primitives and Boolean objects.
+ * + *This class tries to handle {@code null} input gracefully. + * An exception will not be thrown for a {@code null} input. + * Each method documents its behaviour in more detail.
+ * + *#ThreadSafe#
+ * @since 2.0 + */ +public class BooleanUtils { + + /** + *{@code BooleanUtils} instances should NOT be constructed in standard programming. + * Instead, the class should be used as {@code BooleanUtils.negate(true);}.
+ * + *This constructor is public to permit tools that require a JavaBean instance + * to operate.
+ */ + public BooleanUtils() { + super(); + } + + // Boolean utilities + //-------------------------------------------------------------------------- + /** + *Negates the specified boolean.
+ * + *If {@code null} is passed in, {@code null} will be returned.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.negate(Boolean.TRUE) = Boolean.FALSE; + * BooleanUtils.negate(Boolean.FALSE) = Boolean.TRUE; + * BooleanUtils.negate(null) = null; + *+ * + * @param bool the Boolean to negate, may be null + * @return the negated Boolean, or {@code null} if {@code null} input + */ + public static Boolean negate(final Boolean bool) { + if (bool == null) { + return null; + } + return bool.booleanValue() ? Boolean.FALSE : Boolean.TRUE; + } + + // boolean Boolean methods + //----------------------------------------------------------------------- + /** + *
Checks if a {@code Boolean} value is {@code true}, + * handling {@code null} by returning {@code false}.
+ * + *+ * BooleanUtils.isTrue(Boolean.TRUE) = true + * BooleanUtils.isTrue(Boolean.FALSE) = false + * BooleanUtils.isTrue(null) = false + *+ * + * @param bool the boolean to check, null returns {@code false} + * @return {@code true} only if the input is non-null and true + * @since 2.1 + */ + public static boolean isTrue(final Boolean bool) { + return Boolean.TRUE.equals(bool); + } + + /** + *
Checks if a {@code Boolean} value is not {@code true}, + * handling {@code null} by returning {@code true}.
+ * + *+ * BooleanUtils.isNotTrue(Boolean.TRUE) = false + * BooleanUtils.isNotTrue(Boolean.FALSE) = true + * BooleanUtils.isNotTrue(null) = true + *+ * + * @param bool the boolean to check, null returns {@code true} + * @return {@code true} if the input is null or false + * @since 2.3 + */ + public static boolean isNotTrue(final Boolean bool) { + return !isTrue(bool); + } + + /** + *
Checks if a {@code Boolean} value is {@code false}, + * handling {@code null} by returning {@code false}.
+ * + *+ * BooleanUtils.isFalse(Boolean.TRUE) = false + * BooleanUtils.isFalse(Boolean.FALSE) = true + * BooleanUtils.isFalse(null) = false + *+ * + * @param bool the boolean to check, null returns {@code false} + * @return {@code true} only if the input is non-null and false + * @since 2.1 + */ + public static boolean isFalse(final Boolean bool) { + return Boolean.FALSE.equals(bool); + } + + /** + *
Checks if a {@code Boolean} value is not {@code false}, + * handling {@code null} by returning {@code true}.
+ * + *+ * BooleanUtils.isNotFalse(Boolean.TRUE) = true + * BooleanUtils.isNotFalse(Boolean.FALSE) = false + * BooleanUtils.isNotFalse(null) = true + *+ * + * @param bool the boolean to check, null returns {@code true} + * @return {@code true} if the input is null or true + * @since 2.3 + */ + public static boolean isNotFalse(final Boolean bool) { + return !isFalse(bool); + } + + //----------------------------------------------------------------------- + /** + *
Converts a Boolean to a boolean handling {@code null} + * by returning {@code false}.
+ * + *+ * BooleanUtils.toBoolean(Boolean.TRUE) = true + * BooleanUtils.toBoolean(Boolean.FALSE) = false + * BooleanUtils.toBoolean(null) = false + *+ * + * @param bool the boolean to convert + * @return {@code true} or {@code false}, {@code null} returns {@code false} + */ + public static boolean toBoolean(final Boolean bool) { + return bool != null && bool.booleanValue(); + } + + /** + *
Converts a Boolean to a boolean handling {@code null}.
+ * + *+ * BooleanUtils.toBooleanDefaultIfNull(Boolean.TRUE, false) = true + * BooleanUtils.toBooleanDefaultIfNull(Boolean.FALSE, true) = false + * BooleanUtils.toBooleanDefaultIfNull(null, true) = true + *+ * + * @param bool the boolean to convert + * @param valueIfNull the boolean value to return if {@code null} + * @return {@code true} or {@code false} + */ + public static boolean toBooleanDefaultIfNull(final Boolean bool, final boolean valueIfNull) { + if (bool == null) { + return valueIfNull; + } + return bool.booleanValue(); + } + + // Integer to Boolean methods + //----------------------------------------------------------------------- + /** + *
Converts an int to a boolean using the convention that {@code zero} + * is {@code false}.
+ * + *+ * BooleanUtils.toBoolean(0) = false + * BooleanUtils.toBoolean(1) = true + * BooleanUtils.toBoolean(2) = true + *+ * + * @param value the int to convert + * @return {@code true} if non-zero, {@code false} + * if zero + */ + public static boolean toBoolean(final int value) { + return value != 0; + } + + /** + *
Converts an int to a Boolean using the convention that {@code zero} + * is {@code false}.
+ * + *+ * BooleanUtils.toBoolean(0) = Boolean.FALSE + * BooleanUtils.toBoolean(1) = Boolean.TRUE + * BooleanUtils.toBoolean(2) = Boolean.TRUE + *+ * + * @param value the int to convert + * @return Boolean.TRUE if non-zero, Boolean.FALSE if zero, + * {@code null} if {@code null} + */ + public static Boolean toBooleanObject(final int value) { + return value == 0 ? Boolean.FALSE : Boolean.TRUE; + } + + /** + *
Converts an Integer to a Boolean using the convention that {@code zero} + * is {@code false}.
+ * + *{@code null} will be converted to {@code null}.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.toBoolean(Integer.valueOf(0)) = Boolean.FALSE + * BooleanUtils.toBoolean(Integer.valueOf(1)) = Boolean.TRUE + * BooleanUtils.toBoolean(Integer.valueOf(null)) = null + *+ * + * @param value the Integer to convert + * @return Boolean.TRUE if non-zero, Boolean.FALSE if zero, + * {@code null} if {@code null} input + */ + public static Boolean toBooleanObject(final Integer value) { + if (value == null) { + return null; + } + return value.intValue() == 0 ? Boolean.FALSE : Boolean.TRUE; + } + + /** + *
Converts an int to a boolean specifying the conversion values.
+ * + *+ * BooleanUtils.toBoolean(0, 1, 0) = false + * BooleanUtils.toBoolean(1, 1, 0) = true + * BooleanUtils.toBoolean(2, 1, 2) = false + * BooleanUtils.toBoolean(2, 2, 0) = true + *+ * + * @param value the Integer to convert + * @param trueValue the value to match for {@code true} + * @param falseValue the value to match for {@code false} + * @return {@code true} or {@code false} + * @throws IllegalArgumentException if no match + */ + public static boolean toBoolean(final int value, final int trueValue, final int falseValue) { + if (value == trueValue) { + return true; + } + if (value == falseValue) { + return false; + } + // no match + throw new IllegalArgumentException("The Integer did not match either specified value"); + } + + /** + *
Converts an Integer to a boolean specifying the conversion values.
+ * + *+ * BooleanUtils.toBoolean(Integer.valueOf(0), Integer.valueOf(1), Integer.valueOf(0)) = false + * BooleanUtils.toBoolean(Integer.valueOf(1), Integer.valueOf(1), Integer.valueOf(0)) = true + * BooleanUtils.toBoolean(Integer.valueOf(2), Integer.valueOf(1), Integer.valueOf(2)) = false + * BooleanUtils.toBoolean(Integer.valueOf(2), Integer.valueOf(2), Integer.valueOf(0)) = true + * BooleanUtils.toBoolean(null, null, Integer.valueOf(0)) = true + *+ * + * @param value the Integer to convert + * @param trueValue the value to match for {@code true}, may be {@code null} + * @param falseValue the value to match for {@code false}, may be {@code null} + * @return {@code true} or {@code false} + * @throws IllegalArgumentException if no match + */ +/** + *
Converts an Integer to a boolean specifying the conversion values.
+ * + *+ * BooleanUtils.toBoolean(Integer.valueOf(0), Integer.valueOf(1), Integer.valueOf(0)) = false + * BooleanUtils.toBoolean(Integer.valueOf(1), Integer.valueOf(1), Integer.valueOf(0)) = true + * BooleanUtils.toBoolean(Integer.valueOf(2), Integer.valueOf(1), Integer.valueOf(2)) = false + * BooleanUtils.toBoolean(Integer.valueOf(2), Integer.valueOf(2), Integer.valueOf(0)) = true + * BooleanUtils.toBoolean(null, null, Integer.valueOf(0)) = true + *+ * + * @param value + * the Integer to convert + * @param trueValue + * the value to match for {@code true}, may be {@code null} + * @param falseValue + * the value to match for {@code false}, may be {@code null} + * @return {@code true} or {@code false} + * @throws IllegalArgumentException + * if no match + */ +public static boolean toBoolean(final java.lang.Integer value, final java.lang.Integer trueValue, final java.lang.Integer falseValue) { + { + if (/* NPEX_NULL_EXP */ + value.equals(trueValue)) { + return true; + } else if (value.equals(falseValue)) { + return false; + } + } + // no match + throw new java.lang.IllegalArgumentException("The Integer did not match either specified value"); +} + + /** + *
Converts an int to a Boolean specifying the conversion values.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.toBooleanObject(0, 0, 2, 3) = Boolean.TRUE + * BooleanUtils.toBooleanObject(2, 1, 2, 3) = Boolean.FALSE + * BooleanUtils.toBooleanObject(3, 1, 2, 3) = null + *+ * + * @param value the Integer to convert + * @param trueValue the value to match for {@code true} + * @param falseValue the value to match for {@code false} + * @param nullValue the value to to match for {@code null} + * @return Boolean.TRUE, Boolean.FALSE, or {@code null} + * @throws IllegalArgumentException if no match + */ + public static Boolean toBooleanObject(final int value, final int trueValue, final int falseValue, final int nullValue) { + if (value == trueValue) { + return Boolean.TRUE; + } + if (value == falseValue) { + return Boolean.FALSE; + } + if (value == nullValue) { + return null; + } + // no match + throw new IllegalArgumentException("The Integer did not match any specified value"); + } + + /** + *
Converts an Integer to a Boolean specifying the conversion values.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.toBooleanObject(Integer.valueOf(0), Integer.valueOf(0), Integer.valueOf(2), Integer.valueOf(3)) = Boolean.TRUE + * BooleanUtils.toBooleanObject(Integer.valueOf(2), Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(3)) = Boolean.FALSE + * BooleanUtils.toBooleanObject(Integer.valueOf(3), Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(3)) = null + *+ * + * @param value the Integer to convert + * @param trueValue the value to match for {@code true}, may be {@code null} + * @param falseValue the value to match for {@code false}, may be {@code null} + * @param nullValue the value to to match for {@code null}, may be {@code null} + * @return Boolean.TRUE, Boolean.FALSE, or {@code null} + * @throws IllegalArgumentException if no match + */ + public static Boolean toBooleanObject(final Integer value, final Integer trueValue, final Integer falseValue, final Integer nullValue) { + if (value == null) { + if (trueValue == null) { + return Boolean.TRUE; + } + if (falseValue == null) { + return Boolean.FALSE; + } + if (nullValue == null) { + return null; + } + } else if (value.equals(trueValue)) { + return Boolean.TRUE; + } else if (value.equals(falseValue)) { + return Boolean.FALSE; + } else if (value.equals(nullValue)) { + return null; + } + // no match + throw new IllegalArgumentException("The Integer did not match any specified value"); + } + + // Boolean to Integer methods + //----------------------------------------------------------------------- + /** + *
Converts a boolean to an int using the convention that + * {@code zero} is {@code false}.
+ * + *+ * BooleanUtils.toInteger(true) = 1 + * BooleanUtils.toInteger(false) = 0 + *+ * + * @param bool the boolean to convert + * @return one if {@code true}, zero if {@code false} + */ + public static int toInteger(final boolean bool) { + return bool ? 1 : 0; + } + + /** + *
Converts a boolean to an Integer using the convention that + * {@code zero} is {@code false}.
+ * + *+ * BooleanUtils.toIntegerObject(true) = Integer.valueOf(1) + * BooleanUtils.toIntegerObject(false) = Integer.valueOf(0) + *+ * + * @param bool the boolean to convert + * @return one if {@code true}, zero if {@code false} + */ + public static Integer toIntegerObject(final boolean bool) { + return bool ? NumberUtils.INTEGER_ONE : NumberUtils.INTEGER_ZERO; + } + + /** + *
Converts a Boolean to a Integer using the convention that + * {@code zero} is {@code false}.
+ * + *{@code null} will be converted to {@code null}.
+ * + *+ * BooleanUtils.toIntegerObject(Boolean.TRUE) = Integer.valueOf(1) + * BooleanUtils.toIntegerObject(Boolean.FALSE) = Integer.valueOf(0) + *+ * + * @param bool the Boolean to convert + * @return one if Boolean.TRUE, zero if Boolean.FALSE, {@code null} if {@code null} + */ + public static Integer toIntegerObject(final Boolean bool) { + if (bool == null) { + return null; + } + return bool.booleanValue() ? NumberUtils.INTEGER_ONE : NumberUtils.INTEGER_ZERO; + } + + /** + *
Converts a boolean to an int specifying the conversion values.
+ * + *+ * BooleanUtils.toInteger(true, 1, 0) = 1 + * BooleanUtils.toInteger(false, 1, 0) = 0 + *+ * + * @param bool the to convert + * @param trueValue the value to return if {@code true} + * @param falseValue the value to return if {@code false} + * @return the appropriate value + */ + public static int toInteger(final boolean bool, final int trueValue, final int falseValue) { + return bool ? trueValue : falseValue; + } + + /** + *
Converts a Boolean to an int specifying the conversion values.
+ * + *+ * BooleanUtils.toInteger(Boolean.TRUE, 1, 0, 2) = 1 + * BooleanUtils.toInteger(Boolean.FALSE, 1, 0, 2) = 0 + * BooleanUtils.toInteger(null, 1, 0, 2) = 2 + *+ * + * @param bool the Boolean to convert + * @param trueValue the value to return if {@code true} + * @param falseValue the value to return if {@code false} + * @param nullValue the value to return if {@code null} + * @return the appropriate value + */ + public static int toInteger(final Boolean bool, final int trueValue, final int falseValue, final int nullValue) { + if (bool == null) { + return nullValue; + } + return bool.booleanValue() ? trueValue : falseValue; + } + + /** + *
Converts a boolean to an Integer specifying the conversion values.
+ * + *+ * BooleanUtils.toIntegerObject(true, Integer.valueOf(1), Integer.valueOf(0)) = Integer.valueOf(1) + * BooleanUtils.toIntegerObject(false, Integer.valueOf(1), Integer.valueOf(0)) = Integer.valueOf(0) + *+ * + * @param bool the to convert + * @param trueValue the value to return if {@code true}, may be {@code null} + * @param falseValue the value to return if {@code false}, may be {@code null} + * @return the appropriate value + */ + public static Integer toIntegerObject(final boolean bool, final Integer trueValue, final Integer falseValue) { + return bool ? trueValue : falseValue; + } + + /** + *
Converts a Boolean to an Integer specifying the conversion values.
+ * + *+ * BooleanUtils.toIntegerObject(Boolean.TRUE, Integer.valueOf(1), Integer.valueOf(0), Integer.valueOf(2)) = Integer.valueOf(1) + * BooleanUtils.toIntegerObject(Boolean.FALSE, Integer.valueOf(1), Integer.valueOf(0), Integer.valueOf(2)) = Integer.valueOf(0) + * BooleanUtils.toIntegerObject(null, Integer.valueOf(1), Integer.valueOf(0), Integer.valueOf(2)) = Integer.valueOf(2) + *+ * + * @param bool the Boolean to convert + * @param trueValue the value to return if {@code true}, may be {@code null} + * @param falseValue the value to return if {@code false}, may be {@code null} + * @param nullValue the value to return if {@code null}, may be {@code null} + * @return the appropriate value + */ + public static Integer toIntegerObject(final Boolean bool, final Integer trueValue, final Integer falseValue, final Integer nullValue) { + if (bool == null) { + return nullValue; + } + return bool.booleanValue() ? trueValue : falseValue; + } + + // String to Boolean methods + //----------------------------------------------------------------------- + /** + *
Converts a String to a Boolean.
+ * + *{@code 'true'}, {@code 'on'}, {@code 'y'}, {@code 't'} or {@code 'yes'} + * (case insensitive) will return {@code true}. + * {@code 'false'}, {@code 'off'}, {@code 'n'}, {@code 'f'} or {@code 'no'} + * (case insensitive) will return {@code false}. + * Otherwise, {@code null} is returned.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * // N.B. case is not significant + * BooleanUtils.toBooleanObject(null) = null + * BooleanUtils.toBooleanObject("true") = Boolean.TRUE + * BooleanUtils.toBooleanObject("T") = Boolean.TRUE // i.e. T[RUE] + * BooleanUtils.toBooleanObject("false") = Boolean.FALSE + * BooleanUtils.toBooleanObject("f") = Boolean.FALSE // i.e. f[alse] + * BooleanUtils.toBooleanObject("No") = Boolean.FALSE + * BooleanUtils.toBooleanObject("n") = Boolean.FALSE // i.e. n[o] + * BooleanUtils.toBooleanObject("on") = Boolean.TRUE + * BooleanUtils.toBooleanObject("ON") = Boolean.TRUE + * BooleanUtils.toBooleanObject("off") = Boolean.FALSE + * BooleanUtils.toBooleanObject("oFf") = Boolean.FALSE + * BooleanUtils.toBooleanObject("yes") = Boolean.TRUE + * BooleanUtils.toBooleanObject("Y") = Boolean.TRUE // i.e. Y[ES] + * BooleanUtils.toBooleanObject("blue") = null + * BooleanUtils.toBooleanObject("true ") = null // trailing space (too long) + * BooleanUtils.toBooleanObject("ono") = null // does not match on or no + *+ * + * @param str the String to check; upper and lower case are treated as the same + * @return the Boolean value of the string, {@code null} if no match or {@code null} input + */ + public static Boolean toBooleanObject(final String str) { + // Previously used equalsIgnoreCase, which was fast for interned 'true'. + // Non interned 'true' matched 15 times slower. + // + // Optimisation provides same performance as before for interned 'true'. + // Similar performance for null, 'false', and other strings not length 2/3/4. + // 'true'/'TRUE' match 4 times slower, 'tRUE'/'True' 7 times slower. + if (str == "true") { + return Boolean.TRUE; + } + if (str == null) { + return null; + } + switch (str.length()) { + case 1: { + final char ch0 = str.charAt(0); + if (ch0 == 'y' || ch0 == 'Y' || + ch0 == 't' || ch0 == 'T') { + return Boolean.TRUE; + } + if (ch0 == 'n' || ch0 == 'N' || + ch0 == 'f' || ch0 == 'F') { + return Boolean.FALSE; + } + break; + } + case 2: { + final char ch0 = str.charAt(0); + final char ch1 = str.charAt(1); + if ((ch0 == 'o' || ch0 == 'O') && + (ch1 == 'n' || ch1 == 'N') ) { + return Boolean.TRUE; + } + if ((ch0 == 'n' || ch0 == 'N') && + (ch1 == 'o' || ch1 == 'O') ) { + return Boolean.FALSE; + } + break; + } + case 3: { + final char ch0 = str.charAt(0); + final char ch1 = str.charAt(1); + final char ch2 = str.charAt(2); + if ((ch0 == 'y' || ch0 == 'Y') && + (ch1 == 'e' || ch1 == 'E') && + (ch2 == 's' || ch2 == 'S') ) { + return Boolean.TRUE; + } + if ((ch0 == 'o' || ch0 == 'O') && + (ch1 == 'f' || ch1 == 'F') && + (ch2 == 'f' || ch2 == 'F') ) { + return Boolean.FALSE; + } + break; + } + case 4: { + final char ch0 = str.charAt(0); + final char ch1 = str.charAt(1); + final char ch2 = str.charAt(2); + final char ch3 = str.charAt(3); + if ((ch0 == 't' || ch0 == 'T') && + (ch1 == 'r' || ch1 == 'R') && + (ch2 == 'u' || ch2 == 'U') && + (ch3 == 'e' || ch3 == 'E') ) { + return Boolean.TRUE; + } + break; + } + case 5: { + final char ch0 = str.charAt(0); + final char ch1 = str.charAt(1); + final char ch2 = str.charAt(2); + final char ch3 = str.charAt(3); + final char ch4 = str.charAt(4); + if ((ch0 == 'f' || ch0 == 'F') && + (ch1 == 'a' || ch1 == 'A') && + (ch2 == 'l' || ch2 == 'L') && + (ch3 == 's' || ch3 == 'S') && + (ch4 == 'e' || ch4 == 'E') ) { + return Boolean.FALSE; + } + break; + } + default: + break; + } + + return null; + } + + /** + *
Converts a String to a Boolean throwing an exception if no match.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.toBooleanObject("true", "true", "false", "null") = Boolean.TRUE + * BooleanUtils.toBooleanObject("false", "true", "false", "null") = Boolean.FALSE + * BooleanUtils.toBooleanObject("null", "true", "false", "null") = null + *+ * + * @param str the String to check + * @param trueString the String to match for {@code true} (case sensitive), may be {@code null} + * @param falseString the String to match for {@code false} (case sensitive), may be {@code null} + * @param nullString the String to match for {@code null} (case sensitive), may be {@code null} + * @return the Boolean value of the string, {@code null} if either the String matches {@code nullString} + * or if {@code null} input and {@code nullString} is {@code null} + * @throws IllegalArgumentException if the String doesn't match + */ + public static Boolean toBooleanObject(final String str, final String trueString, final String falseString, final String nullString) { + if (str == null) { + if (trueString == null) { + return Boolean.TRUE; + } + if (falseString == null) { + return Boolean.FALSE; + } + if (nullString == null) { + return null; + } + } else if (str.equals(trueString)) { + return Boolean.TRUE; + } else if (str.equals(falseString)) { + return Boolean.FALSE; + } else if (str.equals(nullString)) { + return null; + } + // no match + throw new IllegalArgumentException("The String did not match any specified value"); + } + + // String to boolean methods + //----------------------------------------------------------------------- + /** + *
Converts a String to a boolean (optimised for performance).
+ * + *{@code 'true'}, {@code 'on'}, {@code 'y'}, {@code 't'} or {@code 'yes'} + * (case insensitive) will return {@code true}. Otherwise, + * {@code false} is returned.
+ * + *This method performs 4 times faster (JDK1.4) than + * {@code Boolean.valueOf(String)}. However, this method accepts + * 'on' and 'yes', 't', 'y' as true values. + * + *
+ * BooleanUtils.toBoolean(null) = false + * BooleanUtils.toBoolean("true") = true + * BooleanUtils.toBoolean("TRUE") = true + * BooleanUtils.toBoolean("tRUe") = true + * BooleanUtils.toBoolean("on") = true + * BooleanUtils.toBoolean("yes") = true + * BooleanUtils.toBoolean("false") = false + * BooleanUtils.toBoolean("x gti") = false + * BooleanUtils.toBooleanObject("y") = true + * BooleanUtils.toBooleanObject("n") = false + * BooleanUtils.toBooleanObject("t") = true + * BooleanUtils.toBooleanObject("f") = false + *+ * + * @param str the String to check + * @return the boolean value of the string, {@code false} if no match or the String is null + */ + public static boolean toBoolean(final String str) { + return toBooleanObject(str) == Boolean.TRUE; + } + + /** + *
Converts a String to a Boolean throwing an exception if no match found.
+ * + *+ * BooleanUtils.toBoolean("true", "true", "false") = true + * BooleanUtils.toBoolean("false", "true", "false") = false + *+ * + * @param str the String to check + * @param trueString the String to match for {@code true} (case sensitive), may be {@code null} + * @param falseString the String to match for {@code false} (case sensitive), may be {@code null} + * @return the boolean value of the string + * @throws IllegalArgumentException if the String doesn't match + */ + public static boolean toBoolean(final String str, final String trueString, final String falseString) { + if (str == trueString) { + return true; + } else if (str == falseString) { + return false; + } else if (str != null) { + if (str.equals(trueString)) { + return true; + } else if (str.equals(falseString)) { + return false; + } + } + // no match + throw new IllegalArgumentException("The String did not match either specified value"); + } + + // Boolean to String methods + //----------------------------------------------------------------------- + /** + *
Converts a Boolean to a String returning {@code 'true'}, + * {@code 'false'}, or {@code null}.
+ * + *+ * BooleanUtils.toStringTrueFalse(Boolean.TRUE) = "true" + * BooleanUtils.toStringTrueFalse(Boolean.FALSE) = "false" + * BooleanUtils.toStringTrueFalse(null) = null; + *+ * + * @param bool the Boolean to check + * @return {@code 'true'}, {@code 'false'}, or {@code null} + */ + public static String toStringTrueFalse(final Boolean bool) { + return toString(bool, "true", "false", null); + } + + /** + *
Converts a Boolean to a String returning {@code 'on'}, + * {@code 'off'}, or {@code null}.
+ * + *+ * BooleanUtils.toStringOnOff(Boolean.TRUE) = "on" + * BooleanUtils.toStringOnOff(Boolean.FALSE) = "off" + * BooleanUtils.toStringOnOff(null) = null; + *+ * + * @param bool the Boolean to check + * @return {@code 'on'}, {@code 'off'}, or {@code null} + */ + public static String toStringOnOff(final Boolean bool) { + return toString(bool, "on", "off", null); + } + + /** + *
Converts a Boolean to a String returning {@code 'yes'}, + * {@code 'no'}, or {@code null}.
+ * + *+ * BooleanUtils.toStringYesNo(Boolean.TRUE) = "yes" + * BooleanUtils.toStringYesNo(Boolean.FALSE) = "no" + * BooleanUtils.toStringYesNo(null) = null; + *+ * + * @param bool the Boolean to check + * @return {@code 'yes'}, {@code 'no'}, or {@code null} + */ + public static String toStringYesNo(final Boolean bool) { + return toString(bool, "yes", "no", null); + } + + /** + *
Converts a Boolean to a String returning one of the input Strings.
+ * + *+ * BooleanUtils.toString(Boolean.TRUE, "true", "false", null) = "true" + * BooleanUtils.toString(Boolean.FALSE, "true", "false", null) = "false" + * BooleanUtils.toString(null, "true", "false", null) = null; + *+ * + * @param bool the Boolean to check + * @param trueString the String to return if {@code true}, may be {@code null} + * @param falseString the String to return if {@code false}, may be {@code null} + * @param nullString the String to return if {@code null}, may be {@code null} + * @return one of the three input Strings + */ + public static String toString(final Boolean bool, final String trueString, final String falseString, final String nullString) { + if (bool == null) { + return nullString; + } + return bool.booleanValue() ? trueString : falseString; + } + + // boolean to String methods + //----------------------------------------------------------------------- + /** + *
Converts a boolean to a String returning {@code 'true'} + * or {@code 'false'}.
+ * + *+ * BooleanUtils.toStringTrueFalse(true) = "true" + * BooleanUtils.toStringTrueFalse(false) = "false" + *+ * + * @param bool the Boolean to check + * @return {@code 'true'}, {@code 'false'}, or {@code null} + */ + public static String toStringTrueFalse(final boolean bool) { + return toString(bool, "true", "false"); + } + + /** + *
Converts a boolean to a String returning {@code 'on'} + * or {@code 'off'}.
+ * + *+ * BooleanUtils.toStringOnOff(true) = "on" + * BooleanUtils.toStringOnOff(false) = "off" + *+ * + * @param bool the Boolean to check + * @return {@code 'on'}, {@code 'off'}, or {@code null} + */ + public static String toStringOnOff(final boolean bool) { + return toString(bool, "on", "off"); + } + + /** + *
Converts a boolean to a String returning {@code 'yes'} + * or {@code 'no'}.
+ * + *+ * BooleanUtils.toStringYesNo(true) = "yes" + * BooleanUtils.toStringYesNo(false) = "no" + *+ * + * @param bool the Boolean to check + * @return {@code 'yes'}, {@code 'no'}, or {@code null} + */ + public static String toStringYesNo(final boolean bool) { + return toString(bool, "yes", "no"); + } + + /** + *
Converts a boolean to a String returning one of the input Strings.
+ * + *+ * BooleanUtils.toString(true, "true", "false") = "true" + * BooleanUtils.toString(false, "true", "false") = "false" + *+ * + * @param bool the Boolean to check + * @param trueString the String to return if {@code true}, may be {@code null} + * @param falseString the String to return if {@code false}, may be {@code null} + * @return one of the two input Strings + */ + public static String toString(final boolean bool, final String trueString, final String falseString) { + return bool ? trueString : falseString; + } + + // logical operations + // ---------------------------------------------------------------------- + /** + *
Performs an and on a set of booleans.
+ * + *+ * BooleanUtils.and(true, true) = true + * BooleanUtils.and(false, false) = false + * BooleanUtils.and(true, false) = false + * BooleanUtils.and(true, true, false) = false + * BooleanUtils.and(true, true, true) = true + *+ * + * @param array an array of {@code boolean}s + * @return {@code true} if the and is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + * @since 3.0.1 + */ + public static boolean and(final boolean... array) { + // Validates input + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + for (final boolean element : array) { + if (!element) { + return false; + } + } + return true; + } + + /** + *
Performs an and on an array of Booleans.
+ * + *+ * BooleanUtils.and(Boolean.TRUE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.and(Boolean.FALSE, Boolean.FALSE) = Boolean.FALSE + * BooleanUtils.and(Boolean.TRUE, Boolean.FALSE) = Boolean.FALSE + * BooleanUtils.and(Boolean.TRUE, Boolean.TRUE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.and(Boolean.FALSE, Boolean.FALSE, Boolean.TRUE) = Boolean.FALSE + * BooleanUtils.and(Boolean.TRUE, Boolean.FALSE, Boolean.TRUE) = Boolean.FALSE + *+ * + * @param array an array of {@code Boolean}s + * @return {@code true} if the and is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + * @throws IllegalArgumentException if {@code array} contains a {@code null} + * @since 3.0.1 + */ + public static Boolean and(final Boolean... array) { + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + try { + final boolean[] primitive = ArrayUtils.toPrimitive(array); + return and(primitive) ? Boolean.TRUE : Boolean.FALSE; + } catch (final NullPointerException ex) { + throw new IllegalArgumentException("The array must not contain any null elements"); + } + } + + /** + *
Performs an or on a set of booleans.
+ * + *+ * BooleanUtils.or(true, true) = true + * BooleanUtils.or(false, false) = false + * BooleanUtils.or(true, false) = true + * BooleanUtils.or(true, true, false) = true + * BooleanUtils.or(true, true, true) = true + * BooleanUtils.or(false, false, false) = false + *+ * + * @param array an array of {@code boolean}s + * @return {@code true} if the or is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + * @since 3.0.1 + */ + public static boolean or(final boolean... array) { + // Validates input + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + for (final boolean element : array) { + if (element) { + return true; + } + } + return false; + } + + /** + *
Performs an or on an array of Booleans.
+ * + *+ * BooleanUtils.or(Boolean.TRUE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.or(Boolean.FALSE, Boolean.FALSE) = Boolean.FALSE + * BooleanUtils.or(Boolean.TRUE, Boolean.FALSE) = Boolean.TRUE + * BooleanUtils.or(Boolean.TRUE, Boolean.TRUE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.or(Boolean.FALSE, Boolean.FALSE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.or(Boolean.TRUE, Boolean.FALSE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.or(Boolean.FALSE, Boolean.FALSE, Boolean.FALSE) = Boolean.FALSE + *+ * + * @param array an array of {@code Boolean}s + * @return {@code true} if the or is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + * @throws IllegalArgumentException if {@code array} contains a {@code null} + * @since 3.0.1 + */ + public static Boolean or(final Boolean... array) { + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + try { + final boolean[] primitive = ArrayUtils.toPrimitive(array); + return or(primitive) ? Boolean.TRUE : Boolean.FALSE; + } catch (final NullPointerException ex) { + throw new IllegalArgumentException("The array must not contain any null elements"); + } + } + + /** + *
Performs an xor on a set of booleans.
+ * + *+ * BooleanUtils.xor(true, true) = false + * BooleanUtils.xor(false, false) = false + * BooleanUtils.xor(true, false) = true + * BooleanUtils.xor(true, true) = false + * BooleanUtils.xor(false, false) = false + * BooleanUtils.xor(true, false) = true + *+ * + * @param array an array of {@code boolean}s + * @return {@code true} if the xor is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + */ + public static boolean xor(final boolean... array) { + // Validates input + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + + // false if the neutral element of the xor operator + boolean result = false; + for (final boolean element : array) { + result ^= element; + } + + return result; + } + + /** + *
Performs an xor on an array of Booleans.
+ * + *+ * BooleanUtils.xor(new Boolean[] { Boolean.TRUE, Boolean.TRUE }) = Boolean.FALSE + * BooleanUtils.xor(new Boolean[] { Boolean.FALSE, Boolean.FALSE }) = Boolean.FALSE + * BooleanUtils.xor(new Boolean[] { Boolean.TRUE, Boolean.FALSE }) = Boolean.TRUE + *+ * + * @param array an array of {@code Boolean}s + * @return {@code true} if the xor is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + * @throws IllegalArgumentException if {@code array} contains a {@code null} + */ + public static Boolean xor(final Boolean... array) { + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + try { + final boolean[] primitive = ArrayUtils.toPrimitive(array); + return xor(primitive) ? Boolean.TRUE : Boolean.FALSE; + } catch (final NullPointerException ex) { + throw new IllegalArgumentException("The array must not contain any null elements"); + } + } + + /** + *
Compares two {@code boolean} values. This is the same functionality as provided in Java 7.
+ * + * @param x the first {@code boolean} to compare + * @param y the second {@code boolean} to compare + * @return the value {@code 0} if {@code x == y}; + * a value less than {@code 0} if {@code !x && y}; and + * a value greater than {@code 0} if {@code x && !y} + * @since 3.4 + */ + public static int compare(boolean x, boolean y) { + if (x == y) { + return 0; + } + return x ? 1 : -1; + } + +} diff --git a/Java/commons-lang-BooleanUtils_289/metadata.json b/Java/commons-lang-BooleanUtils_289/metadata.json new file mode 100644 index 000000000..f6b9a2f2b --- /dev/null +++ b/Java/commons-lang-BooleanUtils_289/metadata.json @@ -0,0 +1,21 @@ +{ + "language": "java", + "id": "commons-lang-BooleanUtils_289", + "buggyPath": ".", + "referencePath": null, + "buildCommand": "mvn package -V -B -Denforcer.skip=true -Dcheckstyle.skip=true -Dcobertura.skip=true -Drat.skip=true -Dlicense.skip=true -Dfindbugs.skip=true -Dgpg.skip=true -Dskip.npm=true -Dskip.gulp=true -Dskip.bower=true -Drat.numUnapprovedLicenses=100 -DskipTests=true -DskipITs=true -Dtest=None -DfailIfNoTests=false", + "testCommand": "mvn test -V -B -Denforcer.skip=true -Dcheckstyle.skip=true -Dcobertura.skip=true -Drat.skip=true -Dlicense.skip=true -Dfindbugs.skip=true -Dgpg.skip=true -Dskip.npm=true -Dskip.gulp=true -Dskip.bower=true -Drat.numUnapprovedLicenses=100", + "categories": [ + "safety", + "npe" + ], + "npe": { + "filepath": "src/main/java/org/apache/commons/lang3/BooleanUtils.java", + "line": 312, + "npe_method": "toBoolean", + "deref_field": "value", + "npe_class": "BooleanUtils", + "repo": "commons-lang", + "bug_id": "BooleanUtils_289" + } +} diff --git a/Java/commons-lang-BooleanUtils_289/npe.json b/Java/commons-lang-BooleanUtils_289/npe.json new file mode 100644 index 000000000..bc824bc6a --- /dev/null +++ b/Java/commons-lang-BooleanUtils_289/npe.json @@ -0,0 +1,7 @@ +{ + "filepath": "src/main/java/org/apache/commons/lang3/BooleanUtils.java", + "line": 312, + "npe_method": "toBoolean", + "deref_field": "value", + "npe_class": "BooleanUtils" +} \ No newline at end of file diff --git a/Java/commons-lang-BooleanUtils_356/Dockerfile b/Java/commons-lang-BooleanUtils_356/Dockerfile new file mode 100644 index 000000000..7b7fbe349 --- /dev/null +++ b/Java/commons-lang-BooleanUtils_356/Dockerfile @@ -0,0 +1,18 @@ +FROM ghcr.io/kupl/starlab-benchmarks/java-base:commons-lang + +ENV TZ=Asia/Seoul + +COPY ./metadata.json . +COPY ./npe.json . +COPY ./buggy.java /tmp/buggy.java +RUN export BUGGY_PATH=$(cat metadata.json | jq -r ".npe.filepath") \ + && export BUGGY_LINE=$(cat metadata.json | jq -r ".npe.line") \ + && export BUGGY_MTHD=$(cat metadata.json | jq -r ".npe.npe_method") \ + && mv /tmp/buggy.java $BUGGY_PATH \ + && echo "[{\"filepath\": \"$BUGGY_PATH\", \"line\": $BUGGY_LINE, \"method_name\": \"$BUGGY_MTHD\"}]" | jq . > traces.json + +RUN git init . && git add -A + +RUN $(cat metadata.json | jq -r ".buildCommand") + +RUN $(cat metadata.json | jq -r ".testCommand"); if [ $? -eq 0 ]; then exit 1; fi diff --git a/Java/commons-lang-BooleanUtils_356/buggy.java b/Java/commons-lang-BooleanUtils_356/buggy.java new file mode 100644 index 000000000..f09b5e777 --- /dev/null +++ b/Java/commons-lang-BooleanUtils_356/buggy.java @@ -0,0 +1,1120 @@ +/* + * 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; + +import org.apache.commons.lang3.math.NumberUtils; + +/** + *Operations on boolean primitives and Boolean objects.
+ * + *This class tries to handle {@code null} input gracefully. + * An exception will not be thrown for a {@code null} input. + * Each method documents its behaviour in more detail.
+ * + *#ThreadSafe#
+ * @since 2.0 + */ +public class BooleanUtils { + + /** + *{@code BooleanUtils} instances should NOT be constructed in standard programming. + * Instead, the class should be used as {@code BooleanUtils.negate(true);}.
+ * + *This constructor is public to permit tools that require a JavaBean instance + * to operate.
+ */ + public BooleanUtils() { + super(); + } + + // Boolean utilities + //-------------------------------------------------------------------------- + /** + *Negates the specified boolean.
+ * + *If {@code null} is passed in, {@code null} will be returned.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.negate(Boolean.TRUE) = Boolean.FALSE; + * BooleanUtils.negate(Boolean.FALSE) = Boolean.TRUE; + * BooleanUtils.negate(null) = null; + *+ * + * @param bool the Boolean to negate, may be null + * @return the negated Boolean, or {@code null} if {@code null} input + */ + public static Boolean negate(final Boolean bool) { + if (bool == null) { + return null; + } + return bool.booleanValue() ? Boolean.FALSE : Boolean.TRUE; + } + + // boolean Boolean methods + //----------------------------------------------------------------------- + /** + *
Checks if a {@code Boolean} value is {@code true}, + * handling {@code null} by returning {@code false}.
+ * + *+ * BooleanUtils.isTrue(Boolean.TRUE) = true + * BooleanUtils.isTrue(Boolean.FALSE) = false + * BooleanUtils.isTrue(null) = false + *+ * + * @param bool the boolean to check, null returns {@code false} + * @return {@code true} only if the input is non-null and true + * @since 2.1 + */ + public static boolean isTrue(final Boolean bool) { + return Boolean.TRUE.equals(bool); + } + + /** + *
Checks if a {@code Boolean} value is not {@code true}, + * handling {@code null} by returning {@code true}.
+ * + *+ * BooleanUtils.isNotTrue(Boolean.TRUE) = false + * BooleanUtils.isNotTrue(Boolean.FALSE) = true + * BooleanUtils.isNotTrue(null) = true + *+ * + * @param bool the boolean to check, null returns {@code true} + * @return {@code true} if the input is null or false + * @since 2.3 + */ + public static boolean isNotTrue(final Boolean bool) { + return !isTrue(bool); + } + + /** + *
Checks if a {@code Boolean} value is {@code false}, + * handling {@code null} by returning {@code false}.
+ * + *+ * BooleanUtils.isFalse(Boolean.TRUE) = false + * BooleanUtils.isFalse(Boolean.FALSE) = true + * BooleanUtils.isFalse(null) = false + *+ * + * @param bool the boolean to check, null returns {@code false} + * @return {@code true} only if the input is non-null and false + * @since 2.1 + */ + public static boolean isFalse(final Boolean bool) { + return Boolean.FALSE.equals(bool); + } + + /** + *
Checks if a {@code Boolean} value is not {@code false}, + * handling {@code null} by returning {@code true}.
+ * + *+ * BooleanUtils.isNotFalse(Boolean.TRUE) = true + * BooleanUtils.isNotFalse(Boolean.FALSE) = false + * BooleanUtils.isNotFalse(null) = true + *+ * + * @param bool the boolean to check, null returns {@code true} + * @return {@code true} if the input is null or true + * @since 2.3 + */ + public static boolean isNotFalse(final Boolean bool) { + return !isFalse(bool); + } + + //----------------------------------------------------------------------- + /** + *
Converts a Boolean to a boolean handling {@code null} + * by returning {@code false}.
+ * + *+ * BooleanUtils.toBoolean(Boolean.TRUE) = true + * BooleanUtils.toBoolean(Boolean.FALSE) = false + * BooleanUtils.toBoolean(null) = false + *+ * + * @param bool the boolean to convert + * @return {@code true} or {@code false}, {@code null} returns {@code false} + */ + public static boolean toBoolean(final Boolean bool) { + return bool != null && bool.booleanValue(); + } + + /** + *
Converts a Boolean to a boolean handling {@code null}.
+ * + *+ * BooleanUtils.toBooleanDefaultIfNull(Boolean.TRUE, false) = true + * BooleanUtils.toBooleanDefaultIfNull(Boolean.FALSE, true) = false + * BooleanUtils.toBooleanDefaultIfNull(null, true) = true + *+ * + * @param bool the boolean to convert + * @param valueIfNull the boolean value to return if {@code null} + * @return {@code true} or {@code false} + */ + public static boolean toBooleanDefaultIfNull(final Boolean bool, final boolean valueIfNull) { + if (bool == null) { + return valueIfNull; + } + return bool.booleanValue(); + } + + // Integer to Boolean methods + //----------------------------------------------------------------------- + /** + *
Converts an int to a boolean using the convention that {@code zero} + * is {@code false}.
+ * + *+ * BooleanUtils.toBoolean(0) = false + * BooleanUtils.toBoolean(1) = true + * BooleanUtils.toBoolean(2) = true + *+ * + * @param value the int to convert + * @return {@code true} if non-zero, {@code false} + * if zero + */ + public static boolean toBoolean(final int value) { + return value != 0; + } + + /** + *
Converts an int to a Boolean using the convention that {@code zero} + * is {@code false}.
+ * + *+ * BooleanUtils.toBoolean(0) = Boolean.FALSE + * BooleanUtils.toBoolean(1) = Boolean.TRUE + * BooleanUtils.toBoolean(2) = Boolean.TRUE + *+ * + * @param value the int to convert + * @return Boolean.TRUE if non-zero, Boolean.FALSE if zero, + * {@code null} if {@code null} + */ + public static Boolean toBooleanObject(final int value) { + return value == 0 ? Boolean.FALSE : Boolean.TRUE; + } + + /** + *
Converts an Integer to a Boolean using the convention that {@code zero} + * is {@code false}.
+ * + *{@code null} will be converted to {@code null}.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.toBoolean(Integer.valueOf(0)) = Boolean.FALSE + * BooleanUtils.toBoolean(Integer.valueOf(1)) = Boolean.TRUE + * BooleanUtils.toBoolean(Integer.valueOf(null)) = null + *+ * + * @param value the Integer to convert + * @return Boolean.TRUE if non-zero, Boolean.FALSE if zero, + * {@code null} if {@code null} input + */ + public static Boolean toBooleanObject(final Integer value) { + if (value == null) { + return null; + } + return value.intValue() == 0 ? Boolean.FALSE : Boolean.TRUE; + } + + /** + *
Converts an int to a boolean specifying the conversion values.
+ * + *+ * BooleanUtils.toBoolean(0, 1, 0) = false + * BooleanUtils.toBoolean(1, 1, 0) = true + * BooleanUtils.toBoolean(2, 1, 2) = false + * BooleanUtils.toBoolean(2, 2, 0) = true + *+ * + * @param value the Integer to convert + * @param trueValue the value to match for {@code true} + * @param falseValue the value to match for {@code false} + * @return {@code true} or {@code false} + * @throws IllegalArgumentException if no match + */ + public static boolean toBoolean(final int value, final int trueValue, final int falseValue) { + if (value == trueValue) { + return true; + } + if (value == falseValue) { + return false; + } + // no match + throw new IllegalArgumentException("The Integer did not match either specified value"); + } + + /** + *
Converts an Integer to a boolean specifying the conversion values.
+ * + *+ * BooleanUtils.toBoolean(Integer.valueOf(0), Integer.valueOf(1), Integer.valueOf(0)) = false + * BooleanUtils.toBoolean(Integer.valueOf(1), Integer.valueOf(1), Integer.valueOf(0)) = true + * BooleanUtils.toBoolean(Integer.valueOf(2), Integer.valueOf(1), Integer.valueOf(2)) = false + * BooleanUtils.toBoolean(Integer.valueOf(2), Integer.valueOf(2), Integer.valueOf(0)) = true + * BooleanUtils.toBoolean(null, null, Integer.valueOf(0)) = true + *+ * + * @param value the Integer to convert + * @param trueValue the value to match for {@code true}, may be {@code null} + * @param falseValue the value to match for {@code false}, may be {@code null} + * @return {@code true} or {@code false} + * @throws IllegalArgumentException if no match + */ + public static boolean toBoolean(final Integer value, final Integer trueValue, final Integer falseValue) { + if (value == null) { + if (trueValue == null) { + return true; + } + if (falseValue == null) { + return false; + } + } else if (value.equals(trueValue)) { + return true; + } else if (value.equals(falseValue)) { + return false; + } + // no match + throw new IllegalArgumentException("The Integer did not match either specified value"); + } + + /** + *
Converts an int to a Boolean specifying the conversion values.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.toBooleanObject(0, 0, 2, 3) = Boolean.TRUE + * BooleanUtils.toBooleanObject(2, 1, 2, 3) = Boolean.FALSE + * BooleanUtils.toBooleanObject(3, 1, 2, 3) = null + *+ * + * @param value the Integer to convert + * @param trueValue the value to match for {@code true} + * @param falseValue the value to match for {@code false} + * @param nullValue the value to to match for {@code null} + * @return Boolean.TRUE, Boolean.FALSE, or {@code null} + * @throws IllegalArgumentException if no match + */ + public static Boolean toBooleanObject(final int value, final int trueValue, final int falseValue, final int nullValue) { + if (value == trueValue) { + return Boolean.TRUE; + } + if (value == falseValue) { + return Boolean.FALSE; + } + if (value == nullValue) { + return null; + } + // no match + throw new IllegalArgumentException("The Integer did not match any specified value"); + } + + /** + *
Converts an Integer to a Boolean specifying the conversion values.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.toBooleanObject(Integer.valueOf(0), Integer.valueOf(0), Integer.valueOf(2), Integer.valueOf(3)) = Boolean.TRUE + * BooleanUtils.toBooleanObject(Integer.valueOf(2), Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(3)) = Boolean.FALSE + * BooleanUtils.toBooleanObject(Integer.valueOf(3), Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(3)) = null + *+ * + * @param value the Integer to convert + * @param trueValue the value to match for {@code true}, may be {@code null} + * @param falseValue the value to match for {@code false}, may be {@code null} + * @param nullValue the value to to match for {@code null}, may be {@code null} + * @return Boolean.TRUE, Boolean.FALSE, or {@code null} + * @throws IllegalArgumentException if no match + */ +/** + *
Converts an Integer to a Boolean specifying the conversion values.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.toBooleanObject(Integer.valueOf(0), Integer.valueOf(0), Integer.valueOf(2), Integer.valueOf(3)) = Boolean.TRUE + * BooleanUtils.toBooleanObject(Integer.valueOf(2), Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(3)) = Boolean.FALSE + * BooleanUtils.toBooleanObject(Integer.valueOf(3), Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(3)) = null + *+ * + * @param value + * the Integer to convert + * @param trueValue + * the value to match for {@code true}, may be {@code null} + * @param falseValue + * the value to match for {@code false}, may be {@code null} + * @param nullValue + * the value to to match for {@code null}, may be {@code null} + * @return Boolean.TRUE, Boolean.FALSE, or {@code null} + * @throws IllegalArgumentException + * if no match + */ +public static java.lang.Boolean toBooleanObject(final java.lang.Integer value, final java.lang.Integer trueValue, final java.lang.Integer falseValue, final java.lang.Integer nullValue) { + { + if (/* NPEX_NULL_EXP */ + value.equals(trueValue)) { + return java.lang.Boolean.TRUE; + } else if (value.equals(falseValue)) { + return java.lang.Boolean.FALSE; + } else if (value.equals(nullValue)) { + return null; + } + } + // no match + throw new java.lang.IllegalArgumentException("The Integer did not match any specified value"); +} + + // Boolean to Integer methods + //----------------------------------------------------------------------- + /** + *
Converts a boolean to an int using the convention that + * {@code zero} is {@code false}.
+ * + *+ * BooleanUtils.toInteger(true) = 1 + * BooleanUtils.toInteger(false) = 0 + *+ * + * @param bool the boolean to convert + * @return one if {@code true}, zero if {@code false} + */ + public static int toInteger(final boolean bool) { + return bool ? 1 : 0; + } + + /** + *
Converts a boolean to an Integer using the convention that + * {@code zero} is {@code false}.
+ * + *+ * BooleanUtils.toIntegerObject(true) = Integer.valueOf(1) + * BooleanUtils.toIntegerObject(false) = Integer.valueOf(0) + *+ * + * @param bool the boolean to convert + * @return one if {@code true}, zero if {@code false} + */ + public static Integer toIntegerObject(final boolean bool) { + return bool ? NumberUtils.INTEGER_ONE : NumberUtils.INTEGER_ZERO; + } + + /** + *
Converts a Boolean to a Integer using the convention that + * {@code zero} is {@code false}.
+ * + *{@code null} will be converted to {@code null}.
+ * + *+ * BooleanUtils.toIntegerObject(Boolean.TRUE) = Integer.valueOf(1) + * BooleanUtils.toIntegerObject(Boolean.FALSE) = Integer.valueOf(0) + *+ * + * @param bool the Boolean to convert + * @return one if Boolean.TRUE, zero if Boolean.FALSE, {@code null} if {@code null} + */ + public static Integer toIntegerObject(final Boolean bool) { + if (bool == null) { + return null; + } + return bool.booleanValue() ? NumberUtils.INTEGER_ONE : NumberUtils.INTEGER_ZERO; + } + + /** + *
Converts a boolean to an int specifying the conversion values.
+ * + *+ * BooleanUtils.toInteger(true, 1, 0) = 1 + * BooleanUtils.toInteger(false, 1, 0) = 0 + *+ * + * @param bool the to convert + * @param trueValue the value to return if {@code true} + * @param falseValue the value to return if {@code false} + * @return the appropriate value + */ + public static int toInteger(final boolean bool, final int trueValue, final int falseValue) { + return bool ? trueValue : falseValue; + } + + /** + *
Converts a Boolean to an int specifying the conversion values.
+ * + *+ * BooleanUtils.toInteger(Boolean.TRUE, 1, 0, 2) = 1 + * BooleanUtils.toInteger(Boolean.FALSE, 1, 0, 2) = 0 + * BooleanUtils.toInteger(null, 1, 0, 2) = 2 + *+ * + * @param bool the Boolean to convert + * @param trueValue the value to return if {@code true} + * @param falseValue the value to return if {@code false} + * @param nullValue the value to return if {@code null} + * @return the appropriate value + */ + public static int toInteger(final Boolean bool, final int trueValue, final int falseValue, final int nullValue) { + if (bool == null) { + return nullValue; + } + return bool.booleanValue() ? trueValue : falseValue; + } + + /** + *
Converts a boolean to an Integer specifying the conversion values.
+ * + *+ * BooleanUtils.toIntegerObject(true, Integer.valueOf(1), Integer.valueOf(0)) = Integer.valueOf(1) + * BooleanUtils.toIntegerObject(false, Integer.valueOf(1), Integer.valueOf(0)) = Integer.valueOf(0) + *+ * + * @param bool the to convert + * @param trueValue the value to return if {@code true}, may be {@code null} + * @param falseValue the value to return if {@code false}, may be {@code null} + * @return the appropriate value + */ + public static Integer toIntegerObject(final boolean bool, final Integer trueValue, final Integer falseValue) { + return bool ? trueValue : falseValue; + } + + /** + *
Converts a Boolean to an Integer specifying the conversion values.
+ * + *+ * BooleanUtils.toIntegerObject(Boolean.TRUE, Integer.valueOf(1), Integer.valueOf(0), Integer.valueOf(2)) = Integer.valueOf(1) + * BooleanUtils.toIntegerObject(Boolean.FALSE, Integer.valueOf(1), Integer.valueOf(0), Integer.valueOf(2)) = Integer.valueOf(0) + * BooleanUtils.toIntegerObject(null, Integer.valueOf(1), Integer.valueOf(0), Integer.valueOf(2)) = Integer.valueOf(2) + *+ * + * @param bool the Boolean to convert + * @param trueValue the value to return if {@code true}, may be {@code null} + * @param falseValue the value to return if {@code false}, may be {@code null} + * @param nullValue the value to return if {@code null}, may be {@code null} + * @return the appropriate value + */ + public static Integer toIntegerObject(final Boolean bool, final Integer trueValue, final Integer falseValue, final Integer nullValue) { + if (bool == null) { + return nullValue; + } + return bool.booleanValue() ? trueValue : falseValue; + } + + // String to Boolean methods + //----------------------------------------------------------------------- + /** + *
Converts a String to a Boolean.
+ * + *{@code 'true'}, {@code 'on'}, {@code 'y'}, {@code 't'} or {@code 'yes'} + * (case insensitive) will return {@code true}. + * {@code 'false'}, {@code 'off'}, {@code 'n'}, {@code 'f'} or {@code 'no'} + * (case insensitive) will return {@code false}. + * Otherwise, {@code null} is returned.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * // N.B. case is not significant + * BooleanUtils.toBooleanObject(null) = null + * BooleanUtils.toBooleanObject("true") = Boolean.TRUE + * BooleanUtils.toBooleanObject("T") = Boolean.TRUE // i.e. T[RUE] + * BooleanUtils.toBooleanObject("false") = Boolean.FALSE + * BooleanUtils.toBooleanObject("f") = Boolean.FALSE // i.e. f[alse] + * BooleanUtils.toBooleanObject("No") = Boolean.FALSE + * BooleanUtils.toBooleanObject("n") = Boolean.FALSE // i.e. n[o] + * BooleanUtils.toBooleanObject("on") = Boolean.TRUE + * BooleanUtils.toBooleanObject("ON") = Boolean.TRUE + * BooleanUtils.toBooleanObject("off") = Boolean.FALSE + * BooleanUtils.toBooleanObject("oFf") = Boolean.FALSE + * BooleanUtils.toBooleanObject("yes") = Boolean.TRUE + * BooleanUtils.toBooleanObject("Y") = Boolean.TRUE // i.e. Y[ES] + * BooleanUtils.toBooleanObject("blue") = null + * BooleanUtils.toBooleanObject("true ") = null // trailing space (too long) + * BooleanUtils.toBooleanObject("ono") = null // does not match on or no + *+ * + * @param str the String to check; upper and lower case are treated as the same + * @return the Boolean value of the string, {@code null} if no match or {@code null} input + */ + public static Boolean toBooleanObject(final String str) { + // Previously used equalsIgnoreCase, which was fast for interned 'true'. + // Non interned 'true' matched 15 times slower. + // + // Optimisation provides same performance as before for interned 'true'. + // Similar performance for null, 'false', and other strings not length 2/3/4. + // 'true'/'TRUE' match 4 times slower, 'tRUE'/'True' 7 times slower. + if (str == "true") { + return Boolean.TRUE; + } + if (str == null) { + return null; + } + switch (str.length()) { + case 1: { + final char ch0 = str.charAt(0); + if (ch0 == 'y' || ch0 == 'Y' || + ch0 == 't' || ch0 == 'T') { + return Boolean.TRUE; + } + if (ch0 == 'n' || ch0 == 'N' || + ch0 == 'f' || ch0 == 'F') { + return Boolean.FALSE; + } + break; + } + case 2: { + final char ch0 = str.charAt(0); + final char ch1 = str.charAt(1); + if ((ch0 == 'o' || ch0 == 'O') && + (ch1 == 'n' || ch1 == 'N') ) { + return Boolean.TRUE; + } + if ((ch0 == 'n' || ch0 == 'N') && + (ch1 == 'o' || ch1 == 'O') ) { + return Boolean.FALSE; + } + break; + } + case 3: { + final char ch0 = str.charAt(0); + final char ch1 = str.charAt(1); + final char ch2 = str.charAt(2); + if ((ch0 == 'y' || ch0 == 'Y') && + (ch1 == 'e' || ch1 == 'E') && + (ch2 == 's' || ch2 == 'S') ) { + return Boolean.TRUE; + } + if ((ch0 == 'o' || ch0 == 'O') && + (ch1 == 'f' || ch1 == 'F') && + (ch2 == 'f' || ch2 == 'F') ) { + return Boolean.FALSE; + } + break; + } + case 4: { + final char ch0 = str.charAt(0); + final char ch1 = str.charAt(1); + final char ch2 = str.charAt(2); + final char ch3 = str.charAt(3); + if ((ch0 == 't' || ch0 == 'T') && + (ch1 == 'r' || ch1 == 'R') && + (ch2 == 'u' || ch2 == 'U') && + (ch3 == 'e' || ch3 == 'E') ) { + return Boolean.TRUE; + } + break; + } + case 5: { + final char ch0 = str.charAt(0); + final char ch1 = str.charAt(1); + final char ch2 = str.charAt(2); + final char ch3 = str.charAt(3); + final char ch4 = str.charAt(4); + if ((ch0 == 'f' || ch0 == 'F') && + (ch1 == 'a' || ch1 == 'A') && + (ch2 == 'l' || ch2 == 'L') && + (ch3 == 's' || ch3 == 'S') && + (ch4 == 'e' || ch4 == 'E') ) { + return Boolean.FALSE; + } + break; + } + default: + break; + } + + return null; + } + + /** + *
Converts a String to a Boolean throwing an exception if no match.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.toBooleanObject("true", "true", "false", "null") = Boolean.TRUE + * BooleanUtils.toBooleanObject("false", "true", "false", "null") = Boolean.FALSE + * BooleanUtils.toBooleanObject("null", "true", "false", "null") = null + *+ * + * @param str the String to check + * @param trueString the String to match for {@code true} (case sensitive), may be {@code null} + * @param falseString the String to match for {@code false} (case sensitive), may be {@code null} + * @param nullString the String to match for {@code null} (case sensitive), may be {@code null} + * @return the Boolean value of the string, {@code null} if either the String matches {@code nullString} + * or if {@code null} input and {@code nullString} is {@code null} + * @throws IllegalArgumentException if the String doesn't match + */ + public static Boolean toBooleanObject(final String str, final String trueString, final String falseString, final String nullString) { + if (str == null) { + if (trueString == null) { + return Boolean.TRUE; + } + if (falseString == null) { + return Boolean.FALSE; + } + if (nullString == null) { + return null; + } + } else if (str.equals(trueString)) { + return Boolean.TRUE; + } else if (str.equals(falseString)) { + return Boolean.FALSE; + } else if (str.equals(nullString)) { + return null; + } + // no match + throw new IllegalArgumentException("The String did not match any specified value"); + } + + // String to boolean methods + //----------------------------------------------------------------------- + /** + *
Converts a String to a boolean (optimised for performance).
+ * + *{@code 'true'}, {@code 'on'}, {@code 'y'}, {@code 't'} or {@code 'yes'} + * (case insensitive) will return {@code true}. Otherwise, + * {@code false} is returned.
+ * + *This method performs 4 times faster (JDK1.4) than + * {@code Boolean.valueOf(String)}. However, this method accepts + * 'on' and 'yes', 't', 'y' as true values. + * + *
+ * BooleanUtils.toBoolean(null) = false + * BooleanUtils.toBoolean("true") = true + * BooleanUtils.toBoolean("TRUE") = true + * BooleanUtils.toBoolean("tRUe") = true + * BooleanUtils.toBoolean("on") = true + * BooleanUtils.toBoolean("yes") = true + * BooleanUtils.toBoolean("false") = false + * BooleanUtils.toBoolean("x gti") = false + * BooleanUtils.toBooleanObject("y") = true + * BooleanUtils.toBooleanObject("n") = false + * BooleanUtils.toBooleanObject("t") = true + * BooleanUtils.toBooleanObject("f") = false + *+ * + * @param str the String to check + * @return the boolean value of the string, {@code false} if no match or the String is null + */ + public static boolean toBoolean(final String str) { + return toBooleanObject(str) == Boolean.TRUE; + } + + /** + *
Converts a String to a Boolean throwing an exception if no match found.
+ * + *+ * BooleanUtils.toBoolean("true", "true", "false") = true + * BooleanUtils.toBoolean("false", "true", "false") = false + *+ * + * @param str the String to check + * @param trueString the String to match for {@code true} (case sensitive), may be {@code null} + * @param falseString the String to match for {@code false} (case sensitive), may be {@code null} + * @return the boolean value of the string + * @throws IllegalArgumentException if the String doesn't match + */ + public static boolean toBoolean(final String str, final String trueString, final String falseString) { + if (str == trueString) { + return true; + } else if (str == falseString) { + return false; + } else if (str != null) { + if (str.equals(trueString)) { + return true; + } else if (str.equals(falseString)) { + return false; + } + } + // no match + throw new IllegalArgumentException("The String did not match either specified value"); + } + + // Boolean to String methods + //----------------------------------------------------------------------- + /** + *
Converts a Boolean to a String returning {@code 'true'}, + * {@code 'false'}, or {@code null}.
+ * + *+ * BooleanUtils.toStringTrueFalse(Boolean.TRUE) = "true" + * BooleanUtils.toStringTrueFalse(Boolean.FALSE) = "false" + * BooleanUtils.toStringTrueFalse(null) = null; + *+ * + * @param bool the Boolean to check + * @return {@code 'true'}, {@code 'false'}, or {@code null} + */ + public static String toStringTrueFalse(final Boolean bool) { + return toString(bool, "true", "false", null); + } + + /** + *
Converts a Boolean to a String returning {@code 'on'}, + * {@code 'off'}, or {@code null}.
+ * + *+ * BooleanUtils.toStringOnOff(Boolean.TRUE) = "on" + * BooleanUtils.toStringOnOff(Boolean.FALSE) = "off" + * BooleanUtils.toStringOnOff(null) = null; + *+ * + * @param bool the Boolean to check + * @return {@code 'on'}, {@code 'off'}, or {@code null} + */ + public static String toStringOnOff(final Boolean bool) { + return toString(bool, "on", "off", null); + } + + /** + *
Converts a Boolean to a String returning {@code 'yes'}, + * {@code 'no'}, or {@code null}.
+ * + *+ * BooleanUtils.toStringYesNo(Boolean.TRUE) = "yes" + * BooleanUtils.toStringYesNo(Boolean.FALSE) = "no" + * BooleanUtils.toStringYesNo(null) = null; + *+ * + * @param bool the Boolean to check + * @return {@code 'yes'}, {@code 'no'}, or {@code null} + */ + public static String toStringYesNo(final Boolean bool) { + return toString(bool, "yes", "no", null); + } + + /** + *
Converts a Boolean to a String returning one of the input Strings.
+ * + *+ * BooleanUtils.toString(Boolean.TRUE, "true", "false", null) = "true" + * BooleanUtils.toString(Boolean.FALSE, "true", "false", null) = "false" + * BooleanUtils.toString(null, "true", "false", null) = null; + *+ * + * @param bool the Boolean to check + * @param trueString the String to return if {@code true}, may be {@code null} + * @param falseString the String to return if {@code false}, may be {@code null} + * @param nullString the String to return if {@code null}, may be {@code null} + * @return one of the three input Strings + */ + public static String toString(final Boolean bool, final String trueString, final String falseString, final String nullString) { + if (bool == null) { + return nullString; + } + return bool.booleanValue() ? trueString : falseString; + } + + // boolean to String methods + //----------------------------------------------------------------------- + /** + *
Converts a boolean to a String returning {@code 'true'} + * or {@code 'false'}.
+ * + *+ * BooleanUtils.toStringTrueFalse(true) = "true" + * BooleanUtils.toStringTrueFalse(false) = "false" + *+ * + * @param bool the Boolean to check + * @return {@code 'true'}, {@code 'false'}, or {@code null} + */ + public static String toStringTrueFalse(final boolean bool) { + return toString(bool, "true", "false"); + } + + /** + *
Converts a boolean to a String returning {@code 'on'} + * or {@code 'off'}.
+ * + *+ * BooleanUtils.toStringOnOff(true) = "on" + * BooleanUtils.toStringOnOff(false) = "off" + *+ * + * @param bool the Boolean to check + * @return {@code 'on'}, {@code 'off'}, or {@code null} + */ + public static String toStringOnOff(final boolean bool) { + return toString(bool, "on", "off"); + } + + /** + *
Converts a boolean to a String returning {@code 'yes'} + * or {@code 'no'}.
+ * + *+ * BooleanUtils.toStringYesNo(true) = "yes" + * BooleanUtils.toStringYesNo(false) = "no" + *+ * + * @param bool the Boolean to check + * @return {@code 'yes'}, {@code 'no'}, or {@code null} + */ + public static String toStringYesNo(final boolean bool) { + return toString(bool, "yes", "no"); + } + + /** + *
Converts a boolean to a String returning one of the input Strings.
+ * + *+ * BooleanUtils.toString(true, "true", "false") = "true" + * BooleanUtils.toString(false, "true", "false") = "false" + *+ * + * @param bool the Boolean to check + * @param trueString the String to return if {@code true}, may be {@code null} + * @param falseString the String to return if {@code false}, may be {@code null} + * @return one of the two input Strings + */ + public static String toString(final boolean bool, final String trueString, final String falseString) { + return bool ? trueString : falseString; + } + + // logical operations + // ---------------------------------------------------------------------- + /** + *
Performs an and on a set of booleans.
+ * + *+ * BooleanUtils.and(true, true) = true + * BooleanUtils.and(false, false) = false + * BooleanUtils.and(true, false) = false + * BooleanUtils.and(true, true, false) = false + * BooleanUtils.and(true, true, true) = true + *+ * + * @param array an array of {@code boolean}s + * @return {@code true} if the and is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + * @since 3.0.1 + */ + public static boolean and(final boolean... array) { + // Validates input + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + for (final boolean element : array) { + if (!element) { + return false; + } + } + return true; + } + + /** + *
Performs an and on an array of Booleans.
+ * + *+ * BooleanUtils.and(Boolean.TRUE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.and(Boolean.FALSE, Boolean.FALSE) = Boolean.FALSE + * BooleanUtils.and(Boolean.TRUE, Boolean.FALSE) = Boolean.FALSE + * BooleanUtils.and(Boolean.TRUE, Boolean.TRUE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.and(Boolean.FALSE, Boolean.FALSE, Boolean.TRUE) = Boolean.FALSE + * BooleanUtils.and(Boolean.TRUE, Boolean.FALSE, Boolean.TRUE) = Boolean.FALSE + *+ * + * @param array an array of {@code Boolean}s + * @return {@code true} if the and is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + * @throws IllegalArgumentException if {@code array} contains a {@code null} + * @since 3.0.1 + */ + public static Boolean and(final Boolean... array) { + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + try { + final boolean[] primitive = ArrayUtils.toPrimitive(array); + return and(primitive) ? Boolean.TRUE : Boolean.FALSE; + } catch (final NullPointerException ex) { + throw new IllegalArgumentException("The array must not contain any null elements"); + } + } + + /** + *
Performs an or on a set of booleans.
+ * + *+ * BooleanUtils.or(true, true) = true + * BooleanUtils.or(false, false) = false + * BooleanUtils.or(true, false) = true + * BooleanUtils.or(true, true, false) = true + * BooleanUtils.or(true, true, true) = true + * BooleanUtils.or(false, false, false) = false + *+ * + * @param array an array of {@code boolean}s + * @return {@code true} if the or is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + * @since 3.0.1 + */ + public static boolean or(final boolean... array) { + // Validates input + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + for (final boolean element : array) { + if (element) { + return true; + } + } + return false; + } + + /** + *
Performs an or on an array of Booleans.
+ * + *+ * BooleanUtils.or(Boolean.TRUE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.or(Boolean.FALSE, Boolean.FALSE) = Boolean.FALSE + * BooleanUtils.or(Boolean.TRUE, Boolean.FALSE) = Boolean.TRUE + * BooleanUtils.or(Boolean.TRUE, Boolean.TRUE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.or(Boolean.FALSE, Boolean.FALSE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.or(Boolean.TRUE, Boolean.FALSE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.or(Boolean.FALSE, Boolean.FALSE, Boolean.FALSE) = Boolean.FALSE + *+ * + * @param array an array of {@code Boolean}s + * @return {@code true} if the or is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + * @throws IllegalArgumentException if {@code array} contains a {@code null} + * @since 3.0.1 + */ + public static Boolean or(final Boolean... array) { + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + try { + final boolean[] primitive = ArrayUtils.toPrimitive(array); + return or(primitive) ? Boolean.TRUE : Boolean.FALSE; + } catch (final NullPointerException ex) { + throw new IllegalArgumentException("The array must not contain any null elements"); + } + } + + /** + *
Performs an xor on a set of booleans.
+ * + *+ * BooleanUtils.xor(true, true) = false + * BooleanUtils.xor(false, false) = false + * BooleanUtils.xor(true, false) = true + * BooleanUtils.xor(true, true) = false + * BooleanUtils.xor(false, false) = false + * BooleanUtils.xor(true, false) = true + *+ * + * @param array an array of {@code boolean}s + * @return {@code true} if the xor is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + */ + public static boolean xor(final boolean... array) { + // Validates input + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + + // false if the neutral element of the xor operator + boolean result = false; + for (final boolean element : array) { + result ^= element; + } + + return result; + } + + /** + *
Performs an xor on an array of Booleans.
+ * + *+ * BooleanUtils.xor(new Boolean[] { Boolean.TRUE, Boolean.TRUE }) = Boolean.FALSE + * BooleanUtils.xor(new Boolean[] { Boolean.FALSE, Boolean.FALSE }) = Boolean.FALSE + * BooleanUtils.xor(new Boolean[] { Boolean.TRUE, Boolean.FALSE }) = Boolean.TRUE + *+ * + * @param array an array of {@code Boolean}s + * @return {@code true} if the xor is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + * @throws IllegalArgumentException if {@code array} contains a {@code null} + */ + public static Boolean xor(final Boolean... array) { + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + try { + final boolean[] primitive = ArrayUtils.toPrimitive(array); + return xor(primitive) ? Boolean.TRUE : Boolean.FALSE; + } catch (final NullPointerException ex) { + throw new IllegalArgumentException("The array must not contain any null elements"); + } + } + + /** + *
Compares two {@code boolean} values. This is the same functionality as provided in Java 7.
+ * + * @param x the first {@code boolean} to compare + * @param y the second {@code boolean} to compare + * @return the value {@code 0} if {@code x == y}; + * a value less than {@code 0} if {@code !x && y}; and + * a value greater than {@code 0} if {@code x && !y} + * @since 3.4 + */ + public static int compare(boolean x, boolean y) { + if (x == y) { + return 0; + } + return x ? 1 : -1; + } + +} diff --git a/Java/commons-lang-BooleanUtils_356/metadata.json b/Java/commons-lang-BooleanUtils_356/metadata.json new file mode 100644 index 000000000..126102281 --- /dev/null +++ b/Java/commons-lang-BooleanUtils_356/metadata.json @@ -0,0 +1,21 @@ +{ + "language": "java", + "id": "commons-lang-BooleanUtils_356", + "buggyPath": ".", + "referencePath": null, + "buildCommand": "mvn package -V -B -Denforcer.skip=true -Dcheckstyle.skip=true -Dcobertura.skip=true -Drat.skip=true -Dlicense.skip=true -Dfindbugs.skip=true -Dgpg.skip=true -Dskip.npm=true -Dskip.gulp=true -Dskip.bower=true -Drat.numUnapprovedLicenses=100 -DskipTests=true -DskipITs=true -Dtest=None -DfailIfNoTests=false", + "testCommand": "mvn test -V -B -Denforcer.skip=true -Dcheckstyle.skip=true -Dcobertura.skip=true -Drat.skip=true -Dlicense.skip=true -Dfindbugs.skip=true -Dgpg.skip=true -Dskip.npm=true -Dskip.gulp=true -Dskip.bower=true -Drat.numUnapprovedLicenses=100", + "categories": [ + "safety", + "npe" + ], + "npe": { + "filepath": "src/main/java/org/apache/commons/lang3/BooleanUtils.java", + "line": 381, + "npe_method": "toBooleanObject", + "deref_field": "value", + "npe_class": "BooleanUtils", + "repo": "commons-lang", + "bug_id": "BooleanUtils_356" + } +} diff --git a/Java/commons-lang-BooleanUtils_356/npe.json b/Java/commons-lang-BooleanUtils_356/npe.json new file mode 100644 index 000000000..e640e2012 --- /dev/null +++ b/Java/commons-lang-BooleanUtils_356/npe.json @@ -0,0 +1,7 @@ +{ + "filepath": "src/main/java/org/apache/commons/lang3/BooleanUtils.java", + "line": 381, + "npe_method": "toBooleanObject", + "deref_field": "value", + "npe_class": "BooleanUtils" +} \ No newline at end of file diff --git a/Java/commons-lang-BooleanUtils_426/Dockerfile b/Java/commons-lang-BooleanUtils_426/Dockerfile new file mode 100644 index 000000000..7b7fbe349 --- /dev/null +++ b/Java/commons-lang-BooleanUtils_426/Dockerfile @@ -0,0 +1,18 @@ +FROM ghcr.io/kupl/starlab-benchmarks/java-base:commons-lang + +ENV TZ=Asia/Seoul + +COPY ./metadata.json . +COPY ./npe.json . +COPY ./buggy.java /tmp/buggy.java +RUN export BUGGY_PATH=$(cat metadata.json | jq -r ".npe.filepath") \ + && export BUGGY_LINE=$(cat metadata.json | jq -r ".npe.line") \ + && export BUGGY_MTHD=$(cat metadata.json | jq -r ".npe.npe_method") \ + && mv /tmp/buggy.java $BUGGY_PATH \ + && echo "[{\"filepath\": \"$BUGGY_PATH\", \"line\": $BUGGY_LINE, \"method_name\": \"$BUGGY_MTHD\"}]" | jq . > traces.json + +RUN git init . && git add -A + +RUN $(cat metadata.json | jq -r ".buildCommand") + +RUN $(cat metadata.json | jq -r ".testCommand"); if [ $? -eq 0 ]; then exit 1; fi diff --git a/Java/commons-lang-BooleanUtils_426/buggy.java b/Java/commons-lang-BooleanUtils_426/buggy.java new file mode 100644 index 000000000..52dda0355 --- /dev/null +++ b/Java/commons-lang-BooleanUtils_426/buggy.java @@ -0,0 +1,1119 @@ +/* + * 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; + +import org.apache.commons.lang3.math.NumberUtils; + +/** + *Operations on boolean primitives and Boolean objects.
+ * + *This class tries to handle {@code null} input gracefully. + * An exception will not be thrown for a {@code null} input. + * Each method documents its behaviour in more detail.
+ * + *#ThreadSafe#
+ * @since 2.0 + */ +public class BooleanUtils { + + /** + *{@code BooleanUtils} instances should NOT be constructed in standard programming. + * Instead, the class should be used as {@code BooleanUtils.negate(true);}.
+ * + *This constructor is public to permit tools that require a JavaBean instance + * to operate.
+ */ + public BooleanUtils() { + super(); + } + + // Boolean utilities + //-------------------------------------------------------------------------- + /** + *Negates the specified boolean.
+ * + *If {@code null} is passed in, {@code null} will be returned.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.negate(Boolean.TRUE) = Boolean.FALSE; + * BooleanUtils.negate(Boolean.FALSE) = Boolean.TRUE; + * BooleanUtils.negate(null) = null; + *+ * + * @param bool the Boolean to negate, may be null + * @return the negated Boolean, or {@code null} if {@code null} input + */ + public static Boolean negate(final Boolean bool) { + if (bool == null) { + return null; + } + return bool.booleanValue() ? Boolean.FALSE : Boolean.TRUE; + } + + // boolean Boolean methods + //----------------------------------------------------------------------- + /** + *
Checks if a {@code Boolean} value is {@code true}, + * handling {@code null} by returning {@code false}.
+ * + *+ * BooleanUtils.isTrue(Boolean.TRUE) = true + * BooleanUtils.isTrue(Boolean.FALSE) = false + * BooleanUtils.isTrue(null) = false + *+ * + * @param bool the boolean to check, null returns {@code false} + * @return {@code true} only if the input is non-null and true + * @since 2.1 + */ + public static boolean isTrue(final Boolean bool) { + return Boolean.TRUE.equals(bool); + } + + /** + *
Checks if a {@code Boolean} value is not {@code true}, + * handling {@code null} by returning {@code true}.
+ * + *+ * BooleanUtils.isNotTrue(Boolean.TRUE) = false + * BooleanUtils.isNotTrue(Boolean.FALSE) = true + * BooleanUtils.isNotTrue(null) = true + *+ * + * @param bool the boolean to check, null returns {@code true} + * @return {@code true} if the input is null or false + * @since 2.3 + */ + public static boolean isNotTrue(final Boolean bool) { + return !isTrue(bool); + } + + /** + *
Checks if a {@code Boolean} value is {@code false}, + * handling {@code null} by returning {@code false}.
+ * + *+ * BooleanUtils.isFalse(Boolean.TRUE) = false + * BooleanUtils.isFalse(Boolean.FALSE) = true + * BooleanUtils.isFalse(null) = false + *+ * + * @param bool the boolean to check, null returns {@code false} + * @return {@code true} only if the input is non-null and false + * @since 2.1 + */ + public static boolean isFalse(final Boolean bool) { + return Boolean.FALSE.equals(bool); + } + + /** + *
Checks if a {@code Boolean} value is not {@code false}, + * handling {@code null} by returning {@code true}.
+ * + *+ * BooleanUtils.isNotFalse(Boolean.TRUE) = true + * BooleanUtils.isNotFalse(Boolean.FALSE) = false + * BooleanUtils.isNotFalse(null) = true + *+ * + * @param bool the boolean to check, null returns {@code true} + * @return {@code true} if the input is null or true + * @since 2.3 + */ + public static boolean isNotFalse(final Boolean bool) { + return !isFalse(bool); + } + + //----------------------------------------------------------------------- + /** + *
Converts a Boolean to a boolean handling {@code null} + * by returning {@code false}.
+ * + *+ * BooleanUtils.toBoolean(Boolean.TRUE) = true + * BooleanUtils.toBoolean(Boolean.FALSE) = false + * BooleanUtils.toBoolean(null) = false + *+ * + * @param bool the boolean to convert + * @return {@code true} or {@code false}, {@code null} returns {@code false} + */ + public static boolean toBoolean(final Boolean bool) { + return bool != null && bool.booleanValue(); + } + + /** + *
Converts a Boolean to a boolean handling {@code null}.
+ * + *+ * BooleanUtils.toBooleanDefaultIfNull(Boolean.TRUE, false) = true + * BooleanUtils.toBooleanDefaultIfNull(Boolean.FALSE, true) = false + * BooleanUtils.toBooleanDefaultIfNull(null, true) = true + *+ * + * @param bool the boolean to convert + * @param valueIfNull the boolean value to return if {@code null} + * @return {@code true} or {@code false} + */ + public static boolean toBooleanDefaultIfNull(final Boolean bool, final boolean valueIfNull) { + if (bool == null) { + return valueIfNull; + } + return bool.booleanValue(); + } + + // Integer to Boolean methods + //----------------------------------------------------------------------- + /** + *
Converts an int to a boolean using the convention that {@code zero} + * is {@code false}.
+ * + *+ * BooleanUtils.toBoolean(0) = false + * BooleanUtils.toBoolean(1) = true + * BooleanUtils.toBoolean(2) = true + *+ * + * @param value the int to convert + * @return {@code true} if non-zero, {@code false} + * if zero + */ + public static boolean toBoolean(final int value) { + return value != 0; + } + + /** + *
Converts an int to a Boolean using the convention that {@code zero} + * is {@code false}.
+ * + *+ * BooleanUtils.toBoolean(0) = Boolean.FALSE + * BooleanUtils.toBoolean(1) = Boolean.TRUE + * BooleanUtils.toBoolean(2) = Boolean.TRUE + *+ * + * @param value the int to convert + * @return Boolean.TRUE if non-zero, Boolean.FALSE if zero, + * {@code null} if {@code null} + */ + public static Boolean toBooleanObject(final int value) { + return value == 0 ? Boolean.FALSE : Boolean.TRUE; + } + + /** + *
Converts an Integer to a Boolean using the convention that {@code zero} + * is {@code false}.
+ * + *{@code null} will be converted to {@code null}.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.toBoolean(Integer.valueOf(0)) = Boolean.FALSE + * BooleanUtils.toBoolean(Integer.valueOf(1)) = Boolean.TRUE + * BooleanUtils.toBoolean(Integer.valueOf(null)) = null + *+ * + * @param value the Integer to convert + * @return Boolean.TRUE if non-zero, Boolean.FALSE if zero, + * {@code null} if {@code null} input + */ + public static Boolean toBooleanObject(final Integer value) { + if (value == null) { + return null; + } + return value.intValue() == 0 ? Boolean.FALSE : Boolean.TRUE; + } + + /** + *
Converts an int to a boolean specifying the conversion values.
+ * + *+ * BooleanUtils.toBoolean(0, 1, 0) = false + * BooleanUtils.toBoolean(1, 1, 0) = true + * BooleanUtils.toBoolean(2, 1, 2) = false + * BooleanUtils.toBoolean(2, 2, 0) = true + *+ * + * @param value the Integer to convert + * @param trueValue the value to match for {@code true} + * @param falseValue the value to match for {@code false} + * @return {@code true} or {@code false} + * @throws IllegalArgumentException if no match + */ + public static boolean toBoolean(final int value, final int trueValue, final int falseValue) { + if (value == trueValue) { + return true; + } + if (value == falseValue) { + return false; + } + // no match + throw new IllegalArgumentException("The Integer did not match either specified value"); + } + + /** + *
Converts an Integer to a boolean specifying the conversion values.
+ * + *+ * BooleanUtils.toBoolean(Integer.valueOf(0), Integer.valueOf(1), Integer.valueOf(0)) = false + * BooleanUtils.toBoolean(Integer.valueOf(1), Integer.valueOf(1), Integer.valueOf(0)) = true + * BooleanUtils.toBoolean(Integer.valueOf(2), Integer.valueOf(1), Integer.valueOf(2)) = false + * BooleanUtils.toBoolean(Integer.valueOf(2), Integer.valueOf(2), Integer.valueOf(0)) = true + * BooleanUtils.toBoolean(null, null, Integer.valueOf(0)) = true + *+ * + * @param value the Integer to convert + * @param trueValue the value to match for {@code true}, may be {@code null} + * @param falseValue the value to match for {@code false}, may be {@code null} + * @return {@code true} or {@code false} + * @throws IllegalArgumentException if no match + */ + public static boolean toBoolean(final Integer value, final Integer trueValue, final Integer falseValue) { + if (value == null) { + if (trueValue == null) { + return true; + } + if (falseValue == null) { + return false; + } + } else if (value.equals(trueValue)) { + return true; + } else if (value.equals(falseValue)) { + return false; + } + // no match + throw new IllegalArgumentException("The Integer did not match either specified value"); + } + + /** + *
Converts an int to a Boolean specifying the conversion values.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.toBooleanObject(0, 0, 2, 3) = Boolean.TRUE + * BooleanUtils.toBooleanObject(2, 1, 2, 3) = Boolean.FALSE + * BooleanUtils.toBooleanObject(3, 1, 2, 3) = null + *+ * + * @param value the Integer to convert + * @param trueValue the value to match for {@code true} + * @param falseValue the value to match for {@code false} + * @param nullValue the value to to match for {@code null} + * @return Boolean.TRUE, Boolean.FALSE, or {@code null} + * @throws IllegalArgumentException if no match + */ + public static Boolean toBooleanObject(final int value, final int trueValue, final int falseValue, final int nullValue) { + if (value == trueValue) { + return Boolean.TRUE; + } + if (value == falseValue) { + return Boolean.FALSE; + } + if (value == nullValue) { + return null; + } + // no match + throw new IllegalArgumentException("The Integer did not match any specified value"); + } + + /** + *
Converts an Integer to a Boolean specifying the conversion values.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.toBooleanObject(Integer.valueOf(0), Integer.valueOf(0), Integer.valueOf(2), Integer.valueOf(3)) = Boolean.TRUE + * BooleanUtils.toBooleanObject(Integer.valueOf(2), Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(3)) = Boolean.FALSE + * BooleanUtils.toBooleanObject(Integer.valueOf(3), Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(3)) = null + *+ * + * @param value the Integer to convert + * @param trueValue the value to match for {@code true}, may be {@code null} + * @param falseValue the value to match for {@code false}, may be {@code null} + * @param nullValue the value to to match for {@code null}, may be {@code null} + * @return Boolean.TRUE, Boolean.FALSE, or {@code null} + * @throws IllegalArgumentException if no match + */ + public static Boolean toBooleanObject(final Integer value, final Integer trueValue, final Integer falseValue, final Integer nullValue) { + if (value == null) { + if (trueValue == null) { + return Boolean.TRUE; + } + if (falseValue == null) { + return Boolean.FALSE; + } + if (nullValue == null) { + return null; + } + } else if (value.equals(trueValue)) { + return Boolean.TRUE; + } else if (value.equals(falseValue)) { + return Boolean.FALSE; + } else if (value.equals(nullValue)) { + return null; + } + // no match + throw new IllegalArgumentException("The Integer did not match any specified value"); + } + + // Boolean to Integer methods + //----------------------------------------------------------------------- + /** + *
Converts a boolean to an int using the convention that + * {@code zero} is {@code false}.
+ * + *+ * BooleanUtils.toInteger(true) = 1 + * BooleanUtils.toInteger(false) = 0 + *+ * + * @param bool the boolean to convert + * @return one if {@code true}, zero if {@code false} + */ + public static int toInteger(final boolean bool) { + return bool ? 1 : 0; + } + + /** + *
Converts a boolean to an Integer using the convention that + * {@code zero} is {@code false}.
+ * + *+ * BooleanUtils.toIntegerObject(true) = Integer.valueOf(1) + * BooleanUtils.toIntegerObject(false) = Integer.valueOf(0) + *+ * + * @param bool the boolean to convert + * @return one if {@code true}, zero if {@code false} + */ + public static Integer toIntegerObject(final boolean bool) { + return bool ? NumberUtils.INTEGER_ONE : NumberUtils.INTEGER_ZERO; + } + + /** + *
Converts a Boolean to a Integer using the convention that + * {@code zero} is {@code false}.
+ * + *{@code null} will be converted to {@code null}.
+ * + *+ * BooleanUtils.toIntegerObject(Boolean.TRUE) = Integer.valueOf(1) + * BooleanUtils.toIntegerObject(Boolean.FALSE) = Integer.valueOf(0) + *+ * + * @param bool the Boolean to convert + * @return one if Boolean.TRUE, zero if Boolean.FALSE, {@code null} if {@code null} + */ +/** + *
Converts a Boolean to a Integer using the convention that + * {@code zero} is {@code false}.
+ * + *{@code null} will be converted to {@code null}.
+ * + *+ * BooleanUtils.toIntegerObject(Boolean.TRUE) = Integer.valueOf(1) + * BooleanUtils.toIntegerObject(Boolean.FALSE) = Integer.valueOf(0) + *+ * + * @param bool + * the Boolean to convert + * @return one if Boolean.TRUE, zero if Boolean.FALSE, {@code null} if {@code null} + */ +public static java.lang.Integer toIntegerObject(final java.lang.Boolean bool) { + { + return /* NPEX_NULL_EXP */ + bool.booleanValue() ? org.apache.commons.lang3.math.NumberUtils.INTEGER_ONE : org.apache.commons.lang3.math.NumberUtils.INTEGER_ZERO; + } +} + + /** + *
Converts a boolean to an int specifying the conversion values.
+ * + *+ * BooleanUtils.toInteger(true, 1, 0) = 1 + * BooleanUtils.toInteger(false, 1, 0) = 0 + *+ * + * @param bool the to convert + * @param trueValue the value to return if {@code true} + * @param falseValue the value to return if {@code false} + * @return the appropriate value + */ + public static int toInteger(final boolean bool, final int trueValue, final int falseValue) { + return bool ? trueValue : falseValue; + } + + /** + *
Converts a Boolean to an int specifying the conversion values.
+ * + *+ * BooleanUtils.toInteger(Boolean.TRUE, 1, 0, 2) = 1 + * BooleanUtils.toInteger(Boolean.FALSE, 1, 0, 2) = 0 + * BooleanUtils.toInteger(null, 1, 0, 2) = 2 + *+ * + * @param bool the Boolean to convert + * @param trueValue the value to return if {@code true} + * @param falseValue the value to return if {@code false} + * @param nullValue the value to return if {@code null} + * @return the appropriate value + */ + public static int toInteger(final Boolean bool, final int trueValue, final int falseValue, final int nullValue) { + if (bool == null) { + return nullValue; + } + return bool.booleanValue() ? trueValue : falseValue; + } + + /** + *
Converts a boolean to an Integer specifying the conversion values.
+ * + *+ * BooleanUtils.toIntegerObject(true, Integer.valueOf(1), Integer.valueOf(0)) = Integer.valueOf(1) + * BooleanUtils.toIntegerObject(false, Integer.valueOf(1), Integer.valueOf(0)) = Integer.valueOf(0) + *+ * + * @param bool the to convert + * @param trueValue the value to return if {@code true}, may be {@code null} + * @param falseValue the value to return if {@code false}, may be {@code null} + * @return the appropriate value + */ + public static Integer toIntegerObject(final boolean bool, final Integer trueValue, final Integer falseValue) { + return bool ? trueValue : falseValue; + } + + /** + *
Converts a Boolean to an Integer specifying the conversion values.
+ * + *+ * BooleanUtils.toIntegerObject(Boolean.TRUE, Integer.valueOf(1), Integer.valueOf(0), Integer.valueOf(2)) = Integer.valueOf(1) + * BooleanUtils.toIntegerObject(Boolean.FALSE, Integer.valueOf(1), Integer.valueOf(0), Integer.valueOf(2)) = Integer.valueOf(0) + * BooleanUtils.toIntegerObject(null, Integer.valueOf(1), Integer.valueOf(0), Integer.valueOf(2)) = Integer.valueOf(2) + *+ * + * @param bool the Boolean to convert + * @param trueValue the value to return if {@code true}, may be {@code null} + * @param falseValue the value to return if {@code false}, may be {@code null} + * @param nullValue the value to return if {@code null}, may be {@code null} + * @return the appropriate value + */ + public static Integer toIntegerObject(final Boolean bool, final Integer trueValue, final Integer falseValue, final Integer nullValue) { + if (bool == null) { + return nullValue; + } + return bool.booleanValue() ? trueValue : falseValue; + } + + // String to Boolean methods + //----------------------------------------------------------------------- + /** + *
Converts a String to a Boolean.
+ * + *{@code 'true'}, {@code 'on'}, {@code 'y'}, {@code 't'} or {@code 'yes'} + * (case insensitive) will return {@code true}. + * {@code 'false'}, {@code 'off'}, {@code 'n'}, {@code 'f'} or {@code 'no'} + * (case insensitive) will return {@code false}. + * Otherwise, {@code null} is returned.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * // N.B. case is not significant + * BooleanUtils.toBooleanObject(null) = null + * BooleanUtils.toBooleanObject("true") = Boolean.TRUE + * BooleanUtils.toBooleanObject("T") = Boolean.TRUE // i.e. T[RUE] + * BooleanUtils.toBooleanObject("false") = Boolean.FALSE + * BooleanUtils.toBooleanObject("f") = Boolean.FALSE // i.e. f[alse] + * BooleanUtils.toBooleanObject("No") = Boolean.FALSE + * BooleanUtils.toBooleanObject("n") = Boolean.FALSE // i.e. n[o] + * BooleanUtils.toBooleanObject("on") = Boolean.TRUE + * BooleanUtils.toBooleanObject("ON") = Boolean.TRUE + * BooleanUtils.toBooleanObject("off") = Boolean.FALSE + * BooleanUtils.toBooleanObject("oFf") = Boolean.FALSE + * BooleanUtils.toBooleanObject("yes") = Boolean.TRUE + * BooleanUtils.toBooleanObject("Y") = Boolean.TRUE // i.e. Y[ES] + * BooleanUtils.toBooleanObject("blue") = null + * BooleanUtils.toBooleanObject("true ") = null // trailing space (too long) + * BooleanUtils.toBooleanObject("ono") = null // does not match on or no + *+ * + * @param str the String to check; upper and lower case are treated as the same + * @return the Boolean value of the string, {@code null} if no match or {@code null} input + */ + public static Boolean toBooleanObject(final String str) { + // Previously used equalsIgnoreCase, which was fast for interned 'true'. + // Non interned 'true' matched 15 times slower. + // + // Optimisation provides same performance as before for interned 'true'. + // Similar performance for null, 'false', and other strings not length 2/3/4. + // 'true'/'TRUE' match 4 times slower, 'tRUE'/'True' 7 times slower. + if (str == "true") { + return Boolean.TRUE; + } + if (str == null) { + return null; + } + switch (str.length()) { + case 1: { + final char ch0 = str.charAt(0); + if (ch0 == 'y' || ch0 == 'Y' || + ch0 == 't' || ch0 == 'T') { + return Boolean.TRUE; + } + if (ch0 == 'n' || ch0 == 'N' || + ch0 == 'f' || ch0 == 'F') { + return Boolean.FALSE; + } + break; + } + case 2: { + final char ch0 = str.charAt(0); + final char ch1 = str.charAt(1); + if ((ch0 == 'o' || ch0 == 'O') && + (ch1 == 'n' || ch1 == 'N') ) { + return Boolean.TRUE; + } + if ((ch0 == 'n' || ch0 == 'N') && + (ch1 == 'o' || ch1 == 'O') ) { + return Boolean.FALSE; + } + break; + } + case 3: { + final char ch0 = str.charAt(0); + final char ch1 = str.charAt(1); + final char ch2 = str.charAt(2); + if ((ch0 == 'y' || ch0 == 'Y') && + (ch1 == 'e' || ch1 == 'E') && + (ch2 == 's' || ch2 == 'S') ) { + return Boolean.TRUE; + } + if ((ch0 == 'o' || ch0 == 'O') && + (ch1 == 'f' || ch1 == 'F') && + (ch2 == 'f' || ch2 == 'F') ) { + return Boolean.FALSE; + } + break; + } + case 4: { + final char ch0 = str.charAt(0); + final char ch1 = str.charAt(1); + final char ch2 = str.charAt(2); + final char ch3 = str.charAt(3); + if ((ch0 == 't' || ch0 == 'T') && + (ch1 == 'r' || ch1 == 'R') && + (ch2 == 'u' || ch2 == 'U') && + (ch3 == 'e' || ch3 == 'E') ) { + return Boolean.TRUE; + } + break; + } + case 5: { + final char ch0 = str.charAt(0); + final char ch1 = str.charAt(1); + final char ch2 = str.charAt(2); + final char ch3 = str.charAt(3); + final char ch4 = str.charAt(4); + if ((ch0 == 'f' || ch0 == 'F') && + (ch1 == 'a' || ch1 == 'A') && + (ch2 == 'l' || ch2 == 'L') && + (ch3 == 's' || ch3 == 'S') && + (ch4 == 'e' || ch4 == 'E') ) { + return Boolean.FALSE; + } + break; + } + default: + break; + } + + return null; + } + + /** + *
Converts a String to a Boolean throwing an exception if no match.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.toBooleanObject("true", "true", "false", "null") = Boolean.TRUE + * BooleanUtils.toBooleanObject("false", "true", "false", "null") = Boolean.FALSE + * BooleanUtils.toBooleanObject("null", "true", "false", "null") = null + *+ * + * @param str the String to check + * @param trueString the String to match for {@code true} (case sensitive), may be {@code null} + * @param falseString the String to match for {@code false} (case sensitive), may be {@code null} + * @param nullString the String to match for {@code null} (case sensitive), may be {@code null} + * @return the Boolean value of the string, {@code null} if either the String matches {@code nullString} + * or if {@code null} input and {@code nullString} is {@code null} + * @throws IllegalArgumentException if the String doesn't match + */ + public static Boolean toBooleanObject(final String str, final String trueString, final String falseString, final String nullString) { + if (str == null) { + if (trueString == null) { + return Boolean.TRUE; + } + if (falseString == null) { + return Boolean.FALSE; + } + if (nullString == null) { + return null; + } + } else if (str.equals(trueString)) { + return Boolean.TRUE; + } else if (str.equals(falseString)) { + return Boolean.FALSE; + } else if (str.equals(nullString)) { + return null; + } + // no match + throw new IllegalArgumentException("The String did not match any specified value"); + } + + // String to boolean methods + //----------------------------------------------------------------------- + /** + *
Converts a String to a boolean (optimised for performance).
+ * + *{@code 'true'}, {@code 'on'}, {@code 'y'}, {@code 't'} or {@code 'yes'} + * (case insensitive) will return {@code true}. Otherwise, + * {@code false} is returned.
+ * + *This method performs 4 times faster (JDK1.4) than + * {@code Boolean.valueOf(String)}. However, this method accepts + * 'on' and 'yes', 't', 'y' as true values. + * + *
+ * BooleanUtils.toBoolean(null) = false + * BooleanUtils.toBoolean("true") = true + * BooleanUtils.toBoolean("TRUE") = true + * BooleanUtils.toBoolean("tRUe") = true + * BooleanUtils.toBoolean("on") = true + * BooleanUtils.toBoolean("yes") = true + * BooleanUtils.toBoolean("false") = false + * BooleanUtils.toBoolean("x gti") = false + * BooleanUtils.toBooleanObject("y") = true + * BooleanUtils.toBooleanObject("n") = false + * BooleanUtils.toBooleanObject("t") = true + * BooleanUtils.toBooleanObject("f") = false + *+ * + * @param str the String to check + * @return the boolean value of the string, {@code false} if no match or the String is null + */ + public static boolean toBoolean(final String str) { + return toBooleanObject(str) == Boolean.TRUE; + } + + /** + *
Converts a String to a Boolean throwing an exception if no match found.
+ * + *+ * BooleanUtils.toBoolean("true", "true", "false") = true + * BooleanUtils.toBoolean("false", "true", "false") = false + *+ * + * @param str the String to check + * @param trueString the String to match for {@code true} (case sensitive), may be {@code null} + * @param falseString the String to match for {@code false} (case sensitive), may be {@code null} + * @return the boolean value of the string + * @throws IllegalArgumentException if the String doesn't match + */ + public static boolean toBoolean(final String str, final String trueString, final String falseString) { + if (str == trueString) { + return true; + } else if (str == falseString) { + return false; + } else if (str != null) { + if (str.equals(trueString)) { + return true; + } else if (str.equals(falseString)) { + return false; + } + } + // no match + throw new IllegalArgumentException("The String did not match either specified value"); + } + + // Boolean to String methods + //----------------------------------------------------------------------- + /** + *
Converts a Boolean to a String returning {@code 'true'}, + * {@code 'false'}, or {@code null}.
+ * + *+ * BooleanUtils.toStringTrueFalse(Boolean.TRUE) = "true" + * BooleanUtils.toStringTrueFalse(Boolean.FALSE) = "false" + * BooleanUtils.toStringTrueFalse(null) = null; + *+ * + * @param bool the Boolean to check + * @return {@code 'true'}, {@code 'false'}, or {@code null} + */ + public static String toStringTrueFalse(final Boolean bool) { + return toString(bool, "true", "false", null); + } + + /** + *
Converts a Boolean to a String returning {@code 'on'}, + * {@code 'off'}, or {@code null}.
+ * + *+ * BooleanUtils.toStringOnOff(Boolean.TRUE) = "on" + * BooleanUtils.toStringOnOff(Boolean.FALSE) = "off" + * BooleanUtils.toStringOnOff(null) = null; + *+ * + * @param bool the Boolean to check + * @return {@code 'on'}, {@code 'off'}, or {@code null} + */ + public static String toStringOnOff(final Boolean bool) { + return toString(bool, "on", "off", null); + } + + /** + *
Converts a Boolean to a String returning {@code 'yes'}, + * {@code 'no'}, or {@code null}.
+ * + *+ * BooleanUtils.toStringYesNo(Boolean.TRUE) = "yes" + * BooleanUtils.toStringYesNo(Boolean.FALSE) = "no" + * BooleanUtils.toStringYesNo(null) = null; + *+ * + * @param bool the Boolean to check + * @return {@code 'yes'}, {@code 'no'}, or {@code null} + */ + public static String toStringYesNo(final Boolean bool) { + return toString(bool, "yes", "no", null); + } + + /** + *
Converts a Boolean to a String returning one of the input Strings.
+ * + *+ * BooleanUtils.toString(Boolean.TRUE, "true", "false", null) = "true" + * BooleanUtils.toString(Boolean.FALSE, "true", "false", null) = "false" + * BooleanUtils.toString(null, "true", "false", null) = null; + *+ * + * @param bool the Boolean to check + * @param trueString the String to return if {@code true}, may be {@code null} + * @param falseString the String to return if {@code false}, may be {@code null} + * @param nullString the String to return if {@code null}, may be {@code null} + * @return one of the three input Strings + */ + public static String toString(final Boolean bool, final String trueString, final String falseString, final String nullString) { + if (bool == null) { + return nullString; + } + return bool.booleanValue() ? trueString : falseString; + } + + // boolean to String methods + //----------------------------------------------------------------------- + /** + *
Converts a boolean to a String returning {@code 'true'} + * or {@code 'false'}.
+ * + *+ * BooleanUtils.toStringTrueFalse(true) = "true" + * BooleanUtils.toStringTrueFalse(false) = "false" + *+ * + * @param bool the Boolean to check + * @return {@code 'true'}, {@code 'false'}, or {@code null} + */ + public static String toStringTrueFalse(final boolean bool) { + return toString(bool, "true", "false"); + } + + /** + *
Converts a boolean to a String returning {@code 'on'} + * or {@code 'off'}.
+ * + *+ * BooleanUtils.toStringOnOff(true) = "on" + * BooleanUtils.toStringOnOff(false) = "off" + *+ * + * @param bool the Boolean to check + * @return {@code 'on'}, {@code 'off'}, or {@code null} + */ + public static String toStringOnOff(final boolean bool) { + return toString(bool, "on", "off"); + } + + /** + *
Converts a boolean to a String returning {@code 'yes'} + * or {@code 'no'}.
+ * + *+ * BooleanUtils.toStringYesNo(true) = "yes" + * BooleanUtils.toStringYesNo(false) = "no" + *+ * + * @param bool the Boolean to check + * @return {@code 'yes'}, {@code 'no'}, or {@code null} + */ + public static String toStringYesNo(final boolean bool) { + return toString(bool, "yes", "no"); + } + + /** + *
Converts a boolean to a String returning one of the input Strings.
+ * + *+ * BooleanUtils.toString(true, "true", "false") = "true" + * BooleanUtils.toString(false, "true", "false") = "false" + *+ * + * @param bool the Boolean to check + * @param trueString the String to return if {@code true}, may be {@code null} + * @param falseString the String to return if {@code false}, may be {@code null} + * @return one of the two input Strings + */ + public static String toString(final boolean bool, final String trueString, final String falseString) { + return bool ? trueString : falseString; + } + + // logical operations + // ---------------------------------------------------------------------- + /** + *
Performs an and on a set of booleans.
+ * + *+ * BooleanUtils.and(true, true) = true + * BooleanUtils.and(false, false) = false + * BooleanUtils.and(true, false) = false + * BooleanUtils.and(true, true, false) = false + * BooleanUtils.and(true, true, true) = true + *+ * + * @param array an array of {@code boolean}s + * @return {@code true} if the and is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + * @since 3.0.1 + */ + public static boolean and(final boolean... array) { + // Validates input + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + for (final boolean element : array) { + if (!element) { + return false; + } + } + return true; + } + + /** + *
Performs an and on an array of Booleans.
+ * + *+ * BooleanUtils.and(Boolean.TRUE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.and(Boolean.FALSE, Boolean.FALSE) = Boolean.FALSE + * BooleanUtils.and(Boolean.TRUE, Boolean.FALSE) = Boolean.FALSE + * BooleanUtils.and(Boolean.TRUE, Boolean.TRUE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.and(Boolean.FALSE, Boolean.FALSE, Boolean.TRUE) = Boolean.FALSE + * BooleanUtils.and(Boolean.TRUE, Boolean.FALSE, Boolean.TRUE) = Boolean.FALSE + *+ * + * @param array an array of {@code Boolean}s + * @return {@code true} if the and is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + * @throws IllegalArgumentException if {@code array} contains a {@code null} + * @since 3.0.1 + */ + public static Boolean and(final Boolean... array) { + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + try { + final boolean[] primitive = ArrayUtils.toPrimitive(array); + return and(primitive) ? Boolean.TRUE : Boolean.FALSE; + } catch (final NullPointerException ex) { + throw new IllegalArgumentException("The array must not contain any null elements"); + } + } + + /** + *
Performs an or on a set of booleans.
+ * + *+ * BooleanUtils.or(true, true) = true + * BooleanUtils.or(false, false) = false + * BooleanUtils.or(true, false) = true + * BooleanUtils.or(true, true, false) = true + * BooleanUtils.or(true, true, true) = true + * BooleanUtils.or(false, false, false) = false + *+ * + * @param array an array of {@code boolean}s + * @return {@code true} if the or is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + * @since 3.0.1 + */ + public static boolean or(final boolean... array) { + // Validates input + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + for (final boolean element : array) { + if (element) { + return true; + } + } + return false; + } + + /** + *
Performs an or on an array of Booleans.
+ * + *+ * BooleanUtils.or(Boolean.TRUE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.or(Boolean.FALSE, Boolean.FALSE) = Boolean.FALSE + * BooleanUtils.or(Boolean.TRUE, Boolean.FALSE) = Boolean.TRUE + * BooleanUtils.or(Boolean.TRUE, Boolean.TRUE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.or(Boolean.FALSE, Boolean.FALSE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.or(Boolean.TRUE, Boolean.FALSE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.or(Boolean.FALSE, Boolean.FALSE, Boolean.FALSE) = Boolean.FALSE + *+ * + * @param array an array of {@code Boolean}s + * @return {@code true} if the or is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + * @throws IllegalArgumentException if {@code array} contains a {@code null} + * @since 3.0.1 + */ + public static Boolean or(final Boolean... array) { + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + try { + final boolean[] primitive = ArrayUtils.toPrimitive(array); + return or(primitive) ? Boolean.TRUE : Boolean.FALSE; + } catch (final NullPointerException ex) { + throw new IllegalArgumentException("The array must not contain any null elements"); + } + } + + /** + *
Performs an xor on a set of booleans.
+ * + *+ * BooleanUtils.xor(true, true) = false + * BooleanUtils.xor(false, false) = false + * BooleanUtils.xor(true, false) = true + * BooleanUtils.xor(true, true) = false + * BooleanUtils.xor(false, false) = false + * BooleanUtils.xor(true, false) = true + *+ * + * @param array an array of {@code boolean}s + * @return {@code true} if the xor is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + */ + public static boolean xor(final boolean... array) { + // Validates input + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + + // false if the neutral element of the xor operator + boolean result = false; + for (final boolean element : array) { + result ^= element; + } + + return result; + } + + /** + *
Performs an xor on an array of Booleans.
+ * + *+ * BooleanUtils.xor(new Boolean[] { Boolean.TRUE, Boolean.TRUE }) = Boolean.FALSE + * BooleanUtils.xor(new Boolean[] { Boolean.FALSE, Boolean.FALSE }) = Boolean.FALSE + * BooleanUtils.xor(new Boolean[] { Boolean.TRUE, Boolean.FALSE }) = Boolean.TRUE + *+ * + * @param array an array of {@code Boolean}s + * @return {@code true} if the xor is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + * @throws IllegalArgumentException if {@code array} contains a {@code null} + */ + public static Boolean xor(final Boolean... array) { + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + try { + final boolean[] primitive = ArrayUtils.toPrimitive(array); + return xor(primitive) ? Boolean.TRUE : Boolean.FALSE; + } catch (final NullPointerException ex) { + throw new IllegalArgumentException("The array must not contain any null elements"); + } + } + + /** + *
Compares two {@code boolean} values. This is the same functionality as provided in Java 7.
+ * + * @param x the first {@code boolean} to compare + * @param y the second {@code boolean} to compare + * @return the value {@code 0} if {@code x == y}; + * a value less than {@code 0} if {@code !x && y}; and + * a value greater than {@code 0} if {@code x && !y} + * @since 3.4 + */ + public static int compare(boolean x, boolean y) { + if (x == y) { + return 0; + } + return x ? 1 : -1; + } + +} diff --git a/Java/commons-lang-BooleanUtils_426/metadata.json b/Java/commons-lang-BooleanUtils_426/metadata.json new file mode 100644 index 000000000..d90543c18 --- /dev/null +++ b/Java/commons-lang-BooleanUtils_426/metadata.json @@ -0,0 +1,21 @@ +{ + "language": "java", + "id": "commons-lang-BooleanUtils_426", + "buggyPath": ".", + "referencePath": null, + "buildCommand": "mvn package -V -B -Denforcer.skip=true -Dcheckstyle.skip=true -Dcobertura.skip=true -Drat.skip=true -Dlicense.skip=true -Dfindbugs.skip=true -Dgpg.skip=true -Dskip.npm=true -Dskip.gulp=true -Dskip.bower=true -Drat.numUnapprovedLicenses=100 -DskipTests=true -DskipITs=true -Dtest=None -DfailIfNoTests=false", + "testCommand": "mvn test -V -B -Denforcer.skip=true -Dcheckstyle.skip=true -Dcobertura.skip=true -Drat.skip=true -Dlicense.skip=true -Dfindbugs.skip=true -Dgpg.skip=true -Dskip.npm=true -Dskip.gulp=true -Dskip.bower=true -Drat.numUnapprovedLicenses=100", + "categories": [ + "safety", + "npe" + ], + "npe": { + "filepath": "src/main/java/org/apache/commons/lang3/BooleanUtils.java", + "line": 443, + "npe_method": "toIntegerObject", + "deref_field": "bool", + "npe_class": "BooleanUtils", + "repo": "commons-lang", + "bug_id": "BooleanUtils_426" + } +} diff --git a/Java/commons-lang-BooleanUtils_426/npe.json b/Java/commons-lang-BooleanUtils_426/npe.json new file mode 100644 index 000000000..7db9658d4 --- /dev/null +++ b/Java/commons-lang-BooleanUtils_426/npe.json @@ -0,0 +1,7 @@ +{ + "filepath": "src/main/java/org/apache/commons/lang3/BooleanUtils.java", + "line": 443, + "npe_method": "toIntegerObject", + "deref_field": "bool", + "npe_class": "BooleanUtils" +} \ No newline at end of file diff --git a/Java/commons-lang-BooleanUtils_465/Dockerfile b/Java/commons-lang-BooleanUtils_465/Dockerfile new file mode 100644 index 000000000..7b7fbe349 --- /dev/null +++ b/Java/commons-lang-BooleanUtils_465/Dockerfile @@ -0,0 +1,18 @@ +FROM ghcr.io/kupl/starlab-benchmarks/java-base:commons-lang + +ENV TZ=Asia/Seoul + +COPY ./metadata.json . +COPY ./npe.json . +COPY ./buggy.java /tmp/buggy.java +RUN export BUGGY_PATH=$(cat metadata.json | jq -r ".npe.filepath") \ + && export BUGGY_LINE=$(cat metadata.json | jq -r ".npe.line") \ + && export BUGGY_MTHD=$(cat metadata.json | jq -r ".npe.npe_method") \ + && mv /tmp/buggy.java $BUGGY_PATH \ + && echo "[{\"filepath\": \"$BUGGY_PATH\", \"line\": $BUGGY_LINE, \"method_name\": \"$BUGGY_MTHD\"}]" | jq . > traces.json + +RUN git init . && git add -A + +RUN $(cat metadata.json | jq -r ".buildCommand") + +RUN $(cat metadata.json | jq -r ".testCommand"); if [ $? -eq 0 ]; then exit 1; fi diff --git a/Java/commons-lang-BooleanUtils_465/buggy.java b/Java/commons-lang-BooleanUtils_465/buggy.java new file mode 100644 index 000000000..b7da317a8 --- /dev/null +++ b/Java/commons-lang-BooleanUtils_465/buggy.java @@ -0,0 +1,1123 @@ +/* + * 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; + +import org.apache.commons.lang3.math.NumberUtils; + +/** + *Operations on boolean primitives and Boolean objects.
+ * + *This class tries to handle {@code null} input gracefully. + * An exception will not be thrown for a {@code null} input. + * Each method documents its behaviour in more detail.
+ * + *#ThreadSafe#
+ * @since 2.0 + */ +public class BooleanUtils { + + /** + *{@code BooleanUtils} instances should NOT be constructed in standard programming. + * Instead, the class should be used as {@code BooleanUtils.negate(true);}.
+ * + *This constructor is public to permit tools that require a JavaBean instance + * to operate.
+ */ + public BooleanUtils() { + super(); + } + + // Boolean utilities + //-------------------------------------------------------------------------- + /** + *Negates the specified boolean.
+ * + *If {@code null} is passed in, {@code null} will be returned.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.negate(Boolean.TRUE) = Boolean.FALSE; + * BooleanUtils.negate(Boolean.FALSE) = Boolean.TRUE; + * BooleanUtils.negate(null) = null; + *+ * + * @param bool the Boolean to negate, may be null + * @return the negated Boolean, or {@code null} if {@code null} input + */ + public static Boolean negate(final Boolean bool) { + if (bool == null) { + return null; + } + return bool.booleanValue() ? Boolean.FALSE : Boolean.TRUE; + } + + // boolean Boolean methods + //----------------------------------------------------------------------- + /** + *
Checks if a {@code Boolean} value is {@code true}, + * handling {@code null} by returning {@code false}.
+ * + *+ * BooleanUtils.isTrue(Boolean.TRUE) = true + * BooleanUtils.isTrue(Boolean.FALSE) = false + * BooleanUtils.isTrue(null) = false + *+ * + * @param bool the boolean to check, null returns {@code false} + * @return {@code true} only if the input is non-null and true + * @since 2.1 + */ + public static boolean isTrue(final Boolean bool) { + return Boolean.TRUE.equals(bool); + } + + /** + *
Checks if a {@code Boolean} value is not {@code true}, + * handling {@code null} by returning {@code true}.
+ * + *+ * BooleanUtils.isNotTrue(Boolean.TRUE) = false + * BooleanUtils.isNotTrue(Boolean.FALSE) = true + * BooleanUtils.isNotTrue(null) = true + *+ * + * @param bool the boolean to check, null returns {@code true} + * @return {@code true} if the input is null or false + * @since 2.3 + */ + public static boolean isNotTrue(final Boolean bool) { + return !isTrue(bool); + } + + /** + *
Checks if a {@code Boolean} value is {@code false}, + * handling {@code null} by returning {@code false}.
+ * + *+ * BooleanUtils.isFalse(Boolean.TRUE) = false + * BooleanUtils.isFalse(Boolean.FALSE) = true + * BooleanUtils.isFalse(null) = false + *+ * + * @param bool the boolean to check, null returns {@code false} + * @return {@code true} only if the input is non-null and false + * @since 2.1 + */ + public static boolean isFalse(final Boolean bool) { + return Boolean.FALSE.equals(bool); + } + + /** + *
Checks if a {@code Boolean} value is not {@code false}, + * handling {@code null} by returning {@code true}.
+ * + *+ * BooleanUtils.isNotFalse(Boolean.TRUE) = true + * BooleanUtils.isNotFalse(Boolean.FALSE) = false + * BooleanUtils.isNotFalse(null) = true + *+ * + * @param bool the boolean to check, null returns {@code true} + * @return {@code true} if the input is null or true + * @since 2.3 + */ + public static boolean isNotFalse(final Boolean bool) { + return !isFalse(bool); + } + + //----------------------------------------------------------------------- + /** + *
Converts a Boolean to a boolean handling {@code null} + * by returning {@code false}.
+ * + *+ * BooleanUtils.toBoolean(Boolean.TRUE) = true + * BooleanUtils.toBoolean(Boolean.FALSE) = false + * BooleanUtils.toBoolean(null) = false + *+ * + * @param bool the boolean to convert + * @return {@code true} or {@code false}, {@code null} returns {@code false} + */ + public static boolean toBoolean(final Boolean bool) { + return bool != null && bool.booleanValue(); + } + + /** + *
Converts a Boolean to a boolean handling {@code null}.
+ * + *+ * BooleanUtils.toBooleanDefaultIfNull(Boolean.TRUE, false) = true + * BooleanUtils.toBooleanDefaultIfNull(Boolean.FALSE, true) = false + * BooleanUtils.toBooleanDefaultIfNull(null, true) = true + *+ * + * @param bool the boolean to convert + * @param valueIfNull the boolean value to return if {@code null} + * @return {@code true} or {@code false} + */ + public static boolean toBooleanDefaultIfNull(final Boolean bool, final boolean valueIfNull) { + if (bool == null) { + return valueIfNull; + } + return bool.booleanValue(); + } + + // Integer to Boolean methods + //----------------------------------------------------------------------- + /** + *
Converts an int to a boolean using the convention that {@code zero} + * is {@code false}.
+ * + *+ * BooleanUtils.toBoolean(0) = false + * BooleanUtils.toBoolean(1) = true + * BooleanUtils.toBoolean(2) = true + *+ * + * @param value the int to convert + * @return {@code true} if non-zero, {@code false} + * if zero + */ + public static boolean toBoolean(final int value) { + return value != 0; + } + + /** + *
Converts an int to a Boolean using the convention that {@code zero} + * is {@code false}.
+ * + *+ * BooleanUtils.toBoolean(0) = Boolean.FALSE + * BooleanUtils.toBoolean(1) = Boolean.TRUE + * BooleanUtils.toBoolean(2) = Boolean.TRUE + *+ * + * @param value the int to convert + * @return Boolean.TRUE if non-zero, Boolean.FALSE if zero, + * {@code null} if {@code null} + */ + public static Boolean toBooleanObject(final int value) { + return value == 0 ? Boolean.FALSE : Boolean.TRUE; + } + + /** + *
Converts an Integer to a Boolean using the convention that {@code zero} + * is {@code false}.
+ * + *{@code null} will be converted to {@code null}.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.toBoolean(Integer.valueOf(0)) = Boolean.FALSE + * BooleanUtils.toBoolean(Integer.valueOf(1)) = Boolean.TRUE + * BooleanUtils.toBoolean(Integer.valueOf(null)) = null + *+ * + * @param value the Integer to convert + * @return Boolean.TRUE if non-zero, Boolean.FALSE if zero, + * {@code null} if {@code null} input + */ + public static Boolean toBooleanObject(final Integer value) { + if (value == null) { + return null; + } + return value.intValue() == 0 ? Boolean.FALSE : Boolean.TRUE; + } + + /** + *
Converts an int to a boolean specifying the conversion values.
+ * + *+ * BooleanUtils.toBoolean(0, 1, 0) = false + * BooleanUtils.toBoolean(1, 1, 0) = true + * BooleanUtils.toBoolean(2, 1, 2) = false + * BooleanUtils.toBoolean(2, 2, 0) = true + *+ * + * @param value the Integer to convert + * @param trueValue the value to match for {@code true} + * @param falseValue the value to match for {@code false} + * @return {@code true} or {@code false} + * @throws IllegalArgumentException if no match + */ + public static boolean toBoolean(final int value, final int trueValue, final int falseValue) { + if (value == trueValue) { + return true; + } + if (value == falseValue) { + return false; + } + // no match + throw new IllegalArgumentException("The Integer did not match either specified value"); + } + + /** + *
Converts an Integer to a boolean specifying the conversion values.
+ * + *+ * BooleanUtils.toBoolean(Integer.valueOf(0), Integer.valueOf(1), Integer.valueOf(0)) = false + * BooleanUtils.toBoolean(Integer.valueOf(1), Integer.valueOf(1), Integer.valueOf(0)) = true + * BooleanUtils.toBoolean(Integer.valueOf(2), Integer.valueOf(1), Integer.valueOf(2)) = false + * BooleanUtils.toBoolean(Integer.valueOf(2), Integer.valueOf(2), Integer.valueOf(0)) = true + * BooleanUtils.toBoolean(null, null, Integer.valueOf(0)) = true + *+ * + * @param value the Integer to convert + * @param trueValue the value to match for {@code true}, may be {@code null} + * @param falseValue the value to match for {@code false}, may be {@code null} + * @return {@code true} or {@code false} + * @throws IllegalArgumentException if no match + */ + public static boolean toBoolean(final Integer value, final Integer trueValue, final Integer falseValue) { + if (value == null) { + if (trueValue == null) { + return true; + } + if (falseValue == null) { + return false; + } + } else if (value.equals(trueValue)) { + return true; + } else if (value.equals(falseValue)) { + return false; + } + // no match + throw new IllegalArgumentException("The Integer did not match either specified value"); + } + + /** + *
Converts an int to a Boolean specifying the conversion values.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.toBooleanObject(0, 0, 2, 3) = Boolean.TRUE + * BooleanUtils.toBooleanObject(2, 1, 2, 3) = Boolean.FALSE + * BooleanUtils.toBooleanObject(3, 1, 2, 3) = null + *+ * + * @param value the Integer to convert + * @param trueValue the value to match for {@code true} + * @param falseValue the value to match for {@code false} + * @param nullValue the value to to match for {@code null} + * @return Boolean.TRUE, Boolean.FALSE, or {@code null} + * @throws IllegalArgumentException if no match + */ + public static Boolean toBooleanObject(final int value, final int trueValue, final int falseValue, final int nullValue) { + if (value == trueValue) { + return Boolean.TRUE; + } + if (value == falseValue) { + return Boolean.FALSE; + } + if (value == nullValue) { + return null; + } + // no match + throw new IllegalArgumentException("The Integer did not match any specified value"); + } + + /** + *
Converts an Integer to a Boolean specifying the conversion values.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.toBooleanObject(Integer.valueOf(0), Integer.valueOf(0), Integer.valueOf(2), Integer.valueOf(3)) = Boolean.TRUE + * BooleanUtils.toBooleanObject(Integer.valueOf(2), Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(3)) = Boolean.FALSE + * BooleanUtils.toBooleanObject(Integer.valueOf(3), Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(3)) = null + *+ * + * @param value the Integer to convert + * @param trueValue the value to match for {@code true}, may be {@code null} + * @param falseValue the value to match for {@code false}, may be {@code null} + * @param nullValue the value to to match for {@code null}, may be {@code null} + * @return Boolean.TRUE, Boolean.FALSE, or {@code null} + * @throws IllegalArgumentException if no match + */ + public static Boolean toBooleanObject(final Integer value, final Integer trueValue, final Integer falseValue, final Integer nullValue) { + if (value == null) { + if (trueValue == null) { + return Boolean.TRUE; + } + if (falseValue == null) { + return Boolean.FALSE; + } + if (nullValue == null) { + return null; + } + } else if (value.equals(trueValue)) { + return Boolean.TRUE; + } else if (value.equals(falseValue)) { + return Boolean.FALSE; + } else if (value.equals(nullValue)) { + return null; + } + // no match + throw new IllegalArgumentException("The Integer did not match any specified value"); + } + + // Boolean to Integer methods + //----------------------------------------------------------------------- + /** + *
Converts a boolean to an int using the convention that + * {@code zero} is {@code false}.
+ * + *+ * BooleanUtils.toInteger(true) = 1 + * BooleanUtils.toInteger(false) = 0 + *+ * + * @param bool the boolean to convert + * @return one if {@code true}, zero if {@code false} + */ + public static int toInteger(final boolean bool) { + return bool ? 1 : 0; + } + + /** + *
Converts a boolean to an Integer using the convention that + * {@code zero} is {@code false}.
+ * + *+ * BooleanUtils.toIntegerObject(true) = Integer.valueOf(1) + * BooleanUtils.toIntegerObject(false) = Integer.valueOf(0) + *+ * + * @param bool the boolean to convert + * @return one if {@code true}, zero if {@code false} + */ + public static Integer toIntegerObject(final boolean bool) { + return bool ? NumberUtils.INTEGER_ONE : NumberUtils.INTEGER_ZERO; + } + + /** + *
Converts a Boolean to a Integer using the convention that + * {@code zero} is {@code false}.
+ * + *{@code null} will be converted to {@code null}.
+ * + *+ * BooleanUtils.toIntegerObject(Boolean.TRUE) = Integer.valueOf(1) + * BooleanUtils.toIntegerObject(Boolean.FALSE) = Integer.valueOf(0) + *+ * + * @param bool the Boolean to convert + * @return one if Boolean.TRUE, zero if Boolean.FALSE, {@code null} if {@code null} + */ + public static Integer toIntegerObject(final Boolean bool) { + if (bool == null) { + return null; + } + return bool.booleanValue() ? NumberUtils.INTEGER_ONE : NumberUtils.INTEGER_ZERO; + } + + /** + *
Converts a boolean to an int specifying the conversion values.
+ * + *+ * BooleanUtils.toInteger(true, 1, 0) = 1 + * BooleanUtils.toInteger(false, 1, 0) = 0 + *+ * + * @param bool the to convert + * @param trueValue the value to return if {@code true} + * @param falseValue the value to return if {@code false} + * @return the appropriate value + */ + public static int toInteger(final boolean bool, final int trueValue, final int falseValue) { + return bool ? trueValue : falseValue; + } + + /** + *
Converts a Boolean to an int specifying the conversion values.
+ * + *+ * BooleanUtils.toInteger(Boolean.TRUE, 1, 0, 2) = 1 + * BooleanUtils.toInteger(Boolean.FALSE, 1, 0, 2) = 0 + * BooleanUtils.toInteger(null, 1, 0, 2) = 2 + *+ * + * @param bool the Boolean to convert + * @param trueValue the value to return if {@code true} + * @param falseValue the value to return if {@code false} + * @param nullValue the value to return if {@code null} + * @return the appropriate value + */ +/** + *
Converts a Boolean to an int specifying the conversion values.
+ * + *+ * BooleanUtils.toInteger(Boolean.TRUE, 1, 0, 2) = 1 + * BooleanUtils.toInteger(Boolean.FALSE, 1, 0, 2) = 0 + * BooleanUtils.toInteger(null, 1, 0, 2) = 2 + *+ * + * @param bool + * the Boolean to convert + * @param trueValue + * the value to return if {@code true} + * @param falseValue + * the value to return if {@code false} + * @param nullValue + * the value to return if {@code null} + * @return the appropriate value + */ +public static int toInteger(final java.lang.Boolean bool, final int trueValue, final int falseValue, final int nullValue) { + { + return /* NPEX_NULL_EXP */ + bool.booleanValue() ? trueValue : falseValue; + } +} + + /** + *
Converts a boolean to an Integer specifying the conversion values.
+ * + *+ * BooleanUtils.toIntegerObject(true, Integer.valueOf(1), Integer.valueOf(0)) = Integer.valueOf(1) + * BooleanUtils.toIntegerObject(false, Integer.valueOf(1), Integer.valueOf(0)) = Integer.valueOf(0) + *+ * + * @param bool the to convert + * @param trueValue the value to return if {@code true}, may be {@code null} + * @param falseValue the value to return if {@code false}, may be {@code null} + * @return the appropriate value + */ + public static Integer toIntegerObject(final boolean bool, final Integer trueValue, final Integer falseValue) { + return bool ? trueValue : falseValue; + } + + /** + *
Converts a Boolean to an Integer specifying the conversion values.
+ * + *+ * BooleanUtils.toIntegerObject(Boolean.TRUE, Integer.valueOf(1), Integer.valueOf(0), Integer.valueOf(2)) = Integer.valueOf(1) + * BooleanUtils.toIntegerObject(Boolean.FALSE, Integer.valueOf(1), Integer.valueOf(0), Integer.valueOf(2)) = Integer.valueOf(0) + * BooleanUtils.toIntegerObject(null, Integer.valueOf(1), Integer.valueOf(0), Integer.valueOf(2)) = Integer.valueOf(2) + *+ * + * @param bool the Boolean to convert + * @param trueValue the value to return if {@code true}, may be {@code null} + * @param falseValue the value to return if {@code false}, may be {@code null} + * @param nullValue the value to return if {@code null}, may be {@code null} + * @return the appropriate value + */ + public static Integer toIntegerObject(final Boolean bool, final Integer trueValue, final Integer falseValue, final Integer nullValue) { + if (bool == null) { + return nullValue; + } + return bool.booleanValue() ? trueValue : falseValue; + } + + // String to Boolean methods + //----------------------------------------------------------------------- + /** + *
Converts a String to a Boolean.
+ * + *{@code 'true'}, {@code 'on'}, {@code 'y'}, {@code 't'} or {@code 'yes'} + * (case insensitive) will return {@code true}. + * {@code 'false'}, {@code 'off'}, {@code 'n'}, {@code 'f'} or {@code 'no'} + * (case insensitive) will return {@code false}. + * Otherwise, {@code null} is returned.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * // N.B. case is not significant + * BooleanUtils.toBooleanObject(null) = null + * BooleanUtils.toBooleanObject("true") = Boolean.TRUE + * BooleanUtils.toBooleanObject("T") = Boolean.TRUE // i.e. T[RUE] + * BooleanUtils.toBooleanObject("false") = Boolean.FALSE + * BooleanUtils.toBooleanObject("f") = Boolean.FALSE // i.e. f[alse] + * BooleanUtils.toBooleanObject("No") = Boolean.FALSE + * BooleanUtils.toBooleanObject("n") = Boolean.FALSE // i.e. n[o] + * BooleanUtils.toBooleanObject("on") = Boolean.TRUE + * BooleanUtils.toBooleanObject("ON") = Boolean.TRUE + * BooleanUtils.toBooleanObject("off") = Boolean.FALSE + * BooleanUtils.toBooleanObject("oFf") = Boolean.FALSE + * BooleanUtils.toBooleanObject("yes") = Boolean.TRUE + * BooleanUtils.toBooleanObject("Y") = Boolean.TRUE // i.e. Y[ES] + * BooleanUtils.toBooleanObject("blue") = null + * BooleanUtils.toBooleanObject("true ") = null // trailing space (too long) + * BooleanUtils.toBooleanObject("ono") = null // does not match on or no + *+ * + * @param str the String to check; upper and lower case are treated as the same + * @return the Boolean value of the string, {@code null} if no match or {@code null} input + */ + public static Boolean toBooleanObject(final String str) { + // Previously used equalsIgnoreCase, which was fast for interned 'true'. + // Non interned 'true' matched 15 times slower. + // + // Optimisation provides same performance as before for interned 'true'. + // Similar performance for null, 'false', and other strings not length 2/3/4. + // 'true'/'TRUE' match 4 times slower, 'tRUE'/'True' 7 times slower. + if (str == "true") { + return Boolean.TRUE; + } + if (str == null) { + return null; + } + switch (str.length()) { + case 1: { + final char ch0 = str.charAt(0); + if (ch0 == 'y' || ch0 == 'Y' || + ch0 == 't' || ch0 == 'T') { + return Boolean.TRUE; + } + if (ch0 == 'n' || ch0 == 'N' || + ch0 == 'f' || ch0 == 'F') { + return Boolean.FALSE; + } + break; + } + case 2: { + final char ch0 = str.charAt(0); + final char ch1 = str.charAt(1); + if ((ch0 == 'o' || ch0 == 'O') && + (ch1 == 'n' || ch1 == 'N') ) { + return Boolean.TRUE; + } + if ((ch0 == 'n' || ch0 == 'N') && + (ch1 == 'o' || ch1 == 'O') ) { + return Boolean.FALSE; + } + break; + } + case 3: { + final char ch0 = str.charAt(0); + final char ch1 = str.charAt(1); + final char ch2 = str.charAt(2); + if ((ch0 == 'y' || ch0 == 'Y') && + (ch1 == 'e' || ch1 == 'E') && + (ch2 == 's' || ch2 == 'S') ) { + return Boolean.TRUE; + } + if ((ch0 == 'o' || ch0 == 'O') && + (ch1 == 'f' || ch1 == 'F') && + (ch2 == 'f' || ch2 == 'F') ) { + return Boolean.FALSE; + } + break; + } + case 4: { + final char ch0 = str.charAt(0); + final char ch1 = str.charAt(1); + final char ch2 = str.charAt(2); + final char ch3 = str.charAt(3); + if ((ch0 == 't' || ch0 == 'T') && + (ch1 == 'r' || ch1 == 'R') && + (ch2 == 'u' || ch2 == 'U') && + (ch3 == 'e' || ch3 == 'E') ) { + return Boolean.TRUE; + } + break; + } + case 5: { + final char ch0 = str.charAt(0); + final char ch1 = str.charAt(1); + final char ch2 = str.charAt(2); + final char ch3 = str.charAt(3); + final char ch4 = str.charAt(4); + if ((ch0 == 'f' || ch0 == 'F') && + (ch1 == 'a' || ch1 == 'A') && + (ch2 == 'l' || ch2 == 'L') && + (ch3 == 's' || ch3 == 'S') && + (ch4 == 'e' || ch4 == 'E') ) { + return Boolean.FALSE; + } + break; + } + default: + break; + } + + return null; + } + + /** + *
Converts a String to a Boolean throwing an exception if no match.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.toBooleanObject("true", "true", "false", "null") = Boolean.TRUE + * BooleanUtils.toBooleanObject("false", "true", "false", "null") = Boolean.FALSE + * BooleanUtils.toBooleanObject("null", "true", "false", "null") = null + *+ * + * @param str the String to check + * @param trueString the String to match for {@code true} (case sensitive), may be {@code null} + * @param falseString the String to match for {@code false} (case sensitive), may be {@code null} + * @param nullString the String to match for {@code null} (case sensitive), may be {@code null} + * @return the Boolean value of the string, {@code null} if either the String matches {@code nullString} + * or if {@code null} input and {@code nullString} is {@code null} + * @throws IllegalArgumentException if the String doesn't match + */ + public static Boolean toBooleanObject(final String str, final String trueString, final String falseString, final String nullString) { + if (str == null) { + if (trueString == null) { + return Boolean.TRUE; + } + if (falseString == null) { + return Boolean.FALSE; + } + if (nullString == null) { + return null; + } + } else if (str.equals(trueString)) { + return Boolean.TRUE; + } else if (str.equals(falseString)) { + return Boolean.FALSE; + } else if (str.equals(nullString)) { + return null; + } + // no match + throw new IllegalArgumentException("The String did not match any specified value"); + } + + // String to boolean methods + //----------------------------------------------------------------------- + /** + *
Converts a String to a boolean (optimised for performance).
+ * + *{@code 'true'}, {@code 'on'}, {@code 'y'}, {@code 't'} or {@code 'yes'} + * (case insensitive) will return {@code true}. Otherwise, + * {@code false} is returned.
+ * + *This method performs 4 times faster (JDK1.4) than + * {@code Boolean.valueOf(String)}. However, this method accepts + * 'on' and 'yes', 't', 'y' as true values. + * + *
+ * BooleanUtils.toBoolean(null) = false + * BooleanUtils.toBoolean("true") = true + * BooleanUtils.toBoolean("TRUE") = true + * BooleanUtils.toBoolean("tRUe") = true + * BooleanUtils.toBoolean("on") = true + * BooleanUtils.toBoolean("yes") = true + * BooleanUtils.toBoolean("false") = false + * BooleanUtils.toBoolean("x gti") = false + * BooleanUtils.toBooleanObject("y") = true + * BooleanUtils.toBooleanObject("n") = false + * BooleanUtils.toBooleanObject("t") = true + * BooleanUtils.toBooleanObject("f") = false + *+ * + * @param str the String to check + * @return the boolean value of the string, {@code false} if no match or the String is null + */ + public static boolean toBoolean(final String str) { + return toBooleanObject(str) == Boolean.TRUE; + } + + /** + *
Converts a String to a Boolean throwing an exception if no match found.
+ * + *+ * BooleanUtils.toBoolean("true", "true", "false") = true + * BooleanUtils.toBoolean("false", "true", "false") = false + *+ * + * @param str the String to check + * @param trueString the String to match for {@code true} (case sensitive), may be {@code null} + * @param falseString the String to match for {@code false} (case sensitive), may be {@code null} + * @return the boolean value of the string + * @throws IllegalArgumentException if the String doesn't match + */ + public static boolean toBoolean(final String str, final String trueString, final String falseString) { + if (str == trueString) { + return true; + } else if (str == falseString) { + return false; + } else if (str != null) { + if (str.equals(trueString)) { + return true; + } else if (str.equals(falseString)) { + return false; + } + } + // no match + throw new IllegalArgumentException("The String did not match either specified value"); + } + + // Boolean to String methods + //----------------------------------------------------------------------- + /** + *
Converts a Boolean to a String returning {@code 'true'}, + * {@code 'false'}, or {@code null}.
+ * + *+ * BooleanUtils.toStringTrueFalse(Boolean.TRUE) = "true" + * BooleanUtils.toStringTrueFalse(Boolean.FALSE) = "false" + * BooleanUtils.toStringTrueFalse(null) = null; + *+ * + * @param bool the Boolean to check + * @return {@code 'true'}, {@code 'false'}, or {@code null} + */ + public static String toStringTrueFalse(final Boolean bool) { + return toString(bool, "true", "false", null); + } + + /** + *
Converts a Boolean to a String returning {@code 'on'}, + * {@code 'off'}, or {@code null}.
+ * + *+ * BooleanUtils.toStringOnOff(Boolean.TRUE) = "on" + * BooleanUtils.toStringOnOff(Boolean.FALSE) = "off" + * BooleanUtils.toStringOnOff(null) = null; + *+ * + * @param bool the Boolean to check + * @return {@code 'on'}, {@code 'off'}, or {@code null} + */ + public static String toStringOnOff(final Boolean bool) { + return toString(bool, "on", "off", null); + } + + /** + *
Converts a Boolean to a String returning {@code 'yes'}, + * {@code 'no'}, or {@code null}.
+ * + *+ * BooleanUtils.toStringYesNo(Boolean.TRUE) = "yes" + * BooleanUtils.toStringYesNo(Boolean.FALSE) = "no" + * BooleanUtils.toStringYesNo(null) = null; + *+ * + * @param bool the Boolean to check + * @return {@code 'yes'}, {@code 'no'}, or {@code null} + */ + public static String toStringYesNo(final Boolean bool) { + return toString(bool, "yes", "no", null); + } + + /** + *
Converts a Boolean to a String returning one of the input Strings.
+ * + *+ * BooleanUtils.toString(Boolean.TRUE, "true", "false", null) = "true" + * BooleanUtils.toString(Boolean.FALSE, "true", "false", null) = "false" + * BooleanUtils.toString(null, "true", "false", null) = null; + *+ * + * @param bool the Boolean to check + * @param trueString the String to return if {@code true}, may be {@code null} + * @param falseString the String to return if {@code false}, may be {@code null} + * @param nullString the String to return if {@code null}, may be {@code null} + * @return one of the three input Strings + */ + public static String toString(final Boolean bool, final String trueString, final String falseString, final String nullString) { + if (bool == null) { + return nullString; + } + return bool.booleanValue() ? trueString : falseString; + } + + // boolean to String methods + //----------------------------------------------------------------------- + /** + *
Converts a boolean to a String returning {@code 'true'} + * or {@code 'false'}.
+ * + *+ * BooleanUtils.toStringTrueFalse(true) = "true" + * BooleanUtils.toStringTrueFalse(false) = "false" + *+ * + * @param bool the Boolean to check + * @return {@code 'true'}, {@code 'false'}, or {@code null} + */ + public static String toStringTrueFalse(final boolean bool) { + return toString(bool, "true", "false"); + } + + /** + *
Converts a boolean to a String returning {@code 'on'} + * or {@code 'off'}.
+ * + *+ * BooleanUtils.toStringOnOff(true) = "on" + * BooleanUtils.toStringOnOff(false) = "off" + *+ * + * @param bool the Boolean to check + * @return {@code 'on'}, {@code 'off'}, or {@code null} + */ + public static String toStringOnOff(final boolean bool) { + return toString(bool, "on", "off"); + } + + /** + *
Converts a boolean to a String returning {@code 'yes'} + * or {@code 'no'}.
+ * + *+ * BooleanUtils.toStringYesNo(true) = "yes" + * BooleanUtils.toStringYesNo(false) = "no" + *+ * + * @param bool the Boolean to check + * @return {@code 'yes'}, {@code 'no'}, or {@code null} + */ + public static String toStringYesNo(final boolean bool) { + return toString(bool, "yes", "no"); + } + + /** + *
Converts a boolean to a String returning one of the input Strings.
+ * + *+ * BooleanUtils.toString(true, "true", "false") = "true" + * BooleanUtils.toString(false, "true", "false") = "false" + *+ * + * @param bool the Boolean to check + * @param trueString the String to return if {@code true}, may be {@code null} + * @param falseString the String to return if {@code false}, may be {@code null} + * @return one of the two input Strings + */ + public static String toString(final boolean bool, final String trueString, final String falseString) { + return bool ? trueString : falseString; + } + + // logical operations + // ---------------------------------------------------------------------- + /** + *
Performs an and on a set of booleans.
+ * + *+ * BooleanUtils.and(true, true) = true + * BooleanUtils.and(false, false) = false + * BooleanUtils.and(true, false) = false + * BooleanUtils.and(true, true, false) = false + * BooleanUtils.and(true, true, true) = true + *+ * + * @param array an array of {@code boolean}s + * @return {@code true} if the and is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + * @since 3.0.1 + */ + public static boolean and(final boolean... array) { + // Validates input + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + for (final boolean element : array) { + if (!element) { + return false; + } + } + return true; + } + + /** + *
Performs an and on an array of Booleans.
+ * + *+ * BooleanUtils.and(Boolean.TRUE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.and(Boolean.FALSE, Boolean.FALSE) = Boolean.FALSE + * BooleanUtils.and(Boolean.TRUE, Boolean.FALSE) = Boolean.FALSE + * BooleanUtils.and(Boolean.TRUE, Boolean.TRUE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.and(Boolean.FALSE, Boolean.FALSE, Boolean.TRUE) = Boolean.FALSE + * BooleanUtils.and(Boolean.TRUE, Boolean.FALSE, Boolean.TRUE) = Boolean.FALSE + *+ * + * @param array an array of {@code Boolean}s + * @return {@code true} if the and is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + * @throws IllegalArgumentException if {@code array} contains a {@code null} + * @since 3.0.1 + */ + public static Boolean and(final Boolean... array) { + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + try { + final boolean[] primitive = ArrayUtils.toPrimitive(array); + return and(primitive) ? Boolean.TRUE : Boolean.FALSE; + } catch (final NullPointerException ex) { + throw new IllegalArgumentException("The array must not contain any null elements"); + } + } + + /** + *
Performs an or on a set of booleans.
+ * + *+ * BooleanUtils.or(true, true) = true + * BooleanUtils.or(false, false) = false + * BooleanUtils.or(true, false) = true + * BooleanUtils.or(true, true, false) = true + * BooleanUtils.or(true, true, true) = true + * BooleanUtils.or(false, false, false) = false + *+ * + * @param array an array of {@code boolean}s + * @return {@code true} if the or is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + * @since 3.0.1 + */ + public static boolean or(final boolean... array) { + // Validates input + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + for (final boolean element : array) { + if (element) { + return true; + } + } + return false; + } + + /** + *
Performs an or on an array of Booleans.
+ * + *+ * BooleanUtils.or(Boolean.TRUE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.or(Boolean.FALSE, Boolean.FALSE) = Boolean.FALSE + * BooleanUtils.or(Boolean.TRUE, Boolean.FALSE) = Boolean.TRUE + * BooleanUtils.or(Boolean.TRUE, Boolean.TRUE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.or(Boolean.FALSE, Boolean.FALSE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.or(Boolean.TRUE, Boolean.FALSE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.or(Boolean.FALSE, Boolean.FALSE, Boolean.FALSE) = Boolean.FALSE + *+ * + * @param array an array of {@code Boolean}s + * @return {@code true} if the or is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + * @throws IllegalArgumentException if {@code array} contains a {@code null} + * @since 3.0.1 + */ + public static Boolean or(final Boolean... array) { + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + try { + final boolean[] primitive = ArrayUtils.toPrimitive(array); + return or(primitive) ? Boolean.TRUE : Boolean.FALSE; + } catch (final NullPointerException ex) { + throw new IllegalArgumentException("The array must not contain any null elements"); + } + } + + /** + *
Performs an xor on a set of booleans.
+ * + *+ * BooleanUtils.xor(true, true) = false + * BooleanUtils.xor(false, false) = false + * BooleanUtils.xor(true, false) = true + * BooleanUtils.xor(true, true) = false + * BooleanUtils.xor(false, false) = false + * BooleanUtils.xor(true, false) = true + *+ * + * @param array an array of {@code boolean}s + * @return {@code true} if the xor is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + */ + public static boolean xor(final boolean... array) { + // Validates input + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + + // false if the neutral element of the xor operator + boolean result = false; + for (final boolean element : array) { + result ^= element; + } + + return result; + } + + /** + *
Performs an xor on an array of Booleans.
+ * + *+ * BooleanUtils.xor(new Boolean[] { Boolean.TRUE, Boolean.TRUE }) = Boolean.FALSE + * BooleanUtils.xor(new Boolean[] { Boolean.FALSE, Boolean.FALSE }) = Boolean.FALSE + * BooleanUtils.xor(new Boolean[] { Boolean.TRUE, Boolean.FALSE }) = Boolean.TRUE + *+ * + * @param array an array of {@code Boolean}s + * @return {@code true} if the xor is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + * @throws IllegalArgumentException if {@code array} contains a {@code null} + */ + public static Boolean xor(final Boolean... array) { + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + try { + final boolean[] primitive = ArrayUtils.toPrimitive(array); + return xor(primitive) ? Boolean.TRUE : Boolean.FALSE; + } catch (final NullPointerException ex) { + throw new IllegalArgumentException("The array must not contain any null elements"); + } + } + + /** + *
Compares two {@code boolean} values. This is the same functionality as provided in Java 7.
+ * + * @param x the first {@code boolean} to compare + * @param y the second {@code boolean} to compare + * @return the value {@code 0} if {@code x == y}; + * a value less than {@code 0} if {@code !x && y}; and + * a value greater than {@code 0} if {@code x && !y} + * @since 3.4 + */ + public static int compare(boolean x, boolean y) { + if (x == y) { + return 0; + } + return x ? 1 : -1; + } + +} diff --git a/Java/commons-lang-BooleanUtils_465/metadata.json b/Java/commons-lang-BooleanUtils_465/metadata.json new file mode 100644 index 000000000..304f66aca --- /dev/null +++ b/Java/commons-lang-BooleanUtils_465/metadata.json @@ -0,0 +1,21 @@ +{ + "language": "java", + "id": "commons-lang-BooleanUtils_465", + "buggyPath": ".", + "referencePath": null, + "buildCommand": "mvn package -V -B -Denforcer.skip=true -Dcheckstyle.skip=true -Dcobertura.skip=true -Drat.skip=true -Dlicense.skip=true -Dfindbugs.skip=true -Dgpg.skip=true -Dskip.npm=true -Dskip.gulp=true -Dskip.bower=true -Drat.numUnapprovedLicenses=100 -DskipTests=true -DskipITs=true -Dtest=None -DfailIfNoTests=false", + "testCommand": "mvn test -V -B -Denforcer.skip=true -Dcheckstyle.skip=true -Dcobertura.skip=true -Drat.skip=true -Dlicense.skip=true -Dfindbugs.skip=true -Dgpg.skip=true -Dskip.npm=true -Dskip.gulp=true -Dskip.bower=true -Drat.numUnapprovedLicenses=100", + "categories": [ + "safety", + "npe" + ], + "npe": { + "filepath": "src/main/java/org/apache/commons/lang3/BooleanUtils.java", + "line": 486, + "npe_method": "toInteger", + "deref_field": "bool", + "npe_class": "BooleanUtils", + "repo": "commons-lang", + "bug_id": "BooleanUtils_465" + } +} diff --git a/Java/commons-lang-BooleanUtils_465/npe.json b/Java/commons-lang-BooleanUtils_465/npe.json new file mode 100644 index 000000000..dc3498bd6 --- /dev/null +++ b/Java/commons-lang-BooleanUtils_465/npe.json @@ -0,0 +1,7 @@ +{ + "filepath": "src/main/java/org/apache/commons/lang3/BooleanUtils.java", + "line": 486, + "npe_method": "toInteger", + "deref_field": "bool", + "npe_class": "BooleanUtils" +} \ No newline at end of file diff --git a/Java/commons-lang-BooleanUtils_504/Dockerfile b/Java/commons-lang-BooleanUtils_504/Dockerfile new file mode 100644 index 000000000..7b7fbe349 --- /dev/null +++ b/Java/commons-lang-BooleanUtils_504/Dockerfile @@ -0,0 +1,18 @@ +FROM ghcr.io/kupl/starlab-benchmarks/java-base:commons-lang + +ENV TZ=Asia/Seoul + +COPY ./metadata.json . +COPY ./npe.json . +COPY ./buggy.java /tmp/buggy.java +RUN export BUGGY_PATH=$(cat metadata.json | jq -r ".npe.filepath") \ + && export BUGGY_LINE=$(cat metadata.json | jq -r ".npe.line") \ + && export BUGGY_MTHD=$(cat metadata.json | jq -r ".npe.npe_method") \ + && mv /tmp/buggy.java $BUGGY_PATH \ + && echo "[{\"filepath\": \"$BUGGY_PATH\", \"line\": $BUGGY_LINE, \"method_name\": \"$BUGGY_MTHD\"}]" | jq . > traces.json + +RUN git init . && git add -A + +RUN $(cat metadata.json | jq -r ".buildCommand") + +RUN $(cat metadata.json | jq -r ".testCommand"); if [ $? -eq 0 ]; then exit 1; fi diff --git a/Java/commons-lang-BooleanUtils_504/buggy.java b/Java/commons-lang-BooleanUtils_504/buggy.java new file mode 100644 index 000000000..c2bcec3ef --- /dev/null +++ b/Java/commons-lang-BooleanUtils_504/buggy.java @@ -0,0 +1,1123 @@ +/* + * 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; + +import org.apache.commons.lang3.math.NumberUtils; + +/** + *Operations on boolean primitives and Boolean objects.
+ * + *This class tries to handle {@code null} input gracefully. + * An exception will not be thrown for a {@code null} input. + * Each method documents its behaviour in more detail.
+ * + *#ThreadSafe#
+ * @since 2.0 + */ +public class BooleanUtils { + + /** + *{@code BooleanUtils} instances should NOT be constructed in standard programming. + * Instead, the class should be used as {@code BooleanUtils.negate(true);}.
+ * + *This constructor is public to permit tools that require a JavaBean instance + * to operate.
+ */ + public BooleanUtils() { + super(); + } + + // Boolean utilities + //-------------------------------------------------------------------------- + /** + *Negates the specified boolean.
+ * + *If {@code null} is passed in, {@code null} will be returned.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.negate(Boolean.TRUE) = Boolean.FALSE; + * BooleanUtils.negate(Boolean.FALSE) = Boolean.TRUE; + * BooleanUtils.negate(null) = null; + *+ * + * @param bool the Boolean to negate, may be null + * @return the negated Boolean, or {@code null} if {@code null} input + */ + public static Boolean negate(final Boolean bool) { + if (bool == null) { + return null; + } + return bool.booleanValue() ? Boolean.FALSE : Boolean.TRUE; + } + + // boolean Boolean methods + //----------------------------------------------------------------------- + /** + *
Checks if a {@code Boolean} value is {@code true}, + * handling {@code null} by returning {@code false}.
+ * + *+ * BooleanUtils.isTrue(Boolean.TRUE) = true + * BooleanUtils.isTrue(Boolean.FALSE) = false + * BooleanUtils.isTrue(null) = false + *+ * + * @param bool the boolean to check, null returns {@code false} + * @return {@code true} only if the input is non-null and true + * @since 2.1 + */ + public static boolean isTrue(final Boolean bool) { + return Boolean.TRUE.equals(bool); + } + + /** + *
Checks if a {@code Boolean} value is not {@code true}, + * handling {@code null} by returning {@code true}.
+ * + *+ * BooleanUtils.isNotTrue(Boolean.TRUE) = false + * BooleanUtils.isNotTrue(Boolean.FALSE) = true + * BooleanUtils.isNotTrue(null) = true + *+ * + * @param bool the boolean to check, null returns {@code true} + * @return {@code true} if the input is null or false + * @since 2.3 + */ + public static boolean isNotTrue(final Boolean bool) { + return !isTrue(bool); + } + + /** + *
Checks if a {@code Boolean} value is {@code false}, + * handling {@code null} by returning {@code false}.
+ * + *+ * BooleanUtils.isFalse(Boolean.TRUE) = false + * BooleanUtils.isFalse(Boolean.FALSE) = true + * BooleanUtils.isFalse(null) = false + *+ * + * @param bool the boolean to check, null returns {@code false} + * @return {@code true} only if the input is non-null and false + * @since 2.1 + */ + public static boolean isFalse(final Boolean bool) { + return Boolean.FALSE.equals(bool); + } + + /** + *
Checks if a {@code Boolean} value is not {@code false}, + * handling {@code null} by returning {@code true}.
+ * + *+ * BooleanUtils.isNotFalse(Boolean.TRUE) = true + * BooleanUtils.isNotFalse(Boolean.FALSE) = false + * BooleanUtils.isNotFalse(null) = true + *+ * + * @param bool the boolean to check, null returns {@code true} + * @return {@code true} if the input is null or true + * @since 2.3 + */ + public static boolean isNotFalse(final Boolean bool) { + return !isFalse(bool); + } + + //----------------------------------------------------------------------- + /** + *
Converts a Boolean to a boolean handling {@code null} + * by returning {@code false}.
+ * + *+ * BooleanUtils.toBoolean(Boolean.TRUE) = true + * BooleanUtils.toBoolean(Boolean.FALSE) = false + * BooleanUtils.toBoolean(null) = false + *+ * + * @param bool the boolean to convert + * @return {@code true} or {@code false}, {@code null} returns {@code false} + */ + public static boolean toBoolean(final Boolean bool) { + return bool != null && bool.booleanValue(); + } + + /** + *
Converts a Boolean to a boolean handling {@code null}.
+ * + *+ * BooleanUtils.toBooleanDefaultIfNull(Boolean.TRUE, false) = true + * BooleanUtils.toBooleanDefaultIfNull(Boolean.FALSE, true) = false + * BooleanUtils.toBooleanDefaultIfNull(null, true) = true + *+ * + * @param bool the boolean to convert + * @param valueIfNull the boolean value to return if {@code null} + * @return {@code true} or {@code false} + */ + public static boolean toBooleanDefaultIfNull(final Boolean bool, final boolean valueIfNull) { + if (bool == null) { + return valueIfNull; + } + return bool.booleanValue(); + } + + // Integer to Boolean methods + //----------------------------------------------------------------------- + /** + *
Converts an int to a boolean using the convention that {@code zero} + * is {@code false}.
+ * + *+ * BooleanUtils.toBoolean(0) = false + * BooleanUtils.toBoolean(1) = true + * BooleanUtils.toBoolean(2) = true + *+ * + * @param value the int to convert + * @return {@code true} if non-zero, {@code false} + * if zero + */ + public static boolean toBoolean(final int value) { + return value != 0; + } + + /** + *
Converts an int to a Boolean using the convention that {@code zero} + * is {@code false}.
+ * + *+ * BooleanUtils.toBoolean(0) = Boolean.FALSE + * BooleanUtils.toBoolean(1) = Boolean.TRUE + * BooleanUtils.toBoolean(2) = Boolean.TRUE + *+ * + * @param value the int to convert + * @return Boolean.TRUE if non-zero, Boolean.FALSE if zero, + * {@code null} if {@code null} + */ + public static Boolean toBooleanObject(final int value) { + return value == 0 ? Boolean.FALSE : Boolean.TRUE; + } + + /** + *
Converts an Integer to a Boolean using the convention that {@code zero} + * is {@code false}.
+ * + *{@code null} will be converted to {@code null}.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.toBoolean(Integer.valueOf(0)) = Boolean.FALSE + * BooleanUtils.toBoolean(Integer.valueOf(1)) = Boolean.TRUE + * BooleanUtils.toBoolean(Integer.valueOf(null)) = null + *+ * + * @param value the Integer to convert + * @return Boolean.TRUE if non-zero, Boolean.FALSE if zero, + * {@code null} if {@code null} input + */ + public static Boolean toBooleanObject(final Integer value) { + if (value == null) { + return null; + } + return value.intValue() == 0 ? Boolean.FALSE : Boolean.TRUE; + } + + /** + *
Converts an int to a boolean specifying the conversion values.
+ * + *+ * BooleanUtils.toBoolean(0, 1, 0) = false + * BooleanUtils.toBoolean(1, 1, 0) = true + * BooleanUtils.toBoolean(2, 1, 2) = false + * BooleanUtils.toBoolean(2, 2, 0) = true + *+ * + * @param value the Integer to convert + * @param trueValue the value to match for {@code true} + * @param falseValue the value to match for {@code false} + * @return {@code true} or {@code false} + * @throws IllegalArgumentException if no match + */ + public static boolean toBoolean(final int value, final int trueValue, final int falseValue) { + if (value == trueValue) { + return true; + } + if (value == falseValue) { + return false; + } + // no match + throw new IllegalArgumentException("The Integer did not match either specified value"); + } + + /** + *
Converts an Integer to a boolean specifying the conversion values.
+ * + *+ * BooleanUtils.toBoolean(Integer.valueOf(0), Integer.valueOf(1), Integer.valueOf(0)) = false + * BooleanUtils.toBoolean(Integer.valueOf(1), Integer.valueOf(1), Integer.valueOf(0)) = true + * BooleanUtils.toBoolean(Integer.valueOf(2), Integer.valueOf(1), Integer.valueOf(2)) = false + * BooleanUtils.toBoolean(Integer.valueOf(2), Integer.valueOf(2), Integer.valueOf(0)) = true + * BooleanUtils.toBoolean(null, null, Integer.valueOf(0)) = true + *+ * + * @param value the Integer to convert + * @param trueValue the value to match for {@code true}, may be {@code null} + * @param falseValue the value to match for {@code false}, may be {@code null} + * @return {@code true} or {@code false} + * @throws IllegalArgumentException if no match + */ + public static boolean toBoolean(final Integer value, final Integer trueValue, final Integer falseValue) { + if (value == null) { + if (trueValue == null) { + return true; + } + if (falseValue == null) { + return false; + } + } else if (value.equals(trueValue)) { + return true; + } else if (value.equals(falseValue)) { + return false; + } + // no match + throw new IllegalArgumentException("The Integer did not match either specified value"); + } + + /** + *
Converts an int to a Boolean specifying the conversion values.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.toBooleanObject(0, 0, 2, 3) = Boolean.TRUE + * BooleanUtils.toBooleanObject(2, 1, 2, 3) = Boolean.FALSE + * BooleanUtils.toBooleanObject(3, 1, 2, 3) = null + *+ * + * @param value the Integer to convert + * @param trueValue the value to match for {@code true} + * @param falseValue the value to match for {@code false} + * @param nullValue the value to to match for {@code null} + * @return Boolean.TRUE, Boolean.FALSE, or {@code null} + * @throws IllegalArgumentException if no match + */ + public static Boolean toBooleanObject(final int value, final int trueValue, final int falseValue, final int nullValue) { + if (value == trueValue) { + return Boolean.TRUE; + } + if (value == falseValue) { + return Boolean.FALSE; + } + if (value == nullValue) { + return null; + } + // no match + throw new IllegalArgumentException("The Integer did not match any specified value"); + } + + /** + *
Converts an Integer to a Boolean specifying the conversion values.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.toBooleanObject(Integer.valueOf(0), Integer.valueOf(0), Integer.valueOf(2), Integer.valueOf(3)) = Boolean.TRUE + * BooleanUtils.toBooleanObject(Integer.valueOf(2), Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(3)) = Boolean.FALSE + * BooleanUtils.toBooleanObject(Integer.valueOf(3), Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(3)) = null + *+ * + * @param value the Integer to convert + * @param trueValue the value to match for {@code true}, may be {@code null} + * @param falseValue the value to match for {@code false}, may be {@code null} + * @param nullValue the value to to match for {@code null}, may be {@code null} + * @return Boolean.TRUE, Boolean.FALSE, or {@code null} + * @throws IllegalArgumentException if no match + */ + public static Boolean toBooleanObject(final Integer value, final Integer trueValue, final Integer falseValue, final Integer nullValue) { + if (value == null) { + if (trueValue == null) { + return Boolean.TRUE; + } + if (falseValue == null) { + return Boolean.FALSE; + } + if (nullValue == null) { + return null; + } + } else if (value.equals(trueValue)) { + return Boolean.TRUE; + } else if (value.equals(falseValue)) { + return Boolean.FALSE; + } else if (value.equals(nullValue)) { + return null; + } + // no match + throw new IllegalArgumentException("The Integer did not match any specified value"); + } + + // Boolean to Integer methods + //----------------------------------------------------------------------- + /** + *
Converts a boolean to an int using the convention that + * {@code zero} is {@code false}.
+ * + *+ * BooleanUtils.toInteger(true) = 1 + * BooleanUtils.toInteger(false) = 0 + *+ * + * @param bool the boolean to convert + * @return one if {@code true}, zero if {@code false} + */ + public static int toInteger(final boolean bool) { + return bool ? 1 : 0; + } + + /** + *
Converts a boolean to an Integer using the convention that + * {@code zero} is {@code false}.
+ * + *+ * BooleanUtils.toIntegerObject(true) = Integer.valueOf(1) + * BooleanUtils.toIntegerObject(false) = Integer.valueOf(0) + *+ * + * @param bool the boolean to convert + * @return one if {@code true}, zero if {@code false} + */ + public static Integer toIntegerObject(final boolean bool) { + return bool ? NumberUtils.INTEGER_ONE : NumberUtils.INTEGER_ZERO; + } + + /** + *
Converts a Boolean to a Integer using the convention that + * {@code zero} is {@code false}.
+ * + *{@code null} will be converted to {@code null}.
+ * + *+ * BooleanUtils.toIntegerObject(Boolean.TRUE) = Integer.valueOf(1) + * BooleanUtils.toIntegerObject(Boolean.FALSE) = Integer.valueOf(0) + *+ * + * @param bool the Boolean to convert + * @return one if Boolean.TRUE, zero if Boolean.FALSE, {@code null} if {@code null} + */ + public static Integer toIntegerObject(final Boolean bool) { + if (bool == null) { + return null; + } + return bool.booleanValue() ? NumberUtils.INTEGER_ONE : NumberUtils.INTEGER_ZERO; + } + + /** + *
Converts a boolean to an int specifying the conversion values.
+ * + *+ * BooleanUtils.toInteger(true, 1, 0) = 1 + * BooleanUtils.toInteger(false, 1, 0) = 0 + *+ * + * @param bool the to convert + * @param trueValue the value to return if {@code true} + * @param falseValue the value to return if {@code false} + * @return the appropriate value + */ + public static int toInteger(final boolean bool, final int trueValue, final int falseValue) { + return bool ? trueValue : falseValue; + } + + /** + *
Converts a Boolean to an int specifying the conversion values.
+ * + *+ * BooleanUtils.toInteger(Boolean.TRUE, 1, 0, 2) = 1 + * BooleanUtils.toInteger(Boolean.FALSE, 1, 0, 2) = 0 + * BooleanUtils.toInteger(null, 1, 0, 2) = 2 + *+ * + * @param bool the Boolean to convert + * @param trueValue the value to return if {@code true} + * @param falseValue the value to return if {@code false} + * @param nullValue the value to return if {@code null} + * @return the appropriate value + */ + public static int toInteger(final Boolean bool, final int trueValue, final int falseValue, final int nullValue) { + if (bool == null) { + return nullValue; + } + return bool.booleanValue() ? trueValue : falseValue; + } + + /** + *
Converts a boolean to an Integer specifying the conversion values.
+ * + *+ * BooleanUtils.toIntegerObject(true, Integer.valueOf(1), Integer.valueOf(0)) = Integer.valueOf(1) + * BooleanUtils.toIntegerObject(false, Integer.valueOf(1), Integer.valueOf(0)) = Integer.valueOf(0) + *+ * + * @param bool the to convert + * @param trueValue the value to return if {@code true}, may be {@code null} + * @param falseValue the value to return if {@code false}, may be {@code null} + * @return the appropriate value + */ + public static Integer toIntegerObject(final boolean bool, final Integer trueValue, final Integer falseValue) { + return bool ? trueValue : falseValue; + } + + /** + *
Converts a Boolean to an Integer specifying the conversion values.
+ * + *+ * BooleanUtils.toIntegerObject(Boolean.TRUE, Integer.valueOf(1), Integer.valueOf(0), Integer.valueOf(2)) = Integer.valueOf(1) + * BooleanUtils.toIntegerObject(Boolean.FALSE, Integer.valueOf(1), Integer.valueOf(0), Integer.valueOf(2)) = Integer.valueOf(0) + * BooleanUtils.toIntegerObject(null, Integer.valueOf(1), Integer.valueOf(0), Integer.valueOf(2)) = Integer.valueOf(2) + *+ * + * @param bool the Boolean to convert + * @param trueValue the value to return if {@code true}, may be {@code null} + * @param falseValue the value to return if {@code false}, may be {@code null} + * @param nullValue the value to return if {@code null}, may be {@code null} + * @return the appropriate value + */ +/** + *
Converts a Boolean to an Integer specifying the conversion values.
+ * + *+ * BooleanUtils.toIntegerObject(Boolean.TRUE, Integer.valueOf(1), Integer.valueOf(0), Integer.valueOf(2)) = Integer.valueOf(1) + * BooleanUtils.toIntegerObject(Boolean.FALSE, Integer.valueOf(1), Integer.valueOf(0), Integer.valueOf(2)) = Integer.valueOf(0) + * BooleanUtils.toIntegerObject(null, Integer.valueOf(1), Integer.valueOf(0), Integer.valueOf(2)) = Integer.valueOf(2) + *+ * + * @param bool + * the Boolean to convert + * @param trueValue + * the value to return if {@code true}, may be {@code null} + * @param falseValue + * the value to return if {@code false}, may be {@code null} + * @param nullValue + * the value to return if {@code null}, may be {@code null} + * @return the appropriate value + */ +public static java.lang.Integer toIntegerObject(final java.lang.Boolean bool, final java.lang.Integer trueValue, final java.lang.Integer falseValue, final java.lang.Integer nullValue) { + { + return /* NPEX_NULL_EXP */ + bool.booleanValue() ? trueValue : falseValue; + } +} + + // String to Boolean methods + //----------------------------------------------------------------------- + /** + *
Converts a String to a Boolean.
+ * + *{@code 'true'}, {@code 'on'}, {@code 'y'}, {@code 't'} or {@code 'yes'} + * (case insensitive) will return {@code true}. + * {@code 'false'}, {@code 'off'}, {@code 'n'}, {@code 'f'} or {@code 'no'} + * (case insensitive) will return {@code false}. + * Otherwise, {@code null} is returned.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * // N.B. case is not significant + * BooleanUtils.toBooleanObject(null) = null + * BooleanUtils.toBooleanObject("true") = Boolean.TRUE + * BooleanUtils.toBooleanObject("T") = Boolean.TRUE // i.e. T[RUE] + * BooleanUtils.toBooleanObject("false") = Boolean.FALSE + * BooleanUtils.toBooleanObject("f") = Boolean.FALSE // i.e. f[alse] + * BooleanUtils.toBooleanObject("No") = Boolean.FALSE + * BooleanUtils.toBooleanObject("n") = Boolean.FALSE // i.e. n[o] + * BooleanUtils.toBooleanObject("on") = Boolean.TRUE + * BooleanUtils.toBooleanObject("ON") = Boolean.TRUE + * BooleanUtils.toBooleanObject("off") = Boolean.FALSE + * BooleanUtils.toBooleanObject("oFf") = Boolean.FALSE + * BooleanUtils.toBooleanObject("yes") = Boolean.TRUE + * BooleanUtils.toBooleanObject("Y") = Boolean.TRUE // i.e. Y[ES] + * BooleanUtils.toBooleanObject("blue") = null + * BooleanUtils.toBooleanObject("true ") = null // trailing space (too long) + * BooleanUtils.toBooleanObject("ono") = null // does not match on or no + *+ * + * @param str the String to check; upper and lower case are treated as the same + * @return the Boolean value of the string, {@code null} if no match or {@code null} input + */ + public static Boolean toBooleanObject(final String str) { + // Previously used equalsIgnoreCase, which was fast for interned 'true'. + // Non interned 'true' matched 15 times slower. + // + // Optimisation provides same performance as before for interned 'true'. + // Similar performance for null, 'false', and other strings not length 2/3/4. + // 'true'/'TRUE' match 4 times slower, 'tRUE'/'True' 7 times slower. + if (str == "true") { + return Boolean.TRUE; + } + if (str == null) { + return null; + } + switch (str.length()) { + case 1: { + final char ch0 = str.charAt(0); + if (ch0 == 'y' || ch0 == 'Y' || + ch0 == 't' || ch0 == 'T') { + return Boolean.TRUE; + } + if (ch0 == 'n' || ch0 == 'N' || + ch0 == 'f' || ch0 == 'F') { + return Boolean.FALSE; + } + break; + } + case 2: { + final char ch0 = str.charAt(0); + final char ch1 = str.charAt(1); + if ((ch0 == 'o' || ch0 == 'O') && + (ch1 == 'n' || ch1 == 'N') ) { + return Boolean.TRUE; + } + if ((ch0 == 'n' || ch0 == 'N') && + (ch1 == 'o' || ch1 == 'O') ) { + return Boolean.FALSE; + } + break; + } + case 3: { + final char ch0 = str.charAt(0); + final char ch1 = str.charAt(1); + final char ch2 = str.charAt(2); + if ((ch0 == 'y' || ch0 == 'Y') && + (ch1 == 'e' || ch1 == 'E') && + (ch2 == 's' || ch2 == 'S') ) { + return Boolean.TRUE; + } + if ((ch0 == 'o' || ch0 == 'O') && + (ch1 == 'f' || ch1 == 'F') && + (ch2 == 'f' || ch2 == 'F') ) { + return Boolean.FALSE; + } + break; + } + case 4: { + final char ch0 = str.charAt(0); + final char ch1 = str.charAt(1); + final char ch2 = str.charAt(2); + final char ch3 = str.charAt(3); + if ((ch0 == 't' || ch0 == 'T') && + (ch1 == 'r' || ch1 == 'R') && + (ch2 == 'u' || ch2 == 'U') && + (ch3 == 'e' || ch3 == 'E') ) { + return Boolean.TRUE; + } + break; + } + case 5: { + final char ch0 = str.charAt(0); + final char ch1 = str.charAt(1); + final char ch2 = str.charAt(2); + final char ch3 = str.charAt(3); + final char ch4 = str.charAt(4); + if ((ch0 == 'f' || ch0 == 'F') && + (ch1 == 'a' || ch1 == 'A') && + (ch2 == 'l' || ch2 == 'L') && + (ch3 == 's' || ch3 == 'S') && + (ch4 == 'e' || ch4 == 'E') ) { + return Boolean.FALSE; + } + break; + } + default: + break; + } + + return null; + } + + /** + *
Converts a String to a Boolean throwing an exception if no match.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.toBooleanObject("true", "true", "false", "null") = Boolean.TRUE + * BooleanUtils.toBooleanObject("false", "true", "false", "null") = Boolean.FALSE + * BooleanUtils.toBooleanObject("null", "true", "false", "null") = null + *+ * + * @param str the String to check + * @param trueString the String to match for {@code true} (case sensitive), may be {@code null} + * @param falseString the String to match for {@code false} (case sensitive), may be {@code null} + * @param nullString the String to match for {@code null} (case sensitive), may be {@code null} + * @return the Boolean value of the string, {@code null} if either the String matches {@code nullString} + * or if {@code null} input and {@code nullString} is {@code null} + * @throws IllegalArgumentException if the String doesn't match + */ + public static Boolean toBooleanObject(final String str, final String trueString, final String falseString, final String nullString) { + if (str == null) { + if (trueString == null) { + return Boolean.TRUE; + } + if (falseString == null) { + return Boolean.FALSE; + } + if (nullString == null) { + return null; + } + } else if (str.equals(trueString)) { + return Boolean.TRUE; + } else if (str.equals(falseString)) { + return Boolean.FALSE; + } else if (str.equals(nullString)) { + return null; + } + // no match + throw new IllegalArgumentException("The String did not match any specified value"); + } + + // String to boolean methods + //----------------------------------------------------------------------- + /** + *
Converts a String to a boolean (optimised for performance).
+ * + *{@code 'true'}, {@code 'on'}, {@code 'y'}, {@code 't'} or {@code 'yes'} + * (case insensitive) will return {@code true}. Otherwise, + * {@code false} is returned.
+ * + *This method performs 4 times faster (JDK1.4) than + * {@code Boolean.valueOf(String)}. However, this method accepts + * 'on' and 'yes', 't', 'y' as true values. + * + *
+ * BooleanUtils.toBoolean(null) = false + * BooleanUtils.toBoolean("true") = true + * BooleanUtils.toBoolean("TRUE") = true + * BooleanUtils.toBoolean("tRUe") = true + * BooleanUtils.toBoolean("on") = true + * BooleanUtils.toBoolean("yes") = true + * BooleanUtils.toBoolean("false") = false + * BooleanUtils.toBoolean("x gti") = false + * BooleanUtils.toBooleanObject("y") = true + * BooleanUtils.toBooleanObject("n") = false + * BooleanUtils.toBooleanObject("t") = true + * BooleanUtils.toBooleanObject("f") = false + *+ * + * @param str the String to check + * @return the boolean value of the string, {@code false} if no match or the String is null + */ + public static boolean toBoolean(final String str) { + return toBooleanObject(str) == Boolean.TRUE; + } + + /** + *
Converts a String to a Boolean throwing an exception if no match found.
+ * + *+ * BooleanUtils.toBoolean("true", "true", "false") = true + * BooleanUtils.toBoolean("false", "true", "false") = false + *+ * + * @param str the String to check + * @param trueString the String to match for {@code true} (case sensitive), may be {@code null} + * @param falseString the String to match for {@code false} (case sensitive), may be {@code null} + * @return the boolean value of the string + * @throws IllegalArgumentException if the String doesn't match + */ + public static boolean toBoolean(final String str, final String trueString, final String falseString) { + if (str == trueString) { + return true; + } else if (str == falseString) { + return false; + } else if (str != null) { + if (str.equals(trueString)) { + return true; + } else if (str.equals(falseString)) { + return false; + } + } + // no match + throw new IllegalArgumentException("The String did not match either specified value"); + } + + // Boolean to String methods + //----------------------------------------------------------------------- + /** + *
Converts a Boolean to a String returning {@code 'true'}, + * {@code 'false'}, or {@code null}.
+ * + *+ * BooleanUtils.toStringTrueFalse(Boolean.TRUE) = "true" + * BooleanUtils.toStringTrueFalse(Boolean.FALSE) = "false" + * BooleanUtils.toStringTrueFalse(null) = null; + *+ * + * @param bool the Boolean to check + * @return {@code 'true'}, {@code 'false'}, or {@code null} + */ + public static String toStringTrueFalse(final Boolean bool) { + return toString(bool, "true", "false", null); + } + + /** + *
Converts a Boolean to a String returning {@code 'on'}, + * {@code 'off'}, or {@code null}.
+ * + *+ * BooleanUtils.toStringOnOff(Boolean.TRUE) = "on" + * BooleanUtils.toStringOnOff(Boolean.FALSE) = "off" + * BooleanUtils.toStringOnOff(null) = null; + *+ * + * @param bool the Boolean to check + * @return {@code 'on'}, {@code 'off'}, or {@code null} + */ + public static String toStringOnOff(final Boolean bool) { + return toString(bool, "on", "off", null); + } + + /** + *
Converts a Boolean to a String returning {@code 'yes'}, + * {@code 'no'}, or {@code null}.
+ * + *+ * BooleanUtils.toStringYesNo(Boolean.TRUE) = "yes" + * BooleanUtils.toStringYesNo(Boolean.FALSE) = "no" + * BooleanUtils.toStringYesNo(null) = null; + *+ * + * @param bool the Boolean to check + * @return {@code 'yes'}, {@code 'no'}, or {@code null} + */ + public static String toStringYesNo(final Boolean bool) { + return toString(bool, "yes", "no", null); + } + + /** + *
Converts a Boolean to a String returning one of the input Strings.
+ * + *+ * BooleanUtils.toString(Boolean.TRUE, "true", "false", null) = "true" + * BooleanUtils.toString(Boolean.FALSE, "true", "false", null) = "false" + * BooleanUtils.toString(null, "true", "false", null) = null; + *+ * + * @param bool the Boolean to check + * @param trueString the String to return if {@code true}, may be {@code null} + * @param falseString the String to return if {@code false}, may be {@code null} + * @param nullString the String to return if {@code null}, may be {@code null} + * @return one of the three input Strings + */ + public static String toString(final Boolean bool, final String trueString, final String falseString, final String nullString) { + if (bool == null) { + return nullString; + } + return bool.booleanValue() ? trueString : falseString; + } + + // boolean to String methods + //----------------------------------------------------------------------- + /** + *
Converts a boolean to a String returning {@code 'true'} + * or {@code 'false'}.
+ * + *+ * BooleanUtils.toStringTrueFalse(true) = "true" + * BooleanUtils.toStringTrueFalse(false) = "false" + *+ * + * @param bool the Boolean to check + * @return {@code 'true'}, {@code 'false'}, or {@code null} + */ + public static String toStringTrueFalse(final boolean bool) { + return toString(bool, "true", "false"); + } + + /** + *
Converts a boolean to a String returning {@code 'on'} + * or {@code 'off'}.
+ * + *+ * BooleanUtils.toStringOnOff(true) = "on" + * BooleanUtils.toStringOnOff(false) = "off" + *+ * + * @param bool the Boolean to check + * @return {@code 'on'}, {@code 'off'}, or {@code null} + */ + public static String toStringOnOff(final boolean bool) { + return toString(bool, "on", "off"); + } + + /** + *
Converts a boolean to a String returning {@code 'yes'} + * or {@code 'no'}.
+ * + *+ * BooleanUtils.toStringYesNo(true) = "yes" + * BooleanUtils.toStringYesNo(false) = "no" + *+ * + * @param bool the Boolean to check + * @return {@code 'yes'}, {@code 'no'}, or {@code null} + */ + public static String toStringYesNo(final boolean bool) { + return toString(bool, "yes", "no"); + } + + /** + *
Converts a boolean to a String returning one of the input Strings.
+ * + *+ * BooleanUtils.toString(true, "true", "false") = "true" + * BooleanUtils.toString(false, "true", "false") = "false" + *+ * + * @param bool the Boolean to check + * @param trueString the String to return if {@code true}, may be {@code null} + * @param falseString the String to return if {@code false}, may be {@code null} + * @return one of the two input Strings + */ + public static String toString(final boolean bool, final String trueString, final String falseString) { + return bool ? trueString : falseString; + } + + // logical operations + // ---------------------------------------------------------------------- + /** + *
Performs an and on a set of booleans.
+ * + *+ * BooleanUtils.and(true, true) = true + * BooleanUtils.and(false, false) = false + * BooleanUtils.and(true, false) = false + * BooleanUtils.and(true, true, false) = false + * BooleanUtils.and(true, true, true) = true + *+ * + * @param array an array of {@code boolean}s + * @return {@code true} if the and is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + * @since 3.0.1 + */ + public static boolean and(final boolean... array) { + // Validates input + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + for (final boolean element : array) { + if (!element) { + return false; + } + } + return true; + } + + /** + *
Performs an and on an array of Booleans.
+ * + *+ * BooleanUtils.and(Boolean.TRUE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.and(Boolean.FALSE, Boolean.FALSE) = Boolean.FALSE + * BooleanUtils.and(Boolean.TRUE, Boolean.FALSE) = Boolean.FALSE + * BooleanUtils.and(Boolean.TRUE, Boolean.TRUE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.and(Boolean.FALSE, Boolean.FALSE, Boolean.TRUE) = Boolean.FALSE + * BooleanUtils.and(Boolean.TRUE, Boolean.FALSE, Boolean.TRUE) = Boolean.FALSE + *+ * + * @param array an array of {@code Boolean}s + * @return {@code true} if the and is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + * @throws IllegalArgumentException if {@code array} contains a {@code null} + * @since 3.0.1 + */ + public static Boolean and(final Boolean... array) { + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + try { + final boolean[] primitive = ArrayUtils.toPrimitive(array); + return and(primitive) ? Boolean.TRUE : Boolean.FALSE; + } catch (final NullPointerException ex) { + throw new IllegalArgumentException("The array must not contain any null elements"); + } + } + + /** + *
Performs an or on a set of booleans.
+ * + *+ * BooleanUtils.or(true, true) = true + * BooleanUtils.or(false, false) = false + * BooleanUtils.or(true, false) = true + * BooleanUtils.or(true, true, false) = true + * BooleanUtils.or(true, true, true) = true + * BooleanUtils.or(false, false, false) = false + *+ * + * @param array an array of {@code boolean}s + * @return {@code true} if the or is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + * @since 3.0.1 + */ + public static boolean or(final boolean... array) { + // Validates input + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + for (final boolean element : array) { + if (element) { + return true; + } + } + return false; + } + + /** + *
Performs an or on an array of Booleans.
+ * + *+ * BooleanUtils.or(Boolean.TRUE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.or(Boolean.FALSE, Boolean.FALSE) = Boolean.FALSE + * BooleanUtils.or(Boolean.TRUE, Boolean.FALSE) = Boolean.TRUE + * BooleanUtils.or(Boolean.TRUE, Boolean.TRUE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.or(Boolean.FALSE, Boolean.FALSE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.or(Boolean.TRUE, Boolean.FALSE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.or(Boolean.FALSE, Boolean.FALSE, Boolean.FALSE) = Boolean.FALSE + *+ * + * @param array an array of {@code Boolean}s + * @return {@code true} if the or is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + * @throws IllegalArgumentException if {@code array} contains a {@code null} + * @since 3.0.1 + */ + public static Boolean or(final Boolean... array) { + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + try { + final boolean[] primitive = ArrayUtils.toPrimitive(array); + return or(primitive) ? Boolean.TRUE : Boolean.FALSE; + } catch (final NullPointerException ex) { + throw new IllegalArgumentException("The array must not contain any null elements"); + } + } + + /** + *
Performs an xor on a set of booleans.
+ * + *+ * BooleanUtils.xor(true, true) = false + * BooleanUtils.xor(false, false) = false + * BooleanUtils.xor(true, false) = true + * BooleanUtils.xor(true, true) = false + * BooleanUtils.xor(false, false) = false + * BooleanUtils.xor(true, false) = true + *+ * + * @param array an array of {@code boolean}s + * @return {@code true} if the xor is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + */ + public static boolean xor(final boolean... array) { + // Validates input + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + + // false if the neutral element of the xor operator + boolean result = false; + for (final boolean element : array) { + result ^= element; + } + + return result; + } + + /** + *
Performs an xor on an array of Booleans.
+ * + *+ * BooleanUtils.xor(new Boolean[] { Boolean.TRUE, Boolean.TRUE }) = Boolean.FALSE + * BooleanUtils.xor(new Boolean[] { Boolean.FALSE, Boolean.FALSE }) = Boolean.FALSE + * BooleanUtils.xor(new Boolean[] { Boolean.TRUE, Boolean.FALSE }) = Boolean.TRUE + *+ * + * @param array an array of {@code Boolean}s + * @return {@code true} if the xor is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + * @throws IllegalArgumentException if {@code array} contains a {@code null} + */ + public static Boolean xor(final Boolean... array) { + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + try { + final boolean[] primitive = ArrayUtils.toPrimitive(array); + return xor(primitive) ? Boolean.TRUE : Boolean.FALSE; + } catch (final NullPointerException ex) { + throw new IllegalArgumentException("The array must not contain any null elements"); + } + } + + /** + *
Compares two {@code boolean} values. This is the same functionality as provided in Java 7.
+ * + * @param x the first {@code boolean} to compare + * @param y the second {@code boolean} to compare + * @return the value {@code 0} if {@code x == y}; + * a value less than {@code 0} if {@code !x && y}; and + * a value greater than {@code 0} if {@code x && !y} + * @since 3.4 + */ + public static int compare(boolean x, boolean y) { + if (x == y) { + return 0; + } + return x ? 1 : -1; + } + +} diff --git a/Java/commons-lang-BooleanUtils_504/metadata.json b/Java/commons-lang-BooleanUtils_504/metadata.json new file mode 100644 index 000000000..3a8b79e12 --- /dev/null +++ b/Java/commons-lang-BooleanUtils_504/metadata.json @@ -0,0 +1,21 @@ +{ + "language": "java", + "id": "commons-lang-BooleanUtils_504", + "buggyPath": ".", + "referencePath": null, + "buildCommand": "mvn package -V -B -Denforcer.skip=true -Dcheckstyle.skip=true -Dcobertura.skip=true -Drat.skip=true -Dlicense.skip=true -Dfindbugs.skip=true -Dgpg.skip=true -Dskip.npm=true -Dskip.gulp=true -Dskip.bower=true -Drat.numUnapprovedLicenses=100 -DskipTests=true -DskipITs=true -Dtest=None -DfailIfNoTests=false", + "testCommand": "mvn test -V -B -Denforcer.skip=true -Dcheckstyle.skip=true -Dcobertura.skip=true -Drat.skip=true -Dlicense.skip=true -Dfindbugs.skip=true -Dgpg.skip=true -Dskip.npm=true -Dskip.gulp=true -Dskip.bower=true -Drat.numUnapprovedLicenses=100", + "categories": [ + "safety", + "npe" + ], + "npe": { + "filepath": "src/main/java/org/apache/commons/lang3/BooleanUtils.java", + "line": 525, + "npe_method": "toIntegerObject", + "deref_field": "bool", + "npe_class": "BooleanUtils", + "repo": "commons-lang", + "bug_id": "BooleanUtils_504" + } +} diff --git a/Java/commons-lang-BooleanUtils_504/npe.json b/Java/commons-lang-BooleanUtils_504/npe.json new file mode 100644 index 000000000..d2d0c6f0d --- /dev/null +++ b/Java/commons-lang-BooleanUtils_504/npe.json @@ -0,0 +1,7 @@ +{ + "filepath": "src/main/java/org/apache/commons/lang3/BooleanUtils.java", + "line": 525, + "npe_method": "toIntegerObject", + "deref_field": "bool", + "npe_class": "BooleanUtils" +} \ No newline at end of file diff --git a/Java/commons-lang-BooleanUtils_556/Dockerfile b/Java/commons-lang-BooleanUtils_556/Dockerfile new file mode 100644 index 000000000..7b7fbe349 --- /dev/null +++ b/Java/commons-lang-BooleanUtils_556/Dockerfile @@ -0,0 +1,18 @@ +FROM ghcr.io/kupl/starlab-benchmarks/java-base:commons-lang + +ENV TZ=Asia/Seoul + +COPY ./metadata.json . +COPY ./npe.json . +COPY ./buggy.java /tmp/buggy.java +RUN export BUGGY_PATH=$(cat metadata.json | jq -r ".npe.filepath") \ + && export BUGGY_LINE=$(cat metadata.json | jq -r ".npe.line") \ + && export BUGGY_MTHD=$(cat metadata.json | jq -r ".npe.npe_method") \ + && mv /tmp/buggy.java $BUGGY_PATH \ + && echo "[{\"filepath\": \"$BUGGY_PATH\", \"line\": $BUGGY_LINE, \"method_name\": \"$BUGGY_MTHD\"}]" | jq . > traces.json + +RUN git init . && git add -A + +RUN $(cat metadata.json | jq -r ".buildCommand") + +RUN $(cat metadata.json | jq -r ".testCommand"); if [ $? -eq 0 ]; then exit 1; fi diff --git a/Java/commons-lang-BooleanUtils_556/buggy.java b/Java/commons-lang-BooleanUtils_556/buggy.java new file mode 100644 index 000000000..5bee16bb1 --- /dev/null +++ b/Java/commons-lang-BooleanUtils_556/buggy.java @@ -0,0 +1,1130 @@ +/* + * 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; + +import org.apache.commons.lang3.math.NumberUtils; + +/** + *Operations on boolean primitives and Boolean objects.
+ * + *This class tries to handle {@code null} input gracefully. + * An exception will not be thrown for a {@code null} input. + * Each method documents its behaviour in more detail.
+ * + *#ThreadSafe#
+ * @since 2.0 + */ +public class BooleanUtils { + + /** + *{@code BooleanUtils} instances should NOT be constructed in standard programming. + * Instead, the class should be used as {@code BooleanUtils.negate(true);}.
+ * + *This constructor is public to permit tools that require a JavaBean instance + * to operate.
+ */ + public BooleanUtils() { + super(); + } + + // Boolean utilities + //-------------------------------------------------------------------------- + /** + *Negates the specified boolean.
+ * + *If {@code null} is passed in, {@code null} will be returned.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.negate(Boolean.TRUE) = Boolean.FALSE; + * BooleanUtils.negate(Boolean.FALSE) = Boolean.TRUE; + * BooleanUtils.negate(null) = null; + *+ * + * @param bool the Boolean to negate, may be null + * @return the negated Boolean, or {@code null} if {@code null} input + */ + public static Boolean negate(final Boolean bool) { + if (bool == null) { + return null; + } + return bool.booleanValue() ? Boolean.FALSE : Boolean.TRUE; + } + + // boolean Boolean methods + //----------------------------------------------------------------------- + /** + *
Checks if a {@code Boolean} value is {@code true}, + * handling {@code null} by returning {@code false}.
+ * + *+ * BooleanUtils.isTrue(Boolean.TRUE) = true + * BooleanUtils.isTrue(Boolean.FALSE) = false + * BooleanUtils.isTrue(null) = false + *+ * + * @param bool the boolean to check, null returns {@code false} + * @return {@code true} only if the input is non-null and true + * @since 2.1 + */ + public static boolean isTrue(final Boolean bool) { + return Boolean.TRUE.equals(bool); + } + + /** + *
Checks if a {@code Boolean} value is not {@code true}, + * handling {@code null} by returning {@code true}.
+ * + *+ * BooleanUtils.isNotTrue(Boolean.TRUE) = false + * BooleanUtils.isNotTrue(Boolean.FALSE) = true + * BooleanUtils.isNotTrue(null) = true + *+ * + * @param bool the boolean to check, null returns {@code true} + * @return {@code true} if the input is null or false + * @since 2.3 + */ + public static boolean isNotTrue(final Boolean bool) { + return !isTrue(bool); + } + + /** + *
Checks if a {@code Boolean} value is {@code false}, + * handling {@code null} by returning {@code false}.
+ * + *+ * BooleanUtils.isFalse(Boolean.TRUE) = false + * BooleanUtils.isFalse(Boolean.FALSE) = true + * BooleanUtils.isFalse(null) = false + *+ * + * @param bool the boolean to check, null returns {@code false} + * @return {@code true} only if the input is non-null and false + * @since 2.1 + */ + public static boolean isFalse(final Boolean bool) { + return Boolean.FALSE.equals(bool); + } + + /** + *
Checks if a {@code Boolean} value is not {@code false}, + * handling {@code null} by returning {@code true}.
+ * + *+ * BooleanUtils.isNotFalse(Boolean.TRUE) = true + * BooleanUtils.isNotFalse(Boolean.FALSE) = false + * BooleanUtils.isNotFalse(null) = true + *+ * + * @param bool the boolean to check, null returns {@code true} + * @return {@code true} if the input is null or true + * @since 2.3 + */ + public static boolean isNotFalse(final Boolean bool) { + return !isFalse(bool); + } + + //----------------------------------------------------------------------- + /** + *
Converts a Boolean to a boolean handling {@code null} + * by returning {@code false}.
+ * + *+ * BooleanUtils.toBoolean(Boolean.TRUE) = true + * BooleanUtils.toBoolean(Boolean.FALSE) = false + * BooleanUtils.toBoolean(null) = false + *+ * + * @param bool the boolean to convert + * @return {@code true} or {@code false}, {@code null} returns {@code false} + */ + public static boolean toBoolean(final Boolean bool) { + return bool != null && bool.booleanValue(); + } + + /** + *
Converts a Boolean to a boolean handling {@code null}.
+ * + *+ * BooleanUtils.toBooleanDefaultIfNull(Boolean.TRUE, false) = true + * BooleanUtils.toBooleanDefaultIfNull(Boolean.FALSE, true) = false + * BooleanUtils.toBooleanDefaultIfNull(null, true) = true + *+ * + * @param bool the boolean to convert + * @param valueIfNull the boolean value to return if {@code null} + * @return {@code true} or {@code false} + */ + public static boolean toBooleanDefaultIfNull(final Boolean bool, final boolean valueIfNull) { + if (bool == null) { + return valueIfNull; + } + return bool.booleanValue(); + } + + // Integer to Boolean methods + //----------------------------------------------------------------------- + /** + *
Converts an int to a boolean using the convention that {@code zero} + * is {@code false}.
+ * + *+ * BooleanUtils.toBoolean(0) = false + * BooleanUtils.toBoolean(1) = true + * BooleanUtils.toBoolean(2) = true + *+ * + * @param value the int to convert + * @return {@code true} if non-zero, {@code false} + * if zero + */ + public static boolean toBoolean(final int value) { + return value != 0; + } + + /** + *
Converts an int to a Boolean using the convention that {@code zero} + * is {@code false}.
+ * + *+ * BooleanUtils.toBoolean(0) = Boolean.FALSE + * BooleanUtils.toBoolean(1) = Boolean.TRUE + * BooleanUtils.toBoolean(2) = Boolean.TRUE + *+ * + * @param value the int to convert + * @return Boolean.TRUE if non-zero, Boolean.FALSE if zero, + * {@code null} if {@code null} + */ + public static Boolean toBooleanObject(final int value) { + return value == 0 ? Boolean.FALSE : Boolean.TRUE; + } + + /** + *
Converts an Integer to a Boolean using the convention that {@code zero} + * is {@code false}.
+ * + *{@code null} will be converted to {@code null}.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.toBoolean(Integer.valueOf(0)) = Boolean.FALSE + * BooleanUtils.toBoolean(Integer.valueOf(1)) = Boolean.TRUE + * BooleanUtils.toBoolean(Integer.valueOf(null)) = null + *+ * + * @param value the Integer to convert + * @return Boolean.TRUE if non-zero, Boolean.FALSE if zero, + * {@code null} if {@code null} input + */ + public static Boolean toBooleanObject(final Integer value) { + if (value == null) { + return null; + } + return value.intValue() == 0 ? Boolean.FALSE : Boolean.TRUE; + } + + /** + *
Converts an int to a boolean specifying the conversion values.
+ * + *+ * BooleanUtils.toBoolean(0, 1, 0) = false + * BooleanUtils.toBoolean(1, 1, 0) = true + * BooleanUtils.toBoolean(2, 1, 2) = false + * BooleanUtils.toBoolean(2, 2, 0) = true + *+ * + * @param value the Integer to convert + * @param trueValue the value to match for {@code true} + * @param falseValue the value to match for {@code false} + * @return {@code true} or {@code false} + * @throws IllegalArgumentException if no match + */ + public static boolean toBoolean(final int value, final int trueValue, final int falseValue) { + if (value == trueValue) { + return true; + } + if (value == falseValue) { + return false; + } + // no match + throw new IllegalArgumentException("The Integer did not match either specified value"); + } + + /** + *
Converts an Integer to a boolean specifying the conversion values.
+ * + *+ * BooleanUtils.toBoolean(Integer.valueOf(0), Integer.valueOf(1), Integer.valueOf(0)) = false + * BooleanUtils.toBoolean(Integer.valueOf(1), Integer.valueOf(1), Integer.valueOf(0)) = true + * BooleanUtils.toBoolean(Integer.valueOf(2), Integer.valueOf(1), Integer.valueOf(2)) = false + * BooleanUtils.toBoolean(Integer.valueOf(2), Integer.valueOf(2), Integer.valueOf(0)) = true + * BooleanUtils.toBoolean(null, null, Integer.valueOf(0)) = true + *+ * + * @param value the Integer to convert + * @param trueValue the value to match for {@code true}, may be {@code null} + * @param falseValue the value to match for {@code false}, may be {@code null} + * @return {@code true} or {@code false} + * @throws IllegalArgumentException if no match + */ + public static boolean toBoolean(final Integer value, final Integer trueValue, final Integer falseValue) { + if (value == null) { + if (trueValue == null) { + return true; + } + if (falseValue == null) { + return false; + } + } else if (value.equals(trueValue)) { + return true; + } else if (value.equals(falseValue)) { + return false; + } + // no match + throw new IllegalArgumentException("The Integer did not match either specified value"); + } + + /** + *
Converts an int to a Boolean specifying the conversion values.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.toBooleanObject(0, 0, 2, 3) = Boolean.TRUE + * BooleanUtils.toBooleanObject(2, 1, 2, 3) = Boolean.FALSE + * BooleanUtils.toBooleanObject(3, 1, 2, 3) = null + *+ * + * @param value the Integer to convert + * @param trueValue the value to match for {@code true} + * @param falseValue the value to match for {@code false} + * @param nullValue the value to to match for {@code null} + * @return Boolean.TRUE, Boolean.FALSE, or {@code null} + * @throws IllegalArgumentException if no match + */ + public static Boolean toBooleanObject(final int value, final int trueValue, final int falseValue, final int nullValue) { + if (value == trueValue) { + return Boolean.TRUE; + } + if (value == falseValue) { + return Boolean.FALSE; + } + if (value == nullValue) { + return null; + } + // no match + throw new IllegalArgumentException("The Integer did not match any specified value"); + } + + /** + *
Converts an Integer to a Boolean specifying the conversion values.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.toBooleanObject(Integer.valueOf(0), Integer.valueOf(0), Integer.valueOf(2), Integer.valueOf(3)) = Boolean.TRUE + * BooleanUtils.toBooleanObject(Integer.valueOf(2), Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(3)) = Boolean.FALSE + * BooleanUtils.toBooleanObject(Integer.valueOf(3), Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(3)) = null + *+ * + * @param value the Integer to convert + * @param trueValue the value to match for {@code true}, may be {@code null} + * @param falseValue the value to match for {@code false}, may be {@code null} + * @param nullValue the value to to match for {@code null}, may be {@code null} + * @return Boolean.TRUE, Boolean.FALSE, or {@code null} + * @throws IllegalArgumentException if no match + */ + public static Boolean toBooleanObject(final Integer value, final Integer trueValue, final Integer falseValue, final Integer nullValue) { + if (value == null) { + if (trueValue == null) { + return Boolean.TRUE; + } + if (falseValue == null) { + return Boolean.FALSE; + } + if (nullValue == null) { + return null; + } + } else if (value.equals(trueValue)) { + return Boolean.TRUE; + } else if (value.equals(falseValue)) { + return Boolean.FALSE; + } else if (value.equals(nullValue)) { + return null; + } + // no match + throw new IllegalArgumentException("The Integer did not match any specified value"); + } + + // Boolean to Integer methods + //----------------------------------------------------------------------- + /** + *
Converts a boolean to an int using the convention that + * {@code zero} is {@code false}.
+ * + *+ * BooleanUtils.toInteger(true) = 1 + * BooleanUtils.toInteger(false) = 0 + *+ * + * @param bool the boolean to convert + * @return one if {@code true}, zero if {@code false} + */ + public static int toInteger(final boolean bool) { + return bool ? 1 : 0; + } + + /** + *
Converts a boolean to an Integer using the convention that + * {@code zero} is {@code false}.
+ * + *+ * BooleanUtils.toIntegerObject(true) = Integer.valueOf(1) + * BooleanUtils.toIntegerObject(false) = Integer.valueOf(0) + *+ * + * @param bool the boolean to convert + * @return one if {@code true}, zero if {@code false} + */ + public static Integer toIntegerObject(final boolean bool) { + return bool ? NumberUtils.INTEGER_ONE : NumberUtils.INTEGER_ZERO; + } + + /** + *
Converts a Boolean to a Integer using the convention that + * {@code zero} is {@code false}.
+ * + *{@code null} will be converted to {@code null}.
+ * + *+ * BooleanUtils.toIntegerObject(Boolean.TRUE) = Integer.valueOf(1) + * BooleanUtils.toIntegerObject(Boolean.FALSE) = Integer.valueOf(0) + *+ * + * @param bool the Boolean to convert + * @return one if Boolean.TRUE, zero if Boolean.FALSE, {@code null} if {@code null} + */ + public static Integer toIntegerObject(final Boolean bool) { + if (bool == null) { + return null; + } + return bool.booleanValue() ? NumberUtils.INTEGER_ONE : NumberUtils.INTEGER_ZERO; + } + + /** + *
Converts a boolean to an int specifying the conversion values.
+ * + *+ * BooleanUtils.toInteger(true, 1, 0) = 1 + * BooleanUtils.toInteger(false, 1, 0) = 0 + *+ * + * @param bool the to convert + * @param trueValue the value to return if {@code true} + * @param falseValue the value to return if {@code false} + * @return the appropriate value + */ + public static int toInteger(final boolean bool, final int trueValue, final int falseValue) { + return bool ? trueValue : falseValue; + } + + /** + *
Converts a Boolean to an int specifying the conversion values.
+ * + *+ * BooleanUtils.toInteger(Boolean.TRUE, 1, 0, 2) = 1 + * BooleanUtils.toInteger(Boolean.FALSE, 1, 0, 2) = 0 + * BooleanUtils.toInteger(null, 1, 0, 2) = 2 + *+ * + * @param bool the Boolean to convert + * @param trueValue the value to return if {@code true} + * @param falseValue the value to return if {@code false} + * @param nullValue the value to return if {@code null} + * @return the appropriate value + */ + public static int toInteger(final Boolean bool, final int trueValue, final int falseValue, final int nullValue) { + if (bool == null) { + return nullValue; + } + return bool.booleanValue() ? trueValue : falseValue; + } + + /** + *
Converts a boolean to an Integer specifying the conversion values.
+ * + *+ * BooleanUtils.toIntegerObject(true, Integer.valueOf(1), Integer.valueOf(0)) = Integer.valueOf(1) + * BooleanUtils.toIntegerObject(false, Integer.valueOf(1), Integer.valueOf(0)) = Integer.valueOf(0) + *+ * + * @param bool the to convert + * @param trueValue the value to return if {@code true}, may be {@code null} + * @param falseValue the value to return if {@code false}, may be {@code null} + * @return the appropriate value + */ + public static Integer toIntegerObject(final boolean bool, final Integer trueValue, final Integer falseValue) { + return bool ? trueValue : falseValue; + } + + /** + *
Converts a Boolean to an Integer specifying the conversion values.
+ * + *+ * BooleanUtils.toIntegerObject(Boolean.TRUE, Integer.valueOf(1), Integer.valueOf(0), Integer.valueOf(2)) = Integer.valueOf(1) + * BooleanUtils.toIntegerObject(Boolean.FALSE, Integer.valueOf(1), Integer.valueOf(0), Integer.valueOf(2)) = Integer.valueOf(0) + * BooleanUtils.toIntegerObject(null, Integer.valueOf(1), Integer.valueOf(0), Integer.valueOf(2)) = Integer.valueOf(2) + *+ * + * @param bool the Boolean to convert + * @param trueValue the value to return if {@code true}, may be {@code null} + * @param falseValue the value to return if {@code false}, may be {@code null} + * @param nullValue the value to return if {@code null}, may be {@code null} + * @return the appropriate value + */ + public static Integer toIntegerObject(final Boolean bool, final Integer trueValue, final Integer falseValue, final Integer nullValue) { + if (bool == null) { + return nullValue; + } + return bool.booleanValue() ? trueValue : falseValue; + } + + // String to Boolean methods + //----------------------------------------------------------------------- + /** + *
Converts a String to a Boolean.
+ * + *{@code 'true'}, {@code 'on'}, {@code 'y'}, {@code 't'} or {@code 'yes'} + * (case insensitive) will return {@code true}. + * {@code 'false'}, {@code 'off'}, {@code 'n'}, {@code 'f'} or {@code 'no'} + * (case insensitive) will return {@code false}. + * Otherwise, {@code null} is returned.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * // N.B. case is not significant + * BooleanUtils.toBooleanObject(null) = null + * BooleanUtils.toBooleanObject("true") = Boolean.TRUE + * BooleanUtils.toBooleanObject("T") = Boolean.TRUE // i.e. T[RUE] + * BooleanUtils.toBooleanObject("false") = Boolean.FALSE + * BooleanUtils.toBooleanObject("f") = Boolean.FALSE // i.e. f[alse] + * BooleanUtils.toBooleanObject("No") = Boolean.FALSE + * BooleanUtils.toBooleanObject("n") = Boolean.FALSE // i.e. n[o] + * BooleanUtils.toBooleanObject("on") = Boolean.TRUE + * BooleanUtils.toBooleanObject("ON") = Boolean.TRUE + * BooleanUtils.toBooleanObject("off") = Boolean.FALSE + * BooleanUtils.toBooleanObject("oFf") = Boolean.FALSE + * BooleanUtils.toBooleanObject("yes") = Boolean.TRUE + * BooleanUtils.toBooleanObject("Y") = Boolean.TRUE // i.e. Y[ES] + * BooleanUtils.toBooleanObject("blue") = null + * BooleanUtils.toBooleanObject("true ") = null // trailing space (too long) + * BooleanUtils.toBooleanObject("ono") = null // does not match on or no + *+ * + * @param str the String to check; upper and lower case are treated as the same + * @return the Boolean value of the string, {@code null} if no match or {@code null} input + */ +// String to Boolean methods +// ----------------------------------------------------------------------- +/** + *
Converts a String to a Boolean.
+ * + *{@code 'true'}, {@code 'on'}, {@code 'y'}, {@code 't'} or {@code 'yes'} + * (case insensitive) will return {@code true}. + * {@code 'false'}, {@code 'off'}, {@code 'n'}, {@code 'f'} or {@code 'no'} + * (case insensitive) will return {@code false}. + * Otherwise, {@code null} is returned.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * // N.B. case is not significant + * BooleanUtils.toBooleanObject(null) = null + * BooleanUtils.toBooleanObject("true") = Boolean.TRUE + * BooleanUtils.toBooleanObject("T") = Boolean.TRUE // i.e. T[RUE] + * BooleanUtils.toBooleanObject("false") = Boolean.FALSE + * BooleanUtils.toBooleanObject("f") = Boolean.FALSE // i.e. f[alse] + * BooleanUtils.toBooleanObject("No") = Boolean.FALSE + * BooleanUtils.toBooleanObject("n") = Boolean.FALSE // i.e. n[o] + * BooleanUtils.toBooleanObject("on") = Boolean.TRUE + * BooleanUtils.toBooleanObject("ON") = Boolean.TRUE + * BooleanUtils.toBooleanObject("off") = Boolean.FALSE + * BooleanUtils.toBooleanObject("oFf") = Boolean.FALSE + * BooleanUtils.toBooleanObject("yes") = Boolean.TRUE + * BooleanUtils.toBooleanObject("Y") = Boolean.TRUE // i.e. Y[ES] + * BooleanUtils.toBooleanObject("blue") = null + * BooleanUtils.toBooleanObject("true ") = null // trailing space (too long) + * BooleanUtils.toBooleanObject("ono") = null // does not match on or no + *+ * + * @param str + * the String to check; upper and lower case are treated as the same + * @return the Boolean value of the string, {@code null} if no match or {@code null} input + */ +public static java.lang.Boolean toBooleanObject(final java.lang.String str) { + // Previously used equalsIgnoreCase, which was fast for interned 'true'. + // Non interned 'true' matched 15 times slower. + // + // Optimisation provides same performance as before for interned 'true'. + // Similar performance for null, 'false', and other strings not length 2/3/4. + // 'true'/'TRUE' match 4 times slower, 'tRUE'/'True' 7 times slower. + if (str == "true") { + return java.lang.Boolean.TRUE; + } + { + switch (/* NPEX_NULL_EXP */ + str.length()) { + case 1 : + { + final char ch0 = str.charAt(0); + if ((((ch0 == 'y') || (ch0 == 'Y')) || (ch0 == 't')) || (ch0 == 'T')) { + return java.lang.Boolean.TRUE; + } + if ((((ch0 == 'n') || (ch0 == 'N')) || (ch0 == 'f')) || (ch0 == 'F')) { + return java.lang.Boolean.FALSE; + } + break; + } + case 2 : + { + final char ch0 = str.charAt(0); + final char ch1 = str.charAt(1); + if (((ch0 == 'o') || (ch0 == 'O')) && ((ch1 == 'n') || (ch1 == 'N'))) { + return java.lang.Boolean.TRUE; + } + if (((ch0 == 'n') || (ch0 == 'N')) && ((ch1 == 'o') || (ch1 == 'O'))) { + return java.lang.Boolean.FALSE; + } + break; + } + case 3 : + { + final char ch0 = str.charAt(0); + final char ch1 = str.charAt(1); + final char ch2 = str.charAt(2); + if ((((ch0 == 'y') || (ch0 == 'Y')) && ((ch1 == 'e') || (ch1 == 'E'))) && ((ch2 == 's') || (ch2 == 'S'))) { + return java.lang.Boolean.TRUE; + } + if ((((ch0 == 'o') || (ch0 == 'O')) && ((ch1 == 'f') || (ch1 == 'F'))) && ((ch2 == 'f') || (ch2 == 'F'))) { + return java.lang.Boolean.FALSE; + } + break; + } + case 4 : + { + final char ch0 = str.charAt(0); + final char ch1 = str.charAt(1); + final char ch2 = str.charAt(2); + final char ch3 = str.charAt(3); + if (((((ch0 == 't') || (ch0 == 'T')) && ((ch1 == 'r') || (ch1 == 'R'))) && ((ch2 == 'u') || (ch2 == 'U'))) && ((ch3 == 'e') || (ch3 == 'E'))) { + return java.lang.Boolean.TRUE; + } + break; + } + case 5 : + { + final char ch0 = str.charAt(0); + final char ch1 = str.charAt(1); + final char ch2 = str.charAt(2); + final char ch3 = str.charAt(3); + final char ch4 = str.charAt(4); + if ((((((ch0 == 'f') || (ch0 == 'F')) && ((ch1 == 'a') || (ch1 == 'A'))) && ((ch2 == 'l') || (ch2 == 'L'))) && ((ch3 == 's') || (ch3 == 'S'))) && ((ch4 == 'e') || (ch4 == 'E'))) { + return java.lang.Boolean.FALSE; + } + break; + } + default : + break; + } + return null; + } +} + + /** + *
Converts a String to a Boolean throwing an exception if no match.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.toBooleanObject("true", "true", "false", "null") = Boolean.TRUE + * BooleanUtils.toBooleanObject("false", "true", "false", "null") = Boolean.FALSE + * BooleanUtils.toBooleanObject("null", "true", "false", "null") = null + *+ * + * @param str the String to check + * @param trueString the String to match for {@code true} (case sensitive), may be {@code null} + * @param falseString the String to match for {@code false} (case sensitive), may be {@code null} + * @param nullString the String to match for {@code null} (case sensitive), may be {@code null} + * @return the Boolean value of the string, {@code null} if either the String matches {@code nullString} + * or if {@code null} input and {@code nullString} is {@code null} + * @throws IllegalArgumentException if the String doesn't match + */ + public static Boolean toBooleanObject(final String str, final String trueString, final String falseString, final String nullString) { + if (str == null) { + if (trueString == null) { + return Boolean.TRUE; + } + if (falseString == null) { + return Boolean.FALSE; + } + if (nullString == null) { + return null; + } + } else if (str.equals(trueString)) { + return Boolean.TRUE; + } else if (str.equals(falseString)) { + return Boolean.FALSE; + } else if (str.equals(nullString)) { + return null; + } + // no match + throw new IllegalArgumentException("The String did not match any specified value"); + } + + // String to boolean methods + //----------------------------------------------------------------------- + /** + *
Converts a String to a boolean (optimised for performance).
+ * + *{@code 'true'}, {@code 'on'}, {@code 'y'}, {@code 't'} or {@code 'yes'} + * (case insensitive) will return {@code true}. Otherwise, + * {@code false} is returned.
+ * + *This method performs 4 times faster (JDK1.4) than + * {@code Boolean.valueOf(String)}. However, this method accepts + * 'on' and 'yes', 't', 'y' as true values. + * + *
+ * BooleanUtils.toBoolean(null) = false + * BooleanUtils.toBoolean("true") = true + * BooleanUtils.toBoolean("TRUE") = true + * BooleanUtils.toBoolean("tRUe") = true + * BooleanUtils.toBoolean("on") = true + * BooleanUtils.toBoolean("yes") = true + * BooleanUtils.toBoolean("false") = false + * BooleanUtils.toBoolean("x gti") = false + * BooleanUtils.toBooleanObject("y") = true + * BooleanUtils.toBooleanObject("n") = false + * BooleanUtils.toBooleanObject("t") = true + * BooleanUtils.toBooleanObject("f") = false + *+ * + * @param str the String to check + * @return the boolean value of the string, {@code false} if no match or the String is null + */ + public static boolean toBoolean(final String str) { + return toBooleanObject(str) == Boolean.TRUE; + } + + /** + *
Converts a String to a Boolean throwing an exception if no match found.
+ * + *+ * BooleanUtils.toBoolean("true", "true", "false") = true + * BooleanUtils.toBoolean("false", "true", "false") = false + *+ * + * @param str the String to check + * @param trueString the String to match for {@code true} (case sensitive), may be {@code null} + * @param falseString the String to match for {@code false} (case sensitive), may be {@code null} + * @return the boolean value of the string + * @throws IllegalArgumentException if the String doesn't match + */ + public static boolean toBoolean(final String str, final String trueString, final String falseString) { + if (str == trueString) { + return true; + } else if (str == falseString) { + return false; + } else if (str != null) { + if (str.equals(trueString)) { + return true; + } else if (str.equals(falseString)) { + return false; + } + } + // no match + throw new IllegalArgumentException("The String did not match either specified value"); + } + + // Boolean to String methods + //----------------------------------------------------------------------- + /** + *
Converts a Boolean to a String returning {@code 'true'}, + * {@code 'false'}, or {@code null}.
+ * + *+ * BooleanUtils.toStringTrueFalse(Boolean.TRUE) = "true" + * BooleanUtils.toStringTrueFalse(Boolean.FALSE) = "false" + * BooleanUtils.toStringTrueFalse(null) = null; + *+ * + * @param bool the Boolean to check + * @return {@code 'true'}, {@code 'false'}, or {@code null} + */ + public static String toStringTrueFalse(final Boolean bool) { + return toString(bool, "true", "false", null); + } + + /** + *
Converts a Boolean to a String returning {@code 'on'}, + * {@code 'off'}, or {@code null}.
+ * + *+ * BooleanUtils.toStringOnOff(Boolean.TRUE) = "on" + * BooleanUtils.toStringOnOff(Boolean.FALSE) = "off" + * BooleanUtils.toStringOnOff(null) = null; + *+ * + * @param bool the Boolean to check + * @return {@code 'on'}, {@code 'off'}, or {@code null} + */ + public static String toStringOnOff(final Boolean bool) { + return toString(bool, "on", "off", null); + } + + /** + *
Converts a Boolean to a String returning {@code 'yes'}, + * {@code 'no'}, or {@code null}.
+ * + *+ * BooleanUtils.toStringYesNo(Boolean.TRUE) = "yes" + * BooleanUtils.toStringYesNo(Boolean.FALSE) = "no" + * BooleanUtils.toStringYesNo(null) = null; + *+ * + * @param bool the Boolean to check + * @return {@code 'yes'}, {@code 'no'}, or {@code null} + */ + public static String toStringYesNo(final Boolean bool) { + return toString(bool, "yes", "no", null); + } + + /** + *
Converts a Boolean to a String returning one of the input Strings.
+ * + *+ * BooleanUtils.toString(Boolean.TRUE, "true", "false", null) = "true" + * BooleanUtils.toString(Boolean.FALSE, "true", "false", null) = "false" + * BooleanUtils.toString(null, "true", "false", null) = null; + *+ * + * @param bool the Boolean to check + * @param trueString the String to return if {@code true}, may be {@code null} + * @param falseString the String to return if {@code false}, may be {@code null} + * @param nullString the String to return if {@code null}, may be {@code null} + * @return one of the three input Strings + */ + public static String toString(final Boolean bool, final String trueString, final String falseString, final String nullString) { + if (bool == null) { + return nullString; + } + return bool.booleanValue() ? trueString : falseString; + } + + // boolean to String methods + //----------------------------------------------------------------------- + /** + *
Converts a boolean to a String returning {@code 'true'} + * or {@code 'false'}.
+ * + *+ * BooleanUtils.toStringTrueFalse(true) = "true" + * BooleanUtils.toStringTrueFalse(false) = "false" + *+ * + * @param bool the Boolean to check + * @return {@code 'true'}, {@code 'false'}, or {@code null} + */ + public static String toStringTrueFalse(final boolean bool) { + return toString(bool, "true", "false"); + } + + /** + *
Converts a boolean to a String returning {@code 'on'} + * or {@code 'off'}.
+ * + *+ * BooleanUtils.toStringOnOff(true) = "on" + * BooleanUtils.toStringOnOff(false) = "off" + *+ * + * @param bool the Boolean to check + * @return {@code 'on'}, {@code 'off'}, or {@code null} + */ + public static String toStringOnOff(final boolean bool) { + return toString(bool, "on", "off"); + } + + /** + *
Converts a boolean to a String returning {@code 'yes'} + * or {@code 'no'}.
+ * + *+ * BooleanUtils.toStringYesNo(true) = "yes" + * BooleanUtils.toStringYesNo(false) = "no" + *+ * + * @param bool the Boolean to check + * @return {@code 'yes'}, {@code 'no'}, or {@code null} + */ + public static String toStringYesNo(final boolean bool) { + return toString(bool, "yes", "no"); + } + + /** + *
Converts a boolean to a String returning one of the input Strings.
+ * + *+ * BooleanUtils.toString(true, "true", "false") = "true" + * BooleanUtils.toString(false, "true", "false") = "false" + *+ * + * @param bool the Boolean to check + * @param trueString the String to return if {@code true}, may be {@code null} + * @param falseString the String to return if {@code false}, may be {@code null} + * @return one of the two input Strings + */ + public static String toString(final boolean bool, final String trueString, final String falseString) { + return bool ? trueString : falseString; + } + + // logical operations + // ---------------------------------------------------------------------- + /** + *
Performs an and on a set of booleans.
+ * + *+ * BooleanUtils.and(true, true) = true + * BooleanUtils.and(false, false) = false + * BooleanUtils.and(true, false) = false + * BooleanUtils.and(true, true, false) = false + * BooleanUtils.and(true, true, true) = true + *+ * + * @param array an array of {@code boolean}s + * @return {@code true} if the and is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + * @since 3.0.1 + */ + public static boolean and(final boolean... array) { + // Validates input + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + for (final boolean element : array) { + if (!element) { + return false; + } + } + return true; + } + + /** + *
Performs an and on an array of Booleans.
+ * + *+ * BooleanUtils.and(Boolean.TRUE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.and(Boolean.FALSE, Boolean.FALSE) = Boolean.FALSE + * BooleanUtils.and(Boolean.TRUE, Boolean.FALSE) = Boolean.FALSE + * BooleanUtils.and(Boolean.TRUE, Boolean.TRUE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.and(Boolean.FALSE, Boolean.FALSE, Boolean.TRUE) = Boolean.FALSE + * BooleanUtils.and(Boolean.TRUE, Boolean.FALSE, Boolean.TRUE) = Boolean.FALSE + *+ * + * @param array an array of {@code Boolean}s + * @return {@code true} if the and is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + * @throws IllegalArgumentException if {@code array} contains a {@code null} + * @since 3.0.1 + */ + public static Boolean and(final Boolean... array) { + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + try { + final boolean[] primitive = ArrayUtils.toPrimitive(array); + return and(primitive) ? Boolean.TRUE : Boolean.FALSE; + } catch (final NullPointerException ex) { + throw new IllegalArgumentException("The array must not contain any null elements"); + } + } + + /** + *
Performs an or on a set of booleans.
+ * + *+ * BooleanUtils.or(true, true) = true + * BooleanUtils.or(false, false) = false + * BooleanUtils.or(true, false) = true + * BooleanUtils.or(true, true, false) = true + * BooleanUtils.or(true, true, true) = true + * BooleanUtils.or(false, false, false) = false + *+ * + * @param array an array of {@code boolean}s + * @return {@code true} if the or is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + * @since 3.0.1 + */ + public static boolean or(final boolean... array) { + // Validates input + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + for (final boolean element : array) { + if (element) { + return true; + } + } + return false; + } + + /** + *
Performs an or on an array of Booleans.
+ * + *+ * BooleanUtils.or(Boolean.TRUE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.or(Boolean.FALSE, Boolean.FALSE) = Boolean.FALSE + * BooleanUtils.or(Boolean.TRUE, Boolean.FALSE) = Boolean.TRUE + * BooleanUtils.or(Boolean.TRUE, Boolean.TRUE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.or(Boolean.FALSE, Boolean.FALSE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.or(Boolean.TRUE, Boolean.FALSE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.or(Boolean.FALSE, Boolean.FALSE, Boolean.FALSE) = Boolean.FALSE + *+ * + * @param array an array of {@code Boolean}s + * @return {@code true} if the or is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + * @throws IllegalArgumentException if {@code array} contains a {@code null} + * @since 3.0.1 + */ + public static Boolean or(final Boolean... array) { + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + try { + final boolean[] primitive = ArrayUtils.toPrimitive(array); + return or(primitive) ? Boolean.TRUE : Boolean.FALSE; + } catch (final NullPointerException ex) { + throw new IllegalArgumentException("The array must not contain any null elements"); + } + } + + /** + *
Performs an xor on a set of booleans.
+ * + *+ * BooleanUtils.xor(true, true) = false + * BooleanUtils.xor(false, false) = false + * BooleanUtils.xor(true, false) = true + * BooleanUtils.xor(true, true) = false + * BooleanUtils.xor(false, false) = false + * BooleanUtils.xor(true, false) = true + *+ * + * @param array an array of {@code boolean}s + * @return {@code true} if the xor is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + */ + public static boolean xor(final boolean... array) { + // Validates input + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + + // false if the neutral element of the xor operator + boolean result = false; + for (final boolean element : array) { + result ^= element; + } + + return result; + } + + /** + *
Performs an xor on an array of Booleans.
+ * + *+ * BooleanUtils.xor(new Boolean[] { Boolean.TRUE, Boolean.TRUE }) = Boolean.FALSE + * BooleanUtils.xor(new Boolean[] { Boolean.FALSE, Boolean.FALSE }) = Boolean.FALSE + * BooleanUtils.xor(new Boolean[] { Boolean.TRUE, Boolean.FALSE }) = Boolean.TRUE + *+ * + * @param array an array of {@code Boolean}s + * @return {@code true} if the xor is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + * @throws IllegalArgumentException if {@code array} contains a {@code null} + */ + public static Boolean xor(final Boolean... array) { + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + try { + final boolean[] primitive = ArrayUtils.toPrimitive(array); + return xor(primitive) ? Boolean.TRUE : Boolean.FALSE; + } catch (final NullPointerException ex) { + throw new IllegalArgumentException("The array must not contain any null elements"); + } + } + + /** + *
Compares two {@code boolean} values. This is the same functionality as provided in Java 7.
+ * + * @param x the first {@code boolean} to compare + * @param y the second {@code boolean} to compare + * @return the value {@code 0} if {@code x == y}; + * a value less than {@code 0} if {@code !x && y}; and + * a value greater than {@code 0} if {@code x && !y} + * @since 3.4 + */ + public static int compare(boolean x, boolean y) { + if (x == y) { + return 0; + } + return x ? 1 : -1; + } + +} diff --git a/Java/commons-lang-BooleanUtils_556/metadata.json b/Java/commons-lang-BooleanUtils_556/metadata.json new file mode 100644 index 000000000..d139c0486 --- /dev/null +++ b/Java/commons-lang-BooleanUtils_556/metadata.json @@ -0,0 +1,21 @@ +{ + "language": "java", + "id": "commons-lang-BooleanUtils_556", + "buggyPath": ".", + "referencePath": null, + "buildCommand": "mvn package -V -B -Denforcer.skip=true -Dcheckstyle.skip=true -Dcobertura.skip=true -Drat.skip=true -Dlicense.skip=true -Dfindbugs.skip=true -Dgpg.skip=true -Dskip.npm=true -Dskip.gulp=true -Dskip.bower=true -Drat.numUnapprovedLicenses=100 -DskipTests=true -DskipITs=true -Dtest=None -DfailIfNoTests=false", + "testCommand": "mvn test -V -B -Denforcer.skip=true -Dcheckstyle.skip=true -Dcobertura.skip=true -Drat.skip=true -Dlicense.skip=true -Dfindbugs.skip=true -Dgpg.skip=true -Dskip.npm=true -Dskip.gulp=true -Dskip.bower=true -Drat.numUnapprovedLicenses=100", + "categories": [ + "safety", + "npe" + ], + "npe": { + "filepath": "src/main/java/org/apache/commons/lang3/BooleanUtils.java", + "line": 595, + "npe_method": "toBooleanObject", + "deref_field": "str", + "npe_class": "BooleanUtils", + "repo": "commons-lang", + "bug_id": "BooleanUtils_556" + } +} diff --git a/Java/commons-lang-BooleanUtils_556/npe.json b/Java/commons-lang-BooleanUtils_556/npe.json new file mode 100644 index 000000000..94983cf9f --- /dev/null +++ b/Java/commons-lang-BooleanUtils_556/npe.json @@ -0,0 +1,7 @@ +{ + "filepath": "src/main/java/org/apache/commons/lang3/BooleanUtils.java", + "line": 595, + "npe_method": "toBooleanObject", + "deref_field": "str", + "npe_class": "BooleanUtils" +} \ No newline at end of file diff --git a/Java/commons-lang-BooleanUtils_63/Dockerfile b/Java/commons-lang-BooleanUtils_63/Dockerfile new file mode 100644 index 000000000..7b7fbe349 --- /dev/null +++ b/Java/commons-lang-BooleanUtils_63/Dockerfile @@ -0,0 +1,18 @@ +FROM ghcr.io/kupl/starlab-benchmarks/java-base:commons-lang + +ENV TZ=Asia/Seoul + +COPY ./metadata.json . +COPY ./npe.json . +COPY ./buggy.java /tmp/buggy.java +RUN export BUGGY_PATH=$(cat metadata.json | jq -r ".npe.filepath") \ + && export BUGGY_LINE=$(cat metadata.json | jq -r ".npe.line") \ + && export BUGGY_MTHD=$(cat metadata.json | jq -r ".npe.npe_method") \ + && mv /tmp/buggy.java $BUGGY_PATH \ + && echo "[{\"filepath\": \"$BUGGY_PATH\", \"line\": $BUGGY_LINE, \"method_name\": \"$BUGGY_MTHD\"}]" | jq . > traces.json + +RUN git init . && git add -A + +RUN $(cat metadata.json | jq -r ".buildCommand") + +RUN $(cat metadata.json | jq -r ".testCommand"); if [ $? -eq 0 ]; then exit 1; fi diff --git a/Java/commons-lang-BooleanUtils_63/buggy.java b/Java/commons-lang-BooleanUtils_63/buggy.java new file mode 100644 index 000000000..0c83d5b7c --- /dev/null +++ b/Java/commons-lang-BooleanUtils_63/buggy.java @@ -0,0 +1,1123 @@ +/* + * 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; + +import org.apache.commons.lang3.math.NumberUtils; + +/** + *Operations on boolean primitives and Boolean objects.
+ * + *This class tries to handle {@code null} input gracefully. + * An exception will not be thrown for a {@code null} input. + * Each method documents its behaviour in more detail.
+ * + *#ThreadSafe#
+ * @since 2.0 + */ +public class BooleanUtils { + + /** + *{@code BooleanUtils} instances should NOT be constructed in standard programming. + * Instead, the class should be used as {@code BooleanUtils.negate(true);}.
+ * + *This constructor is public to permit tools that require a JavaBean instance + * to operate.
+ */ + public BooleanUtils() { + super(); + } + + // Boolean utilities + //-------------------------------------------------------------------------- + /** + *Negates the specified boolean.
+ * + *If {@code null} is passed in, {@code null} will be returned.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.negate(Boolean.TRUE) = Boolean.FALSE; + * BooleanUtils.negate(Boolean.FALSE) = Boolean.TRUE; + * BooleanUtils.negate(null) = null; + *+ * + * @param bool the Boolean to negate, may be null + * @return the negated Boolean, or {@code null} if {@code null} input + */ +// Boolean utilities +// -------------------------------------------------------------------------- +/** + *
Negates the specified boolean.
+ * + *If {@code null} is passed in, {@code null} will be returned.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.negate(Boolean.TRUE) = Boolean.FALSE; + * BooleanUtils.negate(Boolean.FALSE) = Boolean.TRUE; + * BooleanUtils.negate(null) = null; + *+ * + * @param bool + * the Boolean to negate, may be null + * @return the negated Boolean, or {@code null} if {@code null} input + */ +public static java.lang.Boolean negate(final java.lang.Boolean bool) { + { + return /* NPEX_NULL_EXP */ + bool.booleanValue() ? java.lang.Boolean.FALSE : java.lang.Boolean.TRUE; + } +} + + // boolean Boolean methods + //----------------------------------------------------------------------- + /** + *
Checks if a {@code Boolean} value is {@code true}, + * handling {@code null} by returning {@code false}.
+ * + *+ * BooleanUtils.isTrue(Boolean.TRUE) = true + * BooleanUtils.isTrue(Boolean.FALSE) = false + * BooleanUtils.isTrue(null) = false + *+ * + * @param bool the boolean to check, null returns {@code false} + * @return {@code true} only if the input is non-null and true + * @since 2.1 + */ + public static boolean isTrue(final Boolean bool) { + return Boolean.TRUE.equals(bool); + } + + /** + *
Checks if a {@code Boolean} value is not {@code true}, + * handling {@code null} by returning {@code true}.
+ * + *+ * BooleanUtils.isNotTrue(Boolean.TRUE) = false + * BooleanUtils.isNotTrue(Boolean.FALSE) = true + * BooleanUtils.isNotTrue(null) = true + *+ * + * @param bool the boolean to check, null returns {@code true} + * @return {@code true} if the input is null or false + * @since 2.3 + */ + public static boolean isNotTrue(final Boolean bool) { + return !isTrue(bool); + } + + /** + *
Checks if a {@code Boolean} value is {@code false}, + * handling {@code null} by returning {@code false}.
+ * + *+ * BooleanUtils.isFalse(Boolean.TRUE) = false + * BooleanUtils.isFalse(Boolean.FALSE) = true + * BooleanUtils.isFalse(null) = false + *+ * + * @param bool the boolean to check, null returns {@code false} + * @return {@code true} only if the input is non-null and false + * @since 2.1 + */ + public static boolean isFalse(final Boolean bool) { + return Boolean.FALSE.equals(bool); + } + + /** + *
Checks if a {@code Boolean} value is not {@code false}, + * handling {@code null} by returning {@code true}.
+ * + *+ * BooleanUtils.isNotFalse(Boolean.TRUE) = true + * BooleanUtils.isNotFalse(Boolean.FALSE) = false + * BooleanUtils.isNotFalse(null) = true + *+ * + * @param bool the boolean to check, null returns {@code true} + * @return {@code true} if the input is null or true + * @since 2.3 + */ + public static boolean isNotFalse(final Boolean bool) { + return !isFalse(bool); + } + + //----------------------------------------------------------------------- + /** + *
Converts a Boolean to a boolean handling {@code null} + * by returning {@code false}.
+ * + *+ * BooleanUtils.toBoolean(Boolean.TRUE) = true + * BooleanUtils.toBoolean(Boolean.FALSE) = false + * BooleanUtils.toBoolean(null) = false + *+ * + * @param bool the boolean to convert + * @return {@code true} or {@code false}, {@code null} returns {@code false} + */ + public static boolean toBoolean(final Boolean bool) { + return bool != null && bool.booleanValue(); + } + + /** + *
Converts a Boolean to a boolean handling {@code null}.
+ * + *+ * BooleanUtils.toBooleanDefaultIfNull(Boolean.TRUE, false) = true + * BooleanUtils.toBooleanDefaultIfNull(Boolean.FALSE, true) = false + * BooleanUtils.toBooleanDefaultIfNull(null, true) = true + *+ * + * @param bool the boolean to convert + * @param valueIfNull the boolean value to return if {@code null} + * @return {@code true} or {@code false} + */ + public static boolean toBooleanDefaultIfNull(final Boolean bool, final boolean valueIfNull) { + if (bool == null) { + return valueIfNull; + } + return bool.booleanValue(); + } + + // Integer to Boolean methods + //----------------------------------------------------------------------- + /** + *
Converts an int to a boolean using the convention that {@code zero} + * is {@code false}.
+ * + *+ * BooleanUtils.toBoolean(0) = false + * BooleanUtils.toBoolean(1) = true + * BooleanUtils.toBoolean(2) = true + *+ * + * @param value the int to convert + * @return {@code true} if non-zero, {@code false} + * if zero + */ + public static boolean toBoolean(final int value) { + return value != 0; + } + + /** + *
Converts an int to a Boolean using the convention that {@code zero} + * is {@code false}.
+ * + *+ * BooleanUtils.toBoolean(0) = Boolean.FALSE + * BooleanUtils.toBoolean(1) = Boolean.TRUE + * BooleanUtils.toBoolean(2) = Boolean.TRUE + *+ * + * @param value the int to convert + * @return Boolean.TRUE if non-zero, Boolean.FALSE if zero, + * {@code null} if {@code null} + */ + public static Boolean toBooleanObject(final int value) { + return value == 0 ? Boolean.FALSE : Boolean.TRUE; + } + + /** + *
Converts an Integer to a Boolean using the convention that {@code zero} + * is {@code false}.
+ * + *{@code null} will be converted to {@code null}.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.toBoolean(Integer.valueOf(0)) = Boolean.FALSE + * BooleanUtils.toBoolean(Integer.valueOf(1)) = Boolean.TRUE + * BooleanUtils.toBoolean(Integer.valueOf(null)) = null + *+ * + * @param value the Integer to convert + * @return Boolean.TRUE if non-zero, Boolean.FALSE if zero, + * {@code null} if {@code null} input + */ + public static Boolean toBooleanObject(final Integer value) { + if (value == null) { + return null; + } + return value.intValue() == 0 ? Boolean.FALSE : Boolean.TRUE; + } + + /** + *
Converts an int to a boolean specifying the conversion values.
+ * + *+ * BooleanUtils.toBoolean(0, 1, 0) = false + * BooleanUtils.toBoolean(1, 1, 0) = true + * BooleanUtils.toBoolean(2, 1, 2) = false + * BooleanUtils.toBoolean(2, 2, 0) = true + *+ * + * @param value the Integer to convert + * @param trueValue the value to match for {@code true} + * @param falseValue the value to match for {@code false} + * @return {@code true} or {@code false} + * @throws IllegalArgumentException if no match + */ + public static boolean toBoolean(final int value, final int trueValue, final int falseValue) { + if (value == trueValue) { + return true; + } + if (value == falseValue) { + return false; + } + // no match + throw new IllegalArgumentException("The Integer did not match either specified value"); + } + + /** + *
Converts an Integer to a boolean specifying the conversion values.
+ * + *+ * BooleanUtils.toBoolean(Integer.valueOf(0), Integer.valueOf(1), Integer.valueOf(0)) = false + * BooleanUtils.toBoolean(Integer.valueOf(1), Integer.valueOf(1), Integer.valueOf(0)) = true + * BooleanUtils.toBoolean(Integer.valueOf(2), Integer.valueOf(1), Integer.valueOf(2)) = false + * BooleanUtils.toBoolean(Integer.valueOf(2), Integer.valueOf(2), Integer.valueOf(0)) = true + * BooleanUtils.toBoolean(null, null, Integer.valueOf(0)) = true + *+ * + * @param value the Integer to convert + * @param trueValue the value to match for {@code true}, may be {@code null} + * @param falseValue the value to match for {@code false}, may be {@code null} + * @return {@code true} or {@code false} + * @throws IllegalArgumentException if no match + */ + public static boolean toBoolean(final Integer value, final Integer trueValue, final Integer falseValue) { + if (value == null) { + if (trueValue == null) { + return true; + } + if (falseValue == null) { + return false; + } + } else if (value.equals(trueValue)) { + return true; + } else if (value.equals(falseValue)) { + return false; + } + // no match + throw new IllegalArgumentException("The Integer did not match either specified value"); + } + + /** + *
Converts an int to a Boolean specifying the conversion values.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.toBooleanObject(0, 0, 2, 3) = Boolean.TRUE + * BooleanUtils.toBooleanObject(2, 1, 2, 3) = Boolean.FALSE + * BooleanUtils.toBooleanObject(3, 1, 2, 3) = null + *+ * + * @param value the Integer to convert + * @param trueValue the value to match for {@code true} + * @param falseValue the value to match for {@code false} + * @param nullValue the value to to match for {@code null} + * @return Boolean.TRUE, Boolean.FALSE, or {@code null} + * @throws IllegalArgumentException if no match + */ + public static Boolean toBooleanObject(final int value, final int trueValue, final int falseValue, final int nullValue) { + if (value == trueValue) { + return Boolean.TRUE; + } + if (value == falseValue) { + return Boolean.FALSE; + } + if (value == nullValue) { + return null; + } + // no match + throw new IllegalArgumentException("The Integer did not match any specified value"); + } + + /** + *
Converts an Integer to a Boolean specifying the conversion values.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.toBooleanObject(Integer.valueOf(0), Integer.valueOf(0), Integer.valueOf(2), Integer.valueOf(3)) = Boolean.TRUE + * BooleanUtils.toBooleanObject(Integer.valueOf(2), Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(3)) = Boolean.FALSE + * BooleanUtils.toBooleanObject(Integer.valueOf(3), Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(3)) = null + *+ * + * @param value the Integer to convert + * @param trueValue the value to match for {@code true}, may be {@code null} + * @param falseValue the value to match for {@code false}, may be {@code null} + * @param nullValue the value to to match for {@code null}, may be {@code null} + * @return Boolean.TRUE, Boolean.FALSE, or {@code null} + * @throws IllegalArgumentException if no match + */ + public static Boolean toBooleanObject(final Integer value, final Integer trueValue, final Integer falseValue, final Integer nullValue) { + if (value == null) { + if (trueValue == null) { + return Boolean.TRUE; + } + if (falseValue == null) { + return Boolean.FALSE; + } + if (nullValue == null) { + return null; + } + } else if (value.equals(trueValue)) { + return Boolean.TRUE; + } else if (value.equals(falseValue)) { + return Boolean.FALSE; + } else if (value.equals(nullValue)) { + return null; + } + // no match + throw new IllegalArgumentException("The Integer did not match any specified value"); + } + + // Boolean to Integer methods + //----------------------------------------------------------------------- + /** + *
Converts a boolean to an int using the convention that + * {@code zero} is {@code false}.
+ * + *+ * BooleanUtils.toInteger(true) = 1 + * BooleanUtils.toInteger(false) = 0 + *+ * + * @param bool the boolean to convert + * @return one if {@code true}, zero if {@code false} + */ + public static int toInteger(final boolean bool) { + return bool ? 1 : 0; + } + + /** + *
Converts a boolean to an Integer using the convention that + * {@code zero} is {@code false}.
+ * + *+ * BooleanUtils.toIntegerObject(true) = Integer.valueOf(1) + * BooleanUtils.toIntegerObject(false) = Integer.valueOf(0) + *+ * + * @param bool the boolean to convert + * @return one if {@code true}, zero if {@code false} + */ + public static Integer toIntegerObject(final boolean bool) { + return bool ? NumberUtils.INTEGER_ONE : NumberUtils.INTEGER_ZERO; + } + + /** + *
Converts a Boolean to a Integer using the convention that + * {@code zero} is {@code false}.
+ * + *{@code null} will be converted to {@code null}.
+ * + *+ * BooleanUtils.toIntegerObject(Boolean.TRUE) = Integer.valueOf(1) + * BooleanUtils.toIntegerObject(Boolean.FALSE) = Integer.valueOf(0) + *+ * + * @param bool the Boolean to convert + * @return one if Boolean.TRUE, zero if Boolean.FALSE, {@code null} if {@code null} + */ + public static Integer toIntegerObject(final Boolean bool) { + if (bool == null) { + return null; + } + return bool.booleanValue() ? NumberUtils.INTEGER_ONE : NumberUtils.INTEGER_ZERO; + } + + /** + *
Converts a boolean to an int specifying the conversion values.
+ * + *+ * BooleanUtils.toInteger(true, 1, 0) = 1 + * BooleanUtils.toInteger(false, 1, 0) = 0 + *+ * + * @param bool the to convert + * @param trueValue the value to return if {@code true} + * @param falseValue the value to return if {@code false} + * @return the appropriate value + */ + public static int toInteger(final boolean bool, final int trueValue, final int falseValue) { + return bool ? trueValue : falseValue; + } + + /** + *
Converts a Boolean to an int specifying the conversion values.
+ * + *+ * BooleanUtils.toInteger(Boolean.TRUE, 1, 0, 2) = 1 + * BooleanUtils.toInteger(Boolean.FALSE, 1, 0, 2) = 0 + * BooleanUtils.toInteger(null, 1, 0, 2) = 2 + *+ * + * @param bool the Boolean to convert + * @param trueValue the value to return if {@code true} + * @param falseValue the value to return if {@code false} + * @param nullValue the value to return if {@code null} + * @return the appropriate value + */ + public static int toInteger(final Boolean bool, final int trueValue, final int falseValue, final int nullValue) { + if (bool == null) { + return nullValue; + } + return bool.booleanValue() ? trueValue : falseValue; + } + + /** + *
Converts a boolean to an Integer specifying the conversion values.
+ * + *+ * BooleanUtils.toIntegerObject(true, Integer.valueOf(1), Integer.valueOf(0)) = Integer.valueOf(1) + * BooleanUtils.toIntegerObject(false, Integer.valueOf(1), Integer.valueOf(0)) = Integer.valueOf(0) + *+ * + * @param bool the to convert + * @param trueValue the value to return if {@code true}, may be {@code null} + * @param falseValue the value to return if {@code false}, may be {@code null} + * @return the appropriate value + */ + public static Integer toIntegerObject(final boolean bool, final Integer trueValue, final Integer falseValue) { + return bool ? trueValue : falseValue; + } + + /** + *
Converts a Boolean to an Integer specifying the conversion values.
+ * + *+ * BooleanUtils.toIntegerObject(Boolean.TRUE, Integer.valueOf(1), Integer.valueOf(0), Integer.valueOf(2)) = Integer.valueOf(1) + * BooleanUtils.toIntegerObject(Boolean.FALSE, Integer.valueOf(1), Integer.valueOf(0), Integer.valueOf(2)) = Integer.valueOf(0) + * BooleanUtils.toIntegerObject(null, Integer.valueOf(1), Integer.valueOf(0), Integer.valueOf(2)) = Integer.valueOf(2) + *+ * + * @param bool the Boolean to convert + * @param trueValue the value to return if {@code true}, may be {@code null} + * @param falseValue the value to return if {@code false}, may be {@code null} + * @param nullValue the value to return if {@code null}, may be {@code null} + * @return the appropriate value + */ + public static Integer toIntegerObject(final Boolean bool, final Integer trueValue, final Integer falseValue, final Integer nullValue) { + if (bool == null) { + return nullValue; + } + return bool.booleanValue() ? trueValue : falseValue; + } + + // String to Boolean methods + //----------------------------------------------------------------------- + /** + *
Converts a String to a Boolean.
+ * + *{@code 'true'}, {@code 'on'}, {@code 'y'}, {@code 't'} or {@code 'yes'} + * (case insensitive) will return {@code true}. + * {@code 'false'}, {@code 'off'}, {@code 'n'}, {@code 'f'} or {@code 'no'} + * (case insensitive) will return {@code false}. + * Otherwise, {@code null} is returned.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * // N.B. case is not significant + * BooleanUtils.toBooleanObject(null) = null + * BooleanUtils.toBooleanObject("true") = Boolean.TRUE + * BooleanUtils.toBooleanObject("T") = Boolean.TRUE // i.e. T[RUE] + * BooleanUtils.toBooleanObject("false") = Boolean.FALSE + * BooleanUtils.toBooleanObject("f") = Boolean.FALSE // i.e. f[alse] + * BooleanUtils.toBooleanObject("No") = Boolean.FALSE + * BooleanUtils.toBooleanObject("n") = Boolean.FALSE // i.e. n[o] + * BooleanUtils.toBooleanObject("on") = Boolean.TRUE + * BooleanUtils.toBooleanObject("ON") = Boolean.TRUE + * BooleanUtils.toBooleanObject("off") = Boolean.FALSE + * BooleanUtils.toBooleanObject("oFf") = Boolean.FALSE + * BooleanUtils.toBooleanObject("yes") = Boolean.TRUE + * BooleanUtils.toBooleanObject("Y") = Boolean.TRUE // i.e. Y[ES] + * BooleanUtils.toBooleanObject("blue") = null + * BooleanUtils.toBooleanObject("true ") = null // trailing space (too long) + * BooleanUtils.toBooleanObject("ono") = null // does not match on or no + *+ * + * @param str the String to check; upper and lower case are treated as the same + * @return the Boolean value of the string, {@code null} if no match or {@code null} input + */ + public static Boolean toBooleanObject(final String str) { + // Previously used equalsIgnoreCase, which was fast for interned 'true'. + // Non interned 'true' matched 15 times slower. + // + // Optimisation provides same performance as before for interned 'true'. + // Similar performance for null, 'false', and other strings not length 2/3/4. + // 'true'/'TRUE' match 4 times slower, 'tRUE'/'True' 7 times slower. + if (str == "true") { + return Boolean.TRUE; + } + if (str == null) { + return null; + } + switch (str.length()) { + case 1: { + final char ch0 = str.charAt(0); + if (ch0 == 'y' || ch0 == 'Y' || + ch0 == 't' || ch0 == 'T') { + return Boolean.TRUE; + } + if (ch0 == 'n' || ch0 == 'N' || + ch0 == 'f' || ch0 == 'F') { + return Boolean.FALSE; + } + break; + } + case 2: { + final char ch0 = str.charAt(0); + final char ch1 = str.charAt(1); + if ((ch0 == 'o' || ch0 == 'O') && + (ch1 == 'n' || ch1 == 'N') ) { + return Boolean.TRUE; + } + if ((ch0 == 'n' || ch0 == 'N') && + (ch1 == 'o' || ch1 == 'O') ) { + return Boolean.FALSE; + } + break; + } + case 3: { + final char ch0 = str.charAt(0); + final char ch1 = str.charAt(1); + final char ch2 = str.charAt(2); + if ((ch0 == 'y' || ch0 == 'Y') && + (ch1 == 'e' || ch1 == 'E') && + (ch2 == 's' || ch2 == 'S') ) { + return Boolean.TRUE; + } + if ((ch0 == 'o' || ch0 == 'O') && + (ch1 == 'f' || ch1 == 'F') && + (ch2 == 'f' || ch2 == 'F') ) { + return Boolean.FALSE; + } + break; + } + case 4: { + final char ch0 = str.charAt(0); + final char ch1 = str.charAt(1); + final char ch2 = str.charAt(2); + final char ch3 = str.charAt(3); + if ((ch0 == 't' || ch0 == 'T') && + (ch1 == 'r' || ch1 == 'R') && + (ch2 == 'u' || ch2 == 'U') && + (ch3 == 'e' || ch3 == 'E') ) { + return Boolean.TRUE; + } + break; + } + case 5: { + final char ch0 = str.charAt(0); + final char ch1 = str.charAt(1); + final char ch2 = str.charAt(2); + final char ch3 = str.charAt(3); + final char ch4 = str.charAt(4); + if ((ch0 == 'f' || ch0 == 'F') && + (ch1 == 'a' || ch1 == 'A') && + (ch2 == 'l' || ch2 == 'L') && + (ch3 == 's' || ch3 == 'S') && + (ch4 == 'e' || ch4 == 'E') ) { + return Boolean.FALSE; + } + break; + } + default: + break; + } + + return null; + } + + /** + *
Converts a String to a Boolean throwing an exception if no match.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.toBooleanObject("true", "true", "false", "null") = Boolean.TRUE + * BooleanUtils.toBooleanObject("false", "true", "false", "null") = Boolean.FALSE + * BooleanUtils.toBooleanObject("null", "true", "false", "null") = null + *+ * + * @param str the String to check + * @param trueString the String to match for {@code true} (case sensitive), may be {@code null} + * @param falseString the String to match for {@code false} (case sensitive), may be {@code null} + * @param nullString the String to match for {@code null} (case sensitive), may be {@code null} + * @return the Boolean value of the string, {@code null} if either the String matches {@code nullString} + * or if {@code null} input and {@code nullString} is {@code null} + * @throws IllegalArgumentException if the String doesn't match + */ + public static Boolean toBooleanObject(final String str, final String trueString, final String falseString, final String nullString) { + if (str == null) { + if (trueString == null) { + return Boolean.TRUE; + } + if (falseString == null) { + return Boolean.FALSE; + } + if (nullString == null) { + return null; + } + } else if (str.equals(trueString)) { + return Boolean.TRUE; + } else if (str.equals(falseString)) { + return Boolean.FALSE; + } else if (str.equals(nullString)) { + return null; + } + // no match + throw new IllegalArgumentException("The String did not match any specified value"); + } + + // String to boolean methods + //----------------------------------------------------------------------- + /** + *
Converts a String to a boolean (optimised for performance).
+ * + *{@code 'true'}, {@code 'on'}, {@code 'y'}, {@code 't'} or {@code 'yes'} + * (case insensitive) will return {@code true}. Otherwise, + * {@code false} is returned.
+ * + *This method performs 4 times faster (JDK1.4) than + * {@code Boolean.valueOf(String)}. However, this method accepts + * 'on' and 'yes', 't', 'y' as true values. + * + *
+ * BooleanUtils.toBoolean(null) = false + * BooleanUtils.toBoolean("true") = true + * BooleanUtils.toBoolean("TRUE") = true + * BooleanUtils.toBoolean("tRUe") = true + * BooleanUtils.toBoolean("on") = true + * BooleanUtils.toBoolean("yes") = true + * BooleanUtils.toBoolean("false") = false + * BooleanUtils.toBoolean("x gti") = false + * BooleanUtils.toBooleanObject("y") = true + * BooleanUtils.toBooleanObject("n") = false + * BooleanUtils.toBooleanObject("t") = true + * BooleanUtils.toBooleanObject("f") = false + *+ * + * @param str the String to check + * @return the boolean value of the string, {@code false} if no match or the String is null + */ + public static boolean toBoolean(final String str) { + return toBooleanObject(str) == Boolean.TRUE; + } + + /** + *
Converts a String to a Boolean throwing an exception if no match found.
+ * + *+ * BooleanUtils.toBoolean("true", "true", "false") = true + * BooleanUtils.toBoolean("false", "true", "false") = false + *+ * + * @param str the String to check + * @param trueString the String to match for {@code true} (case sensitive), may be {@code null} + * @param falseString the String to match for {@code false} (case sensitive), may be {@code null} + * @return the boolean value of the string + * @throws IllegalArgumentException if the String doesn't match + */ + public static boolean toBoolean(final String str, final String trueString, final String falseString) { + if (str == trueString) { + return true; + } else if (str == falseString) { + return false; + } else if (str != null) { + if (str.equals(trueString)) { + return true; + } else if (str.equals(falseString)) { + return false; + } + } + // no match + throw new IllegalArgumentException("The String did not match either specified value"); + } + + // Boolean to String methods + //----------------------------------------------------------------------- + /** + *
Converts a Boolean to a String returning {@code 'true'}, + * {@code 'false'}, or {@code null}.
+ * + *+ * BooleanUtils.toStringTrueFalse(Boolean.TRUE) = "true" + * BooleanUtils.toStringTrueFalse(Boolean.FALSE) = "false" + * BooleanUtils.toStringTrueFalse(null) = null; + *+ * + * @param bool the Boolean to check + * @return {@code 'true'}, {@code 'false'}, or {@code null} + */ + public static String toStringTrueFalse(final Boolean bool) { + return toString(bool, "true", "false", null); + } + + /** + *
Converts a Boolean to a String returning {@code 'on'}, + * {@code 'off'}, or {@code null}.
+ * + *+ * BooleanUtils.toStringOnOff(Boolean.TRUE) = "on" + * BooleanUtils.toStringOnOff(Boolean.FALSE) = "off" + * BooleanUtils.toStringOnOff(null) = null; + *+ * + * @param bool the Boolean to check + * @return {@code 'on'}, {@code 'off'}, or {@code null} + */ + public static String toStringOnOff(final Boolean bool) { + return toString(bool, "on", "off", null); + } + + /** + *
Converts a Boolean to a String returning {@code 'yes'}, + * {@code 'no'}, or {@code null}.
+ * + *+ * BooleanUtils.toStringYesNo(Boolean.TRUE) = "yes" + * BooleanUtils.toStringYesNo(Boolean.FALSE) = "no" + * BooleanUtils.toStringYesNo(null) = null; + *+ * + * @param bool the Boolean to check + * @return {@code 'yes'}, {@code 'no'}, or {@code null} + */ + public static String toStringYesNo(final Boolean bool) { + return toString(bool, "yes", "no", null); + } + + /** + *
Converts a Boolean to a String returning one of the input Strings.
+ * + *+ * BooleanUtils.toString(Boolean.TRUE, "true", "false", null) = "true" + * BooleanUtils.toString(Boolean.FALSE, "true", "false", null) = "false" + * BooleanUtils.toString(null, "true", "false", null) = null; + *+ * + * @param bool the Boolean to check + * @param trueString the String to return if {@code true}, may be {@code null} + * @param falseString the String to return if {@code false}, may be {@code null} + * @param nullString the String to return if {@code null}, may be {@code null} + * @return one of the three input Strings + */ + public static String toString(final Boolean bool, final String trueString, final String falseString, final String nullString) { + if (bool == null) { + return nullString; + } + return bool.booleanValue() ? trueString : falseString; + } + + // boolean to String methods + //----------------------------------------------------------------------- + /** + *
Converts a boolean to a String returning {@code 'true'} + * or {@code 'false'}.
+ * + *+ * BooleanUtils.toStringTrueFalse(true) = "true" + * BooleanUtils.toStringTrueFalse(false) = "false" + *+ * + * @param bool the Boolean to check + * @return {@code 'true'}, {@code 'false'}, or {@code null} + */ + public static String toStringTrueFalse(final boolean bool) { + return toString(bool, "true", "false"); + } + + /** + *
Converts a boolean to a String returning {@code 'on'} + * or {@code 'off'}.
+ * + *+ * BooleanUtils.toStringOnOff(true) = "on" + * BooleanUtils.toStringOnOff(false) = "off" + *+ * + * @param bool the Boolean to check + * @return {@code 'on'}, {@code 'off'}, or {@code null} + */ + public static String toStringOnOff(final boolean bool) { + return toString(bool, "on", "off"); + } + + /** + *
Converts a boolean to a String returning {@code 'yes'} + * or {@code 'no'}.
+ * + *+ * BooleanUtils.toStringYesNo(true) = "yes" + * BooleanUtils.toStringYesNo(false) = "no" + *+ * + * @param bool the Boolean to check + * @return {@code 'yes'}, {@code 'no'}, or {@code null} + */ + public static String toStringYesNo(final boolean bool) { + return toString(bool, "yes", "no"); + } + + /** + *
Converts a boolean to a String returning one of the input Strings.
+ * + *+ * BooleanUtils.toString(true, "true", "false") = "true" + * BooleanUtils.toString(false, "true", "false") = "false" + *+ * + * @param bool the Boolean to check + * @param trueString the String to return if {@code true}, may be {@code null} + * @param falseString the String to return if {@code false}, may be {@code null} + * @return one of the two input Strings + */ + public static String toString(final boolean bool, final String trueString, final String falseString) { + return bool ? trueString : falseString; + } + + // logical operations + // ---------------------------------------------------------------------- + /** + *
Performs an and on a set of booleans.
+ * + *+ * BooleanUtils.and(true, true) = true + * BooleanUtils.and(false, false) = false + * BooleanUtils.and(true, false) = false + * BooleanUtils.and(true, true, false) = false + * BooleanUtils.and(true, true, true) = true + *+ * + * @param array an array of {@code boolean}s + * @return {@code true} if the and is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + * @since 3.0.1 + */ + public static boolean and(final boolean... array) { + // Validates input + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + for (final boolean element : array) { + if (!element) { + return false; + } + } + return true; + } + + /** + *
Performs an and on an array of Booleans.
+ * + *+ * BooleanUtils.and(Boolean.TRUE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.and(Boolean.FALSE, Boolean.FALSE) = Boolean.FALSE + * BooleanUtils.and(Boolean.TRUE, Boolean.FALSE) = Boolean.FALSE + * BooleanUtils.and(Boolean.TRUE, Boolean.TRUE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.and(Boolean.FALSE, Boolean.FALSE, Boolean.TRUE) = Boolean.FALSE + * BooleanUtils.and(Boolean.TRUE, Boolean.FALSE, Boolean.TRUE) = Boolean.FALSE + *+ * + * @param array an array of {@code Boolean}s + * @return {@code true} if the and is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + * @throws IllegalArgumentException if {@code array} contains a {@code null} + * @since 3.0.1 + */ + public static Boolean and(final Boolean... array) { + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + try { + final boolean[] primitive = ArrayUtils.toPrimitive(array); + return and(primitive) ? Boolean.TRUE : Boolean.FALSE; + } catch (final NullPointerException ex) { + throw new IllegalArgumentException("The array must not contain any null elements"); + } + } + + /** + *
Performs an or on a set of booleans.
+ * + *+ * BooleanUtils.or(true, true) = true + * BooleanUtils.or(false, false) = false + * BooleanUtils.or(true, false) = true + * BooleanUtils.or(true, true, false) = true + * BooleanUtils.or(true, true, true) = true + * BooleanUtils.or(false, false, false) = false + *+ * + * @param array an array of {@code boolean}s + * @return {@code true} if the or is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + * @since 3.0.1 + */ + public static boolean or(final boolean... array) { + // Validates input + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + for (final boolean element : array) { + if (element) { + return true; + } + } + return false; + } + + /** + *
Performs an or on an array of Booleans.
+ * + *+ * BooleanUtils.or(Boolean.TRUE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.or(Boolean.FALSE, Boolean.FALSE) = Boolean.FALSE + * BooleanUtils.or(Boolean.TRUE, Boolean.FALSE) = Boolean.TRUE + * BooleanUtils.or(Boolean.TRUE, Boolean.TRUE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.or(Boolean.FALSE, Boolean.FALSE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.or(Boolean.TRUE, Boolean.FALSE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.or(Boolean.FALSE, Boolean.FALSE, Boolean.FALSE) = Boolean.FALSE + *+ * + * @param array an array of {@code Boolean}s + * @return {@code true} if the or is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + * @throws IllegalArgumentException if {@code array} contains a {@code null} + * @since 3.0.1 + */ + public static Boolean or(final Boolean... array) { + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + try { + final boolean[] primitive = ArrayUtils.toPrimitive(array); + return or(primitive) ? Boolean.TRUE : Boolean.FALSE; + } catch (final NullPointerException ex) { + throw new IllegalArgumentException("The array must not contain any null elements"); + } + } + + /** + *
Performs an xor on a set of booleans.
+ * + *+ * BooleanUtils.xor(true, true) = false + * BooleanUtils.xor(false, false) = false + * BooleanUtils.xor(true, false) = true + * BooleanUtils.xor(true, true) = false + * BooleanUtils.xor(false, false) = false + * BooleanUtils.xor(true, false) = true + *+ * + * @param array an array of {@code boolean}s + * @return {@code true} if the xor is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + */ + public static boolean xor(final boolean... array) { + // Validates input + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + + // false if the neutral element of the xor operator + boolean result = false; + for (final boolean element : array) { + result ^= element; + } + + return result; + } + + /** + *
Performs an xor on an array of Booleans.
+ * + *+ * BooleanUtils.xor(new Boolean[] { Boolean.TRUE, Boolean.TRUE }) = Boolean.FALSE + * BooleanUtils.xor(new Boolean[] { Boolean.FALSE, Boolean.FALSE }) = Boolean.FALSE + * BooleanUtils.xor(new Boolean[] { Boolean.TRUE, Boolean.FALSE }) = Boolean.TRUE + *+ * + * @param array an array of {@code Boolean}s + * @return {@code true} if the xor is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + * @throws IllegalArgumentException if {@code array} contains a {@code null} + */ + public static Boolean xor(final Boolean... array) { + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + try { + final boolean[] primitive = ArrayUtils.toPrimitive(array); + return xor(primitive) ? Boolean.TRUE : Boolean.FALSE; + } catch (final NullPointerException ex) { + throw new IllegalArgumentException("The array must not contain any null elements"); + } + } + + /** + *
Compares two {@code boolean} values. This is the same functionality as provided in Java 7.
+ * + * @param x the first {@code boolean} to compare + * @param y the second {@code boolean} to compare + * @return the value {@code 0} if {@code x == y}; + * a value less than {@code 0} if {@code !x && y}; and + * a value greater than {@code 0} if {@code x && !y} + * @since 3.4 + */ + public static int compare(boolean x, boolean y) { + if (x == y) { + return 0; + } + return x ? 1 : -1; + } + +} diff --git a/Java/commons-lang-BooleanUtils_63/metadata.json b/Java/commons-lang-BooleanUtils_63/metadata.json new file mode 100644 index 000000000..33afdfb14 --- /dev/null +++ b/Java/commons-lang-BooleanUtils_63/metadata.json @@ -0,0 +1,21 @@ +{ + "language": "java", + "id": "commons-lang-BooleanUtils_63", + "buggyPath": ".", + "referencePath": null, + "buildCommand": "mvn package -V -B -Denforcer.skip=true -Dcheckstyle.skip=true -Dcobertura.skip=true -Drat.skip=true -Dlicense.skip=true -Dfindbugs.skip=true -Dgpg.skip=true -Dskip.npm=true -Dskip.gulp=true -Dskip.bower=true -Drat.numUnapprovedLicenses=100 -DskipTests=true -DskipITs=true -Dtest=None -DfailIfNoTests=false", + "testCommand": "mvn test -V -B -Denforcer.skip=true -Dcheckstyle.skip=true -Dcobertura.skip=true -Drat.skip=true -Dlicense.skip=true -Dfindbugs.skip=true -Dgpg.skip=true -Dskip.npm=true -Dskip.gulp=true -Dskip.bower=true -Drat.numUnapprovedLicenses=100", + "categories": [ + "safety", + "npe" + ], + "npe": { + "filepath": "src/main/java/org/apache/commons/lang3/BooleanUtils.java", + "line": 84, + "npe_method": "negate", + "deref_field": "bool", + "npe_class": "BooleanUtils", + "repo": "commons-lang", + "bug_id": "BooleanUtils_63" + } +} diff --git a/Java/commons-lang-BooleanUtils_63/npe.json b/Java/commons-lang-BooleanUtils_63/npe.json new file mode 100644 index 000000000..a97ec9e71 --- /dev/null +++ b/Java/commons-lang-BooleanUtils_63/npe.json @@ -0,0 +1,7 @@ +{ + "filepath": "src/main/java/org/apache/commons/lang3/BooleanUtils.java", + "line": 84, + "npe_method": "negate", + "deref_field": "bool", + "npe_class": "BooleanUtils" +} \ No newline at end of file diff --git a/Java/commons-lang-BooleanUtils_656/Dockerfile b/Java/commons-lang-BooleanUtils_656/Dockerfile new file mode 100644 index 000000000..7b7fbe349 --- /dev/null +++ b/Java/commons-lang-BooleanUtils_656/Dockerfile @@ -0,0 +1,18 @@ +FROM ghcr.io/kupl/starlab-benchmarks/java-base:commons-lang + +ENV TZ=Asia/Seoul + +COPY ./metadata.json . +COPY ./npe.json . +COPY ./buggy.java /tmp/buggy.java +RUN export BUGGY_PATH=$(cat metadata.json | jq -r ".npe.filepath") \ + && export BUGGY_LINE=$(cat metadata.json | jq -r ".npe.line") \ + && export BUGGY_MTHD=$(cat metadata.json | jq -r ".npe.npe_method") \ + && mv /tmp/buggy.java $BUGGY_PATH \ + && echo "[{\"filepath\": \"$BUGGY_PATH\", \"line\": $BUGGY_LINE, \"method_name\": \"$BUGGY_MTHD\"}]" | jq . > traces.json + +RUN git init . && git add -A + +RUN $(cat metadata.json | jq -r ".buildCommand") + +RUN $(cat metadata.json | jq -r ".testCommand"); if [ $? -eq 0 ]; then exit 1; fi diff --git a/Java/commons-lang-BooleanUtils_656/buggy.java b/Java/commons-lang-BooleanUtils_656/buggy.java new file mode 100644 index 000000000..92c6bb8c1 --- /dev/null +++ b/Java/commons-lang-BooleanUtils_656/buggy.java @@ -0,0 +1,1121 @@ +/* + * 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; + +import org.apache.commons.lang3.math.NumberUtils; + +/** + *Operations on boolean primitives and Boolean objects.
+ * + *This class tries to handle {@code null} input gracefully. + * An exception will not be thrown for a {@code null} input. + * Each method documents its behaviour in more detail.
+ * + *#ThreadSafe#
+ * @since 2.0 + */ +public class BooleanUtils { + + /** + *{@code BooleanUtils} instances should NOT be constructed in standard programming. + * Instead, the class should be used as {@code BooleanUtils.negate(true);}.
+ * + *This constructor is public to permit tools that require a JavaBean instance + * to operate.
+ */ + public BooleanUtils() { + super(); + } + + // Boolean utilities + //-------------------------------------------------------------------------- + /** + *Negates the specified boolean.
+ * + *If {@code null} is passed in, {@code null} will be returned.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.negate(Boolean.TRUE) = Boolean.FALSE; + * BooleanUtils.negate(Boolean.FALSE) = Boolean.TRUE; + * BooleanUtils.negate(null) = null; + *+ * + * @param bool the Boolean to negate, may be null + * @return the negated Boolean, or {@code null} if {@code null} input + */ + public static Boolean negate(final Boolean bool) { + if (bool == null) { + return null; + } + return bool.booleanValue() ? Boolean.FALSE : Boolean.TRUE; + } + + // boolean Boolean methods + //----------------------------------------------------------------------- + /** + *
Checks if a {@code Boolean} value is {@code true}, + * handling {@code null} by returning {@code false}.
+ * + *+ * BooleanUtils.isTrue(Boolean.TRUE) = true + * BooleanUtils.isTrue(Boolean.FALSE) = false + * BooleanUtils.isTrue(null) = false + *+ * + * @param bool the boolean to check, null returns {@code false} + * @return {@code true} only if the input is non-null and true + * @since 2.1 + */ + public static boolean isTrue(final Boolean bool) { + return Boolean.TRUE.equals(bool); + } + + /** + *
Checks if a {@code Boolean} value is not {@code true}, + * handling {@code null} by returning {@code true}.
+ * + *+ * BooleanUtils.isNotTrue(Boolean.TRUE) = false + * BooleanUtils.isNotTrue(Boolean.FALSE) = true + * BooleanUtils.isNotTrue(null) = true + *+ * + * @param bool the boolean to check, null returns {@code true} + * @return {@code true} if the input is null or false + * @since 2.3 + */ + public static boolean isNotTrue(final Boolean bool) { + return !isTrue(bool); + } + + /** + *
Checks if a {@code Boolean} value is {@code false}, + * handling {@code null} by returning {@code false}.
+ * + *+ * BooleanUtils.isFalse(Boolean.TRUE) = false + * BooleanUtils.isFalse(Boolean.FALSE) = true + * BooleanUtils.isFalse(null) = false + *+ * + * @param bool the boolean to check, null returns {@code false} + * @return {@code true} only if the input is non-null and false + * @since 2.1 + */ + public static boolean isFalse(final Boolean bool) { + return Boolean.FALSE.equals(bool); + } + + /** + *
Checks if a {@code Boolean} value is not {@code false}, + * handling {@code null} by returning {@code true}.
+ * + *+ * BooleanUtils.isNotFalse(Boolean.TRUE) = true + * BooleanUtils.isNotFalse(Boolean.FALSE) = false + * BooleanUtils.isNotFalse(null) = true + *+ * + * @param bool the boolean to check, null returns {@code true} + * @return {@code true} if the input is null or true + * @since 2.3 + */ + public static boolean isNotFalse(final Boolean bool) { + return !isFalse(bool); + } + + //----------------------------------------------------------------------- + /** + *
Converts a Boolean to a boolean handling {@code null} + * by returning {@code false}.
+ * + *+ * BooleanUtils.toBoolean(Boolean.TRUE) = true + * BooleanUtils.toBoolean(Boolean.FALSE) = false + * BooleanUtils.toBoolean(null) = false + *+ * + * @param bool the boolean to convert + * @return {@code true} or {@code false}, {@code null} returns {@code false} + */ + public static boolean toBoolean(final Boolean bool) { + return bool != null && bool.booleanValue(); + } + + /** + *
Converts a Boolean to a boolean handling {@code null}.
+ * + *+ * BooleanUtils.toBooleanDefaultIfNull(Boolean.TRUE, false) = true + * BooleanUtils.toBooleanDefaultIfNull(Boolean.FALSE, true) = false + * BooleanUtils.toBooleanDefaultIfNull(null, true) = true + *+ * + * @param bool the boolean to convert + * @param valueIfNull the boolean value to return if {@code null} + * @return {@code true} or {@code false} + */ + public static boolean toBooleanDefaultIfNull(final Boolean bool, final boolean valueIfNull) { + if (bool == null) { + return valueIfNull; + } + return bool.booleanValue(); + } + + // Integer to Boolean methods + //----------------------------------------------------------------------- + /** + *
Converts an int to a boolean using the convention that {@code zero} + * is {@code false}.
+ * + *+ * BooleanUtils.toBoolean(0) = false + * BooleanUtils.toBoolean(1) = true + * BooleanUtils.toBoolean(2) = true + *+ * + * @param value the int to convert + * @return {@code true} if non-zero, {@code false} + * if zero + */ + public static boolean toBoolean(final int value) { + return value != 0; + } + + /** + *
Converts an int to a Boolean using the convention that {@code zero} + * is {@code false}.
+ * + *+ * BooleanUtils.toBoolean(0) = Boolean.FALSE + * BooleanUtils.toBoolean(1) = Boolean.TRUE + * BooleanUtils.toBoolean(2) = Boolean.TRUE + *+ * + * @param value the int to convert + * @return Boolean.TRUE if non-zero, Boolean.FALSE if zero, + * {@code null} if {@code null} + */ + public static Boolean toBooleanObject(final int value) { + return value == 0 ? Boolean.FALSE : Boolean.TRUE; + } + + /** + *
Converts an Integer to a Boolean using the convention that {@code zero} + * is {@code false}.
+ * + *{@code null} will be converted to {@code null}.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.toBoolean(Integer.valueOf(0)) = Boolean.FALSE + * BooleanUtils.toBoolean(Integer.valueOf(1)) = Boolean.TRUE + * BooleanUtils.toBoolean(Integer.valueOf(null)) = null + *+ * + * @param value the Integer to convert + * @return Boolean.TRUE if non-zero, Boolean.FALSE if zero, + * {@code null} if {@code null} input + */ + public static Boolean toBooleanObject(final Integer value) { + if (value == null) { + return null; + } + return value.intValue() == 0 ? Boolean.FALSE : Boolean.TRUE; + } + + /** + *
Converts an int to a boolean specifying the conversion values.
+ * + *+ * BooleanUtils.toBoolean(0, 1, 0) = false + * BooleanUtils.toBoolean(1, 1, 0) = true + * BooleanUtils.toBoolean(2, 1, 2) = false + * BooleanUtils.toBoolean(2, 2, 0) = true + *+ * + * @param value the Integer to convert + * @param trueValue the value to match for {@code true} + * @param falseValue the value to match for {@code false} + * @return {@code true} or {@code false} + * @throws IllegalArgumentException if no match + */ + public static boolean toBoolean(final int value, final int trueValue, final int falseValue) { + if (value == trueValue) { + return true; + } + if (value == falseValue) { + return false; + } + // no match + throw new IllegalArgumentException("The Integer did not match either specified value"); + } + + /** + *
Converts an Integer to a boolean specifying the conversion values.
+ * + *+ * BooleanUtils.toBoolean(Integer.valueOf(0), Integer.valueOf(1), Integer.valueOf(0)) = false + * BooleanUtils.toBoolean(Integer.valueOf(1), Integer.valueOf(1), Integer.valueOf(0)) = true + * BooleanUtils.toBoolean(Integer.valueOf(2), Integer.valueOf(1), Integer.valueOf(2)) = false + * BooleanUtils.toBoolean(Integer.valueOf(2), Integer.valueOf(2), Integer.valueOf(0)) = true + * BooleanUtils.toBoolean(null, null, Integer.valueOf(0)) = true + *+ * + * @param value the Integer to convert + * @param trueValue the value to match for {@code true}, may be {@code null} + * @param falseValue the value to match for {@code false}, may be {@code null} + * @return {@code true} or {@code false} + * @throws IllegalArgumentException if no match + */ + public static boolean toBoolean(final Integer value, final Integer trueValue, final Integer falseValue) { + if (value == null) { + if (trueValue == null) { + return true; + } + if (falseValue == null) { + return false; + } + } else if (value.equals(trueValue)) { + return true; + } else if (value.equals(falseValue)) { + return false; + } + // no match + throw new IllegalArgumentException("The Integer did not match either specified value"); + } + + /** + *
Converts an int to a Boolean specifying the conversion values.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.toBooleanObject(0, 0, 2, 3) = Boolean.TRUE + * BooleanUtils.toBooleanObject(2, 1, 2, 3) = Boolean.FALSE + * BooleanUtils.toBooleanObject(3, 1, 2, 3) = null + *+ * + * @param value the Integer to convert + * @param trueValue the value to match for {@code true} + * @param falseValue the value to match for {@code false} + * @param nullValue the value to to match for {@code null} + * @return Boolean.TRUE, Boolean.FALSE, or {@code null} + * @throws IllegalArgumentException if no match + */ + public static Boolean toBooleanObject(final int value, final int trueValue, final int falseValue, final int nullValue) { + if (value == trueValue) { + return Boolean.TRUE; + } + if (value == falseValue) { + return Boolean.FALSE; + } + if (value == nullValue) { + return null; + } + // no match + throw new IllegalArgumentException("The Integer did not match any specified value"); + } + + /** + *
Converts an Integer to a Boolean specifying the conversion values.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.toBooleanObject(Integer.valueOf(0), Integer.valueOf(0), Integer.valueOf(2), Integer.valueOf(3)) = Boolean.TRUE + * BooleanUtils.toBooleanObject(Integer.valueOf(2), Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(3)) = Boolean.FALSE + * BooleanUtils.toBooleanObject(Integer.valueOf(3), Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(3)) = null + *+ * + * @param value the Integer to convert + * @param trueValue the value to match for {@code true}, may be {@code null} + * @param falseValue the value to match for {@code false}, may be {@code null} + * @param nullValue the value to to match for {@code null}, may be {@code null} + * @return Boolean.TRUE, Boolean.FALSE, or {@code null} + * @throws IllegalArgumentException if no match + */ + public static Boolean toBooleanObject(final Integer value, final Integer trueValue, final Integer falseValue, final Integer nullValue) { + if (value == null) { + if (trueValue == null) { + return Boolean.TRUE; + } + if (falseValue == null) { + return Boolean.FALSE; + } + if (nullValue == null) { + return null; + } + } else if (value.equals(trueValue)) { + return Boolean.TRUE; + } else if (value.equals(falseValue)) { + return Boolean.FALSE; + } else if (value.equals(nullValue)) { + return null; + } + // no match + throw new IllegalArgumentException("The Integer did not match any specified value"); + } + + // Boolean to Integer methods + //----------------------------------------------------------------------- + /** + *
Converts a boolean to an int using the convention that + * {@code zero} is {@code false}.
+ * + *+ * BooleanUtils.toInteger(true) = 1 + * BooleanUtils.toInteger(false) = 0 + *+ * + * @param bool the boolean to convert + * @return one if {@code true}, zero if {@code false} + */ + public static int toInteger(final boolean bool) { + return bool ? 1 : 0; + } + + /** + *
Converts a boolean to an Integer using the convention that + * {@code zero} is {@code false}.
+ * + *+ * BooleanUtils.toIntegerObject(true) = Integer.valueOf(1) + * BooleanUtils.toIntegerObject(false) = Integer.valueOf(0) + *+ * + * @param bool the boolean to convert + * @return one if {@code true}, zero if {@code false} + */ + public static Integer toIntegerObject(final boolean bool) { + return bool ? NumberUtils.INTEGER_ONE : NumberUtils.INTEGER_ZERO; + } + + /** + *
Converts a Boolean to a Integer using the convention that + * {@code zero} is {@code false}.
+ * + *{@code null} will be converted to {@code null}.
+ * + *+ * BooleanUtils.toIntegerObject(Boolean.TRUE) = Integer.valueOf(1) + * BooleanUtils.toIntegerObject(Boolean.FALSE) = Integer.valueOf(0) + *+ * + * @param bool the Boolean to convert + * @return one if Boolean.TRUE, zero if Boolean.FALSE, {@code null} if {@code null} + */ + public static Integer toIntegerObject(final Boolean bool) { + if (bool == null) { + return null; + } + return bool.booleanValue() ? NumberUtils.INTEGER_ONE : NumberUtils.INTEGER_ZERO; + } + + /** + *
Converts a boolean to an int specifying the conversion values.
+ * + *+ * BooleanUtils.toInteger(true, 1, 0) = 1 + * BooleanUtils.toInteger(false, 1, 0) = 0 + *+ * + * @param bool the to convert + * @param trueValue the value to return if {@code true} + * @param falseValue the value to return if {@code false} + * @return the appropriate value + */ + public static int toInteger(final boolean bool, final int trueValue, final int falseValue) { + return bool ? trueValue : falseValue; + } + + /** + *
Converts a Boolean to an int specifying the conversion values.
+ * + *+ * BooleanUtils.toInteger(Boolean.TRUE, 1, 0, 2) = 1 + * BooleanUtils.toInteger(Boolean.FALSE, 1, 0, 2) = 0 + * BooleanUtils.toInteger(null, 1, 0, 2) = 2 + *+ * + * @param bool the Boolean to convert + * @param trueValue the value to return if {@code true} + * @param falseValue the value to return if {@code false} + * @param nullValue the value to return if {@code null} + * @return the appropriate value + */ + public static int toInteger(final Boolean bool, final int trueValue, final int falseValue, final int nullValue) { + if (bool == null) { + return nullValue; + } + return bool.booleanValue() ? trueValue : falseValue; + } + + /** + *
Converts a boolean to an Integer specifying the conversion values.
+ * + *+ * BooleanUtils.toIntegerObject(true, Integer.valueOf(1), Integer.valueOf(0)) = Integer.valueOf(1) + * BooleanUtils.toIntegerObject(false, Integer.valueOf(1), Integer.valueOf(0)) = Integer.valueOf(0) + *+ * + * @param bool the to convert + * @param trueValue the value to return if {@code true}, may be {@code null} + * @param falseValue the value to return if {@code false}, may be {@code null} + * @return the appropriate value + */ + public static Integer toIntegerObject(final boolean bool, final Integer trueValue, final Integer falseValue) { + return bool ? trueValue : falseValue; + } + + /** + *
Converts a Boolean to an Integer specifying the conversion values.
+ * + *+ * BooleanUtils.toIntegerObject(Boolean.TRUE, Integer.valueOf(1), Integer.valueOf(0), Integer.valueOf(2)) = Integer.valueOf(1) + * BooleanUtils.toIntegerObject(Boolean.FALSE, Integer.valueOf(1), Integer.valueOf(0), Integer.valueOf(2)) = Integer.valueOf(0) + * BooleanUtils.toIntegerObject(null, Integer.valueOf(1), Integer.valueOf(0), Integer.valueOf(2)) = Integer.valueOf(2) + *+ * + * @param bool the Boolean to convert + * @param trueValue the value to return if {@code true}, may be {@code null} + * @param falseValue the value to return if {@code false}, may be {@code null} + * @param nullValue the value to return if {@code null}, may be {@code null} + * @return the appropriate value + */ + public static Integer toIntegerObject(final Boolean bool, final Integer trueValue, final Integer falseValue, final Integer nullValue) { + if (bool == null) { + return nullValue; + } + return bool.booleanValue() ? trueValue : falseValue; + } + + // String to Boolean methods + //----------------------------------------------------------------------- + /** + *
Converts a String to a Boolean.
+ * + *{@code 'true'}, {@code 'on'}, {@code 'y'}, {@code 't'} or {@code 'yes'} + * (case insensitive) will return {@code true}. + * {@code 'false'}, {@code 'off'}, {@code 'n'}, {@code 'f'} or {@code 'no'} + * (case insensitive) will return {@code false}. + * Otherwise, {@code null} is returned.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * // N.B. case is not significant + * BooleanUtils.toBooleanObject(null) = null + * BooleanUtils.toBooleanObject("true") = Boolean.TRUE + * BooleanUtils.toBooleanObject("T") = Boolean.TRUE // i.e. T[RUE] + * BooleanUtils.toBooleanObject("false") = Boolean.FALSE + * BooleanUtils.toBooleanObject("f") = Boolean.FALSE // i.e. f[alse] + * BooleanUtils.toBooleanObject("No") = Boolean.FALSE + * BooleanUtils.toBooleanObject("n") = Boolean.FALSE // i.e. n[o] + * BooleanUtils.toBooleanObject("on") = Boolean.TRUE + * BooleanUtils.toBooleanObject("ON") = Boolean.TRUE + * BooleanUtils.toBooleanObject("off") = Boolean.FALSE + * BooleanUtils.toBooleanObject("oFf") = Boolean.FALSE + * BooleanUtils.toBooleanObject("yes") = Boolean.TRUE + * BooleanUtils.toBooleanObject("Y") = Boolean.TRUE // i.e. Y[ES] + * BooleanUtils.toBooleanObject("blue") = null + * BooleanUtils.toBooleanObject("true ") = null // trailing space (too long) + * BooleanUtils.toBooleanObject("ono") = null // does not match on or no + *+ * + * @param str the String to check; upper and lower case are treated as the same + * @return the Boolean value of the string, {@code null} if no match or {@code null} input + */ + public static Boolean toBooleanObject(final String str) { + // Previously used equalsIgnoreCase, which was fast for interned 'true'. + // Non interned 'true' matched 15 times slower. + // + // Optimisation provides same performance as before for interned 'true'. + // Similar performance for null, 'false', and other strings not length 2/3/4. + // 'true'/'TRUE' match 4 times slower, 'tRUE'/'True' 7 times slower. + if (str == "true") { + return Boolean.TRUE; + } + if (str == null) { + return null; + } + switch (str.length()) { + case 1: { + final char ch0 = str.charAt(0); + if (ch0 == 'y' || ch0 == 'Y' || + ch0 == 't' || ch0 == 'T') { + return Boolean.TRUE; + } + if (ch0 == 'n' || ch0 == 'N' || + ch0 == 'f' || ch0 == 'F') { + return Boolean.FALSE; + } + break; + } + case 2: { + final char ch0 = str.charAt(0); + final char ch1 = str.charAt(1); + if ((ch0 == 'o' || ch0 == 'O') && + (ch1 == 'n' || ch1 == 'N') ) { + return Boolean.TRUE; + } + if ((ch0 == 'n' || ch0 == 'N') && + (ch1 == 'o' || ch1 == 'O') ) { + return Boolean.FALSE; + } + break; + } + case 3: { + final char ch0 = str.charAt(0); + final char ch1 = str.charAt(1); + final char ch2 = str.charAt(2); + if ((ch0 == 'y' || ch0 == 'Y') && + (ch1 == 'e' || ch1 == 'E') && + (ch2 == 's' || ch2 == 'S') ) { + return Boolean.TRUE; + } + if ((ch0 == 'o' || ch0 == 'O') && + (ch1 == 'f' || ch1 == 'F') && + (ch2 == 'f' || ch2 == 'F') ) { + return Boolean.FALSE; + } + break; + } + case 4: { + final char ch0 = str.charAt(0); + final char ch1 = str.charAt(1); + final char ch2 = str.charAt(2); + final char ch3 = str.charAt(3); + if ((ch0 == 't' || ch0 == 'T') && + (ch1 == 'r' || ch1 == 'R') && + (ch2 == 'u' || ch2 == 'U') && + (ch3 == 'e' || ch3 == 'E') ) { + return Boolean.TRUE; + } + break; + } + case 5: { + final char ch0 = str.charAt(0); + final char ch1 = str.charAt(1); + final char ch2 = str.charAt(2); + final char ch3 = str.charAt(3); + final char ch4 = str.charAt(4); + if ((ch0 == 'f' || ch0 == 'F') && + (ch1 == 'a' || ch1 == 'A') && + (ch2 == 'l' || ch2 == 'L') && + (ch3 == 's' || ch3 == 'S') && + (ch4 == 'e' || ch4 == 'E') ) { + return Boolean.FALSE; + } + break; + } + default: + break; + } + + return null; + } + + /** + *
Converts a String to a Boolean throwing an exception if no match.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.toBooleanObject("true", "true", "false", "null") = Boolean.TRUE + * BooleanUtils.toBooleanObject("false", "true", "false", "null") = Boolean.FALSE + * BooleanUtils.toBooleanObject("null", "true", "false", "null") = null + *+ * + * @param str the String to check + * @param trueString the String to match for {@code true} (case sensitive), may be {@code null} + * @param falseString the String to match for {@code false} (case sensitive), may be {@code null} + * @param nullString the String to match for {@code null} (case sensitive), may be {@code null} + * @return the Boolean value of the string, {@code null} if either the String matches {@code nullString} + * or if {@code null} input and {@code nullString} is {@code null} + * @throws IllegalArgumentException if the String doesn't match + */ +/** + *
Converts a String to a Boolean throwing an exception if no match.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.toBooleanObject("true", "true", "false", "null") = Boolean.TRUE + * BooleanUtils.toBooleanObject("false", "true", "false", "null") = Boolean.FALSE + * BooleanUtils.toBooleanObject("null", "true", "false", "null") = null + *+ * + * @param str + * the String to check + * @param trueString + * the String to match for {@code true} (case sensitive), may be {@code null} + * @param falseString + * the String to match for {@code false} (case sensitive), may be {@code null} + * @param nullString + * the String to match for {@code null} (case sensitive), may be {@code null} + * @return the Boolean value of the string, {@code null} if either the String matches {@code nullString} +or if {@code null} input and {@code nullString} is {@code null} + * @throws IllegalArgumentException + * if the String doesn't match + */ +public static java.lang.Boolean toBooleanObject(final java.lang.String str, final java.lang.String trueString, final java.lang.String falseString, final java.lang.String nullString) { + { + if (/* NPEX_NULL_EXP */ + str.equals(trueString)) { + return java.lang.Boolean.TRUE; + } else if (str.equals(falseString)) { + return java.lang.Boolean.FALSE; + } else if (str.equals(nullString)) { + return null; + } + } + // no match + throw new java.lang.IllegalArgumentException("The String did not match any specified value"); +} + + // String to boolean methods + //----------------------------------------------------------------------- + /** + *
Converts a String to a boolean (optimised for performance).
+ * + *{@code 'true'}, {@code 'on'}, {@code 'y'}, {@code 't'} or {@code 'yes'} + * (case insensitive) will return {@code true}. Otherwise, + * {@code false} is returned.
+ * + *This method performs 4 times faster (JDK1.4) than + * {@code Boolean.valueOf(String)}. However, this method accepts + * 'on' and 'yes', 't', 'y' as true values. + * + *
+ * BooleanUtils.toBoolean(null) = false + * BooleanUtils.toBoolean("true") = true + * BooleanUtils.toBoolean("TRUE") = true + * BooleanUtils.toBoolean("tRUe") = true + * BooleanUtils.toBoolean("on") = true + * BooleanUtils.toBoolean("yes") = true + * BooleanUtils.toBoolean("false") = false + * BooleanUtils.toBoolean("x gti") = false + * BooleanUtils.toBooleanObject("y") = true + * BooleanUtils.toBooleanObject("n") = false + * BooleanUtils.toBooleanObject("t") = true + * BooleanUtils.toBooleanObject("f") = false + *+ * + * @param str the String to check + * @return the boolean value of the string, {@code false} if no match or the String is null + */ + public static boolean toBoolean(final String str) { + return toBooleanObject(str) == Boolean.TRUE; + } + + /** + *
Converts a String to a Boolean throwing an exception if no match found.
+ * + *+ * BooleanUtils.toBoolean("true", "true", "false") = true + * BooleanUtils.toBoolean("false", "true", "false") = false + *+ * + * @param str the String to check + * @param trueString the String to match for {@code true} (case sensitive), may be {@code null} + * @param falseString the String to match for {@code false} (case sensitive), may be {@code null} + * @return the boolean value of the string + * @throws IllegalArgumentException if the String doesn't match + */ + public static boolean toBoolean(final String str, final String trueString, final String falseString) { + if (str == trueString) { + return true; + } else if (str == falseString) { + return false; + } else if (str != null) { + if (str.equals(trueString)) { + return true; + } else if (str.equals(falseString)) { + return false; + } + } + // no match + throw new IllegalArgumentException("The String did not match either specified value"); + } + + // Boolean to String methods + //----------------------------------------------------------------------- + /** + *
Converts a Boolean to a String returning {@code 'true'}, + * {@code 'false'}, or {@code null}.
+ * + *+ * BooleanUtils.toStringTrueFalse(Boolean.TRUE) = "true" + * BooleanUtils.toStringTrueFalse(Boolean.FALSE) = "false" + * BooleanUtils.toStringTrueFalse(null) = null; + *+ * + * @param bool the Boolean to check + * @return {@code 'true'}, {@code 'false'}, or {@code null} + */ + public static String toStringTrueFalse(final Boolean bool) { + return toString(bool, "true", "false", null); + } + + /** + *
Converts a Boolean to a String returning {@code 'on'}, + * {@code 'off'}, or {@code null}.
+ * + *+ * BooleanUtils.toStringOnOff(Boolean.TRUE) = "on" + * BooleanUtils.toStringOnOff(Boolean.FALSE) = "off" + * BooleanUtils.toStringOnOff(null) = null; + *+ * + * @param bool the Boolean to check + * @return {@code 'on'}, {@code 'off'}, or {@code null} + */ + public static String toStringOnOff(final Boolean bool) { + return toString(bool, "on", "off", null); + } + + /** + *
Converts a Boolean to a String returning {@code 'yes'}, + * {@code 'no'}, or {@code null}.
+ * + *+ * BooleanUtils.toStringYesNo(Boolean.TRUE) = "yes" + * BooleanUtils.toStringYesNo(Boolean.FALSE) = "no" + * BooleanUtils.toStringYesNo(null) = null; + *+ * + * @param bool the Boolean to check + * @return {@code 'yes'}, {@code 'no'}, or {@code null} + */ + public static String toStringYesNo(final Boolean bool) { + return toString(bool, "yes", "no", null); + } + + /** + *
Converts a Boolean to a String returning one of the input Strings.
+ * + *+ * BooleanUtils.toString(Boolean.TRUE, "true", "false", null) = "true" + * BooleanUtils.toString(Boolean.FALSE, "true", "false", null) = "false" + * BooleanUtils.toString(null, "true", "false", null) = null; + *+ * + * @param bool the Boolean to check + * @param trueString the String to return if {@code true}, may be {@code null} + * @param falseString the String to return if {@code false}, may be {@code null} + * @param nullString the String to return if {@code null}, may be {@code null} + * @return one of the three input Strings + */ + public static String toString(final Boolean bool, final String trueString, final String falseString, final String nullString) { + if (bool == null) { + return nullString; + } + return bool.booleanValue() ? trueString : falseString; + } + + // boolean to String methods + //----------------------------------------------------------------------- + /** + *
Converts a boolean to a String returning {@code 'true'} + * or {@code 'false'}.
+ * + *+ * BooleanUtils.toStringTrueFalse(true) = "true" + * BooleanUtils.toStringTrueFalse(false) = "false" + *+ * + * @param bool the Boolean to check + * @return {@code 'true'}, {@code 'false'}, or {@code null} + */ + public static String toStringTrueFalse(final boolean bool) { + return toString(bool, "true", "false"); + } + + /** + *
Converts a boolean to a String returning {@code 'on'} + * or {@code 'off'}.
+ * + *+ * BooleanUtils.toStringOnOff(true) = "on" + * BooleanUtils.toStringOnOff(false) = "off" + *+ * + * @param bool the Boolean to check + * @return {@code 'on'}, {@code 'off'}, or {@code null} + */ + public static String toStringOnOff(final boolean bool) { + return toString(bool, "on", "off"); + } + + /** + *
Converts a boolean to a String returning {@code 'yes'} + * or {@code 'no'}.
+ * + *+ * BooleanUtils.toStringYesNo(true) = "yes" + * BooleanUtils.toStringYesNo(false) = "no" + *+ * + * @param bool the Boolean to check + * @return {@code 'yes'}, {@code 'no'}, or {@code null} + */ + public static String toStringYesNo(final boolean bool) { + return toString(bool, "yes", "no"); + } + + /** + *
Converts a boolean to a String returning one of the input Strings.
+ * + *+ * BooleanUtils.toString(true, "true", "false") = "true" + * BooleanUtils.toString(false, "true", "false") = "false" + *+ * + * @param bool the Boolean to check + * @param trueString the String to return if {@code true}, may be {@code null} + * @param falseString the String to return if {@code false}, may be {@code null} + * @return one of the two input Strings + */ + public static String toString(final boolean bool, final String trueString, final String falseString) { + return bool ? trueString : falseString; + } + + // logical operations + // ---------------------------------------------------------------------- + /** + *
Performs an and on a set of booleans.
+ * + *+ * BooleanUtils.and(true, true) = true + * BooleanUtils.and(false, false) = false + * BooleanUtils.and(true, false) = false + * BooleanUtils.and(true, true, false) = false + * BooleanUtils.and(true, true, true) = true + *+ * + * @param array an array of {@code boolean}s + * @return {@code true} if the and is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + * @since 3.0.1 + */ + public static boolean and(final boolean... array) { + // Validates input + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + for (final boolean element : array) { + if (!element) { + return false; + } + } + return true; + } + + /** + *
Performs an and on an array of Booleans.
+ * + *+ * BooleanUtils.and(Boolean.TRUE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.and(Boolean.FALSE, Boolean.FALSE) = Boolean.FALSE + * BooleanUtils.and(Boolean.TRUE, Boolean.FALSE) = Boolean.FALSE + * BooleanUtils.and(Boolean.TRUE, Boolean.TRUE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.and(Boolean.FALSE, Boolean.FALSE, Boolean.TRUE) = Boolean.FALSE + * BooleanUtils.and(Boolean.TRUE, Boolean.FALSE, Boolean.TRUE) = Boolean.FALSE + *+ * + * @param array an array of {@code Boolean}s + * @return {@code true} if the and is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + * @throws IllegalArgumentException if {@code array} contains a {@code null} + * @since 3.0.1 + */ + public static Boolean and(final Boolean... array) { + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + try { + final boolean[] primitive = ArrayUtils.toPrimitive(array); + return and(primitive) ? Boolean.TRUE : Boolean.FALSE; + } catch (final NullPointerException ex) { + throw new IllegalArgumentException("The array must not contain any null elements"); + } + } + + /** + *
Performs an or on a set of booleans.
+ * + *+ * BooleanUtils.or(true, true) = true + * BooleanUtils.or(false, false) = false + * BooleanUtils.or(true, false) = true + * BooleanUtils.or(true, true, false) = true + * BooleanUtils.or(true, true, true) = true + * BooleanUtils.or(false, false, false) = false + *+ * + * @param array an array of {@code boolean}s + * @return {@code true} if the or is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + * @since 3.0.1 + */ + public static boolean or(final boolean... array) { + // Validates input + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + for (final boolean element : array) { + if (element) { + return true; + } + } + return false; + } + + /** + *
Performs an or on an array of Booleans.
+ * + *+ * BooleanUtils.or(Boolean.TRUE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.or(Boolean.FALSE, Boolean.FALSE) = Boolean.FALSE + * BooleanUtils.or(Boolean.TRUE, Boolean.FALSE) = Boolean.TRUE + * BooleanUtils.or(Boolean.TRUE, Boolean.TRUE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.or(Boolean.FALSE, Boolean.FALSE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.or(Boolean.TRUE, Boolean.FALSE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.or(Boolean.FALSE, Boolean.FALSE, Boolean.FALSE) = Boolean.FALSE + *+ * + * @param array an array of {@code Boolean}s + * @return {@code true} if the or is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + * @throws IllegalArgumentException if {@code array} contains a {@code null} + * @since 3.0.1 + */ + public static Boolean or(final Boolean... array) { + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + try { + final boolean[] primitive = ArrayUtils.toPrimitive(array); + return or(primitive) ? Boolean.TRUE : Boolean.FALSE; + } catch (final NullPointerException ex) { + throw new IllegalArgumentException("The array must not contain any null elements"); + } + } + + /** + *
Performs an xor on a set of booleans.
+ * + *+ * BooleanUtils.xor(true, true) = false + * BooleanUtils.xor(false, false) = false + * BooleanUtils.xor(true, false) = true + * BooleanUtils.xor(true, true) = false + * BooleanUtils.xor(false, false) = false + * BooleanUtils.xor(true, false) = true + *+ * + * @param array an array of {@code boolean}s + * @return {@code true} if the xor is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + */ + public static boolean xor(final boolean... array) { + // Validates input + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + + // false if the neutral element of the xor operator + boolean result = false; + for (final boolean element : array) { + result ^= element; + } + + return result; + } + + /** + *
Performs an xor on an array of Booleans.
+ * + *+ * BooleanUtils.xor(new Boolean[] { Boolean.TRUE, Boolean.TRUE }) = Boolean.FALSE + * BooleanUtils.xor(new Boolean[] { Boolean.FALSE, Boolean.FALSE }) = Boolean.FALSE + * BooleanUtils.xor(new Boolean[] { Boolean.TRUE, Boolean.FALSE }) = Boolean.TRUE + *+ * + * @param array an array of {@code Boolean}s + * @return {@code true} if the xor is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + * @throws IllegalArgumentException if {@code array} contains a {@code null} + */ + public static Boolean xor(final Boolean... array) { + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + try { + final boolean[] primitive = ArrayUtils.toPrimitive(array); + return xor(primitive) ? Boolean.TRUE : Boolean.FALSE; + } catch (final NullPointerException ex) { + throw new IllegalArgumentException("The array must not contain any null elements"); + } + } + + /** + *
Compares two {@code boolean} values. This is the same functionality as provided in Java 7.
+ * + * @param x the first {@code boolean} to compare + * @param y the second {@code boolean} to compare + * @return the value {@code 0} if {@code x == y}; + * a value less than {@code 0} if {@code !x && y}; and + * a value greater than {@code 0} if {@code x && !y} + * @since 3.4 + */ + public static int compare(boolean x, boolean y) { + if (x == y) { + return 0; + } + return x ? 1 : -1; + } + +} diff --git a/Java/commons-lang-BooleanUtils_656/metadata.json b/Java/commons-lang-BooleanUtils_656/metadata.json new file mode 100644 index 000000000..72189e6fc --- /dev/null +++ b/Java/commons-lang-BooleanUtils_656/metadata.json @@ -0,0 +1,21 @@ +{ + "language": "java", + "id": "commons-lang-BooleanUtils_656", + "buggyPath": ".", + "referencePath": null, + "buildCommand": "mvn package -V -B -Denforcer.skip=true -Dcheckstyle.skip=true -Dcobertura.skip=true -Drat.skip=true -Dlicense.skip=true -Dfindbugs.skip=true -Dgpg.skip=true -Dskip.npm=true -Dskip.gulp=true -Dskip.bower=true -Drat.numUnapprovedLicenses=100 -DskipTests=true -DskipITs=true -Dtest=None -DfailIfNoTests=false", + "testCommand": "mvn test -V -B -Denforcer.skip=true -Dcheckstyle.skip=true -Dcobertura.skip=true -Drat.skip=true -Dlicense.skip=true -Dfindbugs.skip=true -Dgpg.skip=true -Dskip.npm=true -Dskip.gulp=true -Dskip.bower=true -Drat.numUnapprovedLicenses=100", + "categories": [ + "safety", + "npe" + ], + "npe": { + "filepath": "src/main/java/org/apache/commons/lang3/BooleanUtils.java", + "line": 682, + "npe_method": "toBooleanObject", + "deref_field": "str", + "npe_class": "BooleanUtils", + "repo": "commons-lang", + "bug_id": "BooleanUtils_656" + } +} diff --git a/Java/commons-lang-BooleanUtils_656/npe.json b/Java/commons-lang-BooleanUtils_656/npe.json new file mode 100644 index 000000000..b5aebb0dc --- /dev/null +++ b/Java/commons-lang-BooleanUtils_656/npe.json @@ -0,0 +1,7 @@ +{ + "filepath": "src/main/java/org/apache/commons/lang3/BooleanUtils.java", + "line": 682, + "npe_method": "toBooleanObject", + "deref_field": "str", + "npe_class": "BooleanUtils" +} \ No newline at end of file diff --git a/Java/commons-lang-BooleanUtils_731/Dockerfile b/Java/commons-lang-BooleanUtils_731/Dockerfile new file mode 100644 index 000000000..7b7fbe349 --- /dev/null +++ b/Java/commons-lang-BooleanUtils_731/Dockerfile @@ -0,0 +1,18 @@ +FROM ghcr.io/kupl/starlab-benchmarks/java-base:commons-lang + +ENV TZ=Asia/Seoul + +COPY ./metadata.json . +COPY ./npe.json . +COPY ./buggy.java /tmp/buggy.java +RUN export BUGGY_PATH=$(cat metadata.json | jq -r ".npe.filepath") \ + && export BUGGY_LINE=$(cat metadata.json | jq -r ".npe.line") \ + && export BUGGY_MTHD=$(cat metadata.json | jq -r ".npe.npe_method") \ + && mv /tmp/buggy.java $BUGGY_PATH \ + && echo "[{\"filepath\": \"$BUGGY_PATH\", \"line\": $BUGGY_LINE, \"method_name\": \"$BUGGY_MTHD\"}]" | jq . > traces.json + +RUN git init . && git add -A + +RUN $(cat metadata.json | jq -r ".buildCommand") + +RUN $(cat metadata.json | jq -r ".testCommand"); if [ $? -eq 0 ]; then exit 1; fi diff --git a/Java/commons-lang-BooleanUtils_731/buggy.java b/Java/commons-lang-BooleanUtils_731/buggy.java new file mode 100644 index 000000000..b1b63f3ca --- /dev/null +++ b/Java/commons-lang-BooleanUtils_731/buggy.java @@ -0,0 +1,1125 @@ +/* + * 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; + +import org.apache.commons.lang3.math.NumberUtils; + +/** + *Operations on boolean primitives and Boolean objects.
+ * + *This class tries to handle {@code null} input gracefully. + * An exception will not be thrown for a {@code null} input. + * Each method documents its behaviour in more detail.
+ * + *#ThreadSafe#
+ * @since 2.0 + */ +public class BooleanUtils { + + /** + *{@code BooleanUtils} instances should NOT be constructed in standard programming. + * Instead, the class should be used as {@code BooleanUtils.negate(true);}.
+ * + *This constructor is public to permit tools that require a JavaBean instance + * to operate.
+ */ + public BooleanUtils() { + super(); + } + + // Boolean utilities + //-------------------------------------------------------------------------- + /** + *Negates the specified boolean.
+ * + *If {@code null} is passed in, {@code null} will be returned.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.negate(Boolean.TRUE) = Boolean.FALSE; + * BooleanUtils.negate(Boolean.FALSE) = Boolean.TRUE; + * BooleanUtils.negate(null) = null; + *+ * + * @param bool the Boolean to negate, may be null + * @return the negated Boolean, or {@code null} if {@code null} input + */ + public static Boolean negate(final Boolean bool) { + if (bool == null) { + return null; + } + return bool.booleanValue() ? Boolean.FALSE : Boolean.TRUE; + } + + // boolean Boolean methods + //----------------------------------------------------------------------- + /** + *
Checks if a {@code Boolean} value is {@code true}, + * handling {@code null} by returning {@code false}.
+ * + *+ * BooleanUtils.isTrue(Boolean.TRUE) = true + * BooleanUtils.isTrue(Boolean.FALSE) = false + * BooleanUtils.isTrue(null) = false + *+ * + * @param bool the boolean to check, null returns {@code false} + * @return {@code true} only if the input is non-null and true + * @since 2.1 + */ + public static boolean isTrue(final Boolean bool) { + return Boolean.TRUE.equals(bool); + } + + /** + *
Checks if a {@code Boolean} value is not {@code true}, + * handling {@code null} by returning {@code true}.
+ * + *+ * BooleanUtils.isNotTrue(Boolean.TRUE) = false + * BooleanUtils.isNotTrue(Boolean.FALSE) = true + * BooleanUtils.isNotTrue(null) = true + *+ * + * @param bool the boolean to check, null returns {@code true} + * @return {@code true} if the input is null or false + * @since 2.3 + */ + public static boolean isNotTrue(final Boolean bool) { + return !isTrue(bool); + } + + /** + *
Checks if a {@code Boolean} value is {@code false}, + * handling {@code null} by returning {@code false}.
+ * + *+ * BooleanUtils.isFalse(Boolean.TRUE) = false + * BooleanUtils.isFalse(Boolean.FALSE) = true + * BooleanUtils.isFalse(null) = false + *+ * + * @param bool the boolean to check, null returns {@code false} + * @return {@code true} only if the input is non-null and false + * @since 2.1 + */ + public static boolean isFalse(final Boolean bool) { + return Boolean.FALSE.equals(bool); + } + + /** + *
Checks if a {@code Boolean} value is not {@code false}, + * handling {@code null} by returning {@code true}.
+ * + *+ * BooleanUtils.isNotFalse(Boolean.TRUE) = true + * BooleanUtils.isNotFalse(Boolean.FALSE) = false + * BooleanUtils.isNotFalse(null) = true + *+ * + * @param bool the boolean to check, null returns {@code true} + * @return {@code true} if the input is null or true + * @since 2.3 + */ + public static boolean isNotFalse(final Boolean bool) { + return !isFalse(bool); + } + + //----------------------------------------------------------------------- + /** + *
Converts a Boolean to a boolean handling {@code null} + * by returning {@code false}.
+ * + *+ * BooleanUtils.toBoolean(Boolean.TRUE) = true + * BooleanUtils.toBoolean(Boolean.FALSE) = false + * BooleanUtils.toBoolean(null) = false + *+ * + * @param bool the boolean to convert + * @return {@code true} or {@code false}, {@code null} returns {@code false} + */ + public static boolean toBoolean(final Boolean bool) { + return bool != null && bool.booleanValue(); + } + + /** + *
Converts a Boolean to a boolean handling {@code null}.
+ * + *+ * BooleanUtils.toBooleanDefaultIfNull(Boolean.TRUE, false) = true + * BooleanUtils.toBooleanDefaultIfNull(Boolean.FALSE, true) = false + * BooleanUtils.toBooleanDefaultIfNull(null, true) = true + *+ * + * @param bool the boolean to convert + * @param valueIfNull the boolean value to return if {@code null} + * @return {@code true} or {@code false} + */ + public static boolean toBooleanDefaultIfNull(final Boolean bool, final boolean valueIfNull) { + if (bool == null) { + return valueIfNull; + } + return bool.booleanValue(); + } + + // Integer to Boolean methods + //----------------------------------------------------------------------- + /** + *
Converts an int to a boolean using the convention that {@code zero} + * is {@code false}.
+ * + *+ * BooleanUtils.toBoolean(0) = false + * BooleanUtils.toBoolean(1) = true + * BooleanUtils.toBoolean(2) = true + *+ * + * @param value the int to convert + * @return {@code true} if non-zero, {@code false} + * if zero + */ + public static boolean toBoolean(final int value) { + return value != 0; + } + + /** + *
Converts an int to a Boolean using the convention that {@code zero} + * is {@code false}.
+ * + *+ * BooleanUtils.toBoolean(0) = Boolean.FALSE + * BooleanUtils.toBoolean(1) = Boolean.TRUE + * BooleanUtils.toBoolean(2) = Boolean.TRUE + *+ * + * @param value the int to convert + * @return Boolean.TRUE if non-zero, Boolean.FALSE if zero, + * {@code null} if {@code null} + */ + public static Boolean toBooleanObject(final int value) { + return value == 0 ? Boolean.FALSE : Boolean.TRUE; + } + + /** + *
Converts an Integer to a Boolean using the convention that {@code zero} + * is {@code false}.
+ * + *{@code null} will be converted to {@code null}.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.toBoolean(Integer.valueOf(0)) = Boolean.FALSE + * BooleanUtils.toBoolean(Integer.valueOf(1)) = Boolean.TRUE + * BooleanUtils.toBoolean(Integer.valueOf(null)) = null + *+ * + * @param value the Integer to convert + * @return Boolean.TRUE if non-zero, Boolean.FALSE if zero, + * {@code null} if {@code null} input + */ + public static Boolean toBooleanObject(final Integer value) { + if (value == null) { + return null; + } + return value.intValue() == 0 ? Boolean.FALSE : Boolean.TRUE; + } + + /** + *
Converts an int to a boolean specifying the conversion values.
+ * + *+ * BooleanUtils.toBoolean(0, 1, 0) = false + * BooleanUtils.toBoolean(1, 1, 0) = true + * BooleanUtils.toBoolean(2, 1, 2) = false + * BooleanUtils.toBoolean(2, 2, 0) = true + *+ * + * @param value the Integer to convert + * @param trueValue the value to match for {@code true} + * @param falseValue the value to match for {@code false} + * @return {@code true} or {@code false} + * @throws IllegalArgumentException if no match + */ + public static boolean toBoolean(final int value, final int trueValue, final int falseValue) { + if (value == trueValue) { + return true; + } + if (value == falseValue) { + return false; + } + // no match + throw new IllegalArgumentException("The Integer did not match either specified value"); + } + + /** + *
Converts an Integer to a boolean specifying the conversion values.
+ * + *+ * BooleanUtils.toBoolean(Integer.valueOf(0), Integer.valueOf(1), Integer.valueOf(0)) = false + * BooleanUtils.toBoolean(Integer.valueOf(1), Integer.valueOf(1), Integer.valueOf(0)) = true + * BooleanUtils.toBoolean(Integer.valueOf(2), Integer.valueOf(1), Integer.valueOf(2)) = false + * BooleanUtils.toBoolean(Integer.valueOf(2), Integer.valueOf(2), Integer.valueOf(0)) = true + * BooleanUtils.toBoolean(null, null, Integer.valueOf(0)) = true + *+ * + * @param value the Integer to convert + * @param trueValue the value to match for {@code true}, may be {@code null} + * @param falseValue the value to match for {@code false}, may be {@code null} + * @return {@code true} or {@code false} + * @throws IllegalArgumentException if no match + */ + public static boolean toBoolean(final Integer value, final Integer trueValue, final Integer falseValue) { + if (value == null) { + if (trueValue == null) { + return true; + } + if (falseValue == null) { + return false; + } + } else if (value.equals(trueValue)) { + return true; + } else if (value.equals(falseValue)) { + return false; + } + // no match + throw new IllegalArgumentException("The Integer did not match either specified value"); + } + + /** + *
Converts an int to a Boolean specifying the conversion values.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.toBooleanObject(0, 0, 2, 3) = Boolean.TRUE + * BooleanUtils.toBooleanObject(2, 1, 2, 3) = Boolean.FALSE + * BooleanUtils.toBooleanObject(3, 1, 2, 3) = null + *+ * + * @param value the Integer to convert + * @param trueValue the value to match for {@code true} + * @param falseValue the value to match for {@code false} + * @param nullValue the value to to match for {@code null} + * @return Boolean.TRUE, Boolean.FALSE, or {@code null} + * @throws IllegalArgumentException if no match + */ + public static Boolean toBooleanObject(final int value, final int trueValue, final int falseValue, final int nullValue) { + if (value == trueValue) { + return Boolean.TRUE; + } + if (value == falseValue) { + return Boolean.FALSE; + } + if (value == nullValue) { + return null; + } + // no match + throw new IllegalArgumentException("The Integer did not match any specified value"); + } + + /** + *
Converts an Integer to a Boolean specifying the conversion values.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.toBooleanObject(Integer.valueOf(0), Integer.valueOf(0), Integer.valueOf(2), Integer.valueOf(3)) = Boolean.TRUE + * BooleanUtils.toBooleanObject(Integer.valueOf(2), Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(3)) = Boolean.FALSE + * BooleanUtils.toBooleanObject(Integer.valueOf(3), Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(3)) = null + *+ * + * @param value the Integer to convert + * @param trueValue the value to match for {@code true}, may be {@code null} + * @param falseValue the value to match for {@code false}, may be {@code null} + * @param nullValue the value to to match for {@code null}, may be {@code null} + * @return Boolean.TRUE, Boolean.FALSE, or {@code null} + * @throws IllegalArgumentException if no match + */ + public static Boolean toBooleanObject(final Integer value, final Integer trueValue, final Integer falseValue, final Integer nullValue) { + if (value == null) { + if (trueValue == null) { + return Boolean.TRUE; + } + if (falseValue == null) { + return Boolean.FALSE; + } + if (nullValue == null) { + return null; + } + } else if (value.equals(trueValue)) { + return Boolean.TRUE; + } else if (value.equals(falseValue)) { + return Boolean.FALSE; + } else if (value.equals(nullValue)) { + return null; + } + // no match + throw new IllegalArgumentException("The Integer did not match any specified value"); + } + + // Boolean to Integer methods + //----------------------------------------------------------------------- + /** + *
Converts a boolean to an int using the convention that + * {@code zero} is {@code false}.
+ * + *+ * BooleanUtils.toInteger(true) = 1 + * BooleanUtils.toInteger(false) = 0 + *+ * + * @param bool the boolean to convert + * @return one if {@code true}, zero if {@code false} + */ + public static int toInteger(final boolean bool) { + return bool ? 1 : 0; + } + + /** + *
Converts a boolean to an Integer using the convention that + * {@code zero} is {@code false}.
+ * + *+ * BooleanUtils.toIntegerObject(true) = Integer.valueOf(1) + * BooleanUtils.toIntegerObject(false) = Integer.valueOf(0) + *+ * + * @param bool the boolean to convert + * @return one if {@code true}, zero if {@code false} + */ + public static Integer toIntegerObject(final boolean bool) { + return bool ? NumberUtils.INTEGER_ONE : NumberUtils.INTEGER_ZERO; + } + + /** + *
Converts a Boolean to a Integer using the convention that + * {@code zero} is {@code false}.
+ * + *{@code null} will be converted to {@code null}.
+ * + *+ * BooleanUtils.toIntegerObject(Boolean.TRUE) = Integer.valueOf(1) + * BooleanUtils.toIntegerObject(Boolean.FALSE) = Integer.valueOf(0) + *+ * + * @param bool the Boolean to convert + * @return one if Boolean.TRUE, zero if Boolean.FALSE, {@code null} if {@code null} + */ + public static Integer toIntegerObject(final Boolean bool) { + if (bool == null) { + return null; + } + return bool.booleanValue() ? NumberUtils.INTEGER_ONE : NumberUtils.INTEGER_ZERO; + } + + /** + *
Converts a boolean to an int specifying the conversion values.
+ * + *+ * BooleanUtils.toInteger(true, 1, 0) = 1 + * BooleanUtils.toInteger(false, 1, 0) = 0 + *+ * + * @param bool the to convert + * @param trueValue the value to return if {@code true} + * @param falseValue the value to return if {@code false} + * @return the appropriate value + */ + public static int toInteger(final boolean bool, final int trueValue, final int falseValue) { + return bool ? trueValue : falseValue; + } + + /** + *
Converts a Boolean to an int specifying the conversion values.
+ * + *+ * BooleanUtils.toInteger(Boolean.TRUE, 1, 0, 2) = 1 + * BooleanUtils.toInteger(Boolean.FALSE, 1, 0, 2) = 0 + * BooleanUtils.toInteger(null, 1, 0, 2) = 2 + *+ * + * @param bool the Boolean to convert + * @param trueValue the value to return if {@code true} + * @param falseValue the value to return if {@code false} + * @param nullValue the value to return if {@code null} + * @return the appropriate value + */ + public static int toInteger(final Boolean bool, final int trueValue, final int falseValue, final int nullValue) { + if (bool == null) { + return nullValue; + } + return bool.booleanValue() ? trueValue : falseValue; + } + + /** + *
Converts a boolean to an Integer specifying the conversion values.
+ * + *+ * BooleanUtils.toIntegerObject(true, Integer.valueOf(1), Integer.valueOf(0)) = Integer.valueOf(1) + * BooleanUtils.toIntegerObject(false, Integer.valueOf(1), Integer.valueOf(0)) = Integer.valueOf(0) + *+ * + * @param bool the to convert + * @param trueValue the value to return if {@code true}, may be {@code null} + * @param falseValue the value to return if {@code false}, may be {@code null} + * @return the appropriate value + */ + public static Integer toIntegerObject(final boolean bool, final Integer trueValue, final Integer falseValue) { + return bool ? trueValue : falseValue; + } + + /** + *
Converts a Boolean to an Integer specifying the conversion values.
+ * + *+ * BooleanUtils.toIntegerObject(Boolean.TRUE, Integer.valueOf(1), Integer.valueOf(0), Integer.valueOf(2)) = Integer.valueOf(1) + * BooleanUtils.toIntegerObject(Boolean.FALSE, Integer.valueOf(1), Integer.valueOf(0), Integer.valueOf(2)) = Integer.valueOf(0) + * BooleanUtils.toIntegerObject(null, Integer.valueOf(1), Integer.valueOf(0), Integer.valueOf(2)) = Integer.valueOf(2) + *+ * + * @param bool the Boolean to convert + * @param trueValue the value to return if {@code true}, may be {@code null} + * @param falseValue the value to return if {@code false}, may be {@code null} + * @param nullValue the value to return if {@code null}, may be {@code null} + * @return the appropriate value + */ + public static Integer toIntegerObject(final Boolean bool, final Integer trueValue, final Integer falseValue, final Integer nullValue) { + if (bool == null) { + return nullValue; + } + return bool.booleanValue() ? trueValue : falseValue; + } + + // String to Boolean methods + //----------------------------------------------------------------------- + /** + *
Converts a String to a Boolean.
+ * + *{@code 'true'}, {@code 'on'}, {@code 'y'}, {@code 't'} or {@code 'yes'} + * (case insensitive) will return {@code true}. + * {@code 'false'}, {@code 'off'}, {@code 'n'}, {@code 'f'} or {@code 'no'} + * (case insensitive) will return {@code false}. + * Otherwise, {@code null} is returned.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * // N.B. case is not significant + * BooleanUtils.toBooleanObject(null) = null + * BooleanUtils.toBooleanObject("true") = Boolean.TRUE + * BooleanUtils.toBooleanObject("T") = Boolean.TRUE // i.e. T[RUE] + * BooleanUtils.toBooleanObject("false") = Boolean.FALSE + * BooleanUtils.toBooleanObject("f") = Boolean.FALSE // i.e. f[alse] + * BooleanUtils.toBooleanObject("No") = Boolean.FALSE + * BooleanUtils.toBooleanObject("n") = Boolean.FALSE // i.e. n[o] + * BooleanUtils.toBooleanObject("on") = Boolean.TRUE + * BooleanUtils.toBooleanObject("ON") = Boolean.TRUE + * BooleanUtils.toBooleanObject("off") = Boolean.FALSE + * BooleanUtils.toBooleanObject("oFf") = Boolean.FALSE + * BooleanUtils.toBooleanObject("yes") = Boolean.TRUE + * BooleanUtils.toBooleanObject("Y") = Boolean.TRUE // i.e. Y[ES] + * BooleanUtils.toBooleanObject("blue") = null + * BooleanUtils.toBooleanObject("true ") = null // trailing space (too long) + * BooleanUtils.toBooleanObject("ono") = null // does not match on or no + *+ * + * @param str the String to check; upper and lower case are treated as the same + * @return the Boolean value of the string, {@code null} if no match or {@code null} input + */ + public static Boolean toBooleanObject(final String str) { + // Previously used equalsIgnoreCase, which was fast for interned 'true'. + // Non interned 'true' matched 15 times slower. + // + // Optimisation provides same performance as before for interned 'true'. + // Similar performance for null, 'false', and other strings not length 2/3/4. + // 'true'/'TRUE' match 4 times slower, 'tRUE'/'True' 7 times slower. + if (str == "true") { + return Boolean.TRUE; + } + if (str == null) { + return null; + } + switch (str.length()) { + case 1: { + final char ch0 = str.charAt(0); + if (ch0 == 'y' || ch0 == 'Y' || + ch0 == 't' || ch0 == 'T') { + return Boolean.TRUE; + } + if (ch0 == 'n' || ch0 == 'N' || + ch0 == 'f' || ch0 == 'F') { + return Boolean.FALSE; + } + break; + } + case 2: { + final char ch0 = str.charAt(0); + final char ch1 = str.charAt(1); + if ((ch0 == 'o' || ch0 == 'O') && + (ch1 == 'n' || ch1 == 'N') ) { + return Boolean.TRUE; + } + if ((ch0 == 'n' || ch0 == 'N') && + (ch1 == 'o' || ch1 == 'O') ) { + return Boolean.FALSE; + } + break; + } + case 3: { + final char ch0 = str.charAt(0); + final char ch1 = str.charAt(1); + final char ch2 = str.charAt(2); + if ((ch0 == 'y' || ch0 == 'Y') && + (ch1 == 'e' || ch1 == 'E') && + (ch2 == 's' || ch2 == 'S') ) { + return Boolean.TRUE; + } + if ((ch0 == 'o' || ch0 == 'O') && + (ch1 == 'f' || ch1 == 'F') && + (ch2 == 'f' || ch2 == 'F') ) { + return Boolean.FALSE; + } + break; + } + case 4: { + final char ch0 = str.charAt(0); + final char ch1 = str.charAt(1); + final char ch2 = str.charAt(2); + final char ch3 = str.charAt(3); + if ((ch0 == 't' || ch0 == 'T') && + (ch1 == 'r' || ch1 == 'R') && + (ch2 == 'u' || ch2 == 'U') && + (ch3 == 'e' || ch3 == 'E') ) { + return Boolean.TRUE; + } + break; + } + case 5: { + final char ch0 = str.charAt(0); + final char ch1 = str.charAt(1); + final char ch2 = str.charAt(2); + final char ch3 = str.charAt(3); + final char ch4 = str.charAt(4); + if ((ch0 == 'f' || ch0 == 'F') && + (ch1 == 'a' || ch1 == 'A') && + (ch2 == 'l' || ch2 == 'L') && + (ch3 == 's' || ch3 == 'S') && + (ch4 == 'e' || ch4 == 'E') ) { + return Boolean.FALSE; + } + break; + } + default: + break; + } + + return null; + } + + /** + *
Converts a String to a Boolean throwing an exception if no match.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.toBooleanObject("true", "true", "false", "null") = Boolean.TRUE + * BooleanUtils.toBooleanObject("false", "true", "false", "null") = Boolean.FALSE + * BooleanUtils.toBooleanObject("null", "true", "false", "null") = null + *+ * + * @param str the String to check + * @param trueString the String to match for {@code true} (case sensitive), may be {@code null} + * @param falseString the String to match for {@code false} (case sensitive), may be {@code null} + * @param nullString the String to match for {@code null} (case sensitive), may be {@code null} + * @return the Boolean value of the string, {@code null} if either the String matches {@code nullString} + * or if {@code null} input and {@code nullString} is {@code null} + * @throws IllegalArgumentException if the String doesn't match + */ + public static Boolean toBooleanObject(final String str, final String trueString, final String falseString, final String nullString) { + if (str == null) { + if (trueString == null) { + return Boolean.TRUE; + } + if (falseString == null) { + return Boolean.FALSE; + } + if (nullString == null) { + return null; + } + } else if (str.equals(trueString)) { + return Boolean.TRUE; + } else if (str.equals(falseString)) { + return Boolean.FALSE; + } else if (str.equals(nullString)) { + return null; + } + // no match + throw new IllegalArgumentException("The String did not match any specified value"); + } + + // String to boolean methods + //----------------------------------------------------------------------- + /** + *
Converts a String to a boolean (optimised for performance).
+ * + *{@code 'true'}, {@code 'on'}, {@code 'y'}, {@code 't'} or {@code 'yes'} + * (case insensitive) will return {@code true}. Otherwise, + * {@code false} is returned.
+ * + *This method performs 4 times faster (JDK1.4) than + * {@code Boolean.valueOf(String)}. However, this method accepts + * 'on' and 'yes', 't', 'y' as true values. + * + *
+ * BooleanUtils.toBoolean(null) = false + * BooleanUtils.toBoolean("true") = true + * BooleanUtils.toBoolean("TRUE") = true + * BooleanUtils.toBoolean("tRUe") = true + * BooleanUtils.toBoolean("on") = true + * BooleanUtils.toBoolean("yes") = true + * BooleanUtils.toBoolean("false") = false + * BooleanUtils.toBoolean("x gti") = false + * BooleanUtils.toBooleanObject("y") = true + * BooleanUtils.toBooleanObject("n") = false + * BooleanUtils.toBooleanObject("t") = true + * BooleanUtils.toBooleanObject("f") = false + *+ * + * @param str the String to check + * @return the boolean value of the string, {@code false} if no match or the String is null + */ + public static boolean toBoolean(final String str) { + return toBooleanObject(str) == Boolean.TRUE; + } + + /** + *
Converts a String to a Boolean throwing an exception if no match found.
+ * + *+ * BooleanUtils.toBoolean("true", "true", "false") = true + * BooleanUtils.toBoolean("false", "true", "false") = false + *+ * + * @param str the String to check + * @param trueString the String to match for {@code true} (case sensitive), may be {@code null} + * @param falseString the String to match for {@code false} (case sensitive), may be {@code null} + * @return the boolean value of the string + * @throws IllegalArgumentException if the String doesn't match + */ +/** + *
Converts a String to a Boolean throwing an exception if no match found.
+ * + *+ * BooleanUtils.toBoolean("true", "true", "false") = true + * BooleanUtils.toBoolean("false", "true", "false") = false + *+ * + * @param str + * the String to check + * @param trueString + * the String to match for {@code true} (case sensitive), may be {@code null} + * @param falseString + * the String to match for {@code false} (case sensitive), may be {@code null} + * @return the boolean value of the string + * @throws IllegalArgumentException + * if the String doesn't match + */ +public static boolean toBoolean(final java.lang.String str, final java.lang.String trueString, final java.lang.String falseString) { + if (str == trueString) { + return true; + } else if (str == falseString) { + return false; + } else { + { + if (/* NPEX_NULL_EXP */ + str.equals(trueString)) { + return true; + } else if (str.equals(falseString)) { + return false; + } + } + } + // no match + throw new java.lang.IllegalArgumentException("The String did not match either specified value"); +} + + // Boolean to String methods + //----------------------------------------------------------------------- + /** + *
Converts a Boolean to a String returning {@code 'true'}, + * {@code 'false'}, or {@code null}.
+ * + *+ * BooleanUtils.toStringTrueFalse(Boolean.TRUE) = "true" + * BooleanUtils.toStringTrueFalse(Boolean.FALSE) = "false" + * BooleanUtils.toStringTrueFalse(null) = null; + *+ * + * @param bool the Boolean to check + * @return {@code 'true'}, {@code 'false'}, or {@code null} + */ + public static String toStringTrueFalse(final Boolean bool) { + return toString(bool, "true", "false", null); + } + + /** + *
Converts a Boolean to a String returning {@code 'on'}, + * {@code 'off'}, or {@code null}.
+ * + *+ * BooleanUtils.toStringOnOff(Boolean.TRUE) = "on" + * BooleanUtils.toStringOnOff(Boolean.FALSE) = "off" + * BooleanUtils.toStringOnOff(null) = null; + *+ * + * @param bool the Boolean to check + * @return {@code 'on'}, {@code 'off'}, or {@code null} + */ + public static String toStringOnOff(final Boolean bool) { + return toString(bool, "on", "off", null); + } + + /** + *
Converts a Boolean to a String returning {@code 'yes'}, + * {@code 'no'}, or {@code null}.
+ * + *+ * BooleanUtils.toStringYesNo(Boolean.TRUE) = "yes" + * BooleanUtils.toStringYesNo(Boolean.FALSE) = "no" + * BooleanUtils.toStringYesNo(null) = null; + *+ * + * @param bool the Boolean to check + * @return {@code 'yes'}, {@code 'no'}, or {@code null} + */ + public static String toStringYesNo(final Boolean bool) { + return toString(bool, "yes", "no", null); + } + + /** + *
Converts a Boolean to a String returning one of the input Strings.
+ * + *+ * BooleanUtils.toString(Boolean.TRUE, "true", "false", null) = "true" + * BooleanUtils.toString(Boolean.FALSE, "true", "false", null) = "false" + * BooleanUtils.toString(null, "true", "false", null) = null; + *+ * + * @param bool the Boolean to check + * @param trueString the String to return if {@code true}, may be {@code null} + * @param falseString the String to return if {@code false}, may be {@code null} + * @param nullString the String to return if {@code null}, may be {@code null} + * @return one of the three input Strings + */ + public static String toString(final Boolean bool, final String trueString, final String falseString, final String nullString) { + if (bool == null) { + return nullString; + } + return bool.booleanValue() ? trueString : falseString; + } + + // boolean to String methods + //----------------------------------------------------------------------- + /** + *
Converts a boolean to a String returning {@code 'true'} + * or {@code 'false'}.
+ * + *+ * BooleanUtils.toStringTrueFalse(true) = "true" + * BooleanUtils.toStringTrueFalse(false) = "false" + *+ * + * @param bool the Boolean to check + * @return {@code 'true'}, {@code 'false'}, or {@code null} + */ + public static String toStringTrueFalse(final boolean bool) { + return toString(bool, "true", "false"); + } + + /** + *
Converts a boolean to a String returning {@code 'on'} + * or {@code 'off'}.
+ * + *+ * BooleanUtils.toStringOnOff(true) = "on" + * BooleanUtils.toStringOnOff(false) = "off" + *+ * + * @param bool the Boolean to check + * @return {@code 'on'}, {@code 'off'}, or {@code null} + */ + public static String toStringOnOff(final boolean bool) { + return toString(bool, "on", "off"); + } + + /** + *
Converts a boolean to a String returning {@code 'yes'} + * or {@code 'no'}.
+ * + *+ * BooleanUtils.toStringYesNo(true) = "yes" + * BooleanUtils.toStringYesNo(false) = "no" + *+ * + * @param bool the Boolean to check + * @return {@code 'yes'}, {@code 'no'}, or {@code null} + */ + public static String toStringYesNo(final boolean bool) { + return toString(bool, "yes", "no"); + } + + /** + *
Converts a boolean to a String returning one of the input Strings.
+ * + *+ * BooleanUtils.toString(true, "true", "false") = "true" + * BooleanUtils.toString(false, "true", "false") = "false" + *+ * + * @param bool the Boolean to check + * @param trueString the String to return if {@code true}, may be {@code null} + * @param falseString the String to return if {@code false}, may be {@code null} + * @return one of the two input Strings + */ + public static String toString(final boolean bool, final String trueString, final String falseString) { + return bool ? trueString : falseString; + } + + // logical operations + // ---------------------------------------------------------------------- + /** + *
Performs an and on a set of booleans.
+ * + *+ * BooleanUtils.and(true, true) = true + * BooleanUtils.and(false, false) = false + * BooleanUtils.and(true, false) = false + * BooleanUtils.and(true, true, false) = false + * BooleanUtils.and(true, true, true) = true + *+ * + * @param array an array of {@code boolean}s + * @return {@code true} if the and is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + * @since 3.0.1 + */ + public static boolean and(final boolean... array) { + // Validates input + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + for (final boolean element : array) { + if (!element) { + return false; + } + } + return true; + } + + /** + *
Performs an and on an array of Booleans.
+ * + *+ * BooleanUtils.and(Boolean.TRUE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.and(Boolean.FALSE, Boolean.FALSE) = Boolean.FALSE + * BooleanUtils.and(Boolean.TRUE, Boolean.FALSE) = Boolean.FALSE + * BooleanUtils.and(Boolean.TRUE, Boolean.TRUE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.and(Boolean.FALSE, Boolean.FALSE, Boolean.TRUE) = Boolean.FALSE + * BooleanUtils.and(Boolean.TRUE, Boolean.FALSE, Boolean.TRUE) = Boolean.FALSE + *+ * + * @param array an array of {@code Boolean}s + * @return {@code true} if the and is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + * @throws IllegalArgumentException if {@code array} contains a {@code null} + * @since 3.0.1 + */ + public static Boolean and(final Boolean... array) { + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + try { + final boolean[] primitive = ArrayUtils.toPrimitive(array); + return and(primitive) ? Boolean.TRUE : Boolean.FALSE; + } catch (final NullPointerException ex) { + throw new IllegalArgumentException("The array must not contain any null elements"); + } + } + + /** + *
Performs an or on a set of booleans.
+ * + *+ * BooleanUtils.or(true, true) = true + * BooleanUtils.or(false, false) = false + * BooleanUtils.or(true, false) = true + * BooleanUtils.or(true, true, false) = true + * BooleanUtils.or(true, true, true) = true + * BooleanUtils.or(false, false, false) = false + *+ * + * @param array an array of {@code boolean}s + * @return {@code true} if the or is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + * @since 3.0.1 + */ + public static boolean or(final boolean... array) { + // Validates input + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + for (final boolean element : array) { + if (element) { + return true; + } + } + return false; + } + + /** + *
Performs an or on an array of Booleans.
+ * + *+ * BooleanUtils.or(Boolean.TRUE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.or(Boolean.FALSE, Boolean.FALSE) = Boolean.FALSE + * BooleanUtils.or(Boolean.TRUE, Boolean.FALSE) = Boolean.TRUE + * BooleanUtils.or(Boolean.TRUE, Boolean.TRUE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.or(Boolean.FALSE, Boolean.FALSE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.or(Boolean.TRUE, Boolean.FALSE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.or(Boolean.FALSE, Boolean.FALSE, Boolean.FALSE) = Boolean.FALSE + *+ * + * @param array an array of {@code Boolean}s + * @return {@code true} if the or is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + * @throws IllegalArgumentException if {@code array} contains a {@code null} + * @since 3.0.1 + */ + public static Boolean or(final Boolean... array) { + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + try { + final boolean[] primitive = ArrayUtils.toPrimitive(array); + return or(primitive) ? Boolean.TRUE : Boolean.FALSE; + } catch (final NullPointerException ex) { + throw new IllegalArgumentException("The array must not contain any null elements"); + } + } + + /** + *
Performs an xor on a set of booleans.
+ * + *+ * BooleanUtils.xor(true, true) = false + * BooleanUtils.xor(false, false) = false + * BooleanUtils.xor(true, false) = true + * BooleanUtils.xor(true, true) = false + * BooleanUtils.xor(false, false) = false + * BooleanUtils.xor(true, false) = true + *+ * + * @param array an array of {@code boolean}s + * @return {@code true} if the xor is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + */ + public static boolean xor(final boolean... array) { + // Validates input + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + + // false if the neutral element of the xor operator + boolean result = false; + for (final boolean element : array) { + result ^= element; + } + + return result; + } + + /** + *
Performs an xor on an array of Booleans.
+ * + *+ * BooleanUtils.xor(new Boolean[] { Boolean.TRUE, Boolean.TRUE }) = Boolean.FALSE + * BooleanUtils.xor(new Boolean[] { Boolean.FALSE, Boolean.FALSE }) = Boolean.FALSE + * BooleanUtils.xor(new Boolean[] { Boolean.TRUE, Boolean.FALSE }) = Boolean.TRUE + *+ * + * @param array an array of {@code Boolean}s + * @return {@code true} if the xor is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + * @throws IllegalArgumentException if {@code array} contains a {@code null} + */ + public static Boolean xor(final Boolean... array) { + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + try { + final boolean[] primitive = ArrayUtils.toPrimitive(array); + return xor(primitive) ? Boolean.TRUE : Boolean.FALSE; + } catch (final NullPointerException ex) { + throw new IllegalArgumentException("The array must not contain any null elements"); + } + } + + /** + *
Compares two {@code boolean} values. This is the same functionality as provided in Java 7.
+ * + * @param x the first {@code boolean} to compare + * @param y the second {@code boolean} to compare + * @return the value {@code 0} if {@code x == y}; + * a value less than {@code 0} if {@code !x && y}; and + * a value greater than {@code 0} if {@code x && !y} + * @since 3.4 + */ + public static int compare(boolean x, boolean y) { + if (x == y) { + return 0; + } + return x ? 1 : -1; + } + +} diff --git a/Java/commons-lang-BooleanUtils_731/metadata.json b/Java/commons-lang-BooleanUtils_731/metadata.json new file mode 100644 index 000000000..5d240ba7a --- /dev/null +++ b/Java/commons-lang-BooleanUtils_731/metadata.json @@ -0,0 +1,21 @@ +{ + "language": "java", + "id": "commons-lang-BooleanUtils_731", + "buggyPath": ".", + "referencePath": null, + "buildCommand": "mvn package -V -B -Denforcer.skip=true -Dcheckstyle.skip=true -Dcobertura.skip=true -Drat.skip=true -Dlicense.skip=true -Dfindbugs.skip=true -Dgpg.skip=true -Dskip.npm=true -Dskip.gulp=true -Dskip.bower=true -Drat.numUnapprovedLicenses=100 -DskipTests=true -DskipITs=true -Dtest=None -DfailIfNoTests=false", + "testCommand": "mvn test -V -B -Denforcer.skip=true -Dcheckstyle.skip=true -Dcobertura.skip=true -Drat.skip=true -Dlicense.skip=true -Dfindbugs.skip=true -Dgpg.skip=true -Dskip.npm=true -Dskip.gulp=true -Dskip.bower=true -Drat.numUnapprovedLicenses=100", + "categories": [ + "safety", + "npe" + ], + "npe": { + "filepath": "src/main/java/org/apache/commons/lang3/BooleanUtils.java", + "line": 752, + "npe_method": "toBoolean", + "deref_field": "str", + "npe_class": "BooleanUtils", + "repo": "commons-lang", + "bug_id": "BooleanUtils_731" + } +} diff --git a/Java/commons-lang-BooleanUtils_731/npe.json b/Java/commons-lang-BooleanUtils_731/npe.json new file mode 100644 index 000000000..293f2f490 --- /dev/null +++ b/Java/commons-lang-BooleanUtils_731/npe.json @@ -0,0 +1,7 @@ +{ + "filepath": "src/main/java/org/apache/commons/lang3/BooleanUtils.java", + "line": 752, + "npe_method": "toBoolean", + "deref_field": "str", + "npe_class": "BooleanUtils" +} \ No newline at end of file diff --git a/Java/commons-lang-BooleanUtils_811/Dockerfile b/Java/commons-lang-BooleanUtils_811/Dockerfile new file mode 100644 index 000000000..7b7fbe349 --- /dev/null +++ b/Java/commons-lang-BooleanUtils_811/Dockerfile @@ -0,0 +1,18 @@ +FROM ghcr.io/kupl/starlab-benchmarks/java-base:commons-lang + +ENV TZ=Asia/Seoul + +COPY ./metadata.json . +COPY ./npe.json . +COPY ./buggy.java /tmp/buggy.java +RUN export BUGGY_PATH=$(cat metadata.json | jq -r ".npe.filepath") \ + && export BUGGY_LINE=$(cat metadata.json | jq -r ".npe.line") \ + && export BUGGY_MTHD=$(cat metadata.json | jq -r ".npe.npe_method") \ + && mv /tmp/buggy.java $BUGGY_PATH \ + && echo "[{\"filepath\": \"$BUGGY_PATH\", \"line\": $BUGGY_LINE, \"method_name\": \"$BUGGY_MTHD\"}]" | jq . > traces.json + +RUN git init . && git add -A + +RUN $(cat metadata.json | jq -r ".buildCommand") + +RUN $(cat metadata.json | jq -r ".testCommand"); if [ $? -eq 0 ]; then exit 1; fi diff --git a/Java/commons-lang-BooleanUtils_811/buggy.java b/Java/commons-lang-BooleanUtils_811/buggy.java new file mode 100644 index 000000000..ead7f5f2f --- /dev/null +++ b/Java/commons-lang-BooleanUtils_811/buggy.java @@ -0,0 +1,1123 @@ +/* + * 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; + +import org.apache.commons.lang3.math.NumberUtils; + +/** + *Operations on boolean primitives and Boolean objects.
+ * + *This class tries to handle {@code null} input gracefully. + * An exception will not be thrown for a {@code null} input. + * Each method documents its behaviour in more detail.
+ * + *#ThreadSafe#
+ * @since 2.0 + */ +public class BooleanUtils { + + /** + *{@code BooleanUtils} instances should NOT be constructed in standard programming. + * Instead, the class should be used as {@code BooleanUtils.negate(true);}.
+ * + *This constructor is public to permit tools that require a JavaBean instance + * to operate.
+ */ + public BooleanUtils() { + super(); + } + + // Boolean utilities + //-------------------------------------------------------------------------- + /** + *Negates the specified boolean.
+ * + *If {@code null} is passed in, {@code null} will be returned.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.negate(Boolean.TRUE) = Boolean.FALSE; + * BooleanUtils.negate(Boolean.FALSE) = Boolean.TRUE; + * BooleanUtils.negate(null) = null; + *+ * + * @param bool the Boolean to negate, may be null + * @return the negated Boolean, or {@code null} if {@code null} input + */ + public static Boolean negate(final Boolean bool) { + if (bool == null) { + return null; + } + return bool.booleanValue() ? Boolean.FALSE : Boolean.TRUE; + } + + // boolean Boolean methods + //----------------------------------------------------------------------- + /** + *
Checks if a {@code Boolean} value is {@code true}, + * handling {@code null} by returning {@code false}.
+ * + *+ * BooleanUtils.isTrue(Boolean.TRUE) = true + * BooleanUtils.isTrue(Boolean.FALSE) = false + * BooleanUtils.isTrue(null) = false + *+ * + * @param bool the boolean to check, null returns {@code false} + * @return {@code true} only if the input is non-null and true + * @since 2.1 + */ + public static boolean isTrue(final Boolean bool) { + return Boolean.TRUE.equals(bool); + } + + /** + *
Checks if a {@code Boolean} value is not {@code true}, + * handling {@code null} by returning {@code true}.
+ * + *+ * BooleanUtils.isNotTrue(Boolean.TRUE) = false + * BooleanUtils.isNotTrue(Boolean.FALSE) = true + * BooleanUtils.isNotTrue(null) = true + *+ * + * @param bool the boolean to check, null returns {@code true} + * @return {@code true} if the input is null or false + * @since 2.3 + */ + public static boolean isNotTrue(final Boolean bool) { + return !isTrue(bool); + } + + /** + *
Checks if a {@code Boolean} value is {@code false}, + * handling {@code null} by returning {@code false}.
+ * + *+ * BooleanUtils.isFalse(Boolean.TRUE) = false + * BooleanUtils.isFalse(Boolean.FALSE) = true + * BooleanUtils.isFalse(null) = false + *+ * + * @param bool the boolean to check, null returns {@code false} + * @return {@code true} only if the input is non-null and false + * @since 2.1 + */ + public static boolean isFalse(final Boolean bool) { + return Boolean.FALSE.equals(bool); + } + + /** + *
Checks if a {@code Boolean} value is not {@code false}, + * handling {@code null} by returning {@code true}.
+ * + *+ * BooleanUtils.isNotFalse(Boolean.TRUE) = true + * BooleanUtils.isNotFalse(Boolean.FALSE) = false + * BooleanUtils.isNotFalse(null) = true + *+ * + * @param bool the boolean to check, null returns {@code true} + * @return {@code true} if the input is null or true + * @since 2.3 + */ + public static boolean isNotFalse(final Boolean bool) { + return !isFalse(bool); + } + + //----------------------------------------------------------------------- + /** + *
Converts a Boolean to a boolean handling {@code null} + * by returning {@code false}.
+ * + *+ * BooleanUtils.toBoolean(Boolean.TRUE) = true + * BooleanUtils.toBoolean(Boolean.FALSE) = false + * BooleanUtils.toBoolean(null) = false + *+ * + * @param bool the boolean to convert + * @return {@code true} or {@code false}, {@code null} returns {@code false} + */ + public static boolean toBoolean(final Boolean bool) { + return bool != null && bool.booleanValue(); + } + + /** + *
Converts a Boolean to a boolean handling {@code null}.
+ * + *+ * BooleanUtils.toBooleanDefaultIfNull(Boolean.TRUE, false) = true + * BooleanUtils.toBooleanDefaultIfNull(Boolean.FALSE, true) = false + * BooleanUtils.toBooleanDefaultIfNull(null, true) = true + *+ * + * @param bool the boolean to convert + * @param valueIfNull the boolean value to return if {@code null} + * @return {@code true} or {@code false} + */ + public static boolean toBooleanDefaultIfNull(final Boolean bool, final boolean valueIfNull) { + if (bool == null) { + return valueIfNull; + } + return bool.booleanValue(); + } + + // Integer to Boolean methods + //----------------------------------------------------------------------- + /** + *
Converts an int to a boolean using the convention that {@code zero} + * is {@code false}.
+ * + *+ * BooleanUtils.toBoolean(0) = false + * BooleanUtils.toBoolean(1) = true + * BooleanUtils.toBoolean(2) = true + *+ * + * @param value the int to convert + * @return {@code true} if non-zero, {@code false} + * if zero + */ + public static boolean toBoolean(final int value) { + return value != 0; + } + + /** + *
Converts an int to a Boolean using the convention that {@code zero} + * is {@code false}.
+ * + *+ * BooleanUtils.toBoolean(0) = Boolean.FALSE + * BooleanUtils.toBoolean(1) = Boolean.TRUE + * BooleanUtils.toBoolean(2) = Boolean.TRUE + *+ * + * @param value the int to convert + * @return Boolean.TRUE if non-zero, Boolean.FALSE if zero, + * {@code null} if {@code null} + */ + public static Boolean toBooleanObject(final int value) { + return value == 0 ? Boolean.FALSE : Boolean.TRUE; + } + + /** + *
Converts an Integer to a Boolean using the convention that {@code zero} + * is {@code false}.
+ * + *{@code null} will be converted to {@code null}.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.toBoolean(Integer.valueOf(0)) = Boolean.FALSE + * BooleanUtils.toBoolean(Integer.valueOf(1)) = Boolean.TRUE + * BooleanUtils.toBoolean(Integer.valueOf(null)) = null + *+ * + * @param value the Integer to convert + * @return Boolean.TRUE if non-zero, Boolean.FALSE if zero, + * {@code null} if {@code null} input + */ + public static Boolean toBooleanObject(final Integer value) { + if (value == null) { + return null; + } + return value.intValue() == 0 ? Boolean.FALSE : Boolean.TRUE; + } + + /** + *
Converts an int to a boolean specifying the conversion values.
+ * + *+ * BooleanUtils.toBoolean(0, 1, 0) = false + * BooleanUtils.toBoolean(1, 1, 0) = true + * BooleanUtils.toBoolean(2, 1, 2) = false + * BooleanUtils.toBoolean(2, 2, 0) = true + *+ * + * @param value the Integer to convert + * @param trueValue the value to match for {@code true} + * @param falseValue the value to match for {@code false} + * @return {@code true} or {@code false} + * @throws IllegalArgumentException if no match + */ + public static boolean toBoolean(final int value, final int trueValue, final int falseValue) { + if (value == trueValue) { + return true; + } + if (value == falseValue) { + return false; + } + // no match + throw new IllegalArgumentException("The Integer did not match either specified value"); + } + + /** + *
Converts an Integer to a boolean specifying the conversion values.
+ * + *+ * BooleanUtils.toBoolean(Integer.valueOf(0), Integer.valueOf(1), Integer.valueOf(0)) = false + * BooleanUtils.toBoolean(Integer.valueOf(1), Integer.valueOf(1), Integer.valueOf(0)) = true + * BooleanUtils.toBoolean(Integer.valueOf(2), Integer.valueOf(1), Integer.valueOf(2)) = false + * BooleanUtils.toBoolean(Integer.valueOf(2), Integer.valueOf(2), Integer.valueOf(0)) = true + * BooleanUtils.toBoolean(null, null, Integer.valueOf(0)) = true + *+ * + * @param value the Integer to convert + * @param trueValue the value to match for {@code true}, may be {@code null} + * @param falseValue the value to match for {@code false}, may be {@code null} + * @return {@code true} or {@code false} + * @throws IllegalArgumentException if no match + */ + public static boolean toBoolean(final Integer value, final Integer trueValue, final Integer falseValue) { + if (value == null) { + if (trueValue == null) { + return true; + } + if (falseValue == null) { + return false; + } + } else if (value.equals(trueValue)) { + return true; + } else if (value.equals(falseValue)) { + return false; + } + // no match + throw new IllegalArgumentException("The Integer did not match either specified value"); + } + + /** + *
Converts an int to a Boolean specifying the conversion values.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.toBooleanObject(0, 0, 2, 3) = Boolean.TRUE + * BooleanUtils.toBooleanObject(2, 1, 2, 3) = Boolean.FALSE + * BooleanUtils.toBooleanObject(3, 1, 2, 3) = null + *+ * + * @param value the Integer to convert + * @param trueValue the value to match for {@code true} + * @param falseValue the value to match for {@code false} + * @param nullValue the value to to match for {@code null} + * @return Boolean.TRUE, Boolean.FALSE, or {@code null} + * @throws IllegalArgumentException if no match + */ + public static Boolean toBooleanObject(final int value, final int trueValue, final int falseValue, final int nullValue) { + if (value == trueValue) { + return Boolean.TRUE; + } + if (value == falseValue) { + return Boolean.FALSE; + } + if (value == nullValue) { + return null; + } + // no match + throw new IllegalArgumentException("The Integer did not match any specified value"); + } + + /** + *
Converts an Integer to a Boolean specifying the conversion values.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.toBooleanObject(Integer.valueOf(0), Integer.valueOf(0), Integer.valueOf(2), Integer.valueOf(3)) = Boolean.TRUE + * BooleanUtils.toBooleanObject(Integer.valueOf(2), Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(3)) = Boolean.FALSE + * BooleanUtils.toBooleanObject(Integer.valueOf(3), Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(3)) = null + *+ * + * @param value the Integer to convert + * @param trueValue the value to match for {@code true}, may be {@code null} + * @param falseValue the value to match for {@code false}, may be {@code null} + * @param nullValue the value to to match for {@code null}, may be {@code null} + * @return Boolean.TRUE, Boolean.FALSE, or {@code null} + * @throws IllegalArgumentException if no match + */ + public static Boolean toBooleanObject(final Integer value, final Integer trueValue, final Integer falseValue, final Integer nullValue) { + if (value == null) { + if (trueValue == null) { + return Boolean.TRUE; + } + if (falseValue == null) { + return Boolean.FALSE; + } + if (nullValue == null) { + return null; + } + } else if (value.equals(trueValue)) { + return Boolean.TRUE; + } else if (value.equals(falseValue)) { + return Boolean.FALSE; + } else if (value.equals(nullValue)) { + return null; + } + // no match + throw new IllegalArgumentException("The Integer did not match any specified value"); + } + + // Boolean to Integer methods + //----------------------------------------------------------------------- + /** + *
Converts a boolean to an int using the convention that + * {@code zero} is {@code false}.
+ * + *+ * BooleanUtils.toInteger(true) = 1 + * BooleanUtils.toInteger(false) = 0 + *+ * + * @param bool the boolean to convert + * @return one if {@code true}, zero if {@code false} + */ + public static int toInteger(final boolean bool) { + return bool ? 1 : 0; + } + + /** + *
Converts a boolean to an Integer using the convention that + * {@code zero} is {@code false}.
+ * + *+ * BooleanUtils.toIntegerObject(true) = Integer.valueOf(1) + * BooleanUtils.toIntegerObject(false) = Integer.valueOf(0) + *+ * + * @param bool the boolean to convert + * @return one if {@code true}, zero if {@code false} + */ + public static Integer toIntegerObject(final boolean bool) { + return bool ? NumberUtils.INTEGER_ONE : NumberUtils.INTEGER_ZERO; + } + + /** + *
Converts a Boolean to a Integer using the convention that + * {@code zero} is {@code false}.
+ * + *{@code null} will be converted to {@code null}.
+ * + *+ * BooleanUtils.toIntegerObject(Boolean.TRUE) = Integer.valueOf(1) + * BooleanUtils.toIntegerObject(Boolean.FALSE) = Integer.valueOf(0) + *+ * + * @param bool the Boolean to convert + * @return one if Boolean.TRUE, zero if Boolean.FALSE, {@code null} if {@code null} + */ + public static Integer toIntegerObject(final Boolean bool) { + if (bool == null) { + return null; + } + return bool.booleanValue() ? NumberUtils.INTEGER_ONE : NumberUtils.INTEGER_ZERO; + } + + /** + *
Converts a boolean to an int specifying the conversion values.
+ * + *+ * BooleanUtils.toInteger(true, 1, 0) = 1 + * BooleanUtils.toInteger(false, 1, 0) = 0 + *+ * + * @param bool the to convert + * @param trueValue the value to return if {@code true} + * @param falseValue the value to return if {@code false} + * @return the appropriate value + */ + public static int toInteger(final boolean bool, final int trueValue, final int falseValue) { + return bool ? trueValue : falseValue; + } + + /** + *
Converts a Boolean to an int specifying the conversion values.
+ * + *+ * BooleanUtils.toInteger(Boolean.TRUE, 1, 0, 2) = 1 + * BooleanUtils.toInteger(Boolean.FALSE, 1, 0, 2) = 0 + * BooleanUtils.toInteger(null, 1, 0, 2) = 2 + *+ * + * @param bool the Boolean to convert + * @param trueValue the value to return if {@code true} + * @param falseValue the value to return if {@code false} + * @param nullValue the value to return if {@code null} + * @return the appropriate value + */ + public static int toInteger(final Boolean bool, final int trueValue, final int falseValue, final int nullValue) { + if (bool == null) { + return nullValue; + } + return bool.booleanValue() ? trueValue : falseValue; + } + + /** + *
Converts a boolean to an Integer specifying the conversion values.
+ * + *+ * BooleanUtils.toIntegerObject(true, Integer.valueOf(1), Integer.valueOf(0)) = Integer.valueOf(1) + * BooleanUtils.toIntegerObject(false, Integer.valueOf(1), Integer.valueOf(0)) = Integer.valueOf(0) + *+ * + * @param bool the to convert + * @param trueValue the value to return if {@code true}, may be {@code null} + * @param falseValue the value to return if {@code false}, may be {@code null} + * @return the appropriate value + */ + public static Integer toIntegerObject(final boolean bool, final Integer trueValue, final Integer falseValue) { + return bool ? trueValue : falseValue; + } + + /** + *
Converts a Boolean to an Integer specifying the conversion values.
+ * + *+ * BooleanUtils.toIntegerObject(Boolean.TRUE, Integer.valueOf(1), Integer.valueOf(0), Integer.valueOf(2)) = Integer.valueOf(1) + * BooleanUtils.toIntegerObject(Boolean.FALSE, Integer.valueOf(1), Integer.valueOf(0), Integer.valueOf(2)) = Integer.valueOf(0) + * BooleanUtils.toIntegerObject(null, Integer.valueOf(1), Integer.valueOf(0), Integer.valueOf(2)) = Integer.valueOf(2) + *+ * + * @param bool the Boolean to convert + * @param trueValue the value to return if {@code true}, may be {@code null} + * @param falseValue the value to return if {@code false}, may be {@code null} + * @param nullValue the value to return if {@code null}, may be {@code null} + * @return the appropriate value + */ + public static Integer toIntegerObject(final Boolean bool, final Integer trueValue, final Integer falseValue, final Integer nullValue) { + if (bool == null) { + return nullValue; + } + return bool.booleanValue() ? trueValue : falseValue; + } + + // String to Boolean methods + //----------------------------------------------------------------------- + /** + *
Converts a String to a Boolean.
+ * + *{@code 'true'}, {@code 'on'}, {@code 'y'}, {@code 't'} or {@code 'yes'} + * (case insensitive) will return {@code true}. + * {@code 'false'}, {@code 'off'}, {@code 'n'}, {@code 'f'} or {@code 'no'} + * (case insensitive) will return {@code false}. + * Otherwise, {@code null} is returned.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * // N.B. case is not significant + * BooleanUtils.toBooleanObject(null) = null + * BooleanUtils.toBooleanObject("true") = Boolean.TRUE + * BooleanUtils.toBooleanObject("T") = Boolean.TRUE // i.e. T[RUE] + * BooleanUtils.toBooleanObject("false") = Boolean.FALSE + * BooleanUtils.toBooleanObject("f") = Boolean.FALSE // i.e. f[alse] + * BooleanUtils.toBooleanObject("No") = Boolean.FALSE + * BooleanUtils.toBooleanObject("n") = Boolean.FALSE // i.e. n[o] + * BooleanUtils.toBooleanObject("on") = Boolean.TRUE + * BooleanUtils.toBooleanObject("ON") = Boolean.TRUE + * BooleanUtils.toBooleanObject("off") = Boolean.FALSE + * BooleanUtils.toBooleanObject("oFf") = Boolean.FALSE + * BooleanUtils.toBooleanObject("yes") = Boolean.TRUE + * BooleanUtils.toBooleanObject("Y") = Boolean.TRUE // i.e. Y[ES] + * BooleanUtils.toBooleanObject("blue") = null + * BooleanUtils.toBooleanObject("true ") = null // trailing space (too long) + * BooleanUtils.toBooleanObject("ono") = null // does not match on or no + *+ * + * @param str the String to check; upper and lower case are treated as the same + * @return the Boolean value of the string, {@code null} if no match or {@code null} input + */ + public static Boolean toBooleanObject(final String str) { + // Previously used equalsIgnoreCase, which was fast for interned 'true'. + // Non interned 'true' matched 15 times slower. + // + // Optimisation provides same performance as before for interned 'true'. + // Similar performance for null, 'false', and other strings not length 2/3/4. + // 'true'/'TRUE' match 4 times slower, 'tRUE'/'True' 7 times slower. + if (str == "true") { + return Boolean.TRUE; + } + if (str == null) { + return null; + } + switch (str.length()) { + case 1: { + final char ch0 = str.charAt(0); + if (ch0 == 'y' || ch0 == 'Y' || + ch0 == 't' || ch0 == 'T') { + return Boolean.TRUE; + } + if (ch0 == 'n' || ch0 == 'N' || + ch0 == 'f' || ch0 == 'F') { + return Boolean.FALSE; + } + break; + } + case 2: { + final char ch0 = str.charAt(0); + final char ch1 = str.charAt(1); + if ((ch0 == 'o' || ch0 == 'O') && + (ch1 == 'n' || ch1 == 'N') ) { + return Boolean.TRUE; + } + if ((ch0 == 'n' || ch0 == 'N') && + (ch1 == 'o' || ch1 == 'O') ) { + return Boolean.FALSE; + } + break; + } + case 3: { + final char ch0 = str.charAt(0); + final char ch1 = str.charAt(1); + final char ch2 = str.charAt(2); + if ((ch0 == 'y' || ch0 == 'Y') && + (ch1 == 'e' || ch1 == 'E') && + (ch2 == 's' || ch2 == 'S') ) { + return Boolean.TRUE; + } + if ((ch0 == 'o' || ch0 == 'O') && + (ch1 == 'f' || ch1 == 'F') && + (ch2 == 'f' || ch2 == 'F') ) { + return Boolean.FALSE; + } + break; + } + case 4: { + final char ch0 = str.charAt(0); + final char ch1 = str.charAt(1); + final char ch2 = str.charAt(2); + final char ch3 = str.charAt(3); + if ((ch0 == 't' || ch0 == 'T') && + (ch1 == 'r' || ch1 == 'R') && + (ch2 == 'u' || ch2 == 'U') && + (ch3 == 'e' || ch3 == 'E') ) { + return Boolean.TRUE; + } + break; + } + case 5: { + final char ch0 = str.charAt(0); + final char ch1 = str.charAt(1); + final char ch2 = str.charAt(2); + final char ch3 = str.charAt(3); + final char ch4 = str.charAt(4); + if ((ch0 == 'f' || ch0 == 'F') && + (ch1 == 'a' || ch1 == 'A') && + (ch2 == 'l' || ch2 == 'L') && + (ch3 == 's' || ch3 == 'S') && + (ch4 == 'e' || ch4 == 'E') ) { + return Boolean.FALSE; + } + break; + } + default: + break; + } + + return null; + } + + /** + *
Converts a String to a Boolean throwing an exception if no match.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.toBooleanObject("true", "true", "false", "null") = Boolean.TRUE + * BooleanUtils.toBooleanObject("false", "true", "false", "null") = Boolean.FALSE + * BooleanUtils.toBooleanObject("null", "true", "false", "null") = null + *+ * + * @param str the String to check + * @param trueString the String to match for {@code true} (case sensitive), may be {@code null} + * @param falseString the String to match for {@code false} (case sensitive), may be {@code null} + * @param nullString the String to match for {@code null} (case sensitive), may be {@code null} + * @return the Boolean value of the string, {@code null} if either the String matches {@code nullString} + * or if {@code null} input and {@code nullString} is {@code null} + * @throws IllegalArgumentException if the String doesn't match + */ + public static Boolean toBooleanObject(final String str, final String trueString, final String falseString, final String nullString) { + if (str == null) { + if (trueString == null) { + return Boolean.TRUE; + } + if (falseString == null) { + return Boolean.FALSE; + } + if (nullString == null) { + return null; + } + } else if (str.equals(trueString)) { + return Boolean.TRUE; + } else if (str.equals(falseString)) { + return Boolean.FALSE; + } else if (str.equals(nullString)) { + return null; + } + // no match + throw new IllegalArgumentException("The String did not match any specified value"); + } + + // String to boolean methods + //----------------------------------------------------------------------- + /** + *
Converts a String to a boolean (optimised for performance).
+ * + *{@code 'true'}, {@code 'on'}, {@code 'y'}, {@code 't'} or {@code 'yes'} + * (case insensitive) will return {@code true}. Otherwise, + * {@code false} is returned.
+ * + *This method performs 4 times faster (JDK1.4) than + * {@code Boolean.valueOf(String)}. However, this method accepts + * 'on' and 'yes', 't', 'y' as true values. + * + *
+ * BooleanUtils.toBoolean(null) = false + * BooleanUtils.toBoolean("true") = true + * BooleanUtils.toBoolean("TRUE") = true + * BooleanUtils.toBoolean("tRUe") = true + * BooleanUtils.toBoolean("on") = true + * BooleanUtils.toBoolean("yes") = true + * BooleanUtils.toBoolean("false") = false + * BooleanUtils.toBoolean("x gti") = false + * BooleanUtils.toBooleanObject("y") = true + * BooleanUtils.toBooleanObject("n") = false + * BooleanUtils.toBooleanObject("t") = true + * BooleanUtils.toBooleanObject("f") = false + *+ * + * @param str the String to check + * @return the boolean value of the string, {@code false} if no match or the String is null + */ + public static boolean toBoolean(final String str) { + return toBooleanObject(str) == Boolean.TRUE; + } + + /** + *
Converts a String to a Boolean throwing an exception if no match found.
+ * + *+ * BooleanUtils.toBoolean("true", "true", "false") = true + * BooleanUtils.toBoolean("false", "true", "false") = false + *+ * + * @param str the String to check + * @param trueString the String to match for {@code true} (case sensitive), may be {@code null} + * @param falseString the String to match for {@code false} (case sensitive), may be {@code null} + * @return the boolean value of the string + * @throws IllegalArgumentException if the String doesn't match + */ + public static boolean toBoolean(final String str, final String trueString, final String falseString) { + if (str == trueString) { + return true; + } else if (str == falseString) { + return false; + } else if (str != null) { + if (str.equals(trueString)) { + return true; + } else if (str.equals(falseString)) { + return false; + } + } + // no match + throw new IllegalArgumentException("The String did not match either specified value"); + } + + // Boolean to String methods + //----------------------------------------------------------------------- + /** + *
Converts a Boolean to a String returning {@code 'true'}, + * {@code 'false'}, or {@code null}.
+ * + *+ * BooleanUtils.toStringTrueFalse(Boolean.TRUE) = "true" + * BooleanUtils.toStringTrueFalse(Boolean.FALSE) = "false" + * BooleanUtils.toStringTrueFalse(null) = null; + *+ * + * @param bool the Boolean to check + * @return {@code 'true'}, {@code 'false'}, or {@code null} + */ + public static String toStringTrueFalse(final Boolean bool) { + return toString(bool, "true", "false", null); + } + + /** + *
Converts a Boolean to a String returning {@code 'on'}, + * {@code 'off'}, or {@code null}.
+ * + *+ * BooleanUtils.toStringOnOff(Boolean.TRUE) = "on" + * BooleanUtils.toStringOnOff(Boolean.FALSE) = "off" + * BooleanUtils.toStringOnOff(null) = null; + *+ * + * @param bool the Boolean to check + * @return {@code 'on'}, {@code 'off'}, or {@code null} + */ + public static String toStringOnOff(final Boolean bool) { + return toString(bool, "on", "off", null); + } + + /** + *
Converts a Boolean to a String returning {@code 'yes'}, + * {@code 'no'}, or {@code null}.
+ * + *+ * BooleanUtils.toStringYesNo(Boolean.TRUE) = "yes" + * BooleanUtils.toStringYesNo(Boolean.FALSE) = "no" + * BooleanUtils.toStringYesNo(null) = null; + *+ * + * @param bool the Boolean to check + * @return {@code 'yes'}, {@code 'no'}, or {@code null} + */ + public static String toStringYesNo(final Boolean bool) { + return toString(bool, "yes", "no", null); + } + + /** + *
Converts a Boolean to a String returning one of the input Strings.
+ * + *+ * BooleanUtils.toString(Boolean.TRUE, "true", "false", null) = "true" + * BooleanUtils.toString(Boolean.FALSE, "true", "false", null) = "false" + * BooleanUtils.toString(null, "true", "false", null) = null; + *+ * + * @param bool the Boolean to check + * @param trueString the String to return if {@code true}, may be {@code null} + * @param falseString the String to return if {@code false}, may be {@code null} + * @param nullString the String to return if {@code null}, may be {@code null} + * @return one of the three input Strings + */ +/** + *
Converts a Boolean to a String returning one of the input Strings.
+ * + *+ * BooleanUtils.toString(Boolean.TRUE, "true", "false", null) = "true" + * BooleanUtils.toString(Boolean.FALSE, "true", "false", null) = "false" + * BooleanUtils.toString(null, "true", "false", null) = null; + *+ * + * @param bool + * the Boolean to check + * @param trueString + * the String to return if {@code true}, may be {@code null} + * @param falseString + * the String to return if {@code false}, may be {@code null} + * @param nullString + * the String to return if {@code null}, may be {@code null} + * @return one of the three input Strings + */ +public static java.lang.String toString(final java.lang.Boolean bool, final java.lang.String trueString, final java.lang.String falseString, final java.lang.String nullString) { + { + return /* NPEX_NULL_EXP */ + bool.booleanValue() ? trueString : falseString; + } +} + + // boolean to String methods + //----------------------------------------------------------------------- + /** + *
Converts a boolean to a String returning {@code 'true'} + * or {@code 'false'}.
+ * + *+ * BooleanUtils.toStringTrueFalse(true) = "true" + * BooleanUtils.toStringTrueFalse(false) = "false" + *+ * + * @param bool the Boolean to check + * @return {@code 'true'}, {@code 'false'}, or {@code null} + */ + public static String toStringTrueFalse(final boolean bool) { + return toString(bool, "true", "false"); + } + + /** + *
Converts a boolean to a String returning {@code 'on'} + * or {@code 'off'}.
+ * + *+ * BooleanUtils.toStringOnOff(true) = "on" + * BooleanUtils.toStringOnOff(false) = "off" + *+ * + * @param bool the Boolean to check + * @return {@code 'on'}, {@code 'off'}, or {@code null} + */ + public static String toStringOnOff(final boolean bool) { + return toString(bool, "on", "off"); + } + + /** + *
Converts a boolean to a String returning {@code 'yes'} + * or {@code 'no'}.
+ * + *+ * BooleanUtils.toStringYesNo(true) = "yes" + * BooleanUtils.toStringYesNo(false) = "no" + *+ * + * @param bool the Boolean to check + * @return {@code 'yes'}, {@code 'no'}, or {@code null} + */ + public static String toStringYesNo(final boolean bool) { + return toString(bool, "yes", "no"); + } + + /** + *
Converts a boolean to a String returning one of the input Strings.
+ * + *+ * BooleanUtils.toString(true, "true", "false") = "true" + * BooleanUtils.toString(false, "true", "false") = "false" + *+ * + * @param bool the Boolean to check + * @param trueString the String to return if {@code true}, may be {@code null} + * @param falseString the String to return if {@code false}, may be {@code null} + * @return one of the two input Strings + */ + public static String toString(final boolean bool, final String trueString, final String falseString) { + return bool ? trueString : falseString; + } + + // logical operations + // ---------------------------------------------------------------------- + /** + *
Performs an and on a set of booleans.
+ * + *+ * BooleanUtils.and(true, true) = true + * BooleanUtils.and(false, false) = false + * BooleanUtils.and(true, false) = false + * BooleanUtils.and(true, true, false) = false + * BooleanUtils.and(true, true, true) = true + *+ * + * @param array an array of {@code boolean}s + * @return {@code true} if the and is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + * @since 3.0.1 + */ + public static boolean and(final boolean... array) { + // Validates input + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + for (final boolean element : array) { + if (!element) { + return false; + } + } + return true; + } + + /** + *
Performs an and on an array of Booleans.
+ * + *+ * BooleanUtils.and(Boolean.TRUE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.and(Boolean.FALSE, Boolean.FALSE) = Boolean.FALSE + * BooleanUtils.and(Boolean.TRUE, Boolean.FALSE) = Boolean.FALSE + * BooleanUtils.and(Boolean.TRUE, Boolean.TRUE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.and(Boolean.FALSE, Boolean.FALSE, Boolean.TRUE) = Boolean.FALSE + * BooleanUtils.and(Boolean.TRUE, Boolean.FALSE, Boolean.TRUE) = Boolean.FALSE + *+ * + * @param array an array of {@code Boolean}s + * @return {@code true} if the and is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + * @throws IllegalArgumentException if {@code array} contains a {@code null} + * @since 3.0.1 + */ + public static Boolean and(final Boolean... array) { + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + try { + final boolean[] primitive = ArrayUtils.toPrimitive(array); + return and(primitive) ? Boolean.TRUE : Boolean.FALSE; + } catch (final NullPointerException ex) { + throw new IllegalArgumentException("The array must not contain any null elements"); + } + } + + /** + *
Performs an or on a set of booleans.
+ * + *+ * BooleanUtils.or(true, true) = true + * BooleanUtils.or(false, false) = false + * BooleanUtils.or(true, false) = true + * BooleanUtils.or(true, true, false) = true + * BooleanUtils.or(true, true, true) = true + * BooleanUtils.or(false, false, false) = false + *+ * + * @param array an array of {@code boolean}s + * @return {@code true} if the or is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + * @since 3.0.1 + */ + public static boolean or(final boolean... array) { + // Validates input + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + for (final boolean element : array) { + if (element) { + return true; + } + } + return false; + } + + /** + *
Performs an or on an array of Booleans.
+ * + *+ * BooleanUtils.or(Boolean.TRUE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.or(Boolean.FALSE, Boolean.FALSE) = Boolean.FALSE + * BooleanUtils.or(Boolean.TRUE, Boolean.FALSE) = Boolean.TRUE + * BooleanUtils.or(Boolean.TRUE, Boolean.TRUE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.or(Boolean.FALSE, Boolean.FALSE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.or(Boolean.TRUE, Boolean.FALSE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.or(Boolean.FALSE, Boolean.FALSE, Boolean.FALSE) = Boolean.FALSE + *+ * + * @param array an array of {@code Boolean}s + * @return {@code true} if the or is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + * @throws IllegalArgumentException if {@code array} contains a {@code null} + * @since 3.0.1 + */ + public static Boolean or(final Boolean... array) { + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + try { + final boolean[] primitive = ArrayUtils.toPrimitive(array); + return or(primitive) ? Boolean.TRUE : Boolean.FALSE; + } catch (final NullPointerException ex) { + throw new IllegalArgumentException("The array must not contain any null elements"); + } + } + + /** + *
Performs an xor on a set of booleans.
+ * + *+ * BooleanUtils.xor(true, true) = false + * BooleanUtils.xor(false, false) = false + * BooleanUtils.xor(true, false) = true + * BooleanUtils.xor(true, true) = false + * BooleanUtils.xor(false, false) = false + * BooleanUtils.xor(true, false) = true + *+ * + * @param array an array of {@code boolean}s + * @return {@code true} if the xor is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + */ + public static boolean xor(final boolean... array) { + // Validates input + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + + // false if the neutral element of the xor operator + boolean result = false; + for (final boolean element : array) { + result ^= element; + } + + return result; + } + + /** + *
Performs an xor on an array of Booleans.
+ * + *+ * BooleanUtils.xor(new Boolean[] { Boolean.TRUE, Boolean.TRUE }) = Boolean.FALSE + * BooleanUtils.xor(new Boolean[] { Boolean.FALSE, Boolean.FALSE }) = Boolean.FALSE + * BooleanUtils.xor(new Boolean[] { Boolean.TRUE, Boolean.FALSE }) = Boolean.TRUE + *+ * + * @param array an array of {@code Boolean}s + * @return {@code true} if the xor is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + * @throws IllegalArgumentException if {@code array} contains a {@code null} + */ + public static Boolean xor(final Boolean... array) { + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + try { + final boolean[] primitive = ArrayUtils.toPrimitive(array); + return xor(primitive) ? Boolean.TRUE : Boolean.FALSE; + } catch (final NullPointerException ex) { + throw new IllegalArgumentException("The array must not contain any null elements"); + } + } + + /** + *
Compares two {@code boolean} values. This is the same functionality as provided in Java 7.
+ * + * @param x the first {@code boolean} to compare + * @param y the second {@code boolean} to compare + * @return the value {@code 0} if {@code x == y}; + * a value less than {@code 0} if {@code !x && y}; and + * a value greater than {@code 0} if {@code x && !y} + * @since 3.4 + */ + public static int compare(boolean x, boolean y) { + if (x == y) { + return 0; + } + return x ? 1 : -1; + } + +} diff --git a/Java/commons-lang-BooleanUtils_811/metadata.json b/Java/commons-lang-BooleanUtils_811/metadata.json new file mode 100644 index 000000000..41aeb7416 --- /dev/null +++ b/Java/commons-lang-BooleanUtils_811/metadata.json @@ -0,0 +1,21 @@ +{ + "language": "java", + "id": "commons-lang-BooleanUtils_811", + "buggyPath": ".", + "referencePath": null, + "buildCommand": "mvn package -V -B -Denforcer.skip=true -Dcheckstyle.skip=true -Dcobertura.skip=true -Drat.skip=true -Dlicense.skip=true -Dfindbugs.skip=true -Dgpg.skip=true -Dskip.npm=true -Dskip.gulp=true -Dskip.bower=true -Drat.numUnapprovedLicenses=100 -DskipTests=true -DskipITs=true -Dtest=None -DfailIfNoTests=false", + "testCommand": "mvn test -V -B -Denforcer.skip=true -Dcheckstyle.skip=true -Dcobertura.skip=true -Drat.skip=true -Dlicense.skip=true -Dfindbugs.skip=true -Dgpg.skip=true -Dskip.npm=true -Dskip.gulp=true -Dskip.bower=true -Drat.numUnapprovedLicenses=100", + "categories": [ + "safety", + "npe" + ], + "npe": { + "filepath": "src/main/java/org/apache/commons/lang3/BooleanUtils.java", + "line": 832, + "npe_method": "toString", + "deref_field": "bool", + "npe_class": "BooleanUtils", + "repo": "commons-lang", + "bug_id": "BooleanUtils_811" + } +} diff --git a/Java/commons-lang-BooleanUtils_811/npe.json b/Java/commons-lang-BooleanUtils_811/npe.json new file mode 100644 index 000000000..c97ba7df0 --- /dev/null +++ b/Java/commons-lang-BooleanUtils_811/npe.json @@ -0,0 +1,7 @@ +{ + "filepath": "src/main/java/org/apache/commons/lang3/BooleanUtils.java", + "line": 832, + "npe_method": "toString", + "deref_field": "bool", + "npe_class": "BooleanUtils" +} \ No newline at end of file diff --git a/Java/commons-lang-BooleanUtils_905/Dockerfile b/Java/commons-lang-BooleanUtils_905/Dockerfile new file mode 100644 index 000000000..7b7fbe349 --- /dev/null +++ b/Java/commons-lang-BooleanUtils_905/Dockerfile @@ -0,0 +1,18 @@ +FROM ghcr.io/kupl/starlab-benchmarks/java-base:commons-lang + +ENV TZ=Asia/Seoul + +COPY ./metadata.json . +COPY ./npe.json . +COPY ./buggy.java /tmp/buggy.java +RUN export BUGGY_PATH=$(cat metadata.json | jq -r ".npe.filepath") \ + && export BUGGY_LINE=$(cat metadata.json | jq -r ".npe.line") \ + && export BUGGY_MTHD=$(cat metadata.json | jq -r ".npe.npe_method") \ + && mv /tmp/buggy.java $BUGGY_PATH \ + && echo "[{\"filepath\": \"$BUGGY_PATH\", \"line\": $BUGGY_LINE, \"method_name\": \"$BUGGY_MTHD\"}]" | jq . > traces.json + +RUN git init . && git add -A + +RUN $(cat metadata.json | jq -r ".buildCommand") + +RUN $(cat metadata.json | jq -r ".testCommand"); if [ $? -eq 0 ]; then exit 1; fi diff --git a/Java/commons-lang-BooleanUtils_905/buggy.java b/Java/commons-lang-BooleanUtils_905/buggy.java new file mode 100644 index 000000000..9da6f2193 --- /dev/null +++ b/Java/commons-lang-BooleanUtils_905/buggy.java @@ -0,0 +1,1125 @@ +/* + * 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; + +import org.apache.commons.lang3.math.NumberUtils; + +/** + *Operations on boolean primitives and Boolean objects.
+ * + *This class tries to handle {@code null} input gracefully. + * An exception will not be thrown for a {@code null} input. + * Each method documents its behaviour in more detail.
+ * + *#ThreadSafe#
+ * @since 2.0 + */ +public class BooleanUtils { + + /** + *{@code BooleanUtils} instances should NOT be constructed in standard programming. + * Instead, the class should be used as {@code BooleanUtils.negate(true);}.
+ * + *This constructor is public to permit tools that require a JavaBean instance + * to operate.
+ */ + public BooleanUtils() { + super(); + } + + // Boolean utilities + //-------------------------------------------------------------------------- + /** + *Negates the specified boolean.
+ * + *If {@code null} is passed in, {@code null} will be returned.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.negate(Boolean.TRUE) = Boolean.FALSE; + * BooleanUtils.negate(Boolean.FALSE) = Boolean.TRUE; + * BooleanUtils.negate(null) = null; + *+ * + * @param bool the Boolean to negate, may be null + * @return the negated Boolean, or {@code null} if {@code null} input + */ + public static Boolean negate(final Boolean bool) { + if (bool == null) { + return null; + } + return bool.booleanValue() ? Boolean.FALSE : Boolean.TRUE; + } + + // boolean Boolean methods + //----------------------------------------------------------------------- + /** + *
Checks if a {@code Boolean} value is {@code true}, + * handling {@code null} by returning {@code false}.
+ * + *+ * BooleanUtils.isTrue(Boolean.TRUE) = true + * BooleanUtils.isTrue(Boolean.FALSE) = false + * BooleanUtils.isTrue(null) = false + *+ * + * @param bool the boolean to check, null returns {@code false} + * @return {@code true} only if the input is non-null and true + * @since 2.1 + */ + public static boolean isTrue(final Boolean bool) { + return Boolean.TRUE.equals(bool); + } + + /** + *
Checks if a {@code Boolean} value is not {@code true}, + * handling {@code null} by returning {@code true}.
+ * + *+ * BooleanUtils.isNotTrue(Boolean.TRUE) = false + * BooleanUtils.isNotTrue(Boolean.FALSE) = true + * BooleanUtils.isNotTrue(null) = true + *+ * + * @param bool the boolean to check, null returns {@code true} + * @return {@code true} if the input is null or false + * @since 2.3 + */ + public static boolean isNotTrue(final Boolean bool) { + return !isTrue(bool); + } + + /** + *
Checks if a {@code Boolean} value is {@code false}, + * handling {@code null} by returning {@code false}.
+ * + *+ * BooleanUtils.isFalse(Boolean.TRUE) = false + * BooleanUtils.isFalse(Boolean.FALSE) = true + * BooleanUtils.isFalse(null) = false + *+ * + * @param bool the boolean to check, null returns {@code false} + * @return {@code true} only if the input is non-null and false + * @since 2.1 + */ + public static boolean isFalse(final Boolean bool) { + return Boolean.FALSE.equals(bool); + } + + /** + *
Checks if a {@code Boolean} value is not {@code false}, + * handling {@code null} by returning {@code true}.
+ * + *+ * BooleanUtils.isNotFalse(Boolean.TRUE) = true + * BooleanUtils.isNotFalse(Boolean.FALSE) = false + * BooleanUtils.isNotFalse(null) = true + *+ * + * @param bool the boolean to check, null returns {@code true} + * @return {@code true} if the input is null or true + * @since 2.3 + */ + public static boolean isNotFalse(final Boolean bool) { + return !isFalse(bool); + } + + //----------------------------------------------------------------------- + /** + *
Converts a Boolean to a boolean handling {@code null} + * by returning {@code false}.
+ * + *+ * BooleanUtils.toBoolean(Boolean.TRUE) = true + * BooleanUtils.toBoolean(Boolean.FALSE) = false + * BooleanUtils.toBoolean(null) = false + *+ * + * @param bool the boolean to convert + * @return {@code true} or {@code false}, {@code null} returns {@code false} + */ + public static boolean toBoolean(final Boolean bool) { + return bool != null && bool.booleanValue(); + } + + /** + *
Converts a Boolean to a boolean handling {@code null}.
+ * + *+ * BooleanUtils.toBooleanDefaultIfNull(Boolean.TRUE, false) = true + * BooleanUtils.toBooleanDefaultIfNull(Boolean.FALSE, true) = false + * BooleanUtils.toBooleanDefaultIfNull(null, true) = true + *+ * + * @param bool the boolean to convert + * @param valueIfNull the boolean value to return if {@code null} + * @return {@code true} or {@code false} + */ + public static boolean toBooleanDefaultIfNull(final Boolean bool, final boolean valueIfNull) { + if (bool == null) { + return valueIfNull; + } + return bool.booleanValue(); + } + + // Integer to Boolean methods + //----------------------------------------------------------------------- + /** + *
Converts an int to a boolean using the convention that {@code zero} + * is {@code false}.
+ * + *+ * BooleanUtils.toBoolean(0) = false + * BooleanUtils.toBoolean(1) = true + * BooleanUtils.toBoolean(2) = true + *+ * + * @param value the int to convert + * @return {@code true} if non-zero, {@code false} + * if zero + */ + public static boolean toBoolean(final int value) { + return value != 0; + } + + /** + *
Converts an int to a Boolean using the convention that {@code zero} + * is {@code false}.
+ * + *+ * BooleanUtils.toBoolean(0) = Boolean.FALSE + * BooleanUtils.toBoolean(1) = Boolean.TRUE + * BooleanUtils.toBoolean(2) = Boolean.TRUE + *+ * + * @param value the int to convert + * @return Boolean.TRUE if non-zero, Boolean.FALSE if zero, + * {@code null} if {@code null} + */ + public static Boolean toBooleanObject(final int value) { + return value == 0 ? Boolean.FALSE : Boolean.TRUE; + } + + /** + *
Converts an Integer to a Boolean using the convention that {@code zero} + * is {@code false}.
+ * + *{@code null} will be converted to {@code null}.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.toBoolean(Integer.valueOf(0)) = Boolean.FALSE + * BooleanUtils.toBoolean(Integer.valueOf(1)) = Boolean.TRUE + * BooleanUtils.toBoolean(Integer.valueOf(null)) = null + *+ * + * @param value the Integer to convert + * @return Boolean.TRUE if non-zero, Boolean.FALSE if zero, + * {@code null} if {@code null} input + */ + public static Boolean toBooleanObject(final Integer value) { + if (value == null) { + return null; + } + return value.intValue() == 0 ? Boolean.FALSE : Boolean.TRUE; + } + + /** + *
Converts an int to a boolean specifying the conversion values.
+ * + *+ * BooleanUtils.toBoolean(0, 1, 0) = false + * BooleanUtils.toBoolean(1, 1, 0) = true + * BooleanUtils.toBoolean(2, 1, 2) = false + * BooleanUtils.toBoolean(2, 2, 0) = true + *+ * + * @param value the Integer to convert + * @param trueValue the value to match for {@code true} + * @param falseValue the value to match for {@code false} + * @return {@code true} or {@code false} + * @throws IllegalArgumentException if no match + */ + public static boolean toBoolean(final int value, final int trueValue, final int falseValue) { + if (value == trueValue) { + return true; + } + if (value == falseValue) { + return false; + } + // no match + throw new IllegalArgumentException("The Integer did not match either specified value"); + } + + /** + *
Converts an Integer to a boolean specifying the conversion values.
+ * + *+ * BooleanUtils.toBoolean(Integer.valueOf(0), Integer.valueOf(1), Integer.valueOf(0)) = false + * BooleanUtils.toBoolean(Integer.valueOf(1), Integer.valueOf(1), Integer.valueOf(0)) = true + * BooleanUtils.toBoolean(Integer.valueOf(2), Integer.valueOf(1), Integer.valueOf(2)) = false + * BooleanUtils.toBoolean(Integer.valueOf(2), Integer.valueOf(2), Integer.valueOf(0)) = true + * BooleanUtils.toBoolean(null, null, Integer.valueOf(0)) = true + *+ * + * @param value the Integer to convert + * @param trueValue the value to match for {@code true}, may be {@code null} + * @param falseValue the value to match for {@code false}, may be {@code null} + * @return {@code true} or {@code false} + * @throws IllegalArgumentException if no match + */ + public static boolean toBoolean(final Integer value, final Integer trueValue, final Integer falseValue) { + if (value == null) { + if (trueValue == null) { + return true; + } + if (falseValue == null) { + return false; + } + } else if (value.equals(trueValue)) { + return true; + } else if (value.equals(falseValue)) { + return false; + } + // no match + throw new IllegalArgumentException("The Integer did not match either specified value"); + } + + /** + *
Converts an int to a Boolean specifying the conversion values.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.toBooleanObject(0, 0, 2, 3) = Boolean.TRUE + * BooleanUtils.toBooleanObject(2, 1, 2, 3) = Boolean.FALSE + * BooleanUtils.toBooleanObject(3, 1, 2, 3) = null + *+ * + * @param value the Integer to convert + * @param trueValue the value to match for {@code true} + * @param falseValue the value to match for {@code false} + * @param nullValue the value to to match for {@code null} + * @return Boolean.TRUE, Boolean.FALSE, or {@code null} + * @throws IllegalArgumentException if no match + */ + public static Boolean toBooleanObject(final int value, final int trueValue, final int falseValue, final int nullValue) { + if (value == trueValue) { + return Boolean.TRUE; + } + if (value == falseValue) { + return Boolean.FALSE; + } + if (value == nullValue) { + return null; + } + // no match + throw new IllegalArgumentException("The Integer did not match any specified value"); + } + + /** + *
Converts an Integer to a Boolean specifying the conversion values.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.toBooleanObject(Integer.valueOf(0), Integer.valueOf(0), Integer.valueOf(2), Integer.valueOf(3)) = Boolean.TRUE + * BooleanUtils.toBooleanObject(Integer.valueOf(2), Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(3)) = Boolean.FALSE + * BooleanUtils.toBooleanObject(Integer.valueOf(3), Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(3)) = null + *+ * + * @param value the Integer to convert + * @param trueValue the value to match for {@code true}, may be {@code null} + * @param falseValue the value to match for {@code false}, may be {@code null} + * @param nullValue the value to to match for {@code null}, may be {@code null} + * @return Boolean.TRUE, Boolean.FALSE, or {@code null} + * @throws IllegalArgumentException if no match + */ + public static Boolean toBooleanObject(final Integer value, final Integer trueValue, final Integer falseValue, final Integer nullValue) { + if (value == null) { + if (trueValue == null) { + return Boolean.TRUE; + } + if (falseValue == null) { + return Boolean.FALSE; + } + if (nullValue == null) { + return null; + } + } else if (value.equals(trueValue)) { + return Boolean.TRUE; + } else if (value.equals(falseValue)) { + return Boolean.FALSE; + } else if (value.equals(nullValue)) { + return null; + } + // no match + throw new IllegalArgumentException("The Integer did not match any specified value"); + } + + // Boolean to Integer methods + //----------------------------------------------------------------------- + /** + *
Converts a boolean to an int using the convention that + * {@code zero} is {@code false}.
+ * + *+ * BooleanUtils.toInteger(true) = 1 + * BooleanUtils.toInteger(false) = 0 + *+ * + * @param bool the boolean to convert + * @return one if {@code true}, zero if {@code false} + */ + public static int toInteger(final boolean bool) { + return bool ? 1 : 0; + } + + /** + *
Converts a boolean to an Integer using the convention that + * {@code zero} is {@code false}.
+ * + *+ * BooleanUtils.toIntegerObject(true) = Integer.valueOf(1) + * BooleanUtils.toIntegerObject(false) = Integer.valueOf(0) + *+ * + * @param bool the boolean to convert + * @return one if {@code true}, zero if {@code false} + */ + public static Integer toIntegerObject(final boolean bool) { + return bool ? NumberUtils.INTEGER_ONE : NumberUtils.INTEGER_ZERO; + } + + /** + *
Converts a Boolean to a Integer using the convention that + * {@code zero} is {@code false}.
+ * + *{@code null} will be converted to {@code null}.
+ * + *+ * BooleanUtils.toIntegerObject(Boolean.TRUE) = Integer.valueOf(1) + * BooleanUtils.toIntegerObject(Boolean.FALSE) = Integer.valueOf(0) + *+ * + * @param bool the Boolean to convert + * @return one if Boolean.TRUE, zero if Boolean.FALSE, {@code null} if {@code null} + */ + public static Integer toIntegerObject(final Boolean bool) { + if (bool == null) { + return null; + } + return bool.booleanValue() ? NumberUtils.INTEGER_ONE : NumberUtils.INTEGER_ZERO; + } + + /** + *
Converts a boolean to an int specifying the conversion values.
+ * + *+ * BooleanUtils.toInteger(true, 1, 0) = 1 + * BooleanUtils.toInteger(false, 1, 0) = 0 + *+ * + * @param bool the to convert + * @param trueValue the value to return if {@code true} + * @param falseValue the value to return if {@code false} + * @return the appropriate value + */ + public static int toInteger(final boolean bool, final int trueValue, final int falseValue) { + return bool ? trueValue : falseValue; + } + + /** + *
Converts a Boolean to an int specifying the conversion values.
+ * + *+ * BooleanUtils.toInteger(Boolean.TRUE, 1, 0, 2) = 1 + * BooleanUtils.toInteger(Boolean.FALSE, 1, 0, 2) = 0 + * BooleanUtils.toInteger(null, 1, 0, 2) = 2 + *+ * + * @param bool the Boolean to convert + * @param trueValue the value to return if {@code true} + * @param falseValue the value to return if {@code false} + * @param nullValue the value to return if {@code null} + * @return the appropriate value + */ + public static int toInteger(final Boolean bool, final int trueValue, final int falseValue, final int nullValue) { + if (bool == null) { + return nullValue; + } + return bool.booleanValue() ? trueValue : falseValue; + } + + /** + *
Converts a boolean to an Integer specifying the conversion values.
+ * + *+ * BooleanUtils.toIntegerObject(true, Integer.valueOf(1), Integer.valueOf(0)) = Integer.valueOf(1) + * BooleanUtils.toIntegerObject(false, Integer.valueOf(1), Integer.valueOf(0)) = Integer.valueOf(0) + *+ * + * @param bool the to convert + * @param trueValue the value to return if {@code true}, may be {@code null} + * @param falseValue the value to return if {@code false}, may be {@code null} + * @return the appropriate value + */ + public static Integer toIntegerObject(final boolean bool, final Integer trueValue, final Integer falseValue) { + return bool ? trueValue : falseValue; + } + + /** + *
Converts a Boolean to an Integer specifying the conversion values.
+ * + *+ * BooleanUtils.toIntegerObject(Boolean.TRUE, Integer.valueOf(1), Integer.valueOf(0), Integer.valueOf(2)) = Integer.valueOf(1) + * BooleanUtils.toIntegerObject(Boolean.FALSE, Integer.valueOf(1), Integer.valueOf(0), Integer.valueOf(2)) = Integer.valueOf(0) + * BooleanUtils.toIntegerObject(null, Integer.valueOf(1), Integer.valueOf(0), Integer.valueOf(2)) = Integer.valueOf(2) + *+ * + * @param bool the Boolean to convert + * @param trueValue the value to return if {@code true}, may be {@code null} + * @param falseValue the value to return if {@code false}, may be {@code null} + * @param nullValue the value to return if {@code null}, may be {@code null} + * @return the appropriate value + */ + public static Integer toIntegerObject(final Boolean bool, final Integer trueValue, final Integer falseValue, final Integer nullValue) { + if (bool == null) { + return nullValue; + } + return bool.booleanValue() ? trueValue : falseValue; + } + + // String to Boolean methods + //----------------------------------------------------------------------- + /** + *
Converts a String to a Boolean.
+ * + *{@code 'true'}, {@code 'on'}, {@code 'y'}, {@code 't'} or {@code 'yes'} + * (case insensitive) will return {@code true}. + * {@code 'false'}, {@code 'off'}, {@code 'n'}, {@code 'f'} or {@code 'no'} + * (case insensitive) will return {@code false}. + * Otherwise, {@code null} is returned.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * // N.B. case is not significant + * BooleanUtils.toBooleanObject(null) = null + * BooleanUtils.toBooleanObject("true") = Boolean.TRUE + * BooleanUtils.toBooleanObject("T") = Boolean.TRUE // i.e. T[RUE] + * BooleanUtils.toBooleanObject("false") = Boolean.FALSE + * BooleanUtils.toBooleanObject("f") = Boolean.FALSE // i.e. f[alse] + * BooleanUtils.toBooleanObject("No") = Boolean.FALSE + * BooleanUtils.toBooleanObject("n") = Boolean.FALSE // i.e. n[o] + * BooleanUtils.toBooleanObject("on") = Boolean.TRUE + * BooleanUtils.toBooleanObject("ON") = Boolean.TRUE + * BooleanUtils.toBooleanObject("off") = Boolean.FALSE + * BooleanUtils.toBooleanObject("oFf") = Boolean.FALSE + * BooleanUtils.toBooleanObject("yes") = Boolean.TRUE + * BooleanUtils.toBooleanObject("Y") = Boolean.TRUE // i.e. Y[ES] + * BooleanUtils.toBooleanObject("blue") = null + * BooleanUtils.toBooleanObject("true ") = null // trailing space (too long) + * BooleanUtils.toBooleanObject("ono") = null // does not match on or no + *+ * + * @param str the String to check; upper and lower case are treated as the same + * @return the Boolean value of the string, {@code null} if no match or {@code null} input + */ + public static Boolean toBooleanObject(final String str) { + // Previously used equalsIgnoreCase, which was fast for interned 'true'. + // Non interned 'true' matched 15 times slower. + // + // Optimisation provides same performance as before for interned 'true'. + // Similar performance for null, 'false', and other strings not length 2/3/4. + // 'true'/'TRUE' match 4 times slower, 'tRUE'/'True' 7 times slower. + if (str == "true") { + return Boolean.TRUE; + } + if (str == null) { + return null; + } + switch (str.length()) { + case 1: { + final char ch0 = str.charAt(0); + if (ch0 == 'y' || ch0 == 'Y' || + ch0 == 't' || ch0 == 'T') { + return Boolean.TRUE; + } + if (ch0 == 'n' || ch0 == 'N' || + ch0 == 'f' || ch0 == 'F') { + return Boolean.FALSE; + } + break; + } + case 2: { + final char ch0 = str.charAt(0); + final char ch1 = str.charAt(1); + if ((ch0 == 'o' || ch0 == 'O') && + (ch1 == 'n' || ch1 == 'N') ) { + return Boolean.TRUE; + } + if ((ch0 == 'n' || ch0 == 'N') && + (ch1 == 'o' || ch1 == 'O') ) { + return Boolean.FALSE; + } + break; + } + case 3: { + final char ch0 = str.charAt(0); + final char ch1 = str.charAt(1); + final char ch2 = str.charAt(2); + if ((ch0 == 'y' || ch0 == 'Y') && + (ch1 == 'e' || ch1 == 'E') && + (ch2 == 's' || ch2 == 'S') ) { + return Boolean.TRUE; + } + if ((ch0 == 'o' || ch0 == 'O') && + (ch1 == 'f' || ch1 == 'F') && + (ch2 == 'f' || ch2 == 'F') ) { + return Boolean.FALSE; + } + break; + } + case 4: { + final char ch0 = str.charAt(0); + final char ch1 = str.charAt(1); + final char ch2 = str.charAt(2); + final char ch3 = str.charAt(3); + if ((ch0 == 't' || ch0 == 'T') && + (ch1 == 'r' || ch1 == 'R') && + (ch2 == 'u' || ch2 == 'U') && + (ch3 == 'e' || ch3 == 'E') ) { + return Boolean.TRUE; + } + break; + } + case 5: { + final char ch0 = str.charAt(0); + final char ch1 = str.charAt(1); + final char ch2 = str.charAt(2); + final char ch3 = str.charAt(3); + final char ch4 = str.charAt(4); + if ((ch0 == 'f' || ch0 == 'F') && + (ch1 == 'a' || ch1 == 'A') && + (ch2 == 'l' || ch2 == 'L') && + (ch3 == 's' || ch3 == 'S') && + (ch4 == 'e' || ch4 == 'E') ) { + return Boolean.FALSE; + } + break; + } + default: + break; + } + + return null; + } + + /** + *
Converts a String to a Boolean throwing an exception if no match.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.toBooleanObject("true", "true", "false", "null") = Boolean.TRUE + * BooleanUtils.toBooleanObject("false", "true", "false", "null") = Boolean.FALSE + * BooleanUtils.toBooleanObject("null", "true", "false", "null") = null + *+ * + * @param str the String to check + * @param trueString the String to match for {@code true} (case sensitive), may be {@code null} + * @param falseString the String to match for {@code false} (case sensitive), may be {@code null} + * @param nullString the String to match for {@code null} (case sensitive), may be {@code null} + * @return the Boolean value of the string, {@code null} if either the String matches {@code nullString} + * or if {@code null} input and {@code nullString} is {@code null} + * @throws IllegalArgumentException if the String doesn't match + */ + public static Boolean toBooleanObject(final String str, final String trueString, final String falseString, final String nullString) { + if (str == null) { + if (trueString == null) { + return Boolean.TRUE; + } + if (falseString == null) { + return Boolean.FALSE; + } + if (nullString == null) { + return null; + } + } else if (str.equals(trueString)) { + return Boolean.TRUE; + } else if (str.equals(falseString)) { + return Boolean.FALSE; + } else if (str.equals(nullString)) { + return null; + } + // no match + throw new IllegalArgumentException("The String did not match any specified value"); + } + + // String to boolean methods + //----------------------------------------------------------------------- + /** + *
Converts a String to a boolean (optimised for performance).
+ * + *{@code 'true'}, {@code 'on'}, {@code 'y'}, {@code 't'} or {@code 'yes'} + * (case insensitive) will return {@code true}. Otherwise, + * {@code false} is returned.
+ * + *This method performs 4 times faster (JDK1.4) than + * {@code Boolean.valueOf(String)}. However, this method accepts + * 'on' and 'yes', 't', 'y' as true values. + * + *
+ * BooleanUtils.toBoolean(null) = false + * BooleanUtils.toBoolean("true") = true + * BooleanUtils.toBoolean("TRUE") = true + * BooleanUtils.toBoolean("tRUe") = true + * BooleanUtils.toBoolean("on") = true + * BooleanUtils.toBoolean("yes") = true + * BooleanUtils.toBoolean("false") = false + * BooleanUtils.toBoolean("x gti") = false + * BooleanUtils.toBooleanObject("y") = true + * BooleanUtils.toBooleanObject("n") = false + * BooleanUtils.toBooleanObject("t") = true + * BooleanUtils.toBooleanObject("f") = false + *+ * + * @param str the String to check + * @return the boolean value of the string, {@code false} if no match or the String is null + */ + public static boolean toBoolean(final String str) { + return toBooleanObject(str) == Boolean.TRUE; + } + + /** + *
Converts a String to a Boolean throwing an exception if no match found.
+ * + *+ * BooleanUtils.toBoolean("true", "true", "false") = true + * BooleanUtils.toBoolean("false", "true", "false") = false + *+ * + * @param str the String to check + * @param trueString the String to match for {@code true} (case sensitive), may be {@code null} + * @param falseString the String to match for {@code false} (case sensitive), may be {@code null} + * @return the boolean value of the string + * @throws IllegalArgumentException if the String doesn't match + */ + public static boolean toBoolean(final String str, final String trueString, final String falseString) { + if (str == trueString) { + return true; + } else if (str == falseString) { + return false; + } else if (str != null) { + if (str.equals(trueString)) { + return true; + } else if (str.equals(falseString)) { + return false; + } + } + // no match + throw new IllegalArgumentException("The String did not match either specified value"); + } + + // Boolean to String methods + //----------------------------------------------------------------------- + /** + *
Converts a Boolean to a String returning {@code 'true'}, + * {@code 'false'}, or {@code null}.
+ * + *+ * BooleanUtils.toStringTrueFalse(Boolean.TRUE) = "true" + * BooleanUtils.toStringTrueFalse(Boolean.FALSE) = "false" + * BooleanUtils.toStringTrueFalse(null) = null; + *+ * + * @param bool the Boolean to check + * @return {@code 'true'}, {@code 'false'}, or {@code null} + */ + public static String toStringTrueFalse(final Boolean bool) { + return toString(bool, "true", "false", null); + } + + /** + *
Converts a Boolean to a String returning {@code 'on'}, + * {@code 'off'}, or {@code null}.
+ * + *+ * BooleanUtils.toStringOnOff(Boolean.TRUE) = "on" + * BooleanUtils.toStringOnOff(Boolean.FALSE) = "off" + * BooleanUtils.toStringOnOff(null) = null; + *+ * + * @param bool the Boolean to check + * @return {@code 'on'}, {@code 'off'}, or {@code null} + */ + public static String toStringOnOff(final Boolean bool) { + return toString(bool, "on", "off", null); + } + + /** + *
Converts a Boolean to a String returning {@code 'yes'}, + * {@code 'no'}, or {@code null}.
+ * + *+ * BooleanUtils.toStringYesNo(Boolean.TRUE) = "yes" + * BooleanUtils.toStringYesNo(Boolean.FALSE) = "no" + * BooleanUtils.toStringYesNo(null) = null; + *+ * + * @param bool the Boolean to check + * @return {@code 'yes'}, {@code 'no'}, or {@code null} + */ + public static String toStringYesNo(final Boolean bool) { + return toString(bool, "yes", "no", null); + } + + /** + *
Converts a Boolean to a String returning one of the input Strings.
+ * + *+ * BooleanUtils.toString(Boolean.TRUE, "true", "false", null) = "true" + * BooleanUtils.toString(Boolean.FALSE, "true", "false", null) = "false" + * BooleanUtils.toString(null, "true", "false", null) = null; + *+ * + * @param bool the Boolean to check + * @param trueString the String to return if {@code true}, may be {@code null} + * @param falseString the String to return if {@code false}, may be {@code null} + * @param nullString the String to return if {@code null}, may be {@code null} + * @return one of the three input Strings + */ + public static String toString(final Boolean bool, final String trueString, final String falseString, final String nullString) { + if (bool == null) { + return nullString; + } + return bool.booleanValue() ? trueString : falseString; + } + + // boolean to String methods + //----------------------------------------------------------------------- + /** + *
Converts a boolean to a String returning {@code 'true'} + * or {@code 'false'}.
+ * + *+ * BooleanUtils.toStringTrueFalse(true) = "true" + * BooleanUtils.toStringTrueFalse(false) = "false" + *+ * + * @param bool the Boolean to check + * @return {@code 'true'}, {@code 'false'}, or {@code null} + */ + public static String toStringTrueFalse(final boolean bool) { + return toString(bool, "true", "false"); + } + + /** + *
Converts a boolean to a String returning {@code 'on'} + * or {@code 'off'}.
+ * + *+ * BooleanUtils.toStringOnOff(true) = "on" + * BooleanUtils.toStringOnOff(false) = "off" + *+ * + * @param bool the Boolean to check + * @return {@code 'on'}, {@code 'off'}, or {@code null} + */ + public static String toStringOnOff(final boolean bool) { + return toString(bool, "on", "off"); + } + + /** + *
Converts a boolean to a String returning {@code 'yes'} + * or {@code 'no'}.
+ * + *+ * BooleanUtils.toStringYesNo(true) = "yes" + * BooleanUtils.toStringYesNo(false) = "no" + *+ * + * @param bool the Boolean to check + * @return {@code 'yes'}, {@code 'no'}, or {@code null} + */ + public static String toStringYesNo(final boolean bool) { + return toString(bool, "yes", "no"); + } + + /** + *
Converts a boolean to a String returning one of the input Strings.
+ * + *+ * BooleanUtils.toString(true, "true", "false") = "true" + * BooleanUtils.toString(false, "true", "false") = "false" + *+ * + * @param bool the Boolean to check + * @param trueString the String to return if {@code true}, may be {@code null} + * @param falseString the String to return if {@code false}, may be {@code null} + * @return one of the two input Strings + */ + public static String toString(final boolean bool, final String trueString, final String falseString) { + return bool ? trueString : falseString; + } + + // logical operations + // ---------------------------------------------------------------------- + /** + *
Performs an and on a set of booleans.
+ * + *+ * BooleanUtils.and(true, true) = true + * BooleanUtils.and(false, false) = false + * BooleanUtils.and(true, false) = false + * BooleanUtils.and(true, true, false) = false + * BooleanUtils.and(true, true, true) = true + *+ * + * @param array an array of {@code boolean}s + * @return {@code true} if the and is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + * @since 3.0.1 + */ +// logical operations +// ---------------------------------------------------------------------- +/** + *
Performs an and on a set of booleans.
+ * + *+ * BooleanUtils.and(true, true) = true + * BooleanUtils.and(false, false) = false + * BooleanUtils.and(true, false) = false + * BooleanUtils.and(true, true, false) = false + * BooleanUtils.and(true, true, true) = true + *+ * + * @param array + * an array of {@code boolean}s + * @return {@code true} if the and is successful. + * @throws IllegalArgumentException + * if {@code array} is {@code null} + * @throws IllegalArgumentException + * if {@code array} is empty. + * @since 3.0.1 + */ +public static boolean and(final boolean... array) { + { + if (/* NPEX_NULL_EXP */ + array.length == 0) { + throw new java.lang.IllegalArgumentException("Array is empty"); + } + for (final boolean element : array) { + if (!element) { + return false; + } + } + return true; + } +} + + /** + *
Performs an and on an array of Booleans.
+ * + *+ * BooleanUtils.and(Boolean.TRUE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.and(Boolean.FALSE, Boolean.FALSE) = Boolean.FALSE + * BooleanUtils.and(Boolean.TRUE, Boolean.FALSE) = Boolean.FALSE + * BooleanUtils.and(Boolean.TRUE, Boolean.TRUE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.and(Boolean.FALSE, Boolean.FALSE, Boolean.TRUE) = Boolean.FALSE + * BooleanUtils.and(Boolean.TRUE, Boolean.FALSE, Boolean.TRUE) = Boolean.FALSE + *+ * + * @param array an array of {@code Boolean}s + * @return {@code true} if the and is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + * @throws IllegalArgumentException if {@code array} contains a {@code null} + * @since 3.0.1 + */ + public static Boolean and(final Boolean... array) { + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + try { + final boolean[] primitive = ArrayUtils.toPrimitive(array); + return and(primitive) ? Boolean.TRUE : Boolean.FALSE; + } catch (final NullPointerException ex) { + throw new IllegalArgumentException("The array must not contain any null elements"); + } + } + + /** + *
Performs an or on a set of booleans.
+ * + *+ * BooleanUtils.or(true, true) = true + * BooleanUtils.or(false, false) = false + * BooleanUtils.or(true, false) = true + * BooleanUtils.or(true, true, false) = true + * BooleanUtils.or(true, true, true) = true + * BooleanUtils.or(false, false, false) = false + *+ * + * @param array an array of {@code boolean}s + * @return {@code true} if the or is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + * @since 3.0.1 + */ + public static boolean or(final boolean... array) { + // Validates input + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + for (final boolean element : array) { + if (element) { + return true; + } + } + return false; + } + + /** + *
Performs an or on an array of Booleans.
+ * + *+ * BooleanUtils.or(Boolean.TRUE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.or(Boolean.FALSE, Boolean.FALSE) = Boolean.FALSE + * BooleanUtils.or(Boolean.TRUE, Boolean.FALSE) = Boolean.TRUE + * BooleanUtils.or(Boolean.TRUE, Boolean.TRUE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.or(Boolean.FALSE, Boolean.FALSE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.or(Boolean.TRUE, Boolean.FALSE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.or(Boolean.FALSE, Boolean.FALSE, Boolean.FALSE) = Boolean.FALSE + *+ * + * @param array an array of {@code Boolean}s + * @return {@code true} if the or is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + * @throws IllegalArgumentException if {@code array} contains a {@code null} + * @since 3.0.1 + */ + public static Boolean or(final Boolean... array) { + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + try { + final boolean[] primitive = ArrayUtils.toPrimitive(array); + return or(primitive) ? Boolean.TRUE : Boolean.FALSE; + } catch (final NullPointerException ex) { + throw new IllegalArgumentException("The array must not contain any null elements"); + } + } + + /** + *
Performs an xor on a set of booleans.
+ * + *+ * BooleanUtils.xor(true, true) = false + * BooleanUtils.xor(false, false) = false + * BooleanUtils.xor(true, false) = true + * BooleanUtils.xor(true, true) = false + * BooleanUtils.xor(false, false) = false + * BooleanUtils.xor(true, false) = true + *+ * + * @param array an array of {@code boolean}s + * @return {@code true} if the xor is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + */ + public static boolean xor(final boolean... array) { + // Validates input + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + + // false if the neutral element of the xor operator + boolean result = false; + for (final boolean element : array) { + result ^= element; + } + + return result; + } + + /** + *
Performs an xor on an array of Booleans.
+ * + *+ * BooleanUtils.xor(new Boolean[] { Boolean.TRUE, Boolean.TRUE }) = Boolean.FALSE + * BooleanUtils.xor(new Boolean[] { Boolean.FALSE, Boolean.FALSE }) = Boolean.FALSE + * BooleanUtils.xor(new Boolean[] { Boolean.TRUE, Boolean.FALSE }) = Boolean.TRUE + *+ * + * @param array an array of {@code Boolean}s + * @return {@code true} if the xor is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + * @throws IllegalArgumentException if {@code array} contains a {@code null} + */ + public static Boolean xor(final Boolean... array) { + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + try { + final boolean[] primitive = ArrayUtils.toPrimitive(array); + return xor(primitive) ? Boolean.TRUE : Boolean.FALSE; + } catch (final NullPointerException ex) { + throw new IllegalArgumentException("The array must not contain any null elements"); + } + } + + /** + *
Compares two {@code boolean} values. This is the same functionality as provided in Java 7.
+ * + * @param x the first {@code boolean} to compare + * @param y the second {@code boolean} to compare + * @return the value {@code 0} if {@code x == y}; + * a value less than {@code 0} if {@code !x && y}; and + * a value greater than {@code 0} if {@code x && !y} + * @since 3.4 + */ + public static int compare(boolean x, boolean y) { + if (x == y) { + return 0; + } + return x ? 1 : -1; + } + +} diff --git a/Java/commons-lang-BooleanUtils_905/metadata.json b/Java/commons-lang-BooleanUtils_905/metadata.json new file mode 100644 index 000000000..82c2ccc59 --- /dev/null +++ b/Java/commons-lang-BooleanUtils_905/metadata.json @@ -0,0 +1,21 @@ +{ + "language": "java", + "id": "commons-lang-BooleanUtils_905", + "buggyPath": ".", + "referencePath": null, + "buildCommand": "mvn package -V -B -Denforcer.skip=true -Dcheckstyle.skip=true -Dcobertura.skip=true -Drat.skip=true -Dlicense.skip=true -Dfindbugs.skip=true -Dgpg.skip=true -Dskip.npm=true -Dskip.gulp=true -Dskip.bower=true -Drat.numUnapprovedLicenses=100 -DskipTests=true -DskipITs=true -Dtest=None -DfailIfNoTests=false", + "testCommand": "mvn test -V -B -Denforcer.skip=true -Dcheckstyle.skip=true -Dcobertura.skip=true -Drat.skip=true -Dlicense.skip=true -Dfindbugs.skip=true -Dgpg.skip=true -Dskip.npm=true -Dskip.gulp=true -Dskip.bower=true -Drat.numUnapprovedLicenses=100", + "categories": [ + "safety", + "npe" + ], + "npe": { + "filepath": "src/main/java/org/apache/commons/lang3/BooleanUtils.java", + "line": 928, + "npe_method": "and", + "deref_field": "array", + "npe_class": "BooleanUtils", + "repo": "commons-lang", + "bug_id": "BooleanUtils_905" + } +} diff --git a/Java/commons-lang-BooleanUtils_905/npe.json b/Java/commons-lang-BooleanUtils_905/npe.json new file mode 100644 index 000000000..e8685d6e6 --- /dev/null +++ b/Java/commons-lang-BooleanUtils_905/npe.json @@ -0,0 +1,7 @@ +{ + "filepath": "src/main/java/org/apache/commons/lang3/BooleanUtils.java", + "line": 928, + "npe_method": "and", + "deref_field": "array", + "npe_class": "BooleanUtils" +} \ No newline at end of file diff --git a/Java/commons-lang-BooleanUtils_939/Dockerfile b/Java/commons-lang-BooleanUtils_939/Dockerfile new file mode 100644 index 000000000..7b7fbe349 --- /dev/null +++ b/Java/commons-lang-BooleanUtils_939/Dockerfile @@ -0,0 +1,18 @@ +FROM ghcr.io/kupl/starlab-benchmarks/java-base:commons-lang + +ENV TZ=Asia/Seoul + +COPY ./metadata.json . +COPY ./npe.json . +COPY ./buggy.java /tmp/buggy.java +RUN export BUGGY_PATH=$(cat metadata.json | jq -r ".npe.filepath") \ + && export BUGGY_LINE=$(cat metadata.json | jq -r ".npe.line") \ + && export BUGGY_MTHD=$(cat metadata.json | jq -r ".npe.npe_method") \ + && mv /tmp/buggy.java $BUGGY_PATH \ + && echo "[{\"filepath\": \"$BUGGY_PATH\", \"line\": $BUGGY_LINE, \"method_name\": \"$BUGGY_MTHD\"}]" | jq . > traces.json + +RUN git init . && git add -A + +RUN $(cat metadata.json | jq -r ".buildCommand") + +RUN $(cat metadata.json | jq -r ".testCommand"); if [ $? -eq 0 ]; then exit 1; fi diff --git a/Java/commons-lang-BooleanUtils_939/buggy.java b/Java/commons-lang-BooleanUtils_939/buggy.java new file mode 100644 index 000000000..2730e09ee --- /dev/null +++ b/Java/commons-lang-BooleanUtils_939/buggy.java @@ -0,0 +1,1127 @@ +/* + * 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; + +import org.apache.commons.lang3.math.NumberUtils; + +/** + *Operations on boolean primitives and Boolean objects.
+ * + *This class tries to handle {@code null} input gracefully. + * An exception will not be thrown for a {@code null} input. + * Each method documents its behaviour in more detail.
+ * + *#ThreadSafe#
+ * @since 2.0 + */ +public class BooleanUtils { + + /** + *{@code BooleanUtils} instances should NOT be constructed in standard programming. + * Instead, the class should be used as {@code BooleanUtils.negate(true);}.
+ * + *This constructor is public to permit tools that require a JavaBean instance + * to operate.
+ */ + public BooleanUtils() { + super(); + } + + // Boolean utilities + //-------------------------------------------------------------------------- + /** + *Negates the specified boolean.
+ * + *If {@code null} is passed in, {@code null} will be returned.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.negate(Boolean.TRUE) = Boolean.FALSE; + * BooleanUtils.negate(Boolean.FALSE) = Boolean.TRUE; + * BooleanUtils.negate(null) = null; + *+ * + * @param bool the Boolean to negate, may be null + * @return the negated Boolean, or {@code null} if {@code null} input + */ + public static Boolean negate(final Boolean bool) { + if (bool == null) { + return null; + } + return bool.booleanValue() ? Boolean.FALSE : Boolean.TRUE; + } + + // boolean Boolean methods + //----------------------------------------------------------------------- + /** + *
Checks if a {@code Boolean} value is {@code true}, + * handling {@code null} by returning {@code false}.
+ * + *+ * BooleanUtils.isTrue(Boolean.TRUE) = true + * BooleanUtils.isTrue(Boolean.FALSE) = false + * BooleanUtils.isTrue(null) = false + *+ * + * @param bool the boolean to check, null returns {@code false} + * @return {@code true} only if the input is non-null and true + * @since 2.1 + */ + public static boolean isTrue(final Boolean bool) { + return Boolean.TRUE.equals(bool); + } + + /** + *
Checks if a {@code Boolean} value is not {@code true}, + * handling {@code null} by returning {@code true}.
+ * + *+ * BooleanUtils.isNotTrue(Boolean.TRUE) = false + * BooleanUtils.isNotTrue(Boolean.FALSE) = true + * BooleanUtils.isNotTrue(null) = true + *+ * + * @param bool the boolean to check, null returns {@code true} + * @return {@code true} if the input is null or false + * @since 2.3 + */ + public static boolean isNotTrue(final Boolean bool) { + return !isTrue(bool); + } + + /** + *
Checks if a {@code Boolean} value is {@code false}, + * handling {@code null} by returning {@code false}.
+ * + *+ * BooleanUtils.isFalse(Boolean.TRUE) = false + * BooleanUtils.isFalse(Boolean.FALSE) = true + * BooleanUtils.isFalse(null) = false + *+ * + * @param bool the boolean to check, null returns {@code false} + * @return {@code true} only if the input is non-null and false + * @since 2.1 + */ + public static boolean isFalse(final Boolean bool) { + return Boolean.FALSE.equals(bool); + } + + /** + *
Checks if a {@code Boolean} value is not {@code false}, + * handling {@code null} by returning {@code true}.
+ * + *+ * BooleanUtils.isNotFalse(Boolean.TRUE) = true + * BooleanUtils.isNotFalse(Boolean.FALSE) = false + * BooleanUtils.isNotFalse(null) = true + *+ * + * @param bool the boolean to check, null returns {@code true} + * @return {@code true} if the input is null or true + * @since 2.3 + */ + public static boolean isNotFalse(final Boolean bool) { + return !isFalse(bool); + } + + //----------------------------------------------------------------------- + /** + *
Converts a Boolean to a boolean handling {@code null} + * by returning {@code false}.
+ * + *+ * BooleanUtils.toBoolean(Boolean.TRUE) = true + * BooleanUtils.toBoolean(Boolean.FALSE) = false + * BooleanUtils.toBoolean(null) = false + *+ * + * @param bool the boolean to convert + * @return {@code true} or {@code false}, {@code null} returns {@code false} + */ + public static boolean toBoolean(final Boolean bool) { + return bool != null && bool.booleanValue(); + } + + /** + *
Converts a Boolean to a boolean handling {@code null}.
+ * + *+ * BooleanUtils.toBooleanDefaultIfNull(Boolean.TRUE, false) = true + * BooleanUtils.toBooleanDefaultIfNull(Boolean.FALSE, true) = false + * BooleanUtils.toBooleanDefaultIfNull(null, true) = true + *+ * + * @param bool the boolean to convert + * @param valueIfNull the boolean value to return if {@code null} + * @return {@code true} or {@code false} + */ + public static boolean toBooleanDefaultIfNull(final Boolean bool, final boolean valueIfNull) { + if (bool == null) { + return valueIfNull; + } + return bool.booleanValue(); + } + + // Integer to Boolean methods + //----------------------------------------------------------------------- + /** + *
Converts an int to a boolean using the convention that {@code zero} + * is {@code false}.
+ * + *+ * BooleanUtils.toBoolean(0) = false + * BooleanUtils.toBoolean(1) = true + * BooleanUtils.toBoolean(2) = true + *+ * + * @param value the int to convert + * @return {@code true} if non-zero, {@code false} + * if zero + */ + public static boolean toBoolean(final int value) { + return value != 0; + } + + /** + *
Converts an int to a Boolean using the convention that {@code zero} + * is {@code false}.
+ * + *+ * BooleanUtils.toBoolean(0) = Boolean.FALSE + * BooleanUtils.toBoolean(1) = Boolean.TRUE + * BooleanUtils.toBoolean(2) = Boolean.TRUE + *+ * + * @param value the int to convert + * @return Boolean.TRUE if non-zero, Boolean.FALSE if zero, + * {@code null} if {@code null} + */ + public static Boolean toBooleanObject(final int value) { + return value == 0 ? Boolean.FALSE : Boolean.TRUE; + } + + /** + *
Converts an Integer to a Boolean using the convention that {@code zero} + * is {@code false}.
+ * + *{@code null} will be converted to {@code null}.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.toBoolean(Integer.valueOf(0)) = Boolean.FALSE + * BooleanUtils.toBoolean(Integer.valueOf(1)) = Boolean.TRUE + * BooleanUtils.toBoolean(Integer.valueOf(null)) = null + *+ * + * @param value the Integer to convert + * @return Boolean.TRUE if non-zero, Boolean.FALSE if zero, + * {@code null} if {@code null} input + */ + public static Boolean toBooleanObject(final Integer value) { + if (value == null) { + return null; + } + return value.intValue() == 0 ? Boolean.FALSE : Boolean.TRUE; + } + + /** + *
Converts an int to a boolean specifying the conversion values.
+ * + *+ * BooleanUtils.toBoolean(0, 1, 0) = false + * BooleanUtils.toBoolean(1, 1, 0) = true + * BooleanUtils.toBoolean(2, 1, 2) = false + * BooleanUtils.toBoolean(2, 2, 0) = true + *+ * + * @param value the Integer to convert + * @param trueValue the value to match for {@code true} + * @param falseValue the value to match for {@code false} + * @return {@code true} or {@code false} + * @throws IllegalArgumentException if no match + */ + public static boolean toBoolean(final int value, final int trueValue, final int falseValue) { + if (value == trueValue) { + return true; + } + if (value == falseValue) { + return false; + } + // no match + throw new IllegalArgumentException("The Integer did not match either specified value"); + } + + /** + *
Converts an Integer to a boolean specifying the conversion values.
+ * + *+ * BooleanUtils.toBoolean(Integer.valueOf(0), Integer.valueOf(1), Integer.valueOf(0)) = false + * BooleanUtils.toBoolean(Integer.valueOf(1), Integer.valueOf(1), Integer.valueOf(0)) = true + * BooleanUtils.toBoolean(Integer.valueOf(2), Integer.valueOf(1), Integer.valueOf(2)) = false + * BooleanUtils.toBoolean(Integer.valueOf(2), Integer.valueOf(2), Integer.valueOf(0)) = true + * BooleanUtils.toBoolean(null, null, Integer.valueOf(0)) = true + *+ * + * @param value the Integer to convert + * @param trueValue the value to match for {@code true}, may be {@code null} + * @param falseValue the value to match for {@code false}, may be {@code null} + * @return {@code true} or {@code false} + * @throws IllegalArgumentException if no match + */ + public static boolean toBoolean(final Integer value, final Integer trueValue, final Integer falseValue) { + if (value == null) { + if (trueValue == null) { + return true; + } + if (falseValue == null) { + return false; + } + } else if (value.equals(trueValue)) { + return true; + } else if (value.equals(falseValue)) { + return false; + } + // no match + throw new IllegalArgumentException("The Integer did not match either specified value"); + } + + /** + *
Converts an int to a Boolean specifying the conversion values.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.toBooleanObject(0, 0, 2, 3) = Boolean.TRUE + * BooleanUtils.toBooleanObject(2, 1, 2, 3) = Boolean.FALSE + * BooleanUtils.toBooleanObject(3, 1, 2, 3) = null + *+ * + * @param value the Integer to convert + * @param trueValue the value to match for {@code true} + * @param falseValue the value to match for {@code false} + * @param nullValue the value to to match for {@code null} + * @return Boolean.TRUE, Boolean.FALSE, or {@code null} + * @throws IllegalArgumentException if no match + */ + public static Boolean toBooleanObject(final int value, final int trueValue, final int falseValue, final int nullValue) { + if (value == trueValue) { + return Boolean.TRUE; + } + if (value == falseValue) { + return Boolean.FALSE; + } + if (value == nullValue) { + return null; + } + // no match + throw new IllegalArgumentException("The Integer did not match any specified value"); + } + + /** + *
Converts an Integer to a Boolean specifying the conversion values.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.toBooleanObject(Integer.valueOf(0), Integer.valueOf(0), Integer.valueOf(2), Integer.valueOf(3)) = Boolean.TRUE + * BooleanUtils.toBooleanObject(Integer.valueOf(2), Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(3)) = Boolean.FALSE + * BooleanUtils.toBooleanObject(Integer.valueOf(3), Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(3)) = null + *+ * + * @param value the Integer to convert + * @param trueValue the value to match for {@code true}, may be {@code null} + * @param falseValue the value to match for {@code false}, may be {@code null} + * @param nullValue the value to to match for {@code null}, may be {@code null} + * @return Boolean.TRUE, Boolean.FALSE, or {@code null} + * @throws IllegalArgumentException if no match + */ + public static Boolean toBooleanObject(final Integer value, final Integer trueValue, final Integer falseValue, final Integer nullValue) { + if (value == null) { + if (trueValue == null) { + return Boolean.TRUE; + } + if (falseValue == null) { + return Boolean.FALSE; + } + if (nullValue == null) { + return null; + } + } else if (value.equals(trueValue)) { + return Boolean.TRUE; + } else if (value.equals(falseValue)) { + return Boolean.FALSE; + } else if (value.equals(nullValue)) { + return null; + } + // no match + throw new IllegalArgumentException("The Integer did not match any specified value"); + } + + // Boolean to Integer methods + //----------------------------------------------------------------------- + /** + *
Converts a boolean to an int using the convention that + * {@code zero} is {@code false}.
+ * + *+ * BooleanUtils.toInteger(true) = 1 + * BooleanUtils.toInteger(false) = 0 + *+ * + * @param bool the boolean to convert + * @return one if {@code true}, zero if {@code false} + */ + public static int toInteger(final boolean bool) { + return bool ? 1 : 0; + } + + /** + *
Converts a boolean to an Integer using the convention that + * {@code zero} is {@code false}.
+ * + *+ * BooleanUtils.toIntegerObject(true) = Integer.valueOf(1) + * BooleanUtils.toIntegerObject(false) = Integer.valueOf(0) + *+ * + * @param bool the boolean to convert + * @return one if {@code true}, zero if {@code false} + */ + public static Integer toIntegerObject(final boolean bool) { + return bool ? NumberUtils.INTEGER_ONE : NumberUtils.INTEGER_ZERO; + } + + /** + *
Converts a Boolean to a Integer using the convention that + * {@code zero} is {@code false}.
+ * + *{@code null} will be converted to {@code null}.
+ * + *+ * BooleanUtils.toIntegerObject(Boolean.TRUE) = Integer.valueOf(1) + * BooleanUtils.toIntegerObject(Boolean.FALSE) = Integer.valueOf(0) + *+ * + * @param bool the Boolean to convert + * @return one if Boolean.TRUE, zero if Boolean.FALSE, {@code null} if {@code null} + */ + public static Integer toIntegerObject(final Boolean bool) { + if (bool == null) { + return null; + } + return bool.booleanValue() ? NumberUtils.INTEGER_ONE : NumberUtils.INTEGER_ZERO; + } + + /** + *
Converts a boolean to an int specifying the conversion values.
+ * + *+ * BooleanUtils.toInteger(true, 1, 0) = 1 + * BooleanUtils.toInteger(false, 1, 0) = 0 + *+ * + * @param bool the to convert + * @param trueValue the value to return if {@code true} + * @param falseValue the value to return if {@code false} + * @return the appropriate value + */ + public static int toInteger(final boolean bool, final int trueValue, final int falseValue) { + return bool ? trueValue : falseValue; + } + + /** + *
Converts a Boolean to an int specifying the conversion values.
+ * + *+ * BooleanUtils.toInteger(Boolean.TRUE, 1, 0, 2) = 1 + * BooleanUtils.toInteger(Boolean.FALSE, 1, 0, 2) = 0 + * BooleanUtils.toInteger(null, 1, 0, 2) = 2 + *+ * + * @param bool the Boolean to convert + * @param trueValue the value to return if {@code true} + * @param falseValue the value to return if {@code false} + * @param nullValue the value to return if {@code null} + * @return the appropriate value + */ + public static int toInteger(final Boolean bool, final int trueValue, final int falseValue, final int nullValue) { + if (bool == null) { + return nullValue; + } + return bool.booleanValue() ? trueValue : falseValue; + } + + /** + *
Converts a boolean to an Integer specifying the conversion values.
+ * + *+ * BooleanUtils.toIntegerObject(true, Integer.valueOf(1), Integer.valueOf(0)) = Integer.valueOf(1) + * BooleanUtils.toIntegerObject(false, Integer.valueOf(1), Integer.valueOf(0)) = Integer.valueOf(0) + *+ * + * @param bool the to convert + * @param trueValue the value to return if {@code true}, may be {@code null} + * @param falseValue the value to return if {@code false}, may be {@code null} + * @return the appropriate value + */ + public static Integer toIntegerObject(final boolean bool, final Integer trueValue, final Integer falseValue) { + return bool ? trueValue : falseValue; + } + + /** + *
Converts a Boolean to an Integer specifying the conversion values.
+ * + *+ * BooleanUtils.toIntegerObject(Boolean.TRUE, Integer.valueOf(1), Integer.valueOf(0), Integer.valueOf(2)) = Integer.valueOf(1) + * BooleanUtils.toIntegerObject(Boolean.FALSE, Integer.valueOf(1), Integer.valueOf(0), Integer.valueOf(2)) = Integer.valueOf(0) + * BooleanUtils.toIntegerObject(null, Integer.valueOf(1), Integer.valueOf(0), Integer.valueOf(2)) = Integer.valueOf(2) + *+ * + * @param bool the Boolean to convert + * @param trueValue the value to return if {@code true}, may be {@code null} + * @param falseValue the value to return if {@code false}, may be {@code null} + * @param nullValue the value to return if {@code null}, may be {@code null} + * @return the appropriate value + */ + public static Integer toIntegerObject(final Boolean bool, final Integer trueValue, final Integer falseValue, final Integer nullValue) { + if (bool == null) { + return nullValue; + } + return bool.booleanValue() ? trueValue : falseValue; + } + + // String to Boolean methods + //----------------------------------------------------------------------- + /** + *
Converts a String to a Boolean.
+ * + *{@code 'true'}, {@code 'on'}, {@code 'y'}, {@code 't'} or {@code 'yes'} + * (case insensitive) will return {@code true}. + * {@code 'false'}, {@code 'off'}, {@code 'n'}, {@code 'f'} or {@code 'no'} + * (case insensitive) will return {@code false}. + * Otherwise, {@code null} is returned.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * // N.B. case is not significant + * BooleanUtils.toBooleanObject(null) = null + * BooleanUtils.toBooleanObject("true") = Boolean.TRUE + * BooleanUtils.toBooleanObject("T") = Boolean.TRUE // i.e. T[RUE] + * BooleanUtils.toBooleanObject("false") = Boolean.FALSE + * BooleanUtils.toBooleanObject("f") = Boolean.FALSE // i.e. f[alse] + * BooleanUtils.toBooleanObject("No") = Boolean.FALSE + * BooleanUtils.toBooleanObject("n") = Boolean.FALSE // i.e. n[o] + * BooleanUtils.toBooleanObject("on") = Boolean.TRUE + * BooleanUtils.toBooleanObject("ON") = Boolean.TRUE + * BooleanUtils.toBooleanObject("off") = Boolean.FALSE + * BooleanUtils.toBooleanObject("oFf") = Boolean.FALSE + * BooleanUtils.toBooleanObject("yes") = Boolean.TRUE + * BooleanUtils.toBooleanObject("Y") = Boolean.TRUE // i.e. Y[ES] + * BooleanUtils.toBooleanObject("blue") = null + * BooleanUtils.toBooleanObject("true ") = null // trailing space (too long) + * BooleanUtils.toBooleanObject("ono") = null // does not match on or no + *+ * + * @param str the String to check; upper and lower case are treated as the same + * @return the Boolean value of the string, {@code null} if no match or {@code null} input + */ + public static Boolean toBooleanObject(final String str) { + // Previously used equalsIgnoreCase, which was fast for interned 'true'. + // Non interned 'true' matched 15 times slower. + // + // Optimisation provides same performance as before for interned 'true'. + // Similar performance for null, 'false', and other strings not length 2/3/4. + // 'true'/'TRUE' match 4 times slower, 'tRUE'/'True' 7 times slower. + if (str == "true") { + return Boolean.TRUE; + } + if (str == null) { + return null; + } + switch (str.length()) { + case 1: { + final char ch0 = str.charAt(0); + if (ch0 == 'y' || ch0 == 'Y' || + ch0 == 't' || ch0 == 'T') { + return Boolean.TRUE; + } + if (ch0 == 'n' || ch0 == 'N' || + ch0 == 'f' || ch0 == 'F') { + return Boolean.FALSE; + } + break; + } + case 2: { + final char ch0 = str.charAt(0); + final char ch1 = str.charAt(1); + if ((ch0 == 'o' || ch0 == 'O') && + (ch1 == 'n' || ch1 == 'N') ) { + return Boolean.TRUE; + } + if ((ch0 == 'n' || ch0 == 'N') && + (ch1 == 'o' || ch1 == 'O') ) { + return Boolean.FALSE; + } + break; + } + case 3: { + final char ch0 = str.charAt(0); + final char ch1 = str.charAt(1); + final char ch2 = str.charAt(2); + if ((ch0 == 'y' || ch0 == 'Y') && + (ch1 == 'e' || ch1 == 'E') && + (ch2 == 's' || ch2 == 'S') ) { + return Boolean.TRUE; + } + if ((ch0 == 'o' || ch0 == 'O') && + (ch1 == 'f' || ch1 == 'F') && + (ch2 == 'f' || ch2 == 'F') ) { + return Boolean.FALSE; + } + break; + } + case 4: { + final char ch0 = str.charAt(0); + final char ch1 = str.charAt(1); + final char ch2 = str.charAt(2); + final char ch3 = str.charAt(3); + if ((ch0 == 't' || ch0 == 'T') && + (ch1 == 'r' || ch1 == 'R') && + (ch2 == 'u' || ch2 == 'U') && + (ch3 == 'e' || ch3 == 'E') ) { + return Boolean.TRUE; + } + break; + } + case 5: { + final char ch0 = str.charAt(0); + final char ch1 = str.charAt(1); + final char ch2 = str.charAt(2); + final char ch3 = str.charAt(3); + final char ch4 = str.charAt(4); + if ((ch0 == 'f' || ch0 == 'F') && + (ch1 == 'a' || ch1 == 'A') && + (ch2 == 'l' || ch2 == 'L') && + (ch3 == 's' || ch3 == 'S') && + (ch4 == 'e' || ch4 == 'E') ) { + return Boolean.FALSE; + } + break; + } + default: + break; + } + + return null; + } + + /** + *
Converts a String to a Boolean throwing an exception if no match.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.toBooleanObject("true", "true", "false", "null") = Boolean.TRUE + * BooleanUtils.toBooleanObject("false", "true", "false", "null") = Boolean.FALSE + * BooleanUtils.toBooleanObject("null", "true", "false", "null") = null + *+ * + * @param str the String to check + * @param trueString the String to match for {@code true} (case sensitive), may be {@code null} + * @param falseString the String to match for {@code false} (case sensitive), may be {@code null} + * @param nullString the String to match for {@code null} (case sensitive), may be {@code null} + * @return the Boolean value of the string, {@code null} if either the String matches {@code nullString} + * or if {@code null} input and {@code nullString} is {@code null} + * @throws IllegalArgumentException if the String doesn't match + */ + public static Boolean toBooleanObject(final String str, final String trueString, final String falseString, final String nullString) { + if (str == null) { + if (trueString == null) { + return Boolean.TRUE; + } + if (falseString == null) { + return Boolean.FALSE; + } + if (nullString == null) { + return null; + } + } else if (str.equals(trueString)) { + return Boolean.TRUE; + } else if (str.equals(falseString)) { + return Boolean.FALSE; + } else if (str.equals(nullString)) { + return null; + } + // no match + throw new IllegalArgumentException("The String did not match any specified value"); + } + + // String to boolean methods + //----------------------------------------------------------------------- + /** + *
Converts a String to a boolean (optimised for performance).
+ * + *{@code 'true'}, {@code 'on'}, {@code 'y'}, {@code 't'} or {@code 'yes'} + * (case insensitive) will return {@code true}. Otherwise, + * {@code false} is returned.
+ * + *This method performs 4 times faster (JDK1.4) than + * {@code Boolean.valueOf(String)}. However, this method accepts + * 'on' and 'yes', 't', 'y' as true values. + * + *
+ * BooleanUtils.toBoolean(null) = false + * BooleanUtils.toBoolean("true") = true + * BooleanUtils.toBoolean("TRUE") = true + * BooleanUtils.toBoolean("tRUe") = true + * BooleanUtils.toBoolean("on") = true + * BooleanUtils.toBoolean("yes") = true + * BooleanUtils.toBoolean("false") = false + * BooleanUtils.toBoolean("x gti") = false + * BooleanUtils.toBooleanObject("y") = true + * BooleanUtils.toBooleanObject("n") = false + * BooleanUtils.toBooleanObject("t") = true + * BooleanUtils.toBooleanObject("f") = false + *+ * + * @param str the String to check + * @return the boolean value of the string, {@code false} if no match or the String is null + */ + public static boolean toBoolean(final String str) { + return toBooleanObject(str) == Boolean.TRUE; + } + + /** + *
Converts a String to a Boolean throwing an exception if no match found.
+ * + *+ * BooleanUtils.toBoolean("true", "true", "false") = true + * BooleanUtils.toBoolean("false", "true", "false") = false + *+ * + * @param str the String to check + * @param trueString the String to match for {@code true} (case sensitive), may be {@code null} + * @param falseString the String to match for {@code false} (case sensitive), may be {@code null} + * @return the boolean value of the string + * @throws IllegalArgumentException if the String doesn't match + */ + public static boolean toBoolean(final String str, final String trueString, final String falseString) { + if (str == trueString) { + return true; + } else if (str == falseString) { + return false; + } else if (str != null) { + if (str.equals(trueString)) { + return true; + } else if (str.equals(falseString)) { + return false; + } + } + // no match + throw new IllegalArgumentException("The String did not match either specified value"); + } + + // Boolean to String methods + //----------------------------------------------------------------------- + /** + *
Converts a Boolean to a String returning {@code 'true'}, + * {@code 'false'}, or {@code null}.
+ * + *+ * BooleanUtils.toStringTrueFalse(Boolean.TRUE) = "true" + * BooleanUtils.toStringTrueFalse(Boolean.FALSE) = "false" + * BooleanUtils.toStringTrueFalse(null) = null; + *+ * + * @param bool the Boolean to check + * @return {@code 'true'}, {@code 'false'}, or {@code null} + */ + public static String toStringTrueFalse(final Boolean bool) { + return toString(bool, "true", "false", null); + } + + /** + *
Converts a Boolean to a String returning {@code 'on'}, + * {@code 'off'}, or {@code null}.
+ * + *+ * BooleanUtils.toStringOnOff(Boolean.TRUE) = "on" + * BooleanUtils.toStringOnOff(Boolean.FALSE) = "off" + * BooleanUtils.toStringOnOff(null) = null; + *+ * + * @param bool the Boolean to check + * @return {@code 'on'}, {@code 'off'}, or {@code null} + */ + public static String toStringOnOff(final Boolean bool) { + return toString(bool, "on", "off", null); + } + + /** + *
Converts a Boolean to a String returning {@code 'yes'}, + * {@code 'no'}, or {@code null}.
+ * + *+ * BooleanUtils.toStringYesNo(Boolean.TRUE) = "yes" + * BooleanUtils.toStringYesNo(Boolean.FALSE) = "no" + * BooleanUtils.toStringYesNo(null) = null; + *+ * + * @param bool the Boolean to check + * @return {@code 'yes'}, {@code 'no'}, or {@code null} + */ + public static String toStringYesNo(final Boolean bool) { + return toString(bool, "yes", "no", null); + } + + /** + *
Converts a Boolean to a String returning one of the input Strings.
+ * + *+ * BooleanUtils.toString(Boolean.TRUE, "true", "false", null) = "true" + * BooleanUtils.toString(Boolean.FALSE, "true", "false", null) = "false" + * BooleanUtils.toString(null, "true", "false", null) = null; + *+ * + * @param bool the Boolean to check + * @param trueString the String to return if {@code true}, may be {@code null} + * @param falseString the String to return if {@code false}, may be {@code null} + * @param nullString the String to return if {@code null}, may be {@code null} + * @return one of the three input Strings + */ + public static String toString(final Boolean bool, final String trueString, final String falseString, final String nullString) { + if (bool == null) { + return nullString; + } + return bool.booleanValue() ? trueString : falseString; + } + + // boolean to String methods + //----------------------------------------------------------------------- + /** + *
Converts a boolean to a String returning {@code 'true'} + * or {@code 'false'}.
+ * + *+ * BooleanUtils.toStringTrueFalse(true) = "true" + * BooleanUtils.toStringTrueFalse(false) = "false" + *+ * + * @param bool the Boolean to check + * @return {@code 'true'}, {@code 'false'}, or {@code null} + */ + public static String toStringTrueFalse(final boolean bool) { + return toString(bool, "true", "false"); + } + + /** + *
Converts a boolean to a String returning {@code 'on'} + * or {@code 'off'}.
+ * + *+ * BooleanUtils.toStringOnOff(true) = "on" + * BooleanUtils.toStringOnOff(false) = "off" + *+ * + * @param bool the Boolean to check + * @return {@code 'on'}, {@code 'off'}, or {@code null} + */ + public static String toStringOnOff(final boolean bool) { + return toString(bool, "on", "off"); + } + + /** + *
Converts a boolean to a String returning {@code 'yes'} + * or {@code 'no'}.
+ * + *+ * BooleanUtils.toStringYesNo(true) = "yes" + * BooleanUtils.toStringYesNo(false) = "no" + *+ * + * @param bool the Boolean to check + * @return {@code 'yes'}, {@code 'no'}, or {@code null} + */ + public static String toStringYesNo(final boolean bool) { + return toString(bool, "yes", "no"); + } + + /** + *
Converts a boolean to a String returning one of the input Strings.
+ * + *+ * BooleanUtils.toString(true, "true", "false") = "true" + * BooleanUtils.toString(false, "true", "false") = "false" + *+ * + * @param bool the Boolean to check + * @param trueString the String to return if {@code true}, may be {@code null} + * @param falseString the String to return if {@code false}, may be {@code null} + * @return one of the two input Strings + */ + public static String toString(final boolean bool, final String trueString, final String falseString) { + return bool ? trueString : falseString; + } + + // logical operations + // ---------------------------------------------------------------------- + /** + *
Performs an and on a set of booleans.
+ * + *+ * BooleanUtils.and(true, true) = true + * BooleanUtils.and(false, false) = false + * BooleanUtils.and(true, false) = false + * BooleanUtils.and(true, true, false) = false + * BooleanUtils.and(true, true, true) = true + *+ * + * @param array an array of {@code boolean}s + * @return {@code true} if the and is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + * @since 3.0.1 + */ + public static boolean and(final boolean... array) { + // Validates input + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + for (final boolean element : array) { + if (!element) { + return false; + } + } + return true; + } + + /** + *
Performs an and on an array of Booleans.
+ * + *+ * BooleanUtils.and(Boolean.TRUE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.and(Boolean.FALSE, Boolean.FALSE) = Boolean.FALSE + * BooleanUtils.and(Boolean.TRUE, Boolean.FALSE) = Boolean.FALSE + * BooleanUtils.and(Boolean.TRUE, Boolean.TRUE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.and(Boolean.FALSE, Boolean.FALSE, Boolean.TRUE) = Boolean.FALSE + * BooleanUtils.and(Boolean.TRUE, Boolean.FALSE, Boolean.TRUE) = Boolean.FALSE + *+ * + * @param array an array of {@code Boolean}s + * @return {@code true} if the and is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + * @throws IllegalArgumentException if {@code array} contains a {@code null} + * @since 3.0.1 + */ +/** + *
Performs an and on an array of Booleans.
+ * + *+ * BooleanUtils.and(Boolean.TRUE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.and(Boolean.FALSE, Boolean.FALSE) = Boolean.FALSE + * BooleanUtils.and(Boolean.TRUE, Boolean.FALSE) = Boolean.FALSE + * BooleanUtils.and(Boolean.TRUE, Boolean.TRUE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.and(Boolean.FALSE, Boolean.FALSE, Boolean.TRUE) = Boolean.FALSE + * BooleanUtils.and(Boolean.TRUE, Boolean.FALSE, Boolean.TRUE) = Boolean.FALSE + *+ * + * @param array + * an array of {@code Boolean}s + * @return {@code true} if the and is successful. + * @throws IllegalArgumentException + * if {@code array} is {@code null} + * @throws IllegalArgumentException + * if {@code array} is empty. + * @throws IllegalArgumentException + * if {@code array} contains a {@code null} + * @since 3.0.1 + */ +public static java.lang.Boolean and(final java.lang.Boolean... array) { + { + if (/* NPEX_NULL_EXP */ + array.length == 0) { + throw new java.lang.IllegalArgumentException("Array is empty"); + } + try { + final boolean[] primitive = org.apache.commons.lang3.ArrayUtils.toPrimitive(array); + return org.apache.commons.lang3.BooleanUtils.and(primitive) ? java.lang.Boolean.TRUE : java.lang.Boolean.FALSE; + } catch (final java.lang.NullPointerException ex) { + throw new java.lang.IllegalArgumentException("The array must not contain any null elements"); + } + } +} + + /** + *
Performs an or on a set of booleans.
+ * + *+ * BooleanUtils.or(true, true) = true + * BooleanUtils.or(false, false) = false + * BooleanUtils.or(true, false) = true + * BooleanUtils.or(true, true, false) = true + * BooleanUtils.or(true, true, true) = true + * BooleanUtils.or(false, false, false) = false + *+ * + * @param array an array of {@code boolean}s + * @return {@code true} if the or is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + * @since 3.0.1 + */ + public static boolean or(final boolean... array) { + // Validates input + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + for (final boolean element : array) { + if (element) { + return true; + } + } + return false; + } + + /** + *
Performs an or on an array of Booleans.
+ * + *+ * BooleanUtils.or(Boolean.TRUE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.or(Boolean.FALSE, Boolean.FALSE) = Boolean.FALSE + * BooleanUtils.or(Boolean.TRUE, Boolean.FALSE) = Boolean.TRUE + * BooleanUtils.or(Boolean.TRUE, Boolean.TRUE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.or(Boolean.FALSE, Boolean.FALSE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.or(Boolean.TRUE, Boolean.FALSE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.or(Boolean.FALSE, Boolean.FALSE, Boolean.FALSE) = Boolean.FALSE + *+ * + * @param array an array of {@code Boolean}s + * @return {@code true} if the or is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + * @throws IllegalArgumentException if {@code array} contains a {@code null} + * @since 3.0.1 + */ + public static Boolean or(final Boolean... array) { + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + try { + final boolean[] primitive = ArrayUtils.toPrimitive(array); + return or(primitive) ? Boolean.TRUE : Boolean.FALSE; + } catch (final NullPointerException ex) { + throw new IllegalArgumentException("The array must not contain any null elements"); + } + } + + /** + *
Performs an xor on a set of booleans.
+ * + *+ * BooleanUtils.xor(true, true) = false + * BooleanUtils.xor(false, false) = false + * BooleanUtils.xor(true, false) = true + * BooleanUtils.xor(true, true) = false + * BooleanUtils.xor(false, false) = false + * BooleanUtils.xor(true, false) = true + *+ * + * @param array an array of {@code boolean}s + * @return {@code true} if the xor is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + */ + public static boolean xor(final boolean... array) { + // Validates input + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + + // false if the neutral element of the xor operator + boolean result = false; + for (final boolean element : array) { + result ^= element; + } + + return result; + } + + /** + *
Performs an xor on an array of Booleans.
+ * + *+ * BooleanUtils.xor(new Boolean[] { Boolean.TRUE, Boolean.TRUE }) = Boolean.FALSE + * BooleanUtils.xor(new Boolean[] { Boolean.FALSE, Boolean.FALSE }) = Boolean.FALSE + * BooleanUtils.xor(new Boolean[] { Boolean.TRUE, Boolean.FALSE }) = Boolean.TRUE + *+ * + * @param array an array of {@code Boolean}s + * @return {@code true} if the xor is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + * @throws IllegalArgumentException if {@code array} contains a {@code null} + */ + public static Boolean xor(final Boolean... array) { + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + try { + final boolean[] primitive = ArrayUtils.toPrimitive(array); + return xor(primitive) ? Boolean.TRUE : Boolean.FALSE; + } catch (final NullPointerException ex) { + throw new IllegalArgumentException("The array must not contain any null elements"); + } + } + + /** + *
Compares two {@code boolean} values. This is the same functionality as provided in Java 7.
+ * + * @param x the first {@code boolean} to compare + * @param y the second {@code boolean} to compare + * @return the value {@code 0} if {@code x == y}; + * a value less than {@code 0} if {@code !x && y}; and + * a value greater than {@code 0} if {@code x && !y} + * @since 3.4 + */ + public static int compare(boolean x, boolean y) { + if (x == y) { + return 0; + } + return x ? 1 : -1; + } + +} diff --git a/Java/commons-lang-BooleanUtils_939/metadata.json b/Java/commons-lang-BooleanUtils_939/metadata.json new file mode 100644 index 000000000..76ae44d28 --- /dev/null +++ b/Java/commons-lang-BooleanUtils_939/metadata.json @@ -0,0 +1,21 @@ +{ + "language": "java", + "id": "commons-lang-BooleanUtils_939", + "buggyPath": ".", + "referencePath": null, + "buildCommand": "mvn package -V -B -Denforcer.skip=true -Dcheckstyle.skip=true -Dcobertura.skip=true -Drat.skip=true -Dlicense.skip=true -Dfindbugs.skip=true -Dgpg.skip=true -Dskip.npm=true -Dskip.gulp=true -Dskip.bower=true -Drat.numUnapprovedLicenses=100 -DskipTests=true -DskipITs=true -Dtest=None -DfailIfNoTests=false", + "testCommand": "mvn test -V -B -Denforcer.skip=true -Dcheckstyle.skip=true -Dcobertura.skip=true -Drat.skip=true -Dlicense.skip=true -Dfindbugs.skip=true -Dgpg.skip=true -Dskip.npm=true -Dskip.gulp=true -Dskip.bower=true -Drat.numUnapprovedLicenses=100", + "categories": [ + "safety", + "npe" + ], + "npe": { + "filepath": "src/main/java/org/apache/commons/lang3/BooleanUtils.java", + "line": 964, + "npe_method": "and", + "deref_field": "array", + "npe_class": "BooleanUtils", + "repo": "commons-lang", + "bug_id": "BooleanUtils_939" + } +} diff --git a/Java/commons-lang-BooleanUtils_939/npe.json b/Java/commons-lang-BooleanUtils_939/npe.json new file mode 100644 index 000000000..053c5d194 --- /dev/null +++ b/Java/commons-lang-BooleanUtils_939/npe.json @@ -0,0 +1,7 @@ +{ + "filepath": "src/main/java/org/apache/commons/lang3/BooleanUtils.java", + "line": 964, + "npe_method": "and", + "deref_field": "array", + "npe_class": "BooleanUtils" +} \ No newline at end of file diff --git a/Java/commons-lang-BooleanUtils_973/Dockerfile b/Java/commons-lang-BooleanUtils_973/Dockerfile new file mode 100644 index 000000000..7b7fbe349 --- /dev/null +++ b/Java/commons-lang-BooleanUtils_973/Dockerfile @@ -0,0 +1,18 @@ +FROM ghcr.io/kupl/starlab-benchmarks/java-base:commons-lang + +ENV TZ=Asia/Seoul + +COPY ./metadata.json . +COPY ./npe.json . +COPY ./buggy.java /tmp/buggy.java +RUN export BUGGY_PATH=$(cat metadata.json | jq -r ".npe.filepath") \ + && export BUGGY_LINE=$(cat metadata.json | jq -r ".npe.line") \ + && export BUGGY_MTHD=$(cat metadata.json | jq -r ".npe.npe_method") \ + && mv /tmp/buggy.java $BUGGY_PATH \ + && echo "[{\"filepath\": \"$BUGGY_PATH\", \"line\": $BUGGY_LINE, \"method_name\": \"$BUGGY_MTHD\"}]" | jq . > traces.json + +RUN git init . && git add -A + +RUN $(cat metadata.json | jq -r ".buildCommand") + +RUN $(cat metadata.json | jq -r ".testCommand"); if [ $? -eq 0 ]; then exit 1; fi diff --git a/Java/commons-lang-BooleanUtils_973/buggy.java b/Java/commons-lang-BooleanUtils_973/buggy.java new file mode 100644 index 000000000..14498bb6e --- /dev/null +++ b/Java/commons-lang-BooleanUtils_973/buggy.java @@ -0,0 +1,1124 @@ +/* + * 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; + +import org.apache.commons.lang3.math.NumberUtils; + +/** + *Operations on boolean primitives and Boolean objects.
+ * + *This class tries to handle {@code null} input gracefully. + * An exception will not be thrown for a {@code null} input. + * Each method documents its behaviour in more detail.
+ * + *#ThreadSafe#
+ * @since 2.0 + */ +public class BooleanUtils { + + /** + *{@code BooleanUtils} instances should NOT be constructed in standard programming. + * Instead, the class should be used as {@code BooleanUtils.negate(true);}.
+ * + *This constructor is public to permit tools that require a JavaBean instance + * to operate.
+ */ + public BooleanUtils() { + super(); + } + + // Boolean utilities + //-------------------------------------------------------------------------- + /** + *Negates the specified boolean.
+ * + *If {@code null} is passed in, {@code null} will be returned.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.negate(Boolean.TRUE) = Boolean.FALSE; + * BooleanUtils.negate(Boolean.FALSE) = Boolean.TRUE; + * BooleanUtils.negate(null) = null; + *+ * + * @param bool the Boolean to negate, may be null + * @return the negated Boolean, or {@code null} if {@code null} input + */ + public static Boolean negate(final Boolean bool) { + if (bool == null) { + return null; + } + return bool.booleanValue() ? Boolean.FALSE : Boolean.TRUE; + } + + // boolean Boolean methods + //----------------------------------------------------------------------- + /** + *
Checks if a {@code Boolean} value is {@code true}, + * handling {@code null} by returning {@code false}.
+ * + *+ * BooleanUtils.isTrue(Boolean.TRUE) = true + * BooleanUtils.isTrue(Boolean.FALSE) = false + * BooleanUtils.isTrue(null) = false + *+ * + * @param bool the boolean to check, null returns {@code false} + * @return {@code true} only if the input is non-null and true + * @since 2.1 + */ + public static boolean isTrue(final Boolean bool) { + return Boolean.TRUE.equals(bool); + } + + /** + *
Checks if a {@code Boolean} value is not {@code true}, + * handling {@code null} by returning {@code true}.
+ * + *+ * BooleanUtils.isNotTrue(Boolean.TRUE) = false + * BooleanUtils.isNotTrue(Boolean.FALSE) = true + * BooleanUtils.isNotTrue(null) = true + *+ * + * @param bool the boolean to check, null returns {@code true} + * @return {@code true} if the input is null or false + * @since 2.3 + */ + public static boolean isNotTrue(final Boolean bool) { + return !isTrue(bool); + } + + /** + *
Checks if a {@code Boolean} value is {@code false}, + * handling {@code null} by returning {@code false}.
+ * + *+ * BooleanUtils.isFalse(Boolean.TRUE) = false + * BooleanUtils.isFalse(Boolean.FALSE) = true + * BooleanUtils.isFalse(null) = false + *+ * + * @param bool the boolean to check, null returns {@code false} + * @return {@code true} only if the input is non-null and false + * @since 2.1 + */ + public static boolean isFalse(final Boolean bool) { + return Boolean.FALSE.equals(bool); + } + + /** + *
Checks if a {@code Boolean} value is not {@code false}, + * handling {@code null} by returning {@code true}.
+ * + *+ * BooleanUtils.isNotFalse(Boolean.TRUE) = true + * BooleanUtils.isNotFalse(Boolean.FALSE) = false + * BooleanUtils.isNotFalse(null) = true + *+ * + * @param bool the boolean to check, null returns {@code true} + * @return {@code true} if the input is null or true + * @since 2.3 + */ + public static boolean isNotFalse(final Boolean bool) { + return !isFalse(bool); + } + + //----------------------------------------------------------------------- + /** + *
Converts a Boolean to a boolean handling {@code null} + * by returning {@code false}.
+ * + *+ * BooleanUtils.toBoolean(Boolean.TRUE) = true + * BooleanUtils.toBoolean(Boolean.FALSE) = false + * BooleanUtils.toBoolean(null) = false + *+ * + * @param bool the boolean to convert + * @return {@code true} or {@code false}, {@code null} returns {@code false} + */ + public static boolean toBoolean(final Boolean bool) { + return bool != null && bool.booleanValue(); + } + + /** + *
Converts a Boolean to a boolean handling {@code null}.
+ * + *+ * BooleanUtils.toBooleanDefaultIfNull(Boolean.TRUE, false) = true + * BooleanUtils.toBooleanDefaultIfNull(Boolean.FALSE, true) = false + * BooleanUtils.toBooleanDefaultIfNull(null, true) = true + *+ * + * @param bool the boolean to convert + * @param valueIfNull the boolean value to return if {@code null} + * @return {@code true} or {@code false} + */ + public static boolean toBooleanDefaultIfNull(final Boolean bool, final boolean valueIfNull) { + if (bool == null) { + return valueIfNull; + } + return bool.booleanValue(); + } + + // Integer to Boolean methods + //----------------------------------------------------------------------- + /** + *
Converts an int to a boolean using the convention that {@code zero} + * is {@code false}.
+ * + *+ * BooleanUtils.toBoolean(0) = false + * BooleanUtils.toBoolean(1) = true + * BooleanUtils.toBoolean(2) = true + *+ * + * @param value the int to convert + * @return {@code true} if non-zero, {@code false} + * if zero + */ + public static boolean toBoolean(final int value) { + return value != 0; + } + + /** + *
Converts an int to a Boolean using the convention that {@code zero} + * is {@code false}.
+ * + *+ * BooleanUtils.toBoolean(0) = Boolean.FALSE + * BooleanUtils.toBoolean(1) = Boolean.TRUE + * BooleanUtils.toBoolean(2) = Boolean.TRUE + *+ * + * @param value the int to convert + * @return Boolean.TRUE if non-zero, Boolean.FALSE if zero, + * {@code null} if {@code null} + */ + public static Boolean toBooleanObject(final int value) { + return value == 0 ? Boolean.FALSE : Boolean.TRUE; + } + + /** + *
Converts an Integer to a Boolean using the convention that {@code zero} + * is {@code false}.
+ * + *{@code null} will be converted to {@code null}.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.toBoolean(Integer.valueOf(0)) = Boolean.FALSE + * BooleanUtils.toBoolean(Integer.valueOf(1)) = Boolean.TRUE + * BooleanUtils.toBoolean(Integer.valueOf(null)) = null + *+ * + * @param value the Integer to convert + * @return Boolean.TRUE if non-zero, Boolean.FALSE if zero, + * {@code null} if {@code null} input + */ + public static Boolean toBooleanObject(final Integer value) { + if (value == null) { + return null; + } + return value.intValue() == 0 ? Boolean.FALSE : Boolean.TRUE; + } + + /** + *
Converts an int to a boolean specifying the conversion values.
+ * + *+ * BooleanUtils.toBoolean(0, 1, 0) = false + * BooleanUtils.toBoolean(1, 1, 0) = true + * BooleanUtils.toBoolean(2, 1, 2) = false + * BooleanUtils.toBoolean(2, 2, 0) = true + *+ * + * @param value the Integer to convert + * @param trueValue the value to match for {@code true} + * @param falseValue the value to match for {@code false} + * @return {@code true} or {@code false} + * @throws IllegalArgumentException if no match + */ + public static boolean toBoolean(final int value, final int trueValue, final int falseValue) { + if (value == trueValue) { + return true; + } + if (value == falseValue) { + return false; + } + // no match + throw new IllegalArgumentException("The Integer did not match either specified value"); + } + + /** + *
Converts an Integer to a boolean specifying the conversion values.
+ * + *+ * BooleanUtils.toBoolean(Integer.valueOf(0), Integer.valueOf(1), Integer.valueOf(0)) = false + * BooleanUtils.toBoolean(Integer.valueOf(1), Integer.valueOf(1), Integer.valueOf(0)) = true + * BooleanUtils.toBoolean(Integer.valueOf(2), Integer.valueOf(1), Integer.valueOf(2)) = false + * BooleanUtils.toBoolean(Integer.valueOf(2), Integer.valueOf(2), Integer.valueOf(0)) = true + * BooleanUtils.toBoolean(null, null, Integer.valueOf(0)) = true + *+ * + * @param value the Integer to convert + * @param trueValue the value to match for {@code true}, may be {@code null} + * @param falseValue the value to match for {@code false}, may be {@code null} + * @return {@code true} or {@code false} + * @throws IllegalArgumentException if no match + */ + public static boolean toBoolean(final Integer value, final Integer trueValue, final Integer falseValue) { + if (value == null) { + if (trueValue == null) { + return true; + } + if (falseValue == null) { + return false; + } + } else if (value.equals(trueValue)) { + return true; + } else if (value.equals(falseValue)) { + return false; + } + // no match + throw new IllegalArgumentException("The Integer did not match either specified value"); + } + + /** + *
Converts an int to a Boolean specifying the conversion values.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.toBooleanObject(0, 0, 2, 3) = Boolean.TRUE + * BooleanUtils.toBooleanObject(2, 1, 2, 3) = Boolean.FALSE + * BooleanUtils.toBooleanObject(3, 1, 2, 3) = null + *+ * + * @param value the Integer to convert + * @param trueValue the value to match for {@code true} + * @param falseValue the value to match for {@code false} + * @param nullValue the value to to match for {@code null} + * @return Boolean.TRUE, Boolean.FALSE, or {@code null} + * @throws IllegalArgumentException if no match + */ + public static Boolean toBooleanObject(final int value, final int trueValue, final int falseValue, final int nullValue) { + if (value == trueValue) { + return Boolean.TRUE; + } + if (value == falseValue) { + return Boolean.FALSE; + } + if (value == nullValue) { + return null; + } + // no match + throw new IllegalArgumentException("The Integer did not match any specified value"); + } + + /** + *
Converts an Integer to a Boolean specifying the conversion values.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.toBooleanObject(Integer.valueOf(0), Integer.valueOf(0), Integer.valueOf(2), Integer.valueOf(3)) = Boolean.TRUE + * BooleanUtils.toBooleanObject(Integer.valueOf(2), Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(3)) = Boolean.FALSE + * BooleanUtils.toBooleanObject(Integer.valueOf(3), Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(3)) = null + *+ * + * @param value the Integer to convert + * @param trueValue the value to match for {@code true}, may be {@code null} + * @param falseValue the value to match for {@code false}, may be {@code null} + * @param nullValue the value to to match for {@code null}, may be {@code null} + * @return Boolean.TRUE, Boolean.FALSE, or {@code null} + * @throws IllegalArgumentException if no match + */ + public static Boolean toBooleanObject(final Integer value, final Integer trueValue, final Integer falseValue, final Integer nullValue) { + if (value == null) { + if (trueValue == null) { + return Boolean.TRUE; + } + if (falseValue == null) { + return Boolean.FALSE; + } + if (nullValue == null) { + return null; + } + } else if (value.equals(trueValue)) { + return Boolean.TRUE; + } else if (value.equals(falseValue)) { + return Boolean.FALSE; + } else if (value.equals(nullValue)) { + return null; + } + // no match + throw new IllegalArgumentException("The Integer did not match any specified value"); + } + + // Boolean to Integer methods + //----------------------------------------------------------------------- + /** + *
Converts a boolean to an int using the convention that + * {@code zero} is {@code false}.
+ * + *+ * BooleanUtils.toInteger(true) = 1 + * BooleanUtils.toInteger(false) = 0 + *+ * + * @param bool the boolean to convert + * @return one if {@code true}, zero if {@code false} + */ + public static int toInteger(final boolean bool) { + return bool ? 1 : 0; + } + + /** + *
Converts a boolean to an Integer using the convention that + * {@code zero} is {@code false}.
+ * + *+ * BooleanUtils.toIntegerObject(true) = Integer.valueOf(1) + * BooleanUtils.toIntegerObject(false) = Integer.valueOf(0) + *+ * + * @param bool the boolean to convert + * @return one if {@code true}, zero if {@code false} + */ + public static Integer toIntegerObject(final boolean bool) { + return bool ? NumberUtils.INTEGER_ONE : NumberUtils.INTEGER_ZERO; + } + + /** + *
Converts a Boolean to a Integer using the convention that + * {@code zero} is {@code false}.
+ * + *{@code null} will be converted to {@code null}.
+ * + *+ * BooleanUtils.toIntegerObject(Boolean.TRUE) = Integer.valueOf(1) + * BooleanUtils.toIntegerObject(Boolean.FALSE) = Integer.valueOf(0) + *+ * + * @param bool the Boolean to convert + * @return one if Boolean.TRUE, zero if Boolean.FALSE, {@code null} if {@code null} + */ + public static Integer toIntegerObject(final Boolean bool) { + if (bool == null) { + return null; + } + return bool.booleanValue() ? NumberUtils.INTEGER_ONE : NumberUtils.INTEGER_ZERO; + } + + /** + *
Converts a boolean to an int specifying the conversion values.
+ * + *+ * BooleanUtils.toInteger(true, 1, 0) = 1 + * BooleanUtils.toInteger(false, 1, 0) = 0 + *+ * + * @param bool the to convert + * @param trueValue the value to return if {@code true} + * @param falseValue the value to return if {@code false} + * @return the appropriate value + */ + public static int toInteger(final boolean bool, final int trueValue, final int falseValue) { + return bool ? trueValue : falseValue; + } + + /** + *
Converts a Boolean to an int specifying the conversion values.
+ * + *+ * BooleanUtils.toInteger(Boolean.TRUE, 1, 0, 2) = 1 + * BooleanUtils.toInteger(Boolean.FALSE, 1, 0, 2) = 0 + * BooleanUtils.toInteger(null, 1, 0, 2) = 2 + *+ * + * @param bool the Boolean to convert + * @param trueValue the value to return if {@code true} + * @param falseValue the value to return if {@code false} + * @param nullValue the value to return if {@code null} + * @return the appropriate value + */ + public static int toInteger(final Boolean bool, final int trueValue, final int falseValue, final int nullValue) { + if (bool == null) { + return nullValue; + } + return bool.booleanValue() ? trueValue : falseValue; + } + + /** + *
Converts a boolean to an Integer specifying the conversion values.
+ * + *+ * BooleanUtils.toIntegerObject(true, Integer.valueOf(1), Integer.valueOf(0)) = Integer.valueOf(1) + * BooleanUtils.toIntegerObject(false, Integer.valueOf(1), Integer.valueOf(0)) = Integer.valueOf(0) + *+ * + * @param bool the to convert + * @param trueValue the value to return if {@code true}, may be {@code null} + * @param falseValue the value to return if {@code false}, may be {@code null} + * @return the appropriate value + */ + public static Integer toIntegerObject(final boolean bool, final Integer trueValue, final Integer falseValue) { + return bool ? trueValue : falseValue; + } + + /** + *
Converts a Boolean to an Integer specifying the conversion values.
+ * + *+ * BooleanUtils.toIntegerObject(Boolean.TRUE, Integer.valueOf(1), Integer.valueOf(0), Integer.valueOf(2)) = Integer.valueOf(1) + * BooleanUtils.toIntegerObject(Boolean.FALSE, Integer.valueOf(1), Integer.valueOf(0), Integer.valueOf(2)) = Integer.valueOf(0) + * BooleanUtils.toIntegerObject(null, Integer.valueOf(1), Integer.valueOf(0), Integer.valueOf(2)) = Integer.valueOf(2) + *+ * + * @param bool the Boolean to convert + * @param trueValue the value to return if {@code true}, may be {@code null} + * @param falseValue the value to return if {@code false}, may be {@code null} + * @param nullValue the value to return if {@code null}, may be {@code null} + * @return the appropriate value + */ + public static Integer toIntegerObject(final Boolean bool, final Integer trueValue, final Integer falseValue, final Integer nullValue) { + if (bool == null) { + return nullValue; + } + return bool.booleanValue() ? trueValue : falseValue; + } + + // String to Boolean methods + //----------------------------------------------------------------------- + /** + *
Converts a String to a Boolean.
+ * + *{@code 'true'}, {@code 'on'}, {@code 'y'}, {@code 't'} or {@code 'yes'} + * (case insensitive) will return {@code true}. + * {@code 'false'}, {@code 'off'}, {@code 'n'}, {@code 'f'} or {@code 'no'} + * (case insensitive) will return {@code false}. + * Otherwise, {@code null} is returned.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * // N.B. case is not significant + * BooleanUtils.toBooleanObject(null) = null + * BooleanUtils.toBooleanObject("true") = Boolean.TRUE + * BooleanUtils.toBooleanObject("T") = Boolean.TRUE // i.e. T[RUE] + * BooleanUtils.toBooleanObject("false") = Boolean.FALSE + * BooleanUtils.toBooleanObject("f") = Boolean.FALSE // i.e. f[alse] + * BooleanUtils.toBooleanObject("No") = Boolean.FALSE + * BooleanUtils.toBooleanObject("n") = Boolean.FALSE // i.e. n[o] + * BooleanUtils.toBooleanObject("on") = Boolean.TRUE + * BooleanUtils.toBooleanObject("ON") = Boolean.TRUE + * BooleanUtils.toBooleanObject("off") = Boolean.FALSE + * BooleanUtils.toBooleanObject("oFf") = Boolean.FALSE + * BooleanUtils.toBooleanObject("yes") = Boolean.TRUE + * BooleanUtils.toBooleanObject("Y") = Boolean.TRUE // i.e. Y[ES] + * BooleanUtils.toBooleanObject("blue") = null + * BooleanUtils.toBooleanObject("true ") = null // trailing space (too long) + * BooleanUtils.toBooleanObject("ono") = null // does not match on or no + *+ * + * @param str the String to check; upper and lower case are treated as the same + * @return the Boolean value of the string, {@code null} if no match or {@code null} input + */ + public static Boolean toBooleanObject(final String str) { + // Previously used equalsIgnoreCase, which was fast for interned 'true'. + // Non interned 'true' matched 15 times slower. + // + // Optimisation provides same performance as before for interned 'true'. + // Similar performance for null, 'false', and other strings not length 2/3/4. + // 'true'/'TRUE' match 4 times slower, 'tRUE'/'True' 7 times slower. + if (str == "true") { + return Boolean.TRUE; + } + if (str == null) { + return null; + } + switch (str.length()) { + case 1: { + final char ch0 = str.charAt(0); + if (ch0 == 'y' || ch0 == 'Y' || + ch0 == 't' || ch0 == 'T') { + return Boolean.TRUE; + } + if (ch0 == 'n' || ch0 == 'N' || + ch0 == 'f' || ch0 == 'F') { + return Boolean.FALSE; + } + break; + } + case 2: { + final char ch0 = str.charAt(0); + final char ch1 = str.charAt(1); + if ((ch0 == 'o' || ch0 == 'O') && + (ch1 == 'n' || ch1 == 'N') ) { + return Boolean.TRUE; + } + if ((ch0 == 'n' || ch0 == 'N') && + (ch1 == 'o' || ch1 == 'O') ) { + return Boolean.FALSE; + } + break; + } + case 3: { + final char ch0 = str.charAt(0); + final char ch1 = str.charAt(1); + final char ch2 = str.charAt(2); + if ((ch0 == 'y' || ch0 == 'Y') && + (ch1 == 'e' || ch1 == 'E') && + (ch2 == 's' || ch2 == 'S') ) { + return Boolean.TRUE; + } + if ((ch0 == 'o' || ch0 == 'O') && + (ch1 == 'f' || ch1 == 'F') && + (ch2 == 'f' || ch2 == 'F') ) { + return Boolean.FALSE; + } + break; + } + case 4: { + final char ch0 = str.charAt(0); + final char ch1 = str.charAt(1); + final char ch2 = str.charAt(2); + final char ch3 = str.charAt(3); + if ((ch0 == 't' || ch0 == 'T') && + (ch1 == 'r' || ch1 == 'R') && + (ch2 == 'u' || ch2 == 'U') && + (ch3 == 'e' || ch3 == 'E') ) { + return Boolean.TRUE; + } + break; + } + case 5: { + final char ch0 = str.charAt(0); + final char ch1 = str.charAt(1); + final char ch2 = str.charAt(2); + final char ch3 = str.charAt(3); + final char ch4 = str.charAt(4); + if ((ch0 == 'f' || ch0 == 'F') && + (ch1 == 'a' || ch1 == 'A') && + (ch2 == 'l' || ch2 == 'L') && + (ch3 == 's' || ch3 == 'S') && + (ch4 == 'e' || ch4 == 'E') ) { + return Boolean.FALSE; + } + break; + } + default: + break; + } + + return null; + } + + /** + *
Converts a String to a Boolean throwing an exception if no match.
+ * + *NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
+ * + *+ * BooleanUtils.toBooleanObject("true", "true", "false", "null") = Boolean.TRUE + * BooleanUtils.toBooleanObject("false", "true", "false", "null") = Boolean.FALSE + * BooleanUtils.toBooleanObject("null", "true", "false", "null") = null + *+ * + * @param str the String to check + * @param trueString the String to match for {@code true} (case sensitive), may be {@code null} + * @param falseString the String to match for {@code false} (case sensitive), may be {@code null} + * @param nullString the String to match for {@code null} (case sensitive), may be {@code null} + * @return the Boolean value of the string, {@code null} if either the String matches {@code nullString} + * or if {@code null} input and {@code nullString} is {@code null} + * @throws IllegalArgumentException if the String doesn't match + */ + public static Boolean toBooleanObject(final String str, final String trueString, final String falseString, final String nullString) { + if (str == null) { + if (trueString == null) { + return Boolean.TRUE; + } + if (falseString == null) { + return Boolean.FALSE; + } + if (nullString == null) { + return null; + } + } else if (str.equals(trueString)) { + return Boolean.TRUE; + } else if (str.equals(falseString)) { + return Boolean.FALSE; + } else if (str.equals(nullString)) { + return null; + } + // no match + throw new IllegalArgumentException("The String did not match any specified value"); + } + + // String to boolean methods + //----------------------------------------------------------------------- + /** + *
Converts a String to a boolean (optimised for performance).
+ * + *{@code 'true'}, {@code 'on'}, {@code 'y'}, {@code 't'} or {@code 'yes'} + * (case insensitive) will return {@code true}. Otherwise, + * {@code false} is returned.
+ * + *This method performs 4 times faster (JDK1.4) than + * {@code Boolean.valueOf(String)}. However, this method accepts + * 'on' and 'yes', 't', 'y' as true values. + * + *
+ * BooleanUtils.toBoolean(null) = false + * BooleanUtils.toBoolean("true") = true + * BooleanUtils.toBoolean("TRUE") = true + * BooleanUtils.toBoolean("tRUe") = true + * BooleanUtils.toBoolean("on") = true + * BooleanUtils.toBoolean("yes") = true + * BooleanUtils.toBoolean("false") = false + * BooleanUtils.toBoolean("x gti") = false + * BooleanUtils.toBooleanObject("y") = true + * BooleanUtils.toBooleanObject("n") = false + * BooleanUtils.toBooleanObject("t") = true + * BooleanUtils.toBooleanObject("f") = false + *+ * + * @param str the String to check + * @return the boolean value of the string, {@code false} if no match or the String is null + */ + public static boolean toBoolean(final String str) { + return toBooleanObject(str) == Boolean.TRUE; + } + + /** + *
Converts a String to a Boolean throwing an exception if no match found.
+ * + *+ * BooleanUtils.toBoolean("true", "true", "false") = true + * BooleanUtils.toBoolean("false", "true", "false") = false + *+ * + * @param str the String to check + * @param trueString the String to match for {@code true} (case sensitive), may be {@code null} + * @param falseString the String to match for {@code false} (case sensitive), may be {@code null} + * @return the boolean value of the string + * @throws IllegalArgumentException if the String doesn't match + */ + public static boolean toBoolean(final String str, final String trueString, final String falseString) { + if (str == trueString) { + return true; + } else if (str == falseString) { + return false; + } else if (str != null) { + if (str.equals(trueString)) { + return true; + } else if (str.equals(falseString)) { + return false; + } + } + // no match + throw new IllegalArgumentException("The String did not match either specified value"); + } + + // Boolean to String methods + //----------------------------------------------------------------------- + /** + *
Converts a Boolean to a String returning {@code 'true'}, + * {@code 'false'}, or {@code null}.
+ * + *+ * BooleanUtils.toStringTrueFalse(Boolean.TRUE) = "true" + * BooleanUtils.toStringTrueFalse(Boolean.FALSE) = "false" + * BooleanUtils.toStringTrueFalse(null) = null; + *+ * + * @param bool the Boolean to check + * @return {@code 'true'}, {@code 'false'}, or {@code null} + */ + public static String toStringTrueFalse(final Boolean bool) { + return toString(bool, "true", "false", null); + } + + /** + *
Converts a Boolean to a String returning {@code 'on'}, + * {@code 'off'}, or {@code null}.
+ * + *+ * BooleanUtils.toStringOnOff(Boolean.TRUE) = "on" + * BooleanUtils.toStringOnOff(Boolean.FALSE) = "off" + * BooleanUtils.toStringOnOff(null) = null; + *+ * + * @param bool the Boolean to check + * @return {@code 'on'}, {@code 'off'}, or {@code null} + */ + public static String toStringOnOff(final Boolean bool) { + return toString(bool, "on", "off", null); + } + + /** + *
Converts a Boolean to a String returning {@code 'yes'}, + * {@code 'no'}, or {@code null}.
+ * + *+ * BooleanUtils.toStringYesNo(Boolean.TRUE) = "yes" + * BooleanUtils.toStringYesNo(Boolean.FALSE) = "no" + * BooleanUtils.toStringYesNo(null) = null; + *+ * + * @param bool the Boolean to check + * @return {@code 'yes'}, {@code 'no'}, or {@code null} + */ + public static String toStringYesNo(final Boolean bool) { + return toString(bool, "yes", "no", null); + } + + /** + *
Converts a Boolean to a String returning one of the input Strings.
+ * + *+ * BooleanUtils.toString(Boolean.TRUE, "true", "false", null) = "true" + * BooleanUtils.toString(Boolean.FALSE, "true", "false", null) = "false" + * BooleanUtils.toString(null, "true", "false", null) = null; + *+ * + * @param bool the Boolean to check + * @param trueString the String to return if {@code true}, may be {@code null} + * @param falseString the String to return if {@code false}, may be {@code null} + * @param nullString the String to return if {@code null}, may be {@code null} + * @return one of the three input Strings + */ + public static String toString(final Boolean bool, final String trueString, final String falseString, final String nullString) { + if (bool == null) { + return nullString; + } + return bool.booleanValue() ? trueString : falseString; + } + + // boolean to String methods + //----------------------------------------------------------------------- + /** + *
Converts a boolean to a String returning {@code 'true'} + * or {@code 'false'}.
+ * + *+ * BooleanUtils.toStringTrueFalse(true) = "true" + * BooleanUtils.toStringTrueFalse(false) = "false" + *+ * + * @param bool the Boolean to check + * @return {@code 'true'}, {@code 'false'}, or {@code null} + */ + public static String toStringTrueFalse(final boolean bool) { + return toString(bool, "true", "false"); + } + + /** + *
Converts a boolean to a String returning {@code 'on'} + * or {@code 'off'}.
+ * + *+ * BooleanUtils.toStringOnOff(true) = "on" + * BooleanUtils.toStringOnOff(false) = "off" + *+ * + * @param bool the Boolean to check + * @return {@code 'on'}, {@code 'off'}, or {@code null} + */ + public static String toStringOnOff(final boolean bool) { + return toString(bool, "on", "off"); + } + + /** + *
Converts a boolean to a String returning {@code 'yes'} + * or {@code 'no'}.
+ * + *+ * BooleanUtils.toStringYesNo(true) = "yes" + * BooleanUtils.toStringYesNo(false) = "no" + *+ * + * @param bool the Boolean to check + * @return {@code 'yes'}, {@code 'no'}, or {@code null} + */ + public static String toStringYesNo(final boolean bool) { + return toString(bool, "yes", "no"); + } + + /** + *
Converts a boolean to a String returning one of the input Strings.
+ * + *+ * BooleanUtils.toString(true, "true", "false") = "true" + * BooleanUtils.toString(false, "true", "false") = "false" + *+ * + * @param bool the Boolean to check + * @param trueString the String to return if {@code true}, may be {@code null} + * @param falseString the String to return if {@code false}, may be {@code null} + * @return one of the two input Strings + */ + public static String toString(final boolean bool, final String trueString, final String falseString) { + return bool ? trueString : falseString; + } + + // logical operations + // ---------------------------------------------------------------------- + /** + *
Performs an and on a set of booleans.
+ * + *+ * BooleanUtils.and(true, true) = true + * BooleanUtils.and(false, false) = false + * BooleanUtils.and(true, false) = false + * BooleanUtils.and(true, true, false) = false + * BooleanUtils.and(true, true, true) = true + *+ * + * @param array an array of {@code boolean}s + * @return {@code true} if the and is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + * @since 3.0.1 + */ + public static boolean and(final boolean... array) { + // Validates input + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + for (final boolean element : array) { + if (!element) { + return false; + } + } + return true; + } + + /** + *
Performs an and on an array of Booleans.
+ * + *+ * BooleanUtils.and(Boolean.TRUE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.and(Boolean.FALSE, Boolean.FALSE) = Boolean.FALSE + * BooleanUtils.and(Boolean.TRUE, Boolean.FALSE) = Boolean.FALSE + * BooleanUtils.and(Boolean.TRUE, Boolean.TRUE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.and(Boolean.FALSE, Boolean.FALSE, Boolean.TRUE) = Boolean.FALSE + * BooleanUtils.and(Boolean.TRUE, Boolean.FALSE, Boolean.TRUE) = Boolean.FALSE + *+ * + * @param array an array of {@code Boolean}s + * @return {@code true} if the and is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + * @throws IllegalArgumentException if {@code array} contains a {@code null} + * @since 3.0.1 + */ + public static Boolean and(final Boolean... array) { + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + try { + final boolean[] primitive = ArrayUtils.toPrimitive(array); + return and(primitive) ? Boolean.TRUE : Boolean.FALSE; + } catch (final NullPointerException ex) { + throw new IllegalArgumentException("The array must not contain any null elements"); + } + } + + /** + *
Performs an or on a set of booleans.
+ * + *+ * BooleanUtils.or(true, true) = true + * BooleanUtils.or(false, false) = false + * BooleanUtils.or(true, false) = true + * BooleanUtils.or(true, true, false) = true + * BooleanUtils.or(true, true, true) = true + * BooleanUtils.or(false, false, false) = false + *+ * + * @param array an array of {@code boolean}s + * @return {@code true} if the or is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + * @since 3.0.1 + */ +/** + *
Performs an or on a set of booleans.
+ * + *+ * BooleanUtils.or(true, true) = true + * BooleanUtils.or(false, false) = false + * BooleanUtils.or(true, false) = true + * BooleanUtils.or(true, true, false) = true + * BooleanUtils.or(true, true, true) = true + * BooleanUtils.or(false, false, false) = false + *+ * + * @param array + * an array of {@code boolean}s + * @return {@code true} if the or is successful. + * @throws IllegalArgumentException + * if {@code array} is {@code null} + * @throws IllegalArgumentException + * if {@code array} is empty. + * @since 3.0.1 + */ +public static boolean or(final boolean... array) { + { + if (/* NPEX_NULL_EXP */ + array.length == 0) { + throw new java.lang.IllegalArgumentException("Array is empty"); + } + for (final boolean element : array) { + if (element) { + return true; + } + } + return false; + } +} + + /** + *
Performs an or on an array of Booleans.
+ * + *+ * BooleanUtils.or(Boolean.TRUE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.or(Boolean.FALSE, Boolean.FALSE) = Boolean.FALSE + * BooleanUtils.or(Boolean.TRUE, Boolean.FALSE) = Boolean.TRUE + * BooleanUtils.or(Boolean.TRUE, Boolean.TRUE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.or(Boolean.FALSE, Boolean.FALSE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.or(Boolean.TRUE, Boolean.FALSE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtils.or(Boolean.FALSE, Boolean.FALSE, Boolean.FALSE) = Boolean.FALSE + *+ * + * @param array an array of {@code Boolean}s + * @return {@code true} if the or is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + * @throws IllegalArgumentException if {@code array} contains a {@code null} + * @since 3.0.1 + */ + public static Boolean or(final Boolean... array) { + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + try { + final boolean[] primitive = ArrayUtils.toPrimitive(array); + return or(primitive) ? Boolean.TRUE : Boolean.FALSE; + } catch (final NullPointerException ex) { + throw new IllegalArgumentException("The array must not contain any null elements"); + } + } + + /** + *
Performs an xor on a set of booleans.
+ * + *+ * BooleanUtils.xor(true, true) = false + * BooleanUtils.xor(false, false) = false + * BooleanUtils.xor(true, false) = true + * BooleanUtils.xor(true, true) = false + * BooleanUtils.xor(false, false) = false + * BooleanUtils.xor(true, false) = true + *+ * + * @param array an array of {@code boolean}s + * @return {@code true} if the xor is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + */ + public static boolean xor(final boolean... array) { + // Validates input + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + + // false if the neutral element of the xor operator + boolean result = false; + for (final boolean element : array) { + result ^= element; + } + + return result; + } + + /** + *
Performs an xor on an array of Booleans.
+ * + *+ * BooleanUtils.xor(new Boolean[] { Boolean.TRUE, Boolean.TRUE }) = Boolean.FALSE + * BooleanUtils.xor(new Boolean[] { Boolean.FALSE, Boolean.FALSE }) = Boolean.FALSE + * BooleanUtils.xor(new Boolean[] { Boolean.TRUE, Boolean.FALSE }) = Boolean.TRUE + *+ * + * @param array an array of {@code Boolean}s + * @return {@code true} if the xor is successful. + * @throws IllegalArgumentException if {@code array} is {@code null} + * @throws IllegalArgumentException if {@code array} is empty. + * @throws IllegalArgumentException if {@code array} contains a {@code null} + */ + public static Boolean xor(final Boolean... array) { + if (array == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + if (array.length == 0) { + throw new IllegalArgumentException("Array is empty"); + } + try { + final boolean[] primitive = ArrayUtils.toPrimitive(array); + return xor(primitive) ? Boolean.TRUE : Boolean.FALSE; + } catch (final NullPointerException ex) { + throw new IllegalArgumentException("The array must not contain any null elements"); + } + } + + /** + *
Compares two {@code boolean} values. This is the same functionality as provided in Java 7.
+ * + * @param x the first {@code boolean} to compare + * @param y the second {@code boolean} to compare + * @return the value {@code 0} if {@code x == y}; + * a value less than {@code 0} if {@code !x && y}; and + * a value greater than {@code 0} if {@code x && !y} + * @since 3.4 + */ + public static int compare(boolean x, boolean y) { + if (x == y) { + return 0; + } + return x ? 1 : -1; + } + +} diff --git a/Java/commons-lang-BooleanUtils_973/metadata.json b/Java/commons-lang-BooleanUtils_973/metadata.json new file mode 100644 index 000000000..34707fae6 --- /dev/null +++ b/Java/commons-lang-BooleanUtils_973/metadata.json @@ -0,0 +1,21 @@ +{ + "language": "java", + "id": "commons-lang-BooleanUtils_973", + "buggyPath": ".", + "referencePath": null, + "buildCommand": "mvn package -V -B -Denforcer.skip=true -Dcheckstyle.skip=true -Dcobertura.skip=true -Drat.skip=true -Dlicense.skip=true -Dfindbugs.skip=true -Dgpg.skip=true -Dskip.npm=true -Dskip.gulp=true -Dskip.bower=true -Drat.numUnapprovedLicenses=100 -DskipTests=true -DskipITs=true -Dtest=None -DfailIfNoTests=false", + "testCommand": "mvn test -V -B -Denforcer.skip=true -Dcheckstyle.skip=true -Dcobertura.skip=true -Drat.skip=true -Dlicense.skip=true -Dfindbugs.skip=true -Dgpg.skip=true -Dskip.npm=true -Dskip.gulp=true -Dskip.bower=true -Drat.numUnapprovedLicenses=100", + "categories": [ + "safety", + "npe" + ], + "npe": { + "filepath": "src/main/java/org/apache/commons/lang3/BooleanUtils.java", + "line": 995, + "npe_method": "or", + "deref_field": "array", + "npe_class": "BooleanUtils", + "repo": "commons-lang", + "bug_id": "BooleanUtils_973" + } +} diff --git a/Java/commons-lang-BooleanUtils_973/npe.json b/Java/commons-lang-BooleanUtils_973/npe.json new file mode 100644 index 000000000..e17bd8c02 --- /dev/null +++ b/Java/commons-lang-BooleanUtils_973/npe.json @@ -0,0 +1,7 @@ +{ + "filepath": "src/main/java/org/apache/commons/lang3/BooleanUtils.java", + "line": 995, + "npe_method": "or", + "deref_field": "array", + "npe_class": "BooleanUtils" +} \ No newline at end of file