brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From s...@apache.org
Subject [1/2] brooklyn-server git commit: Add Suppliers.incrementingSupplier
Date Wed, 16 Nov 2016 06:45:43 GMT
Repository: brooklyn-server
Updated Branches:
  refs/heads/master 16eab7496 -> ec6c0d0b8


Add Suppliers.incrementingSupplier


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

Branch: refs/heads/master
Commit: a14b7c31f5ea62d4690c877dc960be23cd7df4c9
Parents: a8d81ed
Author: Sam Corbett <sam.corbett@cloudsoftcorp.com>
Authored: Tue Nov 15 10:34:27 2016 +0000
Committer: Sam Corbett <sam.corbett@cloudsoftcorp.com>
Committed: Tue Nov 15 10:35:08 2016 +0000

----------------------------------------------------------------------
 .../entity/group/DynamicClusterImpl.java        |  6 +-
 .../apache/brooklyn/util/guava/Suppliers.java   | 60 ++++++++++++++++++++
 .../brooklyn/util/guava/SuppliersTest.java      | 46 +++++++++++++++
 .../brooklyn/util/pool/BasicPoolTest.java       | 17 +-----
 4 files changed, 113 insertions(+), 16 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/a14b7c31/core/src/main/java/org/apache/brooklyn/entity/group/DynamicClusterImpl.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/brooklyn/entity/group/DynamicClusterImpl.java b/core/src/main/java/org/apache/brooklyn/entity/group/DynamicClusterImpl.java
index 0271134..eedefa7 100644
--- a/core/src/main/java/org/apache/brooklyn/entity/group/DynamicClusterImpl.java
+++ b/core/src/main/java/org/apache/brooklyn/entity/group/DynamicClusterImpl.java
@@ -72,6 +72,7 @@ import org.apache.brooklyn.util.core.task.Tasks;
 import org.apache.brooklyn.util.exceptions.Exceptions;
 import org.apache.brooklyn.util.exceptions.ReferenceWithError;
 import org.apache.brooklyn.util.guava.Maybe;
+import org.apache.brooklyn.util.guava.Suppliers;
 import org.apache.brooklyn.util.javalang.JavaClassNames;
 import org.apache.brooklyn.util.javalang.Reflections;
 import org.apache.brooklyn.util.text.StringPredicates;
@@ -192,6 +193,9 @@ public class DynamicClusterImpl extends AbstractGroupImpl implements DynamicClus
         }
     }
 
+    // Unused as of Brooklyn 0.10.0 but kept to maintain persistence backwards compatibility
+    // when rebinding NEXT_CLUSTER_MEMBER_ID.
+    @Deprecated
     private static class NextClusterMemberIdSupplier implements Supplier<Integer> {
         private AtomicInteger nextId = new AtomicInteger(0);
 
@@ -214,7 +218,7 @@ public class DynamicClusterImpl extends AbstractGroupImpl implements DynamicClus
     private void initialiseMemberId() {
         synchronized (mutex) {
             if (sensors().get(NEXT_CLUSTER_MEMBER_ID) == null) {
-                sensors().set(NEXT_CLUSTER_MEMBER_ID, new NextClusterMemberIdSupplier());
+                sensors().set(NEXT_CLUSTER_MEMBER_ID, Suppliers.incrementing());
             }
         }
     }

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/a14b7c31/utils/common/src/main/java/org/apache/brooklyn/util/guava/Suppliers.java
----------------------------------------------------------------------
diff --git a/utils/common/src/main/java/org/apache/brooklyn/util/guava/Suppliers.java b/utils/common/src/main/java/org/apache/brooklyn/util/guava/Suppliers.java
new file mode 100644
index 0000000..9e135bc
--- /dev/null
+++ b/utils/common/src/main/java/org/apache/brooklyn/util/guava/Suppliers.java
@@ -0,0 +1,60 @@
+/*
+ * 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.guava;
+
+import java.util.concurrent.atomic.AtomicInteger;
+
+import com.google.common.base.Supplier;
+
+public class Suppliers {
+
+    private Suppliers() {}
+
+    /**
+     * @return A supplier that atomically increments the value returned on each call to
+     * {@link Supplier#get get}, starting from zero.
+     */
+    public static Supplier<Integer> incrementing() {
+        return incrementing(0);
+    }
+
+    /**
+     * @return A supplier that atomically increments the value returned on each call to
+     * {@link Supplier#get get}, starting from <code>initialValue</code>.
+     */
+    public static Supplier<Integer> incrementing(int initialValue) {
+        return new IncrementingSupplier(initialValue);
+    }
+
+    private static class IncrementingSupplier implements Supplier<Integer> {
+        private final AtomicInteger value;
+
+        private IncrementingSupplier(int initialValue) {
+            this.value = new AtomicInteger(initialValue);
+        }
+
+        @Override
+        public Integer get() {
+            return value.getAndIncrement();
+        }
+    }
+
+
+}

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/a14b7c31/utils/common/src/test/java/org/apache/brooklyn/util/guava/SuppliersTest.java
----------------------------------------------------------------------
diff --git a/utils/common/src/test/java/org/apache/brooklyn/util/guava/SuppliersTest.java
b/utils/common/src/test/java/org/apache/brooklyn/util/guava/SuppliersTest.java
new file mode 100644
index 0000000..09d17f5
--- /dev/null
+++ b/utils/common/src/test/java/org/apache/brooklyn/util/guava/SuppliersTest.java
@@ -0,0 +1,46 @@
+/*
+ * 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.guava;
+
+import static org.testng.Assert.assertEquals;
+
+import org.testng.annotations.Test;
+
+import com.google.common.base.Supplier;
+
+public class SuppliersTest {
+
+    @Test
+    public void testIncrementing() throws Exception {
+        Supplier<Integer> supplier = Suppliers.incrementing();
+        assertEquals(supplier.get(), Integer.valueOf(0));
+        assertEquals(supplier.get(), Integer.valueOf(1));
+        assertEquals(supplier.get(), Integer.valueOf(2));
+    }
+
+    @Test
+    public void testIncrementingFrom() throws Exception {
+        Supplier<Integer> supplier = Suppliers.incrementing(9876);
+        assertEquals(supplier.get(), Integer.valueOf(9876));
+        assertEquals(supplier.get(), Integer.valueOf(9877));
+        assertEquals(supplier.get(), Integer.valueOf(9878));
+    }
+
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/a14b7c31/utils/common/src/test/java/org/apache/brooklyn/util/pool/BasicPoolTest.java
----------------------------------------------------------------------
diff --git a/utils/common/src/test/java/org/apache/brooklyn/util/pool/BasicPoolTest.java b/utils/common/src/test/java/org/apache/brooklyn/util/pool/BasicPoolTest.java
index 16d3877..c4c1945 100644
--- a/utils/common/src/test/java/org/apache/brooklyn/util/pool/BasicPoolTest.java
+++ b/utils/common/src/test/java/org/apache/brooklyn/util/pool/BasicPoolTest.java
@@ -27,13 +27,10 @@ import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.CopyOnWriteArrayList;
 import java.util.concurrent.Executors;
-import java.util.concurrent.atomic.AtomicInteger;
 
 import javax.annotation.Nullable;
 
-import org.apache.brooklyn.util.pool.BasicPool;
-import org.apache.brooklyn.util.pool.Lease;
-import org.apache.brooklyn.util.pool.Pool;
+import org.apache.brooklyn.util.guava.Suppliers;
 import org.testng.annotations.AfterMethod;
 import org.testng.annotations.BeforeMethod;
 import org.testng.annotations.Test;
@@ -58,18 +55,8 @@ public class BasicPoolTest {
     
     @BeforeMethod(alwaysRun=true)
     public void setUp() throws Exception {
-        // Note we use final theCounter variable, rather than just a field, to guarantee

-        // that each sequential test run won't be accessing the same field value if a test
-        // doesn't tear down and keeps executing in another thread for some reason.
-        
-        final AtomicInteger theCounter = new AtomicInteger(0);
         closedVals = new CopyOnWriteArrayList<Integer>();
-        
-        supplier = new Supplier<Integer>() {
-            @Override public Integer get() {
-                return theCounter.getAndIncrement();
-            }
-        };
+        supplier = Suppliers.incrementing();
         closer = new Function<Integer,Void>() {
             @Override public Void apply(@Nullable Integer input) {
                 closedVals.add(input);


Mime
View raw message