Skip to content

Commit

Permalink
[JENKINS-51779] Avoid com.google.common.collect.Iterators.skip (#3481)
Browse files Browse the repository at this point in the history
* [JENKINS-51779] Avoid com.google.common.collect.Iterators.skip.

* Reviewers preferred for the new method to be restricted for now.
  • Loading branch information
jglick authored and oleg-nenashev committed Jun 8, 2018
1 parent 64e4679 commit a846c36
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 7 deletions.
20 changes: 20 additions & 0 deletions core/src/main/java/hudson/util/Iterators.java
Expand Up @@ -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;

Expand All @@ -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.
Expand Down Expand Up @@ -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();
}
}

}
2 changes: 1 addition & 1 deletion core/src/main/java/hudson/util/RunList.java
Expand Up @@ -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());
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/jenkins/widgets/HistoryPageFilter.java
Expand Up @@ -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;
Expand Down
29 changes: 24 additions & 5 deletions core/src/test/java/hudson/util/IteratorsTest.java
Expand Up @@ -32,6 +32,7 @@
import java.util.List;

import org.junit.Test;
import org.jvnet.hudson.test.Issue;

/**
* @author Kohsuke Kawaguchi
Expand Down Expand Up @@ -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));
}

}

0 comments on commit a846c36

Please sign in to comment.