提交 817fac55 编写于 作者: K kohsuke

adding a sequence list generator.


git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@5442 71c3de6d-444a-0410-be80-ed276b4c234a
上级 c49b8d76
......@@ -5,6 +5,7 @@ import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.ListIterator;
import java.util.AbstractList;
/**
* Varios {@link Iterator} implementations.
......@@ -79,4 +80,40 @@ public class Iterators {
}
};
}
/**
* Returns a list that represents [start,end).
*
* For example sequence(1,5,1)={1,2,3,4}, and sequence(7,1,-2)={7.5,3}
*/
public static List<Integer> sequence(final int start, int end, final int step) {
final int size = (end-start)/step;
if(size<0) throw new IllegalArgumentException("List size is negative");
return new AbstractList<Integer>() {
public Integer get(int index) {
return start+index*step;
}
public int size() {
return size;
}
};
}
public static List<Integer> sequence(int start, int end) {
return sequence(start,end,1);
}
/**
* The short cut for {@code reverse(sequence(start,end,step))}.
*/
public static List<Integer> reverseSequence(int start, int end, int step) {
return sequence(end-1,start-1,-step);
}
public static List<Integer> reverseSequence(int start, int end) {
return reverseSequence(start,end,1);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册