brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From henev...@apache.org
Subject [21/26] git commit: make consistent `Mutable{List, Set, Map}.as{Immutable, Unmodifiable, UnmodifiableCopy}()` methods, and more forgiving if null is present, deprecatign the inconsistent and break-prone `toImmutable()`
Date Fri, 29 Aug 2014 23:01:21 GMT
make consistent `Mutable{List,Set,Map}.as{Immutable,Unmodifiable,UnmodifiableCopy}()` methods,
and more forgiving if null is present, deprecatign the inconsistent and break-prone `toImmutable()`


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

Branch: refs/heads/master
Commit: ab1381b413543b69de62b498fe61f05b46cf9cae
Parents: 5db500e
Author: Alex Heneveld <alex.heneveld@cloudsoftcorp.com>
Authored: Tue Aug 26 22:17:20 2014 -0500
Committer: Alex Heneveld <alex.heneveld@cloudsoftcorp.com>
Committed: Wed Aug 27 02:21:22 2014 -0400

----------------------------------------------------------------------
 .../drivers/downloads/BasicDownloadTargets.java |  2 +-
 .../brooklyn/event/basic/ListConfigKey.java     |  2 +-
 .../java/brooklyn/event/basic/SetConfigKey.java |  2 +-
 .../brooklyn/entity/basic/EntityTypeTest.java   |  4 +--
 .../brooklyn/location/jclouds/JcloudsUtil.java  |  2 +-
 .../brooklyn/entity/chef/KnifeTaskFactory.java  |  4 +--
 .../rest/transform/LocationTransformer.java     |  2 +-
 .../brooklyn/util/collections/MutableList.java  | 23 +++++++++++++++-
 .../brooklyn/util/collections/MutableMap.java   | 21 +++++++++++++++
 .../brooklyn/util/collections/MutableSet.java   | 28 +++++++++++++++++++-
 .../util/collections/MutableListTest.java       | 23 ++++++++++++++++
 11 files changed, 102 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/ab1381b4/core/src/main/java/brooklyn/entity/drivers/downloads/BasicDownloadTargets.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/entity/drivers/downloads/BasicDownloadTargets.java
b/core/src/main/java/brooklyn/entity/drivers/downloads/BasicDownloadTargets.java
index eca50ea..2b1c545 100644
--- a/core/src/main/java/brooklyn/entity/drivers/downloads/BasicDownloadTargets.java
+++ b/core/src/main/java/brooklyn/entity/drivers/downloads/BasicDownloadTargets.java
@@ -94,7 +94,7 @@ public class BasicDownloadTargets implements DownloadTargets {
     
     protected BasicDownloadTargets(Builder builder) {
         primaries = ImmutableList.copyOf(builder.primaries);
-        fallbacks = MutableList.<String>builder().addAll(builder.fallbacks).removeAll(builder.primaries).build().toImmutable();
+        fallbacks = MutableList.<String>builder().addAll(builder.fallbacks).removeAll(builder.primaries).build().asUnmodifiable();
         canContinueResolving = builder.canContinueResolving;
     }
 

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/ab1381b4/core/src/main/java/brooklyn/event/basic/ListConfigKey.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/event/basic/ListConfigKey.java b/core/src/main/java/brooklyn/event/basic/ListConfigKey.java
index b56d9e9..0ad4d54 100644
--- a/core/src/main/java/brooklyn/event/basic/ListConfigKey.java
+++ b/core/src/main/java/brooklyn/event/basic/ListConfigKey.java
@@ -76,7 +76,7 @@ public class ListConfigKey<V> extends AbstractCollectionConfigKey<List<?
extends
     protected List<Object> merge(boolean unmodifiable, Iterable<?>... sets) {
         MutableList<Object> result = MutableList.of();
         for (Iterable<?> set: sets) result.addAll(set);
-        if (unmodifiable) return result.toImmutable();
+        if (unmodifiable) return result.asUnmodifiable();
         return result;
     }
 

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/ab1381b4/core/src/main/java/brooklyn/event/basic/SetConfigKey.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/event/basic/SetConfigKey.java b/core/src/main/java/brooklyn/event/basic/SetConfigKey.java
index 2f04609..fcd4afe 100644
--- a/core/src/main/java/brooklyn/event/basic/SetConfigKey.java
+++ b/core/src/main/java/brooklyn/event/basic/SetConfigKey.java
@@ -67,7 +67,7 @@ public class SetConfigKey<V> extends AbstractCollectionConfigKey<Set<?
extends V
     protected Set<Object> merge(boolean unmodifiable, Iterable<?>... sets) {
         MutableSet<Object> result = MutableSet.of();
         for (Iterable<?> set: sets) result.addAll(set);
-        if (unmodifiable) return result.toImmutable();
+        if (unmodifiable) return result.asUnmodifiable();
         return result;
     }
     

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/ab1381b4/core/src/test/java/brooklyn/entity/basic/EntityTypeTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/brooklyn/entity/basic/EntityTypeTest.java b/core/src/test/java/brooklyn/entity/basic/EntityTypeTest.java
index 5c54e22..6dd5693 100644
--- a/core/src/test/java/brooklyn/entity/basic/EntityTypeTest.java
+++ b/core/src/test/java/brooklyn/entity/basic/EntityTypeTest.java
@@ -210,7 +210,7 @@ public class EntityTypeTest extends BrooklynAppUnitTestSupport {
     public void testRemoveSensor() throws Exception {
         entity.getMutableEntityType().removeSensor(SENSOR_ADDED);
         assertEquals(entity.getEntityType().getSensors(), 
-                MutableSet.builder().addAll(DEFAULT_SENSORS).remove(SENSOR_ADDED).build().toImmutable());
+                MutableSet.builder().addAll(DEFAULT_SENSORS).remove(SENSOR_ADDED).build().asUnmodifiable());
         
         TestUtils.assertEventually(
                 Suppliers.ofInstance(listener.events), 
@@ -222,7 +222,7 @@ public class EntityTypeTest extends BrooklynAppUnitTestSupport {
         entity.getMutableEntityType().removeSensor(SENSOR_ADDED.getName());
         entity.getMutableEntityType().removeSensor(POLICY_ADDED.getName());
         assertEquals(entity.getEntityType().getSensors(), 
-                MutableSet.builder().addAll(DEFAULT_SENSORS).remove(SENSOR_ADDED).remove(POLICY_ADDED).build().toImmutable());
+                MutableSet.builder().addAll(DEFAULT_SENSORS).remove(SENSOR_ADDED).remove(POLICY_ADDED).build().asUnmodifiable());
         
         TestUtils.assertEventually(
                 CollectionFunctionals.sizeSupplier(listener.events), 

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/ab1381b4/locations/jclouds/src/main/java/brooklyn/location/jclouds/JcloudsUtil.java
----------------------------------------------------------------------
diff --git a/locations/jclouds/src/main/java/brooklyn/location/jclouds/JcloudsUtil.java b/locations/jclouds/src/main/java/brooklyn/location/jclouds/JcloudsUtil.java
index 9cb4830..e71f727 100644
--- a/locations/jclouds/src/main/java/brooklyn/location/jclouds/JcloudsUtil.java
+++ b/locations/jclouds/src/main/java/brooklyn/location/jclouds/JcloudsUtil.java
@@ -273,7 +273,7 @@ public class JcloudsUtil implements JcloudsLocationConfig {
                 .put("credential", credential)
                 .putIfNotNull("endpoint", endpoint)
                 .build()
-                .toImmutable();
+                .asUnmodifiable();
 
         if (allowReuse) {
             ComputeService result = cachedComputeServices.get(cacheKey);

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/ab1381b4/software/base/src/main/java/brooklyn/entity/chef/KnifeTaskFactory.java
----------------------------------------------------------------------
diff --git a/software/base/src/main/java/brooklyn/entity/chef/KnifeTaskFactory.java b/software/base/src/main/java/brooklyn/entity/chef/KnifeTaskFactory.java
index 243ff8d..8a0ffed 100644
--- a/software/base/src/main/java/brooklyn/entity/chef/KnifeTaskFactory.java
+++ b/software/base/src/main/java/brooklyn/entity/chef/KnifeTaskFactory.java
@@ -72,7 +72,7 @@ public class KnifeTaskFactory<RET> extends SystemProcessTaskFactory<KnifeTaskFac
         MutableList<Function<ProcessTaskWrapper<?>, Void>> result = MutableList.copyOf(super.getCompletionListeners());
         if (throwOnCommonKnifeErrors != Boolean.FALSE)
             insertKnifeCompletionListenerIntoCompletionListenersList(result);
-        return result.toImmutable();
+        return result.asUnmodifiable();
     }
     
     public KnifeTaskFactory<RET> notThrowingOnCommonKnifeErrors() {
@@ -127,7 +127,7 @@ public class KnifeTaskFactory<RET> extends SystemProcessTaskFactory<KnifeTaskFac
         }
         if (numKnifes==0)
             result.add(buildKnifeCommand(numKnifes++));
-        return result.toImmutable();
+        return result.asUnmodifiable();
     }
     
     /** creates the command for running knife.

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/ab1381b4/usage/rest-server/src/main/java/brooklyn/rest/transform/LocationTransformer.java
----------------------------------------------------------------------
diff --git a/usage/rest-server/src/main/java/brooklyn/rest/transform/LocationTransformer.java
b/usage/rest-server/src/main/java/brooklyn/rest/transform/LocationTransformer.java
index d9b5ff5..79549f2 100644
--- a/usage/rest-server/src/main/java/brooklyn/rest/transform/LocationTransformer.java
+++ b/usage/rest-server/src/main/java/brooklyn/rest/transform/LocationTransformer.java
@@ -179,7 +179,7 @@ public class LocationTransformer {
             MutableMap.of("self", URI.create("/v1/locations/" + l.getId()))
                 .addIfNotNull("parent", l.getParent()!=null ? URI.create("/v1/locations/"+l.getParent().getId())
: null)
                 .addIfNotNull("spec", specId!=null ? URI.create("/v1/locations/"+specId)
: null)
-                .toImmutable() );
+                .asUnmodifiable() );
     }
     
 }

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/ab1381b4/utils/common/src/main/java/brooklyn/util/collections/MutableList.java
----------------------------------------------------------------------
diff --git a/utils/common/src/main/java/brooklyn/util/collections/MutableList.java b/utils/common/src/main/java/brooklyn/util/collections/MutableList.java
index 2c00243..a6638da 100644
--- a/utils/common/src/main/java/brooklyn/util/collections/MutableList.java
+++ b/utils/common/src/main/java/brooklyn/util/collections/MutableList.java
@@ -20,16 +20,22 @@ package brooklyn.util.collections;
 
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.Collections;
 import java.util.Iterator;
 import java.util.List;
 
 import javax.annotation.Nullable;
 
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
 import com.google.common.collect.ImmutableList;
 
 public class MutableList<V> extends ArrayList<V> {
     private static final long serialVersionUID = -5533940507175152491L;
 
+    private static final Logger log = LoggerFactory.getLogger(MutableList.class);
+    
     public static <V> MutableList<V> of() {
         return new MutableList<V>();
     }
@@ -74,10 +80,25 @@ public class MutableList<V> extends ArrayList<V> {
         }
     }
     
+    /** @deprecated since 0.7.0, use {@link #asImmutableCopy()}, or {@link #asUnmodifiable()}
/ {@link #asUnmodifiableCopy()} */ @Deprecated
     public ImmutableList<V> toImmutable() {
         return ImmutableList.copyOf(this);
     }
-    
+    public List<V> asImmutableCopy() {
+        try {
+            return ImmutableList.copyOf(this);
+        } catch (Exception e) {
+            log.warn("Error converting list to Immutable, using unmodifiable instead: "+e,
e);
+            return asUnmodifiableCopy();
+        }
+    }
+    public List<V> asUnmodifiable() {
+        return Collections.unmodifiableList(this);
+    }
+    public List<V> asUnmodifiableCopy() {
+        return Collections.unmodifiableList(MutableList.copyOf(this));
+    }
+
     public static <V> Builder<V> builder() {
         return new Builder<V>();
     }

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/ab1381b4/utils/common/src/main/java/brooklyn/util/collections/MutableMap.java
----------------------------------------------------------------------
diff --git a/utils/common/src/main/java/brooklyn/util/collections/MutableMap.java b/utils/common/src/main/java/brooklyn/util/collections/MutableMap.java
index 2d21d67..2630dbf 100644
--- a/utils/common/src/main/java/brooklyn/util/collections/MutableMap.java
+++ b/utils/common/src/main/java/brooklyn/util/collections/MutableMap.java
@@ -18,6 +18,7 @@
  */
 package brooklyn.util.collections;
 
+import java.util.Collections;
 import java.util.Iterator;
 import java.util.LinkedHashMap;
 import java.util.Map;
@@ -25,6 +26,9 @@ import java.util.Map.Entry;
 
 import javax.annotation.Nullable;
 
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
 import brooklyn.util.guava.Maybe;
 
 import com.google.common.base.Predicate;
@@ -33,7 +37,9 @@ import com.google.common.collect.ImmutableMap;
 /** Map impl, exposing simple builder operations (add) in a fluent-style API,
  * where the final map is mutable.  You can also toImmutable. */
 public class MutableMap<K,V> extends LinkedHashMap<K,V> {
+    
     private static final long serialVersionUID = -2463168443382874384L;
+    private static final Logger log = LoggerFactory.getLogger(MutableMap.class);
 
     public static <K,V> MutableMap<K,V> of() {
         return new MutableMap<K,V>();
@@ -136,9 +142,24 @@ public class MutableMap<K,V> extends LinkedHashMap<K,V> {
         return Maybe.absent("No entry for key '"+key+"' in this map");
     }
     
+    /** @deprecated since 0.7.0, use {@link #asImmutableCopy()}, or {@link #asUnmodifiable()}
/ {@link #asUnmodifiableCopy()} */ @Deprecated
     public ImmutableMap<K,V> toImmutable() {
         return ImmutableMap.copyOf(this);
     }
+    public Map<K,V> asImmutableCopy() {
+        try {
+            return ImmutableMap.copyOf(this);
+        } catch (Exception e) {
+            log.warn("Error converting list to Immutable, using unmodifiable instead: "+e,
e);
+            return asUnmodifiableCopy();
+        }
+    }
+    public Map<K,V> asUnmodifiable() {
+        return Collections.unmodifiableMap(this);
+    }
+    public Map<K,V> asUnmodifiableCopy() {
+        return Collections.unmodifiableMap(MutableMap.copyOf(this));
+    }
     
     public static <K, V> Builder<K, V> builder() {
         return new Builder<K,V>();

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/ab1381b4/utils/common/src/main/java/brooklyn/util/collections/MutableSet.java
----------------------------------------------------------------------
diff --git a/utils/common/src/main/java/brooklyn/util/collections/MutableSet.java b/utils/common/src/main/java/brooklyn/util/collections/MutableSet.java
index cee4176..0653cfd 100644
--- a/utils/common/src/main/java/brooklyn/util/collections/MutableSet.java
+++ b/utils/common/src/main/java/brooklyn/util/collections/MutableSet.java
@@ -26,11 +26,16 @@ import java.util.Set;
 
 import javax.annotation.Nullable;
 
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
 import com.google.common.collect.ImmutableSet;
 import com.google.common.collect.Sets;
 
 public class MutableSet<V> extends LinkedHashSet<V> {
+    
     private static final long serialVersionUID = 2330133488446834595L;
+    private static final Logger log = LoggerFactory.getLogger(MutableSet.class);
 
     public static <V> MutableSet<V> of() {
         return new MutableSet<V>();
@@ -69,10 +74,25 @@ public class MutableSet<V> extends LinkedHashSet<V> {
         super((source instanceof Collection) ? (Collection<? extends V>)source : Sets.newLinkedHashSet(source));
     }
     
+    /** @deprecated since 0.7.0, use {@link #asImmutableCopy()}, or {@link #asUnmodifiable()}
/ {@link #asUnmodifiableCopy()} */ @Deprecated
     public Set<V> toImmutable() {
-    	// Don't use ImmutableSet as that does not accept nulls
+        // Don't use ImmutableSet as that does not accept nulls
         return Collections.unmodifiableSet(Sets.newLinkedHashSet(this));
     }
+    public Set<V> asImmutableCopy() {
+        try {
+            return ImmutableSet.copyOf(this);
+        } catch (Exception e) {
+            log.warn("Error converting list to Immutable, using unmodifiable instead: "+e,
e);
+            return asUnmodifiableCopy();
+        }
+    }
+    public Set<V> asUnmodifiable() {
+        return Collections.unmodifiableSet(this);
+    }
+    public Set<V> asUnmodifiableCopy() {
+        return Collections.unmodifiableSet(MutableSet.copyOf(this));
+    }
     
     public static <V> Builder<V> builder() {
         return new Builder<V>();
@@ -103,6 +123,12 @@ public class MutableSet<V> extends LinkedHashSet<V> {
             return this;
         }
         
+        public Builder<V> addAll(V[] values) {
+            for (V v : values) {
+                result.add(v);
+            }
+            return this;
+        }
         public Builder<V> addAll(Iterable<? extends V> iterable) {
             if (iterable instanceof Collection) {
                 result.addAll((Collection<? extends V>) iterable);

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/ab1381b4/utils/common/src/test/java/brooklyn/util/collections/MutableListTest.java
----------------------------------------------------------------------
diff --git a/utils/common/src/test/java/brooklyn/util/collections/MutableListTest.java b/utils/common/src/test/java/brooklyn/util/collections/MutableListTest.java
index 089cfe4..e7dc5bf 100644
--- a/utils/common/src/test/java/brooklyn/util/collections/MutableListTest.java
+++ b/utils/common/src/test/java/brooklyn/util/collections/MutableListTest.java
@@ -53,4 +53,27 @@ public class MutableListTest {
         Assert.assertEquals(b, a);
     }
 
+    public void testContainingNullAndUnmodifiable() {
+        MutableList<Object> x = MutableList.<Object>of("x", null);
+        Assert.assertTrue(x.contains(null));
+        
+        List<Object> x1 = x.asUnmodifiable();
+        List<Object> x2 = x.asUnmodifiableCopy();
+        List<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"));
+    }
+    
 }


Mime
View raw message