You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by thomas menzel <de...@tomsit.de> on 2016/08/06 20:43:03 UTC
[lang] new method: ObjectsUtils.equalsOneOf()
Hi folks,
I'm proposing a new function on ObjectsUtils:
/**
* Checks if the given {@code ref} is contained at least once in
* {@code others}. If {@code} is null or empty then {@code false} is
* returned.
* <p>
* This is a more readable replacement for the idiomatic:
* {@code Arrays.asList(others).contains(ref)}.
*
* <pre>
* ObjectUtils.equalsOneOf(1, 2, 1)) = true
* ObjectUtils.equalsOneOf(1, 1, 2)) = true
* ObjectUtils.equalsOneOf(1, null, 1)) = true
* ObjectUtils.equalsOneOf(null, 1, null)) = true
* ObjectUtils.equalsOneOf("b", "a b c".split(" "))) = true
* ObjectUtils.equalsOneOf(null, null)) = false
* ObjectUtils.equalsOneOf(1, null)) = false
* ObjectUtils.equalsOneOf(null, new Object[0])) = false
* ObjectUtils.equalsOneOf(1, 2, 3)) = false
* </pre>
*
* @param ref
* the ref value to check the others against
* @param others
* the others
* @return true, iff {@code ref} is contained at least once in
* {@code others}
* @since 3.5
*/
public static boolean equalsOneOf(final Object ref, final Object...
others) {
return ArrayUtils.isNotEmpty(others) &&
ArrayUtils.contains(others, ref);
}
Use Case:
comparing a value to several others in a condition thus avoiding the often
seen
Other1.equals(string) || Other2.equals(string) || .
Since this is a shorthand for the above, the OTHER values are usually not in
a collection, e.g.
ObjectUtils.equalsOneOf(e, FooEnum.X, FooEnum.Y );
ObjectUtils.equalsOneOf(string, "A", "B", "C");
One could also accomplish the same thing with:
Arrays.asList(Other1, Other2, ..).contains(ref) but this is less readable
and also the List is created needlessly.
Note, there is a similar function on StringUtils:
containsAny(CharSequence cs, CharSequence...
searchCharSequences)
but obviously only for Strings and doing the "conains" check vs. the
proposed equals-check.
The code for the function is in the last commit of the fork and includes a
full unit test:
https://github.com/elonderin/commons-lang-equalsOneOf
As a bonus I have also added the companion functions in the same commit:
public static boolean allNull(final Object... values)
public static boolean anyNull(final Object... values)
which is in the same vain as isNull/isNotNull etc. to promote readability.
Also with unit tests.
Hope u like these and add them to the grand collection of util functions.
Thomas Menzel
Re: [lang] new method: ObjectsUtils.equalsOneOf()
Posted by thomas menzel <de...@tomsit.de>.
Hi Paul and Gary,
thanks for the input. The use case, as stated, is to have a more readable
*shorthand* when coding conditions and having the need to compare to an
arbitrary list of values.
I know the ArrayUtils and CollectionUtils and use it frequently, but i'd like to
have as little boilerplate as possible. Hence i know that this is possible and i
used under the hood:
ArrayUtils.contains(new Object[] { 1, 5, "" }, 2)
albeit, it requires the boilerplate new Object[] {}.
I also recognize, that looking at the line, one might wonder which args fulfill
which role, but i have used this in the past project and people catch up really
fast on that, so i thought it a good idea to share it.
I also was considering of putting this into ArrayUtils like so
equalsOneOf(Object ref, Object... others), but
a) we then run here into the problem, that in that class the arrays is always
the 1st arg and for this method it cannot, due to the varargs nature.
b) none of current util classes fit more natural to me than ObjectUtils for such
a common case.
We also could do a more functional approach, but havent given it much thought
yet how this could be done in an elegant way, so we can avoid the explicit
declaration of an array with new Object[] {}.
In the end, the code i want to write should be short and readable, if that can
be accomplished an even better way, then by all means can we do this another
way.
tom
> Gary Gregory <ga...@gmail.com> hat am 7. August 2016 um 00:26
> geschrieben:
>
> I think we should stick with the contains prefix.
>
> Gary
>
> On Aug 6, 2016 2:43 PM, "Paul Benedict" <pb...@apache.org> wrote:
>
> > Hi Thomas. The only issue I see with introducing a "equalsOneOf" is that it
> > setups the pondering for other uses... greater than one of, less than one
> > of, and whatever of one of. With that said, I would rather see the proposed
> > method accept a function/visitor callback to determine the check. If you're
> > dealing with an array, I would put this inside of ArrayUtils. Otherwise, if
> > you really have a collection, I already know Commons Collections can do
> > what you want with a callback.
> >
> > Cheers,
> > Paul
> >
> > On Sat, Aug 6, 2016 at 3:43 PM, thomas menzel <de...@tomsit.de> wrote:
> >
> > > Hi folks,
> > >
> > > I'm proposing a new function on ObjectsUtils:
> > >
> > > /**
> > >
> > > * Checks if the given {@code ref} is contained at least once in
> > >
> > > * {@code others}. If {@code} is null or empty then {@code false} is
> > >
> > > * returned.
> > >
> > > *
> > >
> > > * This is a more readable replacement for the idiomatic:
> > >
> > > * {@code Arrays.asList(others).contains(ref)}.
> > >
> > > *
> > >
> > > *
> > >
> > > * ObjectUtils.equalsOneOf(1, 2, 1)) = true
> > >
> > > * ObjectUtils.equalsOneOf(1, 1, 2)) = true
> > >
> > > * ObjectUtils.equalsOneOf(1, null, 1)) = true
> > >
> > > * ObjectUtils.equalsOneOf(null, 1, null)) = true
> > >
> > > * ObjectUtils.equalsOneOf("b", "a b c".split(" "))) = true
> > >
> > > * ObjectUtils.equalsOneOf(null, null)) = false
> > >
> > > * ObjectUtils.equalsOneOf(1, null)) = false
> > >
> > > * ObjectUtils.equalsOneOf(null, new Object[0])) = false
> > >
> > > * ObjectUtils.equalsOneOf(1, 2, 3)) = false
> > >
> > > *
> > >
> > > *
> > >
> > > * @param ref
> > >
> > > * the ref value to check the others against
> > >
> > > * @param others
> > >
> > > * the others
> > >
> > > * @return true, iff {@code ref} is contained at least once in
> > >
> > > * {@code others}
> > >
> > > * @since 3.5
> > >
> > > */
> > >
> > > public static boolean equalsOneOf(final Object ref, final Object...
> > > others) {
> > >
> > > return ArrayUtils.isNotEmpty(others) &&
> > > ArrayUtils.contains(others, ref);
> > >
> > > }
> > >
> > > Use Case:
> > >
> > > comparing a value to several others in a condition thus avoiding the
> >
> > often
> >
> > > seen
> > >
> > > Other1.equals(string) || Other2.equals(string) || .
> > >
> > > Since this is a shorthand for the above, the OTHER values are usually not
> > > in
> > > a collection, e.g.
> > >
> > > ObjectUtils.equalsOneOf(e, FooEnum.X, FooEnum.Y );
> > >
> > > ObjectUtils.equalsOneOf(string, "A", "B", "C");
> > >
> > > One could also accomplish the same thing with:
> > >
> > > Arrays.asList(Other1, Other2, ..).contains(ref) but this is less readable
> > > and also the List is created needlessly.
> > >
> > > Note, there is a similar function on StringUtils:
> > >
> > > containsAny(CharSequence cs, CharSequence...
> > > searchCharSequences)
> > >
> > > but obviously only for Strings and doing the "conains" check vs. the
> > > proposed equals-check.
> > >
> > > The code for the function is in the last commit of the fork and includes
> >
> > a
> >
> > > full unit test:
> > >
> > > https://github.com/elonderin/commons-lang-equalsOneOf
> > >
> > > As a bonus I have also added the companion functions in the same commit:
> > >
> > > public static boolean allNull(final Object... values)
> > >
> > > public static boolean anyNull(final Object... values)
> > >
> > > which is in the same vain as isNull/isNotNull etc. to promote
> >
> > readability.
> >
> > > Also with unit tests.
> > >
> > > Hope u like these and add them to the grand collection of util functions.
> > >
> > > Thomas Menzel
> >
> > >
>
> >
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org
Re: [lang] new method: ObjectsUtils.equalsOneOf()
Posted by Gary Gregory <ga...@gmail.com>.
I think we should stick with the contains prefix.
Gary
On Aug 6, 2016 2:43 PM, "Paul Benedict" <pb...@apache.org> wrote:
> Hi Thomas. The only issue I see with introducing a "equalsOneOf" is that it
> setups the pondering for other uses... greater than one of, less than one
> of, and whatever of one of. With that said, I would rather see the proposed
> method accept a function/visitor callback to determine the check. If you're
> dealing with an array, I would put this inside of ArrayUtils. Otherwise, if
> you really have a collection, I already know Commons Collections can do
> what you want with a callback.
>
> Cheers,
> Paul
>
> On Sat, Aug 6, 2016 at 3:43 PM, thomas menzel <de...@tomsit.de> wrote:
>
> > Hi folks,
> >
> >
> >
> > I'm proposing a new function on ObjectsUtils:
> >
> >
> >
> > /**
> >
> > * Checks if the given {@code ref} is contained at least once in
> >
> > * {@code others}. If {@code} is null or empty then {@code false} is
> >
> > * returned.
> >
> > * <p>
> >
> > * This is a more readable replacement for the idiomatic:
> >
> > * {@code Arrays.asList(others).contains(ref)}.
> >
> > *
> >
> > * <pre>
> >
> > * ObjectUtils.equalsOneOf(1, 2, 1)) = true
> >
> > * ObjectUtils.equalsOneOf(1, 1, 2)) = true
> >
> > * ObjectUtils.equalsOneOf(1, null, 1)) = true
> >
> > * ObjectUtils.equalsOneOf(null, 1, null)) = true
> >
> > * ObjectUtils.equalsOneOf("b", "a b c".split(" "))) = true
> >
> > * ObjectUtils.equalsOneOf(null, null)) = false
> >
> > * ObjectUtils.equalsOneOf(1, null)) = false
> >
> > * ObjectUtils.equalsOneOf(null, new Object[0])) = false
> >
> > * ObjectUtils.equalsOneOf(1, 2, 3)) = false
> >
> > * </pre>
> >
> > *
> >
> > * @param ref
> >
> > * the ref value to check the others against
> >
> > * @param others
> >
> > * the others
> >
> > * @return true, iff {@code ref} is contained at least once in
> >
> > * {@code others}
> >
> > * @since 3.5
> >
> > */
> >
> > public static boolean equalsOneOf(final Object ref, final Object...
> > others) {
> >
> > return ArrayUtils.isNotEmpty(others) &&
> > ArrayUtils.contains(others, ref);
> >
> > }
> >
> >
> >
> > Use Case:
> >
> > comparing a value to several others in a condition thus avoiding the
> often
> > seen
> >
> > Other1.equals(string) || Other2.equals(string) || .
> >
> >
> >
> > Since this is a shorthand for the above, the OTHER values are usually not
> > in
> > a collection, e.g.
> >
> >
> >
> > ObjectUtils.equalsOneOf(e, FooEnum.X, FooEnum.Y );
> >
> > ObjectUtils.equalsOneOf(string, "A", "B", "C");
> >
> >
> >
> > One could also accomplish the same thing with:
> >
> > Arrays.asList(Other1, Other2, ..).contains(ref) but this is less readable
> > and also the List is created needlessly.
> >
> >
> >
> >
> >
> > Note, there is a similar function on StringUtils:
> >
> > containsAny(CharSequence cs, CharSequence...
> > searchCharSequences)
> >
> > but obviously only for Strings and doing the "conains" check vs. the
> > proposed equals-check.
> >
> >
> >
> > The code for the function is in the last commit of the fork and includes
> a
> > full unit test:
> >
> > https://github.com/elonderin/commons-lang-equalsOneOf
> >
> >
> >
> >
> >
> > As a bonus I have also added the companion functions in the same commit:
> >
> > public static boolean allNull(final Object... values)
> >
> > public static boolean anyNull(final Object... values)
> >
> >
> >
> > which is in the same vain as isNull/isNotNull etc. to promote
> readability.
> >
> > Also with unit tests.
> >
> >
> >
> > Hope u like these and add them to the grand collection of util functions.
> >
> >
> >
> >
> >
> > Thomas Menzel
> >
> >
>
Re: [lang] new method: ObjectsUtils.equalsOneOf()
Posted by Paul Benedict <pb...@apache.org>.
Hi Thomas. The only issue I see with introducing a "equalsOneOf" is that it
setups the pondering for other uses... greater than one of, less than one
of, and whatever of one of. With that said, I would rather see the proposed
method accept a function/visitor callback to determine the check. If you're
dealing with an array, I would put this inside of ArrayUtils. Otherwise, if
you really have a collection, I already know Commons Collections can do
what you want with a callback.
Cheers,
Paul
On Sat, Aug 6, 2016 at 3:43 PM, thomas menzel <de...@tomsit.de> wrote:
> Hi folks,
>
>
>
> I'm proposing a new function on ObjectsUtils:
>
>
>
> /**
>
> * Checks if the given {@code ref} is contained at least once in
>
> * {@code others}. If {@code} is null or empty then {@code false} is
>
> * returned.
>
> * <p>
>
> * This is a more readable replacement for the idiomatic:
>
> * {@code Arrays.asList(others).contains(ref)}.
>
> *
>
> * <pre>
>
> * ObjectUtils.equalsOneOf(1, 2, 1)) = true
>
> * ObjectUtils.equalsOneOf(1, 1, 2)) = true
>
> * ObjectUtils.equalsOneOf(1, null, 1)) = true
>
> * ObjectUtils.equalsOneOf(null, 1, null)) = true
>
> * ObjectUtils.equalsOneOf("b", "a b c".split(" "))) = true
>
> * ObjectUtils.equalsOneOf(null, null)) = false
>
> * ObjectUtils.equalsOneOf(1, null)) = false
>
> * ObjectUtils.equalsOneOf(null, new Object[0])) = false
>
> * ObjectUtils.equalsOneOf(1, 2, 3)) = false
>
> * </pre>
>
> *
>
> * @param ref
>
> * the ref value to check the others against
>
> * @param others
>
> * the others
>
> * @return true, iff {@code ref} is contained at least once in
>
> * {@code others}
>
> * @since 3.5
>
> */
>
> public static boolean equalsOneOf(final Object ref, final Object...
> others) {
>
> return ArrayUtils.isNotEmpty(others) &&
> ArrayUtils.contains(others, ref);
>
> }
>
>
>
> Use Case:
>
> comparing a value to several others in a condition thus avoiding the often
> seen
>
> Other1.equals(string) || Other2.equals(string) || .
>
>
>
> Since this is a shorthand for the above, the OTHER values are usually not
> in
> a collection, e.g.
>
>
>
> ObjectUtils.equalsOneOf(e, FooEnum.X, FooEnum.Y );
>
> ObjectUtils.equalsOneOf(string, "A", "B", "C");
>
>
>
> One could also accomplish the same thing with:
>
> Arrays.asList(Other1, Other2, ..).contains(ref) but this is less readable
> and also the List is created needlessly.
>
>
>
>
>
> Note, there is a similar function on StringUtils:
>
> containsAny(CharSequence cs, CharSequence...
> searchCharSequences)
>
> but obviously only for Strings and doing the "conains" check vs. the
> proposed equals-check.
>
>
>
> The code for the function is in the last commit of the fork and includes a
> full unit test:
>
> https://github.com/elonderin/commons-lang-equalsOneOf
>
>
>
>
>
> As a bonus I have also added the companion functions in the same commit:
>
> public static boolean allNull(final Object... values)
>
> public static boolean anyNull(final Object... values)
>
>
>
> which is in the same vain as isNull/isNotNull etc. to promote readability.
>
> Also with unit tests.
>
>
>
> Hope u like these and add them to the grand collection of util functions.
>
>
>
>
>
> Thomas Menzel
>
>
Re: [lang] new method: ObjectsUtils.equalsOneOf()
Posted by thomas menzel <de...@tomsit.de>.
Hi Stian,
thx for ur +1
The rational behind equalsOneOf(null, null)== false, is thatÂ
a) all or most methods in this class with varargs==null result in false
b) it also enforce the notion of the name, that i'm comparing against a
"collection of values" and this collection is set up by me in source code right
now.
For other cases, where i already have an array or know its just one var i
wouldnt need this and could use Objects.equals() or ArrayUtils.contains(). For
the latter i now can use this new method as well.
tom
> Stian Soiland-Reyes <st...@apache.org> hat am 7. August 2016 um 07:42
> geschrieben:
>
> +1 for the proposal!
>
> I think ObjectUtils is a good home, the implied array or collection is an
> implementation detail.
>
> What is your reasoning for null != null ?
>
> If you accept a Function to compare with, that would break the varargs
> mechanism (or always require a Function), perhaps such a function could
> have a more functional name like firstWhich ? (Which first argument is a
> selector function)
>
> On 6 Aug 2016 10:43 p.m., "thomas menzel" <de...@tomsit.de> wrote:
>
> Hi folks,
>
> I'm proposing a new function on ObjectsUtils:
>
> /**
>
> * Checks if the given {@code ref} is contained at least once in
>
> * {@code others}. If {@code} is null or empty then {@code false} is
>
> * returned.
>
> *
>
> * This is a more readable replacement for the idiomatic:
>
> * {@code Arrays.asList(others).contains(ref)}.
>
> *
>
> *
>
> * ObjectUtils.equalsOneOf(1, 2, 1)) = true
>
> * ObjectUtils.equalsOneOf(1, 1, 2)) = true
>
> * ObjectUtils.equalsOneOf(1, null, 1)) = true
>
> * ObjectUtils.equalsOneOf(null, 1, null)) = true
>
> * ObjectUtils.equalsOneOf("b", "a b c".split(" "))) = true
>
> * ObjectUtils.equalsOneOf(null, null)) = false
>
> * ObjectUtils.equalsOneOf(1, null)) = false
>
> * ObjectUtils.equalsOneOf(null, new Object[0])) = false
>
> * ObjectUtils.equalsOneOf(1, 2, 3)) = false
>
> *
>
> *
>
> * @param ref
>
> * the ref value to check the others against
>
> * @param others
>
> * the others
>
> * @return true, iff {@code ref} is contained at least once in
>
> * {@code others}
>
> * @since 3.5
>
> */
>
> public static boolean equalsOneOf(final Object ref, final Object...
> others) {
>
> return ArrayUtils.isNotEmpty(others) &&
> ArrayUtils.contains(others, ref);
>
> }
>
> Use Case:
>
> comparing a value to several others in a condition thus avoiding the often
> seen
>
> Other1.equals(string) || Other2.equals(string) || .
>
> Since this is a shorthand for the above, the OTHER values are usually not in
> a collection, e.g.
>
> ObjectUtils.equalsOneOf(e, FooEnum.X, FooEnum.Y );
>
> ObjectUtils.equalsOneOf(string, "A", "B", "C");
>
> One could also accomplish the same thing with:
>
> Arrays.asList(Other1, Other2, ..).contains(ref) but this is less readable
> and also the List is created needlessly.
>
> Note, there is a similar function on StringUtils:
>
> containsAny(CharSequence cs, CharSequence...
> searchCharSequences)
>
> but obviously only for Strings and doing the "conains" check vs. the
> proposed equals-check.
>
> The code for the function is in the last commit of the fork and includes a
> full unit test:
>
> https://github.com/elonderin/commons-lang-equalsOneOf
>
> As a bonus I have also added the companion functions in the same commit:
>
> public static boolean allNull(final Object... values)
>
> public static boolean anyNull(final Object... values)
>
> which is in the same vain as isNull/isNotNull etc. to promote readability.
>
> Also with unit tests.
>
> Hope u like these and add them to the grand collection of util functions.
>
> Thomas Menzel
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org
Re: [lang] new method: ObjectsUtils.equalsOneOf()
Posted by Stian Soiland-Reyes <st...@apache.org>.
+1 for the proposal!
I think ObjectUtils is a good home, the implied array or collection is an
implementation detail.
What is your reasoning for null != null ?
If you accept a Function to compare with, that would break the varargs
mechanism (or always require a Function), perhaps such a function could
have a more functional name like firstWhich ? (Which first argument is a
selector function)
On 6 Aug 2016 10:43 p.m., "thomas menzel" <de...@tomsit.de> wrote:
Hi folks,
I'm proposing a new function on ObjectsUtils:
/**
* Checks if the given {@code ref} is contained at least once in
* {@code others}. If {@code} is null or empty then {@code false} is
* returned.
* <p>
* This is a more readable replacement for the idiomatic:
* {@code Arrays.asList(others).contains(ref)}.
*
* <pre>
* ObjectUtils.equalsOneOf(1, 2, 1)) = true
* ObjectUtils.equalsOneOf(1, 1, 2)) = true
* ObjectUtils.equalsOneOf(1, null, 1)) = true
* ObjectUtils.equalsOneOf(null, 1, null)) = true
* ObjectUtils.equalsOneOf("b", "a b c".split(" "))) = true
* ObjectUtils.equalsOneOf(null, null)) = false
* ObjectUtils.equalsOneOf(1, null)) = false
* ObjectUtils.equalsOneOf(null, new Object[0])) = false
* ObjectUtils.equalsOneOf(1, 2, 3)) = false
* </pre>
*
* @param ref
* the ref value to check the others against
* @param others
* the others
* @return true, iff {@code ref} is contained at least once in
* {@code others}
* @since 3.5
*/
public static boolean equalsOneOf(final Object ref, final Object...
others) {
return ArrayUtils.isNotEmpty(others) &&
ArrayUtils.contains(others, ref);
}
Use Case:
comparing a value to several others in a condition thus avoiding the often
seen
Other1.equals(string) || Other2.equals(string) || .
Since this is a shorthand for the above, the OTHER values are usually not in
a collection, e.g.
ObjectUtils.equalsOneOf(e, FooEnum.X, FooEnum.Y );
ObjectUtils.equalsOneOf(string, "A", "B", "C");
One could also accomplish the same thing with:
Arrays.asList(Other1, Other2, ..).contains(ref) but this is less readable
and also the List is created needlessly.
Note, there is a similar function on StringUtils:
containsAny(CharSequence cs, CharSequence...
searchCharSequences)
but obviously only for Strings and doing the "conains" check vs. the
proposed equals-check.
The code for the function is in the last commit of the fork and includes a
full unit test:
https://github.com/elonderin/commons-lang-equalsOneOf
As a bonus I have also added the companion functions in the same commit:
public static boolean allNull(final Object... values)
public static boolean anyNull(final Object... values)
which is in the same vain as isNull/isNotNull etc. to promote readability.
Also with unit tests.
Hope u like these and add them to the grand collection of util functions.
Thomas Menzel