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 <garydgreg...@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" <pbened...@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 <d...@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-unsubscr...@commons.apache.org
For additional commands, e-mail: dev-h...@commons.apache.org

Reply via email to