Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
[JENKINS-51779] Avoid com.google.common.collect.Iterators.skip (#3481)
* [JENKINS-51779] Avoid com.google.common.collect.Iterators.skip.
* Reviewers preferred for the new method to be restricted for now.
- Loading branch information
|
@@ -23,6 +23,7 @@ |
|
|
*/ |
|
|
package hudson.util; |
|
|
|
|
|
import com.google.common.annotations.Beta; |
|
|
import com.google.common.base.Predicates; |
|
|
import com.google.common.collect.ImmutableList; |
|
|
|
|
@@ -35,6 +36,9 @@ |
|
|
import java.util.Arrays; |
|
|
import java.util.Set; |
|
|
import java.util.HashSet; |
|
|
import javax.annotation.Nonnull; |
|
|
import org.kohsuke.accmod.Restricted; |
|
|
import org.kohsuke.accmod.restrictions.NoExternalUse; |
|
|
|
|
|
/** |
|
|
* Varios {@link Iterator} implementations. |
|
@@ -403,4 +407,20 @@ public void remove() { |
|
|
public interface CountingPredicate<T> { |
|
|
boolean apply(int index, T input); |
|
|
} |
|
|
|
|
|
/** |
|
|
* Similar to {@link com.google.common.collect.Iterators#skip} except not {@link Beta}. |
|
|
* @param iterator some iterator |
|
|
* @param count a nonnegative count |
|
|
*/ |
|
|
@Restricted(NoExternalUse.class) |
|
|
public static void skip(@Nonnull Iterator<?> iterator, int count) { |
|
|
if (count < 0) { |
|
|
throw new IllegalArgumentException(); |
|
|
} |
|
|
while (iterator.hasNext() && count-- > 0) { |
|
|
iterator.next(); |
|
|
} |
|
|
} |
|
|
|
|
|
} |
|
@@ -150,7 +150,7 @@ public R get(int index) { |
|
|
public List<R> subList(int fromIndex, int toIndex) { |
|
|
List<R> r = new ArrayList<R>(); |
|
|
Iterator<R> itr = iterator(); |
|
|
Iterators.skip(itr,fromIndex); |
|
|
hudson.util.Iterators.skip(itr, fromIndex); |
|
|
for (int i=toIndex-fromIndex; i>0; i--) { |
|
|
r.add(itr.next()); |
|
|
} |
|
|
|
@@ -24,14 +24,14 @@ |
|
|
package jenkins.widgets; |
|
|
|
|
|
import com.google.common.collect.Iterables; |
|
|
import com.google.common.collect.Iterators; |
|
|
import hudson.model.AbstractBuild; |
|
|
import hudson.model.Job; |
|
|
import hudson.model.ParameterValue; |
|
|
import hudson.model.ParametersAction; |
|
|
import hudson.model.Queue; |
|
|
import hudson.model.Run; |
|
|
import hudson.search.UserSearchProperty; |
|
|
import hudson.util.Iterators; |
|
|
import hudson.widgets.HistoryWidget; |
|
|
|
|
|
import javax.annotation.Nonnull; |
|
|
|
@@ -32,6 +32,7 @@ |
|
|
import java.util.List; |
|
|
|
|
|
import org.junit.Test; |
|
|
import org.jvnet.hudson.test.Issue; |
|
|
|
|
|
/** |
|
|
* @author Kohsuke Kawaguchi |
|
@@ -77,9 +78,27 @@ public void limit() { |
|
|
assertEquals("[]", com.google.common.collect.Iterators.toString(Iterators.limit(asList(1,2,4,6).iterator(), EVEN))); |
|
|
} |
|
|
|
|
|
public static final CountingPredicate<Integer> EVEN = new CountingPredicate<Integer>() { |
|
|
public boolean apply(int index, Integer input) { |
|
|
return input % 2 == 0; |
|
|
} |
|
|
}; |
|
|
public static final CountingPredicate<Integer> EVEN = (index, input) -> input % 2 == 0; |
|
|
|
|
|
@Issue("JENKINS-51779") |
|
|
@Test |
|
|
public void skip() { |
|
|
List<Integer> lst = Iterators.sequence(1, 4); |
|
|
Iterator<Integer> it = lst.iterator(); |
|
|
Iterators.skip(it, 0); |
|
|
assertEquals("[1, 2, 3]", com.google.common.collect.Iterators.toString(it)); |
|
|
it = lst.iterator(); |
|
|
Iterators.skip(it, 1); |
|
|
assertEquals("[2, 3]", com.google.common.collect.Iterators.toString(it)); |
|
|
it = lst.iterator(); |
|
|
Iterators.skip(it, 2); |
|
|
assertEquals("[3]", com.google.common.collect.Iterators.toString(it)); |
|
|
it = lst.iterator(); |
|
|
Iterators.skip(it, 3); |
|
|
assertEquals("[]", com.google.common.collect.Iterators.toString(it)); |
|
|
it = lst.iterator(); |
|
|
Iterators.skip(it, 4); |
|
|
assertEquals("[]", com.google.common.collect.Iterators.toString(it)); |
|
|
} |
|
|
|
|
|
} |