Repository: incubator-brooklyn
Updated Branches:
  refs/heads/master ee29db530 -> 61b5d7052


Adds MutableSet.Builder.addIfNotNull

- And adds MutableList.Builder.addIfNotNull
- Adds MutableSetTest

Project: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/repo
Commit: 
http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/commit/ddfb6e29
Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/ddfb6e29
Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/ddfb6e29

Branch: refs/heads/master
Commit: ddfb6e29a3df314ab4bc6c4aeefded6d61a4486d
Parents: f29b3fb
Author: Aled Sage <[email protected]>
Authored: Tue Oct 20 21:35:35 2015 +0100
Committer: Aled Sage <[email protected]>
Committed: Tue Oct 20 21:37:50 2015 +0100

----------------------------------------------------------------------
 .../brooklyn/util/collections/MutableList.java  |   5 +
 .../brooklyn/util/collections/MutableSet.java   |   5 +
 .../util/collections/MutableListTest.java       |   6 +-
 .../util/collections/MutableSetTest.java        | 123 +++++++++++++++++++
 4 files changed, 138 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/ddfb6e29/utils/common/src/main/java/org/apache/brooklyn/util/collections/MutableList.java
----------------------------------------------------------------------
diff --git 
a/utils/common/src/main/java/org/apache/brooklyn/util/collections/MutableList.java
 
b/utils/common/src/main/java/org/apache/brooklyn/util/collections/MutableList.java
index 4f8206a..9aa3a99 100644
--- 
a/utils/common/src/main/java/org/apache/brooklyn/util/collections/MutableList.java
+++ 
b/utils/common/src/main/java/org/apache/brooklyn/util/collections/MutableList.java
@@ -118,6 +118,11 @@ public class MutableList<V> extends ArrayList<V> {
 
         public Builder() {}
 
+        public Builder<V> addIfNotNull(V value) {
+            if (value != null) result.add(value);
+            return this;
+        }
+
         public Builder<V> add(V value) {
             result.add(value);
             return this;

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/ddfb6e29/utils/common/src/main/java/org/apache/brooklyn/util/collections/MutableSet.java
----------------------------------------------------------------------
diff --git 
a/utils/common/src/main/java/org/apache/brooklyn/util/collections/MutableSet.java
 
b/utils/common/src/main/java/org/apache/brooklyn/util/collections/MutableSet.java
index f851305..af7f0fb 100644
--- 
a/utils/common/src/main/java/org/apache/brooklyn/util/collections/MutableSet.java
+++ 
b/utils/common/src/main/java/org/apache/brooklyn/util/collections/MutableSet.java
@@ -118,6 +118,11 @@ public class MutableSet<V> extends LinkedHashSet<V> {
 
         public Builder() {}
 
+        public Builder<V> addIfNotNull(V value) {
+            if (value != null) result.add(value);
+            return this;
+        }
+
         public Builder<V> add(V value) {
             result.add(value);
             return this;

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/ddfb6e29/utils/common/src/test/java/org/apache/brooklyn/util/collections/MutableListTest.java
----------------------------------------------------------------------
diff --git 
a/utils/common/src/test/java/org/apache/brooklyn/util/collections/MutableListTest.java
 
b/utils/common/src/test/java/org/apache/brooklyn/util/collections/MutableListTest.java
index 5d3a140..046d434 100644
--- 
a/utils/common/src/test/java/org/apache/brooklyn/util/collections/MutableListTest.java
+++ 
b/utils/common/src/test/java/org/apache/brooklyn/util/collections/MutableListTest.java
@@ -21,7 +21,6 @@ package org.apache.brooklyn.util.collections;
 import java.util.Arrays;
 import java.util.List;
 
-import org.apache.brooklyn.util.collections.MutableList;
 import org.testng.Assert;
 import org.testng.annotations.Test;
 
@@ -40,6 +39,11 @@ public class MutableListTest {
         Assert.assertEquals(vals, ImmutableList.of(1,2,3));
     }
     
+    public void testBuilderAddIfNotNull() throws Exception {
+        List<Object> vals = 
MutableList.builder().addIfNotNull(1).addIfNotNull(null).build();
+        Assert.assertEquals(vals, ImmutableList.of(1));
+    }
+    
     public void testBuilderAddIterable() throws Exception {
         List<Object> vals = 
MutableList.builder().addAll(ImmutableList.of(1,2)).addAll(ImmutableList.of(2,3)).build();
         Assert.assertEquals(vals, ImmutableList.of(1,2,2,3));

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/ddfb6e29/utils/common/src/test/java/org/apache/brooklyn/util/collections/MutableSetTest.java
----------------------------------------------------------------------
diff --git 
a/utils/common/src/test/java/org/apache/brooklyn/util/collections/MutableSetTest.java
 
b/utils/common/src/test/java/org/apache/brooklyn/util/collections/MutableSetTest.java
new file mode 100644
index 0000000..0e14464
--- /dev/null
+++ 
b/utils/common/src/test/java/org/apache/brooklyn/util/collections/MutableSetTest.java
@@ -0,0 +1,123 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.brooklyn.util.collections;
+
+import java.util.Set;
+
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+import com.google.common.collect.ImmutableSet;
+
+@Test
+public class MutableSetTest {
+
+    public void testBuilderAddArray() throws Exception {
+        Set<Object> vals = MutableSet.builder().addAll(new Object[] 
{1,2,3}).build();
+        Assert.assertEquals(vals, ImmutableSet.of(1,2,3));
+    }
+    
+    public void testBuilderAddVarargs() throws Exception {
+        Set<Object> vals = MutableSet.builder().add(1,2,3).build();
+        Assert.assertEquals(vals, ImmutableSet.of(1,2,3));
+    }
+    
+    public void testBuilderAddIfNotNull() throws Exception {
+        Set<Object> vals = 
MutableSet.builder().addIfNotNull(1).addIfNotNull(null).build();
+        Assert.assertEquals(vals, ImmutableSet.of(1));
+    }
+    
+    public void testBuilderAddIterable() throws Exception {
+        Set<Object> vals = 
MutableSet.builder().addAll(ImmutableSet.of(1,2)).addAll(ImmutableSet.of(2,3)).build();
+        Assert.assertEquals(vals, ImmutableSet.of(1,2,3));
+    }
+    
+    public void testBuilderAddIterator() throws Exception {
+        Set<Object> vals = 
MutableSet.builder().addAll(ImmutableSet.of(1,2).iterator()).build();
+        Assert.assertEquals(vals, ImmutableSet.of(1,2));
+    }
+    
+    public void testBuilderRemoval() throws Exception {
+        Set<Object> vals = MutableSet.builder()
+                .add(1,2,3)
+                .remove(2)
+                .add(4)
+                .build();
+        Assert.assertEquals(vals, ImmutableSet.of(1,3,4));
+    }
+    
+    public void testBuilderRemoveAll() throws Exception {
+        Set<Object> vals = MutableSet.builder()
+                .add(1,2,3)
+                .removeAll(ImmutableSet.of(2,3))
+                .add(4)
+                .build();
+        Assert.assertEquals(vals, ImmutableSet.of(1,4));
+    }
+    
+    public void testEqualsExact() {
+        Set<Object> a = MutableSet.<Object>of("a", 1, "b", false);
+        Set<Object> b = MutableSet.<Object>of("a", 1, "b", false);
+        Assert.assertEquals(a, b);
+    }
+    
+    public void testEqualsUnordered() {
+        Set<Object> a = MutableSet.<Object>of("a", 1, "b", false);
+        Set<Object> b = MutableSet.<Object>of("b", false, "a", 1);
+        Assert.assertEquals(a, b);
+    }
+
+    public void testEqualsDifferentTypes() {
+        Set<?> a = MutableSet.<Object>of("a", 1, "b", false);
+        Set<?> b = ImmutableSet.of("a", 1, "b", false);
+        Assert.assertEquals(a, b);
+        Assert.assertEquals(b, a);
+    }
+
+    public void testEqualsDifferentTypes2() {
+        Set<Object> a = MutableSet.<Object>of("http");
+        Set<?> b = ImmutableSet.of("http");
+        Assert.assertEquals(a, b);
+        Assert.assertEquals(b, a);
+    }
+
+    public void testContainingNullAndUnmodifiable() {
+        MutableSet<Object> x = MutableSet.<Object>of("x", null);
+        Assert.assertTrue(x.contains(null));
+        
+        Set<Object> x1 = x.asUnmodifiable();
+        Set<Object> x2 = x.asUnmodifiableCopy();
+        Set<Object> x3 = x.asImmutableCopy();
+        
+        x.remove(null);
+        Assert.assertFalse(x.contains(null));
+        Assert.assertFalse(x1.contains(null));
+        Assert.assertTrue(x2.contains(null));
+        Assert.assertTrue(x3.contains(null));
+        
+        try { x1.remove("x"); Assert.fail(); } catch (Exception e) { /* 
expected */ }
+        try { x2.remove("x"); Assert.fail(); } catch (Exception e) { /* 
expected */ }
+        try { x3.remove("x"); Assert.fail(); } catch (Exception e) { /* 
expected */ }
+        
+        Assert.assertTrue(x1.contains("x"));
+        Assert.assertTrue(x2.contains("x"));
+        Assert.assertTrue(x3.contains("x"));
+    }
+    
+}

Reply via email to