commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From thomas menzel <...@tomsit.de>
Subject Re: [lang] new method: ObjectsUtils.equalsOneOf()
Date Mon, 08 Aug 2016 09:07:22 GMT
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 <stain@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" <dev@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


Mime
View raw message