Here you can find the source of isOneOf(String value, String... values)
Parameter | Description |
---|---|
value | Value to validate. |
values | Array of values to search in. |
public static boolean isOneOf(String value, String... values)
//package com.java2s; //License from project: Open Source License import java.text.Collator; import java.util.Locale; public class Main { /**//from www. j a v a2 s.c o m * Checks if the given value exists in any of the values provided.<br/> * If the value to search for is null and the values to search in <b>contain</b> at lest one null value, then this operation * returns <b>true</b>. * * @param value * Value to validate. * @param values * Array of values to search in. * @return True only if the value is contained in the given array. * @see #isOneOfIgnoreCase(String, String...) */ public static boolean isOneOf(String value, String... values) { return isOneOf(value, (Object[]) values); } /** * Checks if the given value exists in any of the values provided.<br/> * If the value to search for is null and the values to search in <b>contain</b> at lest one null value, then this operation * returns <b>true</b>. * * @param value * Value to validate. * @param values * Array of values to search in. * @return True only if the value is contained in the given array. * @see #isOneOfIgnoreCase(String, String...) */ public static boolean isOneOf(String value, Object... values) { return isOneOf(false, value, values); } /** * Checks if the given value exists in any of the values provided. * * @param ignoreCase * Toggles 'ignoreCase' mode on/off. * @param value * Value to validate. * @param values * Array of values to search in. * @return True only if the value is contained in the given array. */ private static boolean isOneOf(boolean ignoreCase, String value, Object... values) { if (values != null) { for (Object item : values) { if (value == null && item == null) { return true; } else if (value != null && item != null) { if ((!ignoreCase && value.equals(item.toString())) || (ignoreCase && value.equalsIgnoreCase(item.toString()))) { return true; } } } } return false; } /** * Compare the two strings.<br/> * Internally using {@link StringUtils#isOneOf(String, String...)}. * * @param str1 * string to compare with * @param str2 * string to compare to * @return True if the strings match, false otherwise. * @see #equalsIgnoreCase(String, String) * @see #isOneOf(String, String...) */ public static boolean equals(String str1, String str2) { return equals(str1, (Object) str2); } /** * Compare the two strings.<br/> * Internally using {@link StringUtils#isOneOf(String, String...)}. * * @param str1 * string to compare with * @param str2 * string to compare to * @return True if the strings match, false otherwise. * @see #equalsIgnoreCase(String, String) * @see #isOneOf(String, String...) */ public static boolean equals(String str1, Object str2) { return isOneOf(str1, str2); } /** * Compare the two strings.<br/> * * @param str1 * string to compare with * @param str2 * string to compare to * @param collator * pre-prepared {@link Collator} to perform the comparison with * @return True if the strings match according with the provided {@link Collator strength}, false otherwise. * @see #equals(String, String, int) * @see #equals(String, Object, int) * @see #equals(String, Object, Collator) */ public static boolean equals(String str1, String str2, Collator collator) { return equals(str1, (Object) str2, collator); } /** * Compare the two strings.<br/> * Internally using {@link StringUtils#equals(String, Object, int)}. * * @param str1 * string to compare with * @param str2 * string to compare to * @param strength * {@link Collator} value indicating the strength of the comparison to perform. Must be one of {@link Collator#PRIMARY} , * {@link Collator#SECONDARY} or {@link Collator#TERTIARY} * @return True if the strings match according with the provided {@link Collator strength}, false otherwise. * @see #equals(String, Object, int) * @see #equals(String, String, Collator) * @see #equals(String, Object, Collator) */ public static boolean equals(String str1, String str2, int strength) { return equals(str1, (Object) str2, strength); } /** * Compare the two strings.<br/> * Internally using {@link StringUtils#equals(String, Object, Collator)}. * * @param str * string to compare with * @param obj * string to compare to * @param strength * {@link Collator} value indicating the strength of the comparison to perform. Must be one of {@link Collator#PRIMARY} , * {@link Collator#SECONDARY} or {@link Collator#TERTIARY} * @return True if the strings match according with the provided {@link Collator strength}, false otherwise. * @see #equals(String, String, int) * @see #equals(String, String, Collator) * @see #equals(String, Object, Collator) */ public static boolean equals(String str, Object obj, int strength) { if (strength == Collator.PRIMARY || strength == Collator.SECONDARY || strength == Collator.TERTIARY) { Collator collator = Collator.getInstance(new Locale("pt", "PT")); collator.setStrength(strength); return equals(str, obj, collator); } return equals(str, obj); } /** * Compare the two strings.<br/> * * @param str * string to compare with * @param obj * string to compare to * @param collator * pre-prepared {@link Collator} to perform the comparison with * @return True if the strings match according with the provided {@link Collator strength}, false otherwise. * @see #equals(String, String, int) * @see #equals(String, Object, int) * @see #equals(String, String, Collator) */ public static boolean equals(String str, Object obj, Collator collator) { if (str != null && obj != null) { if (collator == null) { collator = Collator.getInstance(new Locale("pt", "PT")); } return collator.equals(str, obj.toString()); } return equals(str, obj); } /** * Compare the two strings, ignoring letter-case.<br/> * Internally uses {@link StringUtils#isOneOfIgnoreCase(String, String...)}. * * @param str1 * string to compare with * @param str2 * string to compare to * @return True if the strings match regardless of letter-case, false otherwise. * @see #equalsIgnoreCase(String, String) * @see #isOneOfIgnoreCase(String, String...) */ public static boolean equalsIgnoreCase(String str1, String str2) { return equalsIgnoreCase(str1, (Object) str2); } /** * Compare the two strings, ignoring letter-case.<br/> * Internally uses {@link StringUtils#isOneOfIgnoreCase(String, String...)}. * * @param str1 * string to compare with * @param str2 * string to compare to * @return True if the strings match regardless of letter-case, false otherwise. * @see #equalsIgnoreCase(String, String) * @see #isOneOfIgnoreCase(String, String...) */ public static boolean equalsIgnoreCase(String str1, Object str2) { return isOneOfIgnoreCase(str1, str2); } /** * Checks if the given value exists in any of the values provided, regardless of letter-case.<br/> * If the value to search for is null and the values to search in <b>contain</b> at lest one null value, then this operation * returns <b>true</b>. * * @param value * Value to validate. * @param values * Array of values to search in. * @return True only if the value is contained in the given array. * @see #isOneOf(String, String...) */ public static boolean isOneOfIgnoreCase(String value, String... values) { return isOneOfIgnoreCase(value, (Object[]) values); } /** * Checks if the given value exists in any of the values provided, regardless of letter-case.<br/> * If the value to search for is null and the values to search in <b>contain</b> at lest one null value, then this operation * returns <b>true</b>. * * @param value * Value to validate. * @param values * Array of values to search in. * @return True only if the value is contained in the given array. * @see #isOneOf(String, String...) */ public static boolean isOneOfIgnoreCase(String value, Object... values) { return isOneOf(true, value, values); } }