You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@polygene.apache.org by pa...@apache.org on 2016/11/28 16:07:57 UTC

[12/14] zest-java git commit: introduce single() Collector, preferred over Iterables.single()

introduce single() Collector, preferred over Iterables.single()


Project: http://git-wip-us.apache.org/repos/asf/zest-java/repo
Commit: http://git-wip-us.apache.org/repos/asf/zest-java/commit/d7f000c9
Tree: http://git-wip-us.apache.org/repos/asf/zest-java/tree/d7f000c9
Diff: http://git-wip-us.apache.org/repos/asf/zest-java/diff/d7f000c9

Branch: refs/heads/develop
Commit: d7f000c987431cee7da87b8724e06ff96d5f864e
Parents: fdf596b
Author: Paul Merlin <pa...@apache.org>
Authored: Mon Nov 28 12:52:32 2016 +0100
Committer: Paul Merlin <pa...@apache.org>
Committed: Mon Nov 28 12:52:32 2016 +0100

----------------------------------------------------------------------
 .../org/apache/zest/api/util/Collectors.java    | 54 +++++++++++++++++
 .../apache/zest/api/util/CollectorsTest.java    | 64 ++++++++++++++++++++
 .../zest/test/cache/MemoryCachePoolMixin.java   |  4 +-
 .../zest/library/rest/common/Resource.java      |  9 +--
 .../library/rest/common/link/LinksUtil.java     |  9 +--
 5 files changed, 130 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/zest-java/blob/d7f000c9/core/api/src/main/java/org/apache/zest/api/util/Collectors.java
----------------------------------------------------------------------
diff --git a/core/api/src/main/java/org/apache/zest/api/util/Collectors.java b/core/api/src/main/java/org/apache/zest/api/util/Collectors.java
new file mode 100644
index 0000000..c977059
--- /dev/null
+++ b/core/api/src/main/java/org/apache/zest/api/util/Collectors.java
@@ -0,0 +1,54 @@
+/*
+ *  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.zest.api.util;
+
+import java.util.Optional;
+import java.util.function.Supplier;
+import java.util.stream.Collector;
+
+public class Collectors
+{
+    /**
+     * Collect a single element.
+     * @param <T> Element type
+     * @return The single element
+     * @throws IllegalArgumentException if no or more than one element
+     */
+    public static <T> Collector<T, ?, T> single()
+        throws IllegalArgumentException
+    {
+        Supplier<T> thrower = () ->
+        {
+            throw new IllegalArgumentException( "No or more than one element in stream" );
+        };
+        return java.util.stream.Collectors.collectingAndThen( singleOrEmpty(),
+                                                              optional -> optional.orElseGet( thrower ) );
+    }
+
+    /**
+     * Collect an optional single element.
+     * @param <T> Element type
+     * @return An optional single element, empty if no or more than one element
+     */
+    public static <T> Collector<T, ?, Optional<T>> singleOrEmpty()
+    {
+        return java.util.stream.Collectors.reducing( ( a, b ) -> null );
+    }
+
+    private Collectors() {}
+}

http://git-wip-us.apache.org/repos/asf/zest-java/blob/d7f000c9/core/api/src/test/java/org/apache/zest/api/util/CollectorsTest.java
----------------------------------------------------------------------
diff --git a/core/api/src/test/java/org/apache/zest/api/util/CollectorsTest.java b/core/api/src/test/java/org/apache/zest/api/util/CollectorsTest.java
new file mode 100644
index 0000000..6aeb871
--- /dev/null
+++ b/core/api/src/test/java/org/apache/zest/api/util/CollectorsTest.java
@@ -0,0 +1,64 @@
+/*
+ *  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.zest.api.util;
+
+import java.util.Optional;
+import java.util.stream.Stream;
+import org.junit.Test;
+
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.fail;
+
+public class CollectorsTest
+{
+    @Test
+    public void singleOrEmpty()
+    {
+        assertEquals( Optional.empty(), Stream.of().collect( Collectors.singleOrEmpty() ) );
+        assertEquals( Optional.of( 1 ), Stream.of( 1 ).collect( Collectors.singleOrEmpty() ) );
+        assertEquals( Optional.empty(), Stream.of( 1, 1 ).collect( Collectors.singleOrEmpty() ) );
+        assertEquals( Optional.empty(), Stream.of( 1, 1, 1 ).collect( Collectors.singleOrEmpty() ) );
+    }
+
+    @Test
+    public void single()
+    {
+        assertThat( Stream.of( 1L ).collect( Collectors.single() ), is( 1L ) );
+
+        try
+        {
+            Stream.of().collect( Collectors.single() );
+            fail( "Should have failed" );
+        }
+        catch( IllegalArgumentException ex ) {}
+        try
+        {
+            Stream.of( 1, 1 ).collect( Collectors.single() );
+            fail( "Should have failed" );
+        }
+        catch( IllegalArgumentException ex ) {}
+        try
+        {
+            Stream.of( 1, 1, 1 ).collect( Collectors.single() );
+            fail( "Should have failed" );
+        }
+        catch( IllegalArgumentException ex ) {}
+    }
+}

http://git-wip-us.apache.org/repos/asf/zest-java/blob/d7f000c9/core/testsupport/src/main/java/org/apache/zest/test/cache/MemoryCachePoolMixin.java
----------------------------------------------------------------------
diff --git a/core/testsupport/src/main/java/org/apache/zest/test/cache/MemoryCachePoolMixin.java b/core/testsupport/src/main/java/org/apache/zest/test/cache/MemoryCachePoolMixin.java
index 46fe8b6..e462127 100644
--- a/core/testsupport/src/main/java/org/apache/zest/test/cache/MemoryCachePoolMixin.java
+++ b/core/testsupport/src/main/java/org/apache/zest/test/cache/MemoryCachePoolMixin.java
@@ -23,7 +23,7 @@ import java.util.concurrent.ConcurrentHashMap;
 import org.apache.zest.api.util.NullArgumentException;
 import org.apache.zest.spi.cache.Cache;
 
-import static org.apache.zest.functional.Iterables.single;
+import static org.apache.zest.api.util.Collectors.single;
 
 /**
  * In-Memory CachePool Mixin based on ConcurrentHashMap.
@@ -80,6 +80,6 @@ public abstract class MemoryCachePoolMixin
     @Override
     public MemoryCacheImpl<?> singleCache()
     {
-        return single( caches.values() );
+        return caches.values().stream().collect( single() );
     }
 }

http://git-wip-us.apache.org/repos/asf/zest-java/blob/d7f000c9/libraries/rest-common/src/main/java/org/apache/zest/library/rest/common/Resource.java
----------------------------------------------------------------------
diff --git a/libraries/rest-common/src/main/java/org/apache/zest/library/rest/common/Resource.java b/libraries/rest-common/src/main/java/org/apache/zest/library/rest/common/Resource.java
index 5bd62f2..8e611d0 100644
--- a/libraries/rest-common/src/main/java/org/apache/zest/library/rest/common/Resource.java
+++ b/libraries/rest-common/src/main/java/org/apache/zest/library/rest/common/Resource.java
@@ -26,10 +26,11 @@ import org.apache.zest.api.common.UseDefaults;
 import org.apache.zest.api.mixin.Mixins;
 import org.apache.zest.api.property.Property;
 import org.apache.zest.api.value.ValueComposite;
-import org.apache.zest.functional.Iterables;
 import org.apache.zest.library.rest.common.link.Link;
 import org.apache.zest.library.rest.common.link.LinksUtil;
 
+import static org.apache.zest.api.util.Collectors.single;
+
 /**
  * Value representing a whole resource in a URL path. Allows listing of available
  * queries, commands, sub-resources and an index.
@@ -62,19 +63,19 @@ public interface Resource
         @Override
         public Link query( String relation )
         {
-            return Iterables.single( Iterables.filter( LinksUtil.withRel( relation ), queries().get() ) );
+            return queries().get().stream().filter( LinksUtil.withRel( relation ) ).collect( single() );
         }
 
         @Override
         public Link command( String relation )
         {
-            return Iterables.single(Iterables.filter( LinksUtil.withRel( relation ), commands().get() ));
+            return commands().get().stream().filter( LinksUtil.withRel( relation ) ).collect( single() );
         }
 
         @Override
         public Link resource( String relation )
         {
-            return Iterables.single(Iterables.filter( LinksUtil.withRel( relation ), resources().get() ));
+            return resources().get().stream().filter( LinksUtil.withRel( relation ) ).collect( single() );
         }
     }
 }

http://git-wip-us.apache.org/repos/asf/zest-java/blob/d7f000c9/libraries/rest-common/src/main/java/org/apache/zest/library/rest/common/link/LinksUtil.java
----------------------------------------------------------------------
diff --git a/libraries/rest-common/src/main/java/org/apache/zest/library/rest/common/link/LinksUtil.java b/libraries/rest-common/src/main/java/org/apache/zest/library/rest/common/link/LinksUtil.java
index 4f592a6..cfcc08d 100644
--- a/libraries/rest-common/src/main/java/org/apache/zest/library/rest/common/link/LinksUtil.java
+++ b/libraries/rest-common/src/main/java/org/apache/zest/library/rest/common/link/LinksUtil.java
@@ -22,7 +22,8 @@ package org.apache.zest.library.rest.common.link;
 
 import java.util.function.Function;
 import java.util.function.Predicate;
-import org.apache.zest.functional.Iterables;
+
+import static org.apache.zest.api.util.Collectors.single;
 
 /**
  * Helper methods for links
@@ -52,14 +53,14 @@ public final class LinksUtil
       };
    }
 
-    public static Link withRel(String rel, Links links)
+    public static Link withRel( String rel, Links links )
     {
-        return Iterables.single( Iterables.filter( withRel( rel ), links.links().get() ) );
+        return links.links().get().stream().filter( withRel( rel ) ).collect( single() );
     }
 
     public static Link withId(String id, Links links)
     {
-        return Iterables.single( Iterables.filter( withId( id ), links.links().get() ) );
+        return links.links().get().stream().filter( withId( id ) ).collect( single() );
     }
 
    public static Function<Link, String> toRel()