Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added utility for compacting a list (i.e. removing nulls)

  • Loading branch information...
commit 2b38a39f7217e2dfb6f52f4fb508d2d1c72df8ca 1 parent 7c95e28
Luke Daley alkemist authored
26 subprojects/core/src/main/groovy/org/gradle/util/CollectionUtils.java
View
@@ -16,8 +16,8 @@
package org.gradle.util;
import com.google.common.collect.Lists;
-import org.gradle.api.specs.Spec;
import org.gradle.api.Transformer;
+import org.gradle.api.specs.Spec;
import java.util.*;
@@ -72,4 +72,28 @@
}
return result;
}
+
+ public static <E> List<E> compact(List<E> list) {
+ boolean foundAtLeastOneNull = false;
+ List<E> compacted = null;
+ int i = 0;
+
+ for (E element : list) {
+ if (element == null) {
+ if (!foundAtLeastOneNull) {
+ compacted = new ArrayList<E>(list.size());
+ if (i > 0) {
+ compacted.addAll(list.subList(0, i));
+ }
+ }
+ foundAtLeastOneNull = true;
+ } else if (foundAtLeastOneNull) {
+ compacted.add(element);
+ }
+ ++i;
+ }
+
+ return foundAtLeastOneNull ? compacted : list;
+ }
+
}
54 subprojects/core/src/test/groovy/org/gradle/util/CollectionUtilsTest.groovy
View
@@ -15,52 +15,64 @@
*/
package org.gradle.util
-import org.gradle.api.specs.Spec
-import org.gradle.api.specs.Specs
import org.gradle.api.Transformer
-
-import spock.lang.*
+import org.gradle.api.specs.Specs
+import spock.lang.Specification
+import static org.gradle.util.CollectionUtils.*
class CollectionUtilsTest extends Specification {
def "list filtering"() {
given:
def spec = Specs.convertClosureToSpec { it < 5 }
- def filter = { Integer[] nums -> CollectionUtils.filter(nums as List, spec) }
-
+ def filter = { Integer[] nums -> filter(nums as List, spec) }
+
expect:
- filter(1,2,3) == [1,2,3]
- filter(7,8,9) == []
+ filter(1, 2, 3) == [1, 2, 3]
+ filter(7, 8, 9) == []
filter() == []
- filter(4,5,6) == [4]
+ filter(4, 5, 6) == [4]
}
-
+
def "list collecting"() {
- def transformer = new Transformer() { def transform(i) { i * 2 } }
- def collect = { Integer[] nums -> CollectionUtils.collect(nums as List, transformer) }
-
+ def transformer = new Transformer() {
+ def transform(i) { i * 2 }
+ }
+ def collect = { Integer[] nums -> collect(nums as List, transformer) }
+
expect:
- collect(1,2,3) == [2,4,6]
+ collect(1, 2, 3) == [2, 4, 6]
collect() == []
}
def "set filtering"() {
given:
def spec = Specs.convertClosureToSpec { it < 5 }
- def filter = { Integer[] nums -> CollectionUtils.filter(nums as Set, spec) }
-
+ def filter = { Integer[] nums -> filter(nums as Set, spec) }
+
expect:
- filter(1,2,3) == [1,2,3] as Set
- filter(7,8,9).empty
+ filter(1, 2, 3) == [1, 2, 3] as Set
+ filter(7, 8, 9).empty
filter().empty
- filter(4,5,6) == [4] as Set
+ filter(4, 5, 6) == [4] as Set
}
def toStringList() {
def list = [42, "string"]
expect:
- CollectionUtils.toStringList([]) == []
- CollectionUtils.toStringList(list) == ["42", "string"]
+ toStringList([]) == []
+ toStringList(list) == ["42", "string"]
+ }
+
+ def "list compacting"() {
+ expect:
+ compact([1, null, 2]) == [1, 2]
+ compact([null, 1, 2]) == [1, 2]
+ compact([1, 2, null]) == [1, 2]
+
+ def l = [1, 2, 3]
+ compact(l).is l
+
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.