You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Benedikt Ritter <br...@apache.org> on 2013/10/14 21:22:49 UTC

Fwd: svn commit: r1532031 - /commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/BooleanUtils.java

Thoughts? Maybe this should be renamed to "isOneTrue(boolean...)" in the
next major release?!

---------- Forwarded message ----------
From: <br...@apache.org>
Date: 2013/10/14
Subject: svn commit: r1532031 -
/commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/BooleanUtils.java
To: commits@commons.apache.org


Author: britter
Date: Mon Oct 14 19:21:02 2013
New Revision: 1532031

URL: http://svn.apache.org/r1532031
Log:
Document (wrong?) behavior of xor methods

Modified:

commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/BooleanUtils.java

Modified:
commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/BooleanUtils.java
URL:
http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/BooleanUtils.java?rev=1532031&r1=1532030&r2=1532031&view=diff
==============================================================================
---
commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/BooleanUtils.java
(original)
+++
commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/BooleanUtils.java
Mon Oct 14 19:21:02 2013
@@ -1028,6 +1028,14 @@ public class BooleanUtils {
      *   BooleanUtils.xor(true, false)  = true
      * </pre>
      *
+     * <p>Note that this method behaves different from using the binary
XOR operator (^). Instead of combining the given
+     * booleans using the XOR operator from left to right, this method
counts the appearances of true in the given
+     * array. It will only return true if exactly one boolean in the given
array is true:</p>
+     * <pre>
+     *   true ^ true ^ false ^ true                 = true
+     *   BooleanUtils.xor(true, true, false, true)  = false
+     * </pre>
+     *
      * @param array  an array of {@code boolean}s
      * @return {@code true} if the xor is successful.
      * @throws IllegalArgumentException if {@code array} is {@code null}
@@ -1069,6 +1077,9 @@ public class BooleanUtils {
      *   BooleanUtils.xor(new Boolean[] { Boolean.TRUE, Boolean.FALSE })
 = Boolean.TRUE
      * </pre>
      *
+     * <p>Note that this method behaves different from using the binary
XOR operator (^). See
+     * {@link #xor(boolean...)}.</p>
+     *
      * @param array  an array of {@code Boolean}s
      * @return {@code true} if the xor is successful.
      * @throws IllegalArgumentException if {@code array} is {@code null}





-- 
http://people.apache.org/~britter/
http://www.systemoutprintln.de/
http://twitter.com/BenediktRitter
http://github.com/britter

Re: svn commit: r1532031 - /commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/BooleanUtils.java

Posted by Henri Yandell <fl...@gmail.com>.
Feels like the new method is a more general Array one. Not sure if we have
it already.

ArrayUtils.countMatching(array, value) == 1

Note also that 'isOneTrue' doesn't mean the same as 'isOnlyOneTrue'.

Hen

On Tuesday, October 15, 2013, Benedikt Ritter wrote:

> I have created LANG-921 and am currently working on this. I prefer to make
> the behavior correct as Hen has suggested and add two new method isOneTrue
> and isOneFalse.
>
>
> 2013/10/15 Matt Benson <gu...@gmail.com>
>
> > Another alternative:  preserve the current #xor() method with
> > documentation, and implement the ^ analogue as #binaryXor().
> >
> > Matt
> >
> >
> > On Tue, Oct 15, 2013 at 9:05 AM, Matt Benson <gu...@gmail.com>
> wrote:
> >
> > > In this case we should perhaps provide a #oneTrue(boolean...) method to
> > > preserve the availability of the current behavior, which may also
> > > occasionally have its place.
> > >
> > > Matt
> > >
> > >
> > > On Mon, Oct 14, 2013 at 11:10 PM, Henri Yandell <flamefew@gmail.com
> > >wrote:
> > >
> > >> I think we treat it as a bug and fix. The code is fine for the
> explained
> > >> use of an array of length 2, but fails when you move to larger chains.
> > >> Given the common meaning of the name, I think people would have
> noticed
> > if
> > >> they'd needed xor to work for arrays > 2 and it didn't. So a) I think
> we
> > >> can fix and b) I suspect it's not a highly used method.
> > >>
> > >> So rather than the note, I'm +1 to fixing the code.
> > >>
> > >> Hen
> > >>
> > >>
> > >> On Mon, Oct 14, 2013 at 12:22 PM, Benedikt Ritter <britter@apache.org
> > >> >wrote:
> > >>
> > >> > Thoughts? Maybe this should be renamed to "isOneTrue(boolean...)" in
> > the
> > >> > next major release?!
> > >> >
> > >> > ---------- Forwarded message ----------
> > >> > From: <br...@apache.org>
> > >> > Date: 2013/10/14
> > >> > Subject: svn commit: r1532031 -
> > >> >
> > >> >
> > >>
> >
> /commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/BooleanUtils.java
> > >> > To: commits@commons.apache.org
> > >> >
> > >> >
> > >> > Author: britter
> > >> > Date: Mon Oct 14 19:21:02 2013
> > >> > New Revision: 1532031
> > >> >
> > >> > URL: http://svn.apache.org/r1532031
> > >> > Log:
> > >> > Document (wrong?) behavior of xor methods
> > >> >
> > >> > Modified:
> > >> >
> > >> >
> > >> >
> > >>
> >
> commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/BooleanUtils.java
> > >> >
> > >> > Modified:
> > >> >
> > >> >
> > >>
> >
> commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/BooleanUtils.java
> > >> > URL:
> > >> >
> > >> >
> > >>
> >
> http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/BooleanUtils.java?rev=1532031&r1=1532030&r2=1532031&view=diff
> > >> >
> > >> >
> > >>
> >
> ==============================================================================
> > >> > ---
> > >> >
> > >> >
> > >>
> >
> commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/BooleanUtils.java
> > >> > (original)
> > >> > +++
> > >> >
> > >> >
> > >>
> >
> commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/BooleanUtils.java
> > >> > Mon Oct 14 19:21:02 2013
> > >> > @@ -1028,6 +1028,14 @@ public class BooleanUtils {
> > >> >       *   BooleanUtils.xor(true, false)  = true
> > >> >       * </pre>
> > >> >       *
> > >

Re: svn commit: r1532031 - /commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/BooleanUtils.java

Posted by Benedikt Ritter <br...@apache.org>.
I have created LANG-921 and am currently working on this. I prefer to make
the behavior correct as Hen has suggested and add two new method isOneTrue
and isOneFalse.


2013/10/15 Matt Benson <gu...@gmail.com>

> Another alternative:  preserve the current #xor() method with
> documentation, and implement the ^ analogue as #binaryXor().
>
> Matt
>
>
> On Tue, Oct 15, 2013 at 9:05 AM, Matt Benson <gu...@gmail.com> wrote:
>
> > In this case we should perhaps provide a #oneTrue(boolean...) method to
> > preserve the availability of the current behavior, which may also
> > occasionally have its place.
> >
> > Matt
> >
> >
> > On Mon, Oct 14, 2013 at 11:10 PM, Henri Yandell <flamefew@gmail.com
> >wrote:
> >
> >> I think we treat it as a bug and fix. The code is fine for the explained
> >> use of an array of length 2, but fails when you move to larger chains.
> >> Given the common meaning of the name, I think people would have noticed
> if
> >> they'd needed xor to work for arrays > 2 and it didn't. So a) I think we
> >> can fix and b) I suspect it's not a highly used method.
> >>
> >> So rather than the note, I'm +1 to fixing the code.
> >>
> >> Hen
> >>
> >>
> >> On Mon, Oct 14, 2013 at 12:22 PM, Benedikt Ritter <britter@apache.org
> >> >wrote:
> >>
> >> > Thoughts? Maybe this should be renamed to "isOneTrue(boolean...)" in
> the
> >> > next major release?!
> >> >
> >> > ---------- Forwarded message ----------
> >> > From: <br...@apache.org>
> >> > Date: 2013/10/14
> >> > Subject: svn commit: r1532031 -
> >> >
> >> >
> >>
> /commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/BooleanUtils.java
> >> > To: commits@commons.apache.org
> >> >
> >> >
> >> > Author: britter
> >> > Date: Mon Oct 14 19:21:02 2013
> >> > New Revision: 1532031
> >> >
> >> > URL: http://svn.apache.org/r1532031
> >> > Log:
> >> > Document (wrong?) behavior of xor methods
> >> >
> >> > Modified:
> >> >
> >> >
> >> >
> >>
> commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/BooleanUtils.java
> >> >
> >> > Modified:
> >> >
> >> >
> >>
> commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/BooleanUtils.java
> >> > URL:
> >> >
> >> >
> >>
> http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/BooleanUtils.java?rev=1532031&r1=1532030&r2=1532031&view=diff
> >> >
> >> >
> >>
> ==============================================================================
> >> > ---
> >> >
> >> >
> >>
> commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/BooleanUtils.java
> >> > (original)
> >> > +++
> >> >
> >> >
> >>
> commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/BooleanUtils.java
> >> > Mon Oct 14 19:21:02 2013
> >> > @@ -1028,6 +1028,14 @@ public class BooleanUtils {
> >> >       *   BooleanUtils.xor(true, false)  = true
> >> >       * </pre>
> >> >       *
> >> > +     * <p>Note that this method behaves different from using the
> binary
> >> > XOR operator (^). Instead of combining the given
> >> > +     * booleans using the XOR operator from left to right, this
> method
> >> > counts the appearances of true in the given
> >> > +     * array. It will only return true if exactly one boolean in the
> >> given
> >> > array is true:</p>
> >> > +     * <pre>
> >> > +     *   true ^ true ^ false ^ true                 = true
> >> > +     *   BooleanUtils.xor(true, true, false, true)  = false
> >> > +     * </pre>
> >> > +     *
> >> >       * @param array  an array of {@code boolean}s
> >> >       * @return {@code true} if the xor is successful.
> >> >       * @throws IllegalArgumentException if {@code array} is {@code
> >> null}
> >> > @@ -1069,6 +1077,9 @@ public class BooleanUtils {
> >> >       *   BooleanUtils.xor(new Boolean[] { Boolean.TRUE, Boolean.FALSE
> >> })
> >> >  = Boolean.TRUE
> >> >       * </pre>
> >> >       *
> >> > +     * <p>Note that this method behaves different from using the
> binary
> >> > XOR operator (^). See
> >> > +     * {@link #xor(boolean...)}.</p>
> >> > +     *
> >> >       * @param array  an array of {@code Boolean}s
> >> >       * @return {@code true} if the xor is successful.
> >> >       * @throws IllegalArgumentException if {@code array} is {@code
> >> null}
> >> >
> >> >
> >> >
> >> >
> >> >
> >> > --
> >> > http://people.apache.org/~britter/
> >> > http://www.systemoutprintln.de/
> >> > http://twitter.com/BenediktRitter
> >> > http://github.com/britter
> >> >
> >>
> >
> >
>



-- 
http://people.apache.org/~britter/
http://www.systemoutprintln.de/
http://twitter.com/BenediktRitter
http://github.com/britter

Re: svn commit: r1532031 - /commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/BooleanUtils.java

Posted by Matt Benson <gu...@gmail.com>.
Another alternative:  preserve the current #xor() method with
documentation, and implement the ^ analogue as #binaryXor().

Matt


On Tue, Oct 15, 2013 at 9:05 AM, Matt Benson <gu...@gmail.com> wrote:

> In this case we should perhaps provide a #oneTrue(boolean...) method to
> preserve the availability of the current behavior, which may also
> occasionally have its place.
>
> Matt
>
>
> On Mon, Oct 14, 2013 at 11:10 PM, Henri Yandell <fl...@gmail.com>wrote:
>
>> I think we treat it as a bug and fix. The code is fine for the explained
>> use of an array of length 2, but fails when you move to larger chains.
>> Given the common meaning of the name, I think people would have noticed if
>> they'd needed xor to work for arrays > 2 and it didn't. So a) I think we
>> can fix and b) I suspect it's not a highly used method.
>>
>> So rather than the note, I'm +1 to fixing the code.
>>
>> Hen
>>
>>
>> On Mon, Oct 14, 2013 at 12:22 PM, Benedikt Ritter <britter@apache.org
>> >wrote:
>>
>> > Thoughts? Maybe this should be renamed to "isOneTrue(boolean...)" in the
>> > next major release?!
>> >
>> > ---------- Forwarded message ----------
>> > From: <br...@apache.org>
>> > Date: 2013/10/14
>> > Subject: svn commit: r1532031 -
>> >
>> >
>> /commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/BooleanUtils.java
>> > To: commits@commons.apache.org
>> >
>> >
>> > Author: britter
>> > Date: Mon Oct 14 19:21:02 2013
>> > New Revision: 1532031
>> >
>> > URL: http://svn.apache.org/r1532031
>> > Log:
>> > Document (wrong?) behavior of xor methods
>> >
>> > Modified:
>> >
>> >
>> >
>> commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/BooleanUtils.java
>> >
>> > Modified:
>> >
>> >
>> commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/BooleanUtils.java
>> > URL:
>> >
>> >
>> http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/BooleanUtils.java?rev=1532031&r1=1532030&r2=1532031&view=diff
>> >
>> >
>> ==============================================================================
>> > ---
>> >
>> >
>> commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/BooleanUtils.java
>> > (original)
>> > +++
>> >
>> >
>> commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/BooleanUtils.java
>> > Mon Oct 14 19:21:02 2013
>> > @@ -1028,6 +1028,14 @@ public class BooleanUtils {
>> >       *   BooleanUtils.xor(true, false)  = true
>> >       * </pre>
>> >       *
>> > +     * <p>Note that this method behaves different from using the binary
>> > XOR operator (^). Instead of combining the given
>> > +     * booleans using the XOR operator from left to right, this method
>> > counts the appearances of true in the given
>> > +     * array. It will only return true if exactly one boolean in the
>> given
>> > array is true:</p>
>> > +     * <pre>
>> > +     *   true ^ true ^ false ^ true                 = true
>> > +     *   BooleanUtils.xor(true, true, false, true)  = false
>> > +     * </pre>
>> > +     *
>> >       * @param array  an array of {@code boolean}s
>> >       * @return {@code true} if the xor is successful.
>> >       * @throws IllegalArgumentException if {@code array} is {@code
>> null}
>> > @@ -1069,6 +1077,9 @@ public class BooleanUtils {
>> >       *   BooleanUtils.xor(new Boolean[] { Boolean.TRUE, Boolean.FALSE
>> })
>> >  = Boolean.TRUE
>> >       * </pre>
>> >       *
>> > +     * <p>Note that this method behaves different from using the binary
>> > XOR operator (^). See
>> > +     * {@link #xor(boolean...)}.</p>
>> > +     *
>> >       * @param array  an array of {@code Boolean}s
>> >       * @return {@code true} if the xor is successful.
>> >       * @throws IllegalArgumentException if {@code array} is {@code
>> null}
>> >
>> >
>> >
>> >
>> >
>> > --
>> > http://people.apache.org/~britter/
>> > http://www.systemoutprintln.de/
>> > http://twitter.com/BenediktRitter
>> > http://github.com/britter
>> >
>>
>
>

Re: svn commit: r1532031 - /commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/BooleanUtils.java

Posted by Matt Benson <gu...@gmail.com>.
In this case we should perhaps provide a #oneTrue(boolean...) method to
preserve the availability of the current behavior, which may also
occasionally have its place.

Matt


On Mon, Oct 14, 2013 at 11:10 PM, Henri Yandell <fl...@gmail.com> wrote:

> I think we treat it as a bug and fix. The code is fine for the explained
> use of an array of length 2, but fails when you move to larger chains.
> Given the common meaning of the name, I think people would have noticed if
> they'd needed xor to work for arrays > 2 and it didn't. So a) I think we
> can fix and b) I suspect it's not a highly used method.
>
> So rather than the note, I'm +1 to fixing the code.
>
> Hen
>
>
> On Mon, Oct 14, 2013 at 12:22 PM, Benedikt Ritter <britter@apache.org
> >wrote:
>
> > Thoughts? Maybe this should be renamed to "isOneTrue(boolean...)" in the
> > next major release?!
> >
> > ---------- Forwarded message ----------
> > From: <br...@apache.org>
> > Date: 2013/10/14
> > Subject: svn commit: r1532031 -
> >
> >
> /commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/BooleanUtils.java
> > To: commits@commons.apache.org
> >
> >
> > Author: britter
> > Date: Mon Oct 14 19:21:02 2013
> > New Revision: 1532031
> >
> > URL: http://svn.apache.org/r1532031
> > Log:
> > Document (wrong?) behavior of xor methods
> >
> > Modified:
> >
> >
> >
> commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/BooleanUtils.java
> >
> > Modified:
> >
> >
> commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/BooleanUtils.java
> > URL:
> >
> >
> http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/BooleanUtils.java?rev=1532031&r1=1532030&r2=1532031&view=diff
> >
> >
> ==============================================================================
> > ---
> >
> >
> commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/BooleanUtils.java
> > (original)
> > +++
> >
> >
> commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/BooleanUtils.java
> > Mon Oct 14 19:21:02 2013
> > @@ -1028,6 +1028,14 @@ public class BooleanUtils {
> >       *   BooleanUtils.xor(true, false)  = true
> >       * </pre>
> >       *
> > +     * <p>Note that this method behaves different from using the binary
> > XOR operator (^). Instead of combining the given
> > +     * booleans using the XOR operator from left to right, this method
> > counts the appearances of true in the given
> > +     * array. It will only return true if exactly one boolean in the
> given
> > array is true:</p>
> > +     * <pre>
> > +     *   true ^ true ^ false ^ true                 = true
> > +     *   BooleanUtils.xor(true, true, false, true)  = false
> > +     * </pre>
> > +     *
> >       * @param array  an array of {@code boolean}s
> >       * @return {@code true} if the xor is successful.
> >       * @throws IllegalArgumentException if {@code array} is {@code null}
> > @@ -1069,6 +1077,9 @@ public class BooleanUtils {
> >       *   BooleanUtils.xor(new Boolean[] { Boolean.TRUE, Boolean.FALSE })
> >  = Boolean.TRUE
> >       * </pre>
> >       *
> > +     * <p>Note that this method behaves different from using the binary
> > XOR operator (^). See
> > +     * {@link #xor(boolean...)}.</p>
> > +     *
> >       * @param array  an array of {@code Boolean}s
> >       * @return {@code true} if the xor is successful.
> >       * @throws IllegalArgumentException if {@code array} is {@code null}
> >
> >
> >
> >
> >
> > --
> > http://people.apache.org/~britter/
> > http://www.systemoutprintln.de/
> > http://twitter.com/BenediktRitter
> > http://github.com/britter
> >
>

Re: svn commit: r1532031 - /commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/BooleanUtils.java

Posted by Henri Yandell <fl...@gmail.com>.
I think we treat it as a bug and fix. The code is fine for the explained
use of an array of length 2, but fails when you move to larger chains.
Given the common meaning of the name, I think people would have noticed if
they'd needed xor to work for arrays > 2 and it didn't. So a) I think we
can fix and b) I suspect it's not a highly used method.

So rather than the note, I'm +1 to fixing the code.

Hen


On Mon, Oct 14, 2013 at 12:22 PM, Benedikt Ritter <br...@apache.org>wrote:

> Thoughts? Maybe this should be renamed to "isOneTrue(boolean...)" in the
> next major release?!
>
> ---------- Forwarded message ----------
> From: <br...@apache.org>
> Date: 2013/10/14
> Subject: svn commit: r1532031 -
>
> /commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/BooleanUtils.java
> To: commits@commons.apache.org
>
>
> Author: britter
> Date: Mon Oct 14 19:21:02 2013
> New Revision: 1532031
>
> URL: http://svn.apache.org/r1532031
> Log:
> Document (wrong?) behavior of xor methods
>
> Modified:
>
>
> commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/BooleanUtils.java
>
> Modified:
>
> commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/BooleanUtils.java
> URL:
>
> http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/BooleanUtils.java?rev=1532031&r1=1532030&r2=1532031&view=diff
>
> ==============================================================================
> ---
>
> commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/BooleanUtils.java
> (original)
> +++
>
> commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/BooleanUtils.java
> Mon Oct 14 19:21:02 2013
> @@ -1028,6 +1028,14 @@ public class BooleanUtils {
>       *   BooleanUtils.xor(true, false)  = true
>       * </pre>
>       *
> +     * <p>Note that this method behaves different from using the binary
> XOR operator (^). Instead of combining the given
> +     * booleans using the XOR operator from left to right, this method
> counts the appearances of true in the given
> +     * array. It will only return true if exactly one boolean in the given
> array is true:</p>
> +     * <pre>
> +     *   true ^ true ^ false ^ true                 = true
> +     *   BooleanUtils.xor(true, true, false, true)  = false
> +     * </pre>
> +     *
>       * @param array  an array of {@code boolean}s
>       * @return {@code true} if the xor is successful.
>       * @throws IllegalArgumentException if {@code array} is {@code null}
> @@ -1069,6 +1077,9 @@ public class BooleanUtils {
>       *   BooleanUtils.xor(new Boolean[] { Boolean.TRUE, Boolean.FALSE })
>  = Boolean.TRUE
>       * </pre>
>       *
> +     * <p>Note that this method behaves different from using the binary
> XOR operator (^). See
> +     * {@link #xor(boolean...)}.</p>
> +     *
>       * @param array  an array of {@code Boolean}s
>       * @return {@code true} if the xor is successful.
>       * @throws IllegalArgumentException if {@code array} is {@code null}
>
>
>
>
>
> --
> http://people.apache.org/~britter/
> http://www.systemoutprintln.de/
> http://twitter.com/BenediktRitter
> http://github.com/britter
>