Add @since tags to firstElement methods

This commit is contained in:
Juergen Hoeller 2019-12-05 16:36:55 +01:00
parent d503bc2804
commit c2141e2e93
1 changed files with 41 additions and 39 deletions

View File

@ -313,6 +313,47 @@ public abstract class CollectionUtils {
return candidate;
}
/**
* Retrieve the first element of the given Set, using {@link SortedSet#first()}
* or otherwise using the iterator.
* @param set the Set to check (may be {@code null} or empty)
* @return the first element, or {@code null} if none
* @since 5.2.3
* @see SortedSet
* @see LinkedHashMap#keySet()
* @see java.util.LinkedHashSet
*/
@Nullable
public static <T> T firstElement(@Nullable Set<T> set) {
if (isEmpty(set)) {
return null;
}
if (set instanceof SortedSet) {
return ((SortedSet<T>) set).first();
}
Iterator<T> it = set.iterator();
T first = null;
if (it.hasNext()) {
first = it.next();
}
return first;
}
/**
* Retrieve the first element of the given List, accessing the zero index.
* @param list the List to check (may be {@code null} or empty)
* @return the first element, or {@code null} if none
* @since 5.2.3
*/
@Nullable
public static <T> T firstElement(@Nullable List<T> list) {
if (isEmpty(list)) {
return null;
}
return list.get(0);
}
/**
* Retrieve the last element of the given Set, using {@link SortedSet#last()}
* or otherwise iterating over all elements (assuming a linked set).
@ -355,45 +396,6 @@ public abstract class CollectionUtils {
return list.get(list.size() - 1);
}
/**
* Retrieve the first element of the given Set, using {@link SortedSet#first()}
* or otherwise using the iterator.
* @param set the Set to check (may be {@code null} or empty)
* @return the first element, or {@code null} if none
* @see SortedSet
* @see LinkedHashMap#keySet()
* @see java.util.LinkedHashSet
*/
@Nullable
public static <T> T firstElement(@Nullable Set<T> set) {
if (isEmpty(set)) {
return null;
}
if (set instanceof SortedSet) {
return ((SortedSet<T>) set).first();
}
Iterator<T> it = set.iterator();
T first = null;
if (it.hasNext()) {
first = it.next();
}
return first;
}
/**
* Retrieve the first element of the given List, accessing the zero index.
* @param list the List to check (may be {@code null} or empty)
* @return the first element, or {@code null} if none
*/
@Nullable
public static <T> T firstElement(@Nullable List<T> list) {
if (isEmpty(list)) {
return null;
}
return list.get(0);
}
/**
* Marshal the elements from the given enumeration into an array of the given type.
* Enumeration elements must be assignable to the type of the given array. The array