Skip to content

Commit

Permalink
Add @SInCE tags to firstElement methods
Browse files Browse the repository at this point in the history
  • Loading branch information
jhoeller committed Dec 5, 2019
1 parent d503bc2 commit c2141e2
Showing 1 changed file with 31 additions and 29 deletions.
Expand Up @@ -314,84 +314,86 @@ else if (candidate != val.getClass()) {
}

/**
* Retrieve the last element of the given Set, using {@link SortedSet#last()}
* or otherwise iterating over all elements (assuming a linked set).
* 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 last element, or {@code null} if none
* @since 5.0.3
* @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 lastElement(@Nullable Set<T> set) {
public static <T> T firstElement(@Nullable Set<T> set) {
if (isEmpty(set)) {
return null;
}
if (set instanceof SortedSet) {
return ((SortedSet<T>) set).last();
return ((SortedSet<T>) set).first();
}

// Full iteration necessary...
Iterator<T> it = set.iterator();
T last = null;
while (it.hasNext()) {
last = it.next();
T first = null;
if (it.hasNext()) {
first = it.next();
}
return last;
return first;
}

/**
* Retrieve the last element of the given List, accessing the highest index.
* 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 last element, or {@code null} if none
* @since 5.0.3
* @return the first element, or {@code null} if none
* @since 5.2.3
*/
@Nullable
public static <T> T lastElement(@Nullable List<T> list) {
public static <T> T firstElement(@Nullable List<T> list) {
if (isEmpty(list)) {
return null;
}
return list.get(list.size() - 1);
return list.get(0);
}

/**
* Retrieve the first element of the given Set, using {@link SortedSet#first()}
* or otherwise using the iterator.
* Retrieve the last element of the given Set, using {@link SortedSet#last()}
* or otherwise iterating over all elements (assuming a linked set).
* @param set the Set to check (may be {@code null} or empty)
* @return the first element, or {@code null} if none
* @return the last element, or {@code null} if none
* @since 5.0.3
* @see SortedSet
* @see LinkedHashMap#keySet()
* @see java.util.LinkedHashSet
*/
@Nullable
public static <T> T firstElement(@Nullable Set<T> set) {
public static <T> T lastElement(@Nullable Set<T> set) {
if (isEmpty(set)) {
return null;
}
if (set instanceof SortedSet) {
return ((SortedSet<T>) set).first();
return ((SortedSet<T>) set).last();
}

// Full iteration necessary...
Iterator<T> it = set.iterator();
T first = null;
if (it.hasNext()) {
first = it.next();
T last = null;
while (it.hasNext()) {
last = it.next();
}
return first;
return last;
}

/**
* Retrieve the first element of the given List, accessing the zero index.
* Retrieve the last element of the given List, accessing the highest index.
* @param list the List to check (may be {@code null} or empty)
* @return the first element, or {@code null} if none
* @return the last element, or {@code null} if none
* @since 5.0.3
*/
@Nullable
public static <T> T firstElement(@Nullable List<T> list) {
public static <T> T lastElement(@Nullable List<T> list) {
if (isEmpty(list)) {
return null;
}
return list.get(0);
return list.get(list.size() - 1);
}

/**
Expand Down

1 comment on commit c2141e2

@sbrannen
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #24135

Please sign in to comment.