You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by Steve Bosman <st...@gmail.com> on 2022/05/04 12:09:18 UTC

Re: [jira] [Commented] (LANG-1627) BooleanUtils.xor not behaving as expected with any odd number of true's

I've started working somewhere where I can work on opensource during
downtime. I can see this has been open a while and I'm happy to pick it up.
I'm thinking one PR with some javadoc clarifications to the original method
for three or more booleans, and if still wanted another PR with the
requested function.

Would that be okay?

Steve

On Tue, 29 Dec 2020, 19:34 Alberto Scotto (Jira), <ji...@apache.org> wrote:

>
>     [
> https://issues.apache.org/jira/browse/LANG-1627?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17256142#comment-17256142
> ]
>
> Alberto Scotto commented on LANG-1627:
> --------------------------------------
>
> {quote}[https://en.wikipedia.org/wiki/XOR_gate#More_than_two_inputs]
> {quote}
> Interesting! So there exists one such boolean function which works as I
> expect.
>
> BTW, my use case is the following:
>
> I have a web service which accepts a request with a number of fields, say
> 3, with the constraint that exactly one among the 3 fields must be not null.
>
> I think it's a pretty common scenario, so it would make sense to add this
> other semantic of xor to BooleanUtils. We may call it xorOneHot. I would be
> keen on keeping "xor" in the name so that users can find it when using
> autocompletion and are looking for "xor".
>
> I'm working on an implementation already. Just shout if you'd like me to
> submit a PR.
>
> > BooleanUtils.xor not behaving as expected with any odd number of true's
> > -----------------------------------------------------------------------
> >
> >                 Key: LANG-1627
> >                 URL: https://issues.apache.org/jira/browse/LANG-1627
> >             Project: Commons Lang
> >          Issue Type: Bug
> >    Affects Versions: 3.11
> >            Reporter: Alberto Scotto
> >            Priority: Major
> >
> > Hi,
> > I was expecting a xor function that takes a variable number of arguments
> to *return true if and only if exactly one among all of the arguments is
> true*, regardless of the number of arguments.
> > This holds true given three false's:
> > {code:java}
> > @Test
> > public void threeFalse() {
> >  boolean[] bools = new boolean[]{Boolean.FALSE, Boolean.FALSE,
> Boolean.FALSE};
> >  assertFalse(BooleanUtils.xor(bools));
> > }{code}
> >
> >  It also holds true given 4 true's, as well as for any even number of
> trues.
> > {code:java}
> >     @Test
> >     public void fourTrue() {
> >         boolean[] bools = new boolean[]{Boolean.TRUE, Boolean.TRUE,
> Boolean.TRUE, Boolean.TRUE};
> >         assertFalse(BooleanUtils.xor(bools));
> >     }
> > {code}
> > The above tests pass.
> > But with any odd number of true's that doesn't hold anymore:
> >
> > {code:java}
> > @Test
> > public void threeTrue() {
> >  boolean[] bools = new boolean[]{Boolean.TRUE, Boolean.TRUE,
> Boolean.TRUE};
> >  assertFalse(BooleanUtils.xor(bools));
> > }
> > {code}
> > This test fails.
> > That was totally unexpected to me.
> >  But as it turns out, even
> > {noformat}
> > true ^ true ^ true{noformat}
> > evaluates to true. That was unexpected too to me, at a first sight.
> > The thing is that xor (I mean the original boolean operator) is a binary
> operator, so if you want to make it n-ary, one simple solution is to apply
> it in two by two: ((a ^ b) ^ c) ^ d
> >  And that's what is done in the implementation of the method
> BooleanUtils#xor.
> > But that brings to BooleanUtils.xor(true, true, true) == true, and at
> the same time BooleanUtils.xor(true, true, true, true) == false, which just
> doesn't sound right to me.
> > Whether or not you agree with me that that is a bug of the method,
> please at least update the Javadoc, because right now it is not providing
> the user enough information. Look:
> > {code:java}
> > Performs an xor on a set of booleans.
> >          BooleanUtils.xor(true, true)   = false
> >          BooleanUtils.xor(false, false) = false
> >          BooleanUtils.xor(true, false)  = true
> > {code}
> >
> > Thanks.
> > Cheers
>
>
>
> --
> This message was sent by Atlassian Jira
> (v8.3.4#803005)
>

Re: [jira] [Commented] (LANG-1627) BooleanUtils.xor not behaving as expected with any odd number of true's

Posted by Gary Gregory <ga...@gmail.com>.
PRs are welcome :-)

Gary

On Wed, May 4, 2022, 08:09 Steve Bosman <st...@gmail.com> wrote:

> I've started working somewhere where I can work on opensource during
> downtime. I can see this has been open a while and I'm happy to pick it up.
> I'm thinking one PR with some javadoc clarifications to the original method
> for three or more booleans, and if still wanted another PR with the
> requested function.
>
> Would that be okay?
>
> Steve
>
> On Tue, 29 Dec 2020, 19:34 Alberto Scotto (Jira), <ji...@apache.org> wrote:
>
> >
> >     [
> >
> https://issues.apache.org/jira/browse/LANG-1627?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17256142#comment-17256142
> > ]
> >
> > Alberto Scotto commented on LANG-1627:
> > --------------------------------------
> >
> > {quote}[https://en.wikipedia.org/wiki/XOR_gate#More_than_two_inputs]
> > {quote}
> > Interesting! So there exists one such boolean function which works as I
> > expect.
> >
> > BTW, my use case is the following:
> >
> > I have a web service which accepts a request with a number of fields, say
> > 3, with the constraint that exactly one among the 3 fields must be not
> null.
> >
> > I think it's a pretty common scenario, so it would make sense to add this
> > other semantic of xor to BooleanUtils. We may call it xorOneHot. I would
> be
> > keen on keeping "xor" in the name so that users can find it when using
> > autocompletion and are looking for "xor".
> >
> > I'm working on an implementation already. Just shout if you'd like me to
> > submit a PR.
> >
> > > BooleanUtils.xor not behaving as expected with any odd number of true's
> > > -----------------------------------------------------------------------
> > >
> > >                 Key: LANG-1627
> > >                 URL: https://issues.apache.org/jira/browse/LANG-1627
> > >             Project: Commons Lang
> > >          Issue Type: Bug
> > >    Affects Versions: 3.11
> > >            Reporter: Alberto Scotto
> > >            Priority: Major
> > >
> > > Hi,
> > > I was expecting a xor function that takes a variable number of
> arguments
> > to *return true if and only if exactly one among all of the arguments is
> > true*, regardless of the number of arguments.
> > > This holds true given three false's:
> > > {code:java}
> > > @Test
> > > public void threeFalse() {
> > >  boolean[] bools = new boolean[]{Boolean.FALSE, Boolean.FALSE,
> > Boolean.FALSE};
> > >  assertFalse(BooleanUtils.xor(bools));
> > > }{code}
> > >
> > >  It also holds true given 4 true's, as well as for any even number of
> > trues.
> > > {code:java}
> > >     @Test
> > >     public void fourTrue() {
> > >         boolean[] bools = new boolean[]{Boolean.TRUE, Boolean.TRUE,
> > Boolean.TRUE, Boolean.TRUE};
> > >         assertFalse(BooleanUtils.xor(bools));
> > >     }
> > > {code}
> > > The above tests pass.
> > > But with any odd number of true's that doesn't hold anymore:
> > >
> > > {code:java}
> > > @Test
> > > public void threeTrue() {
> > >  boolean[] bools = new boolean[]{Boolean.TRUE, Boolean.TRUE,
> > Boolean.TRUE};
> > >  assertFalse(BooleanUtils.xor(bools));
> > > }
> > > {code}
> > > This test fails.
> > > That was totally unexpected to me.
> > >  But as it turns out, even
> > > {noformat}
> > > true ^ true ^ true{noformat}
> > > evaluates to true. That was unexpected too to me, at a first sight.
> > > The thing is that xor (I mean the original boolean operator) is a
> binary
> > operator, so if you want to make it n-ary, one simple solution is to
> apply
> > it in two by two: ((a ^ b) ^ c) ^ d
> > >  And that's what is done in the implementation of the method
> > BooleanUtils#xor.
> > > But that brings to BooleanUtils.xor(true, true, true) == true, and at
> > the same time BooleanUtils.xor(true, true, true, true) == false, which
> just
> > doesn't sound right to me.
> > > Whether or not you agree with me that that is a bug of the method,
> > please at least update the Javadoc, because right now it is not providing
> > the user enough information. Look:
> > > {code:java}
> > > Performs an xor on a set of booleans.
> > >          BooleanUtils.xor(true, true)   = false
> > >          BooleanUtils.xor(false, false) = false
> > >          BooleanUtils.xor(true, false)  = true
> > > {code}
> > >
> > > Thanks.
> > > Cheers
> >
> >
> >
> > --
> > This message was sent by Atlassian Jira
> > (v8.3.4#803005)
> >
>