garydgregory commented on a change in pull request #91: Add drain method to
CollectionUtils
URL: https://github.com/apache/commons-collections/pull/91#discussion_r339282840
##########
File path: src/main/java/org/apache/commons/collections4/CollectionUtils.java
##########
@@ -1791,6 +1791,63 @@ public static int maxSize(final Collection<? extends
Object> coll) {
return list;
}
+ /**
+ * Removes elements whose index are between startIndex and endIndex in the
collection and returns them.
+ * This method would have the side-effect of modifying the input
collections.
+ *
+ * @param <E> the type of object the {@link Collection} contains
+ * @param input the collection will be operated,can't be null
+ * @param startIndex the start index to remove element,can't be less than 0
+ * @param endIndex the end index to remove,can't be less than startIndex
+ * @return collection of elements that removed from the input collection
+ */
+ public static <E> Collection<E> removeRange(Collection<E> input, int
startIndex, int endIndex) {
+ if (endIndex < startIndex) {
+ throw new IllegalArgumentException("The start index can't less
than the end index.");
+ }
+ return CollectionUtils.removeCount(input, startIndex,
endIndex-startIndex);
+ }
+
+ /**
+ * Removes the specified number of elements from the start index in the
collection and returns them.
+ * This method would have the side-effect of modifying the input
collections.
+ *
+ * @param <E> the type of object the {@link Collection} contains
+ * @param input the collection will be operated,can't be null
+ * @param startIndex the start index to remove element,can't be less than 0
+ * @param count the specified number to remove,can't be less than 1
+ * @return collection of elements that removed from the input collection
+ */
+ public static <E> Collection<E> removeCount(Collection<E> input, int
startIndex, int count) {
+ if (null == input) {
+ throw new IllegalArgumentException("The collection can't be
null.");
+ }
+ if (startIndex < 0) {
+ throw new IllegalArgumentException("The start index can't less
than 0.");
+ }
+ if (count < 0) {
+ throw new IllegalArgumentException("The count can't less than 0.");
+ }
+ if (input.size() < startIndex + count) {
+ throw new IllegalArgumentException(
+ "The sum of start index and count cann't be greater than
the size of collection.");
+ }
+
+ Collection<E> result = new ArrayList<E>(count);
+ Iterator<E> iterator = input.iterator();
+ while (count > 0) {
+ if(startIndex > 0) {
Review comment:
"if(" -> "if ("
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services