/** * Determine whether the given Collection only contains a single unique * object. * * @param collection the Collection to check * @return {@code true} if the collection contains a single reference or * multiple references to the same instance, {@code false} else */ public static boolean hasUniqueObject(Collection<?> collection) { if (isEmpty(collection)) { return false; } boolean hasCandidate = false; Object candidate = null; for (Object elem : collection) { if (!hasCandidate) { hasCandidate = true; candidate = elem; } else if (candidate != elem) { return false; } } return true; }
/** * Find a single value of one of the given types in the given Collection: * searching the Collection for a value of the first type, then searching * for a value of the second type, etc. * * @param collection the collection to search * @param types the types to look for, in prioritized order * @return a value of one of the given types found if there is a clear * match, or {@code null} if none or more than one such value found */ public static Object findValueOfType(Collection<?> collection, Class<?>[] types) { if (isEmpty(collection) || ObjectUtils.isEmpty(types)) { return null; } for (Class<?> type : types) { Object value = findValueOfType(collection, type); if (value != null) { return value; } } return null; }
/** * Determine whether the given Collection only contains a single unique * object. * * @param collection the Collection to check * @return {@code true} if the collection contains a single reference or * multiple references to the same instance, {@code false} else */ public static boolean hasUniqueObject(Collection<?> collection) { if (isEmpty(collection)) { return false; } boolean hasCandidate = false; Object candidate = null; for (Object elem : collection) { if (!hasCandidate) { hasCandidate = true; candidate = elem; } else if (candidate != elem) { return false; } } return true; }
/** * Find a single value of one of the given types in the given Collection: * searching the Collection for a value of the first type, then searching * for a value of the second type, etc. * * @param collection * the collection to search * @param types * the types to look for, in prioritized order * @return a value of one of the given types found if there is a clear * match, or {@code null} if none or more than one such value found */ public static Object findValueOfType(Collection<?> collection, Class<?>[] types) { if (isEmpty(collection) || ObjectUtils.isEmpty(types)) { return null; } for (Class<?> type : types) { Object value = findValueOfType(collection, type); if (value != null) { return value; } } return null; }
/** * Determine whether the given Collection only contains a single unique * object. * * @param collection * the Collection to check * @return {@code true} if the collection contains a single reference or * multiple references to the same instance, {@code false} else */ public static boolean hasUniqueObject(Collection<?> collection) { if (isEmpty(collection)) { return false; } boolean hasCandidate = false; Object candidate = null; for (Object elem : collection) { if (!hasCandidate) { hasCandidate = true; candidate = elem; } else if (candidate != elem) { return false; } } return true; }
/** * Find a single value of one of the given types in the given Collection: * searching the Collection for a value of the first type, then searching * for a value of the second type, etc. * * @param collection the collection to search * @param types the types to look for, in prioritized order * @return a value of one of the given types found if there is a clear * match, or {@code null} if none or more than one such value found */ public static Object findValueOfType(Collection<?> collection, Class<?>[] types) { if (isEmpty(collection) || ObjectUtils.isEmpty(types)) { return null; } for (Class<?> type : types) { Object value = findValueOfType(collection, type); if (value != null) { return value; } } return null; }
/** * Return {@code true} if any element in '{@code candidates}' is contained * in '{@code source}'; otherwise returns {@code false}. * * @param source the source Collection * @param candidates the candidates to search for * @return whether any of the candidates has been found */ public static boolean containsAny(Collection<?> source, Collection<?> candidates) { if (isEmpty(source) || isEmpty(candidates)) { return false; } for (Object candidate : candidates) { if (source.contains(candidate)) { return true; } } return false; }
public String select(List<String> clientProtocols) { if (!CollectionUtils.isEmpty(clientProtocols)) { for (String p : supportedProtocols) { if (clientProtocols.contains(p)) { log.debug("ALPN local server selected protocol -> {}", p); return p; } } } return null; } }
public String select(List<String> clientProtocols) { if (!CollectionUtils.isEmpty(clientProtocols)) { for (String p : supportedProtocols) { if (clientProtocols.contains(p)) { log.debug("ALPN local server selected protocol -> {}", p); return p; } } } return null; } }
/** * Return {@code true} if any element in '{@code candidates}' is contained * in '{@code source}'; otherwise returns {@code false}. * * @param source the source Collection * @param candidates the candidates to search for * @return whether any of the candidates has been found */ public static boolean containsAny(Collection<?> source, Collection<?> candidates) { if (isEmpty(source) || isEmpty(candidates)) { return false; } for (Object candidate : candidates) { if (source.contains(candidate)) { return true; } } return false; }
/** * Return {@code true} if any element in '{@code candidates}' is contained * in '{@code source}'; otherwise returns {@code false}. * * @param source * the source Collection * @param candidates * the candidates to search for * @return whether any of the candidates has been found */ public static boolean containsAny(Collection<?> source, Collection<?> candidates) { if (isEmpty(source) || isEmpty(candidates)) { return false; } for (Object candidate : candidates) { if (source.contains(candidate)) { return true; } } return false; }
/** * Assert that a collection has elements; that is, it must not be * {@code null} and must have at least one element. * * <pre class="code"> * Assert.notEmpty(collection, "Collection must have elements"); * </pre> * * @param collection the collection to check * @param message the exception message to use if the assertion fails * @throws IllegalArgumentException if the collection is {@code null} or has no elements */ public static void notEmpty(Collection<?> collection, String message) { if (CollectionUtils.isEmpty(collection)) { throw new IllegalArgumentException(message); } }
/** * Assert that a Map has entries; that is, it must not be {@code null} and * must have at least one entry. * * <pre class="code"> * Assert.notEmpty(map, "Map must have entries"); * </pre> * * @param map the map to check * @param message the exception message to use if the assertion fails * @throws IllegalArgumentException if the map is {@code null} or has no entries */ public static void notEmpty(Map<?, ?> map, String message) { if (CollectionUtils.isEmpty(map)) { throw new IllegalArgumentException(message); } }
/** * Return the first element in '{@code candidates}' that is contained in ' * {@code source}'. If no element in '{@code candidates}' is present in ' * {@code source}' returns {@code null}. Iteration order is * {@link Collection} implementation specific. * * @param source the source Collection * @param candidates the candidates to search for * @return the first present object, or {@code null} if not found */ @SuppressWarnings("unchecked") public static <E> E findFirstMatch(Collection<?> source, Collection<E> candidates) { if (isEmpty(source) || isEmpty(candidates)) { return null; } for (Object candidate : candidates) { if (source.contains(candidate)) { return (E) candidate; } } return null; }
/** * Return the first element in '{@code candidates}' that is contained in ' * {@code source}'. If no element in '{@code candidates}' is present in ' * {@code source}' returns {@code null}. Iteration order is * {@link Collection} implementation specific. * * @param source the source Collection * @param candidates the candidates to search for * @return the first present object, or {@code null} if not found */ @SuppressWarnings("unchecked") public static <E> E findFirstMatch(Collection<?> source, Collection<E> candidates) { if (isEmpty(source) || isEmpty(candidates)) { return null; } for (Object candidate : candidates) { if (source.contains(candidate)) { return (E) candidate; } } return null; }
/** * Assert that a Map has entries; that is, it must not be {@code null} and * must have at least one entry. * * <pre class="code"> * Assert.notEmpty(map, "Map must have entries"); * </pre> * * @param map the map to check * @param message the exception message to use if the assertion fails * @throws IllegalArgumentException if the map is {@code null} or has no entries */ public static void notEmpty(Map<?, ?> map, String message) { if (CollectionUtils.isEmpty(map)) { throw new IllegalArgumentException(message); } }
/** * Assert that a collection has elements; that is, it must not be * {@code null} and must have at least one element. * * <pre class="code"> * Assert.notEmpty(collection, "Collection must have elements"); * </pre> * * @param collection the collection to check * @param message the exception message to use if the assertion fails * @throws IllegalArgumentException if the collection is {@code null} or has no elements */ public static void notEmpty(Collection<?> collection, String message) { if (CollectionUtils.isEmpty(collection)) { throw new IllegalArgumentException(message); } }
/** * Return the first element in '{@code candidates}' that is contained in ' * {@code source}'. If no element in '{@code candidates}' is present in ' * {@code source}' returns {@code null}. Iteration order is * {@link Collection} implementation specific. * * @param source * the source Collection * @param candidates * the candidates to search for * @return the first present object, or {@code null} if not found */ @SuppressWarnings("unchecked") public static <E> E findFirstMatch(Collection<?> source, Collection<E> candidates) { if (isEmpty(source) || isEmpty(candidates)) { return null; } for (Object candidate : candidates) { if (source.contains(candidate)) { return (E) candidate; } } return null; }
/** * Assert that a collection has elements; that is, it must not be * {@code null} and must have at least one element. * * <pre class="code"> * Assert.notEmpty(collection, "Collection must have elements"); * </pre> * * @param collection * the collection to check * @param message * the exception message to use if the assertion fails * @throws IllegalArgumentException * if the collection is {@code null} or has no elements */ public static void notEmpty(Collection<?> collection, String message) { if (CollectionUtils.isEmpty(collection)) { throw new IllegalArgumentException(message); } }
/** * Assert that a Map has entries; that is, it must not be {@code null} and * must have at least one entry. * * <pre class="code"> * Assert.notEmpty(map, "Map must have entries"); * </pre> * * @param map * the map to check * @param message * the exception message to use if the assertion fails * @throws IllegalArgumentException * if the map is {@code null} or has no entries */ public static void notEmpty(Map<?, ?> map, String message) { if (CollectionUtils.isEmpty(map)) { throw new IllegalArgumentException(message); } }