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. > > > > * <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 > > > > >