brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From grk...@apache.org
Subject [1/2] git commit: Added presentInstances method to Maybe
Date Wed, 01 Oct 2014 13:57:10 GMT
Repository: incubator-brooklyn
Updated Branches:
  refs/heads/master 6fa0d22ce -> ff3870777


Added presentInstances method to Maybe


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

Branch: refs/heads/master
Commit: 2b376156cd541edbc9946fdf37b08cb0b9386db5
Parents: bbe2940
Author: Andrew Kennedy <grkvlt@apache.org>
Authored: Wed Oct 1 14:54:31 2014 +0100
Committer: Andrew Kennedy <grkvlt@apache.org>
Committed: Wed Oct 1 14:55:34 2014 +0100

----------------------------------------------------------------------
 .../main/java/brooklyn/util/guava/Maybe.java    | 33 ++++++++++++++++++++
 1 file changed, 33 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/2b376156/utils/common/src/main/java/brooklyn/util/guava/Maybe.java
----------------------------------------------------------------------
diff --git a/utils/common/src/main/java/brooklyn/util/guava/Maybe.java b/utils/common/src/main/java/brooklyn/util/guava/Maybe.java
index 07da835..41d7a06 100644
--- a/utils/common/src/main/java/brooklyn/util/guava/Maybe.java
+++ b/utils/common/src/main/java/brooklyn/util/guava/Maybe.java
@@ -18,6 +18,8 @@
  */
 package brooklyn.util.guava;
 
+import static com.google.common.base.Preconditions.checkNotNull;
+
 import java.io.Serializable;
 import java.lang.ref.SoftReference;
 import java.util.Collections;
@@ -29,10 +31,12 @@ import javax.annotation.Nullable;
 
 import brooklyn.util.javalang.JavaClassNames;
 
+import com.google.common.annotations.Beta;
 import com.google.common.base.Function;
 import com.google.common.base.Optional;
 import com.google.common.base.Preconditions;
 import com.google.common.base.Supplier;
+import com.google.common.collect.AbstractIterator;
 import com.google.common.collect.ImmutableSet;
 
 /** Like Guava Optional but permitting null and permitting errors to be thrown. */
@@ -160,6 +164,35 @@ public abstract class Maybe<T> implements Serializable, Supplier<T>
{
         };
         return absent();
     }
+
+    /**
+     * Returns the value of each present instance from the supplied {@code maybes}, in order,
+     * skipping over occurrences of {@link Maybe#absent()}. Iterators are unmodifiable and
are
+     * evaluated lazily.
+     *
+     * @see Optional#presentInstances(Iterable)
+     */
+    @Beta
+    public static <T> Iterable<T> presentInstances(final Iterable<? extends
Maybe<? extends T>> maybes) {
+        checkNotNull(maybes);
+        return new Iterable<T>() {
+            @Override
+            public Iterator<T> iterator() {
+                return new AbstractIterator<T>() {
+                    private final Iterator<? extends Maybe<? extends T>> iterator
= checkNotNull(maybes.iterator());
+
+                    @Override
+                    protected T computeNext() {
+                        while (iterator.hasNext()) {
+                            Maybe<? extends T> maybe = iterator.next();
+                            if (maybe.isPresent()) { return maybe.get(); }
+                        }
+                        return endOfData();
+                    }
+                };
+            }
+        };
+    }
     
     public static class Absent<T> extends Maybe<T> {
         private static final long serialVersionUID = -757170462010887057L;


Mime
View raw message