Skip to content

Commit

Permalink
Minor optimiazion on Last
Browse files Browse the repository at this point in the history
  • Loading branch information
dmfs committed Dec 8, 2024
1 parent 2fb46dc commit fb78ccc
Showing 1 changed file with 10 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package org.saynotobugs.confidence.utils;

import org.dmfs.jems2.Optional;
import org.dmfs.jems2.optional.LazyDelegatingOptional;
import org.dmfs.jems2.optional.Present;

import java.util.Iterator;

import static org.dmfs.jems2.optional.Absent.absent;

public final class Last<T> extends LazyDelegatingOptional<T>
Expand All @@ -12,12 +13,16 @@ public Last(Iterable<? extends T> delegates)
{
super(() ->
{
Optional<T> last = absent();
for (T value : delegates)
Iterator<? extends T> iterator = delegates.iterator();
while (iterator.hasNext())
{
last = new Present<>(value);
T next = iterator.next();
if (!iterator.hasNext())
{
return new Present<>(next);
}
}
return last;
return absent();
}
);
}
Expand Down

0 comments on commit fb78ccc

Please sign in to comment.