Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 75AA6200BDA for ; Mon, 28 Nov 2016 17:07:48 +0100 (CET) Received: by cust-asf.ponee.io (Postfix) id 73F37160B0D; Mon, 28 Nov 2016 16:07:48 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 72A39160B2D for ; Mon, 28 Nov 2016 17:07:47 +0100 (CET) Received: (qmail 11454 invoked by uid 500); 28 Nov 2016 16:07:46 -0000 Mailing-List: contact commits-help@zest.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@zest.apache.org Delivered-To: mailing list commits@zest.apache.org Received: (qmail 11238 invoked by uid 99); 28 Nov 2016 16:07:46 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 28 Nov 2016 16:07:46 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 611A5F17F6; Mon, 28 Nov 2016 16:07:46 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: paulmerlin@apache.org To: commits@zest.apache.org Date: Mon, 28 Nov 2016 16:07:57 -0000 Message-Id: In-Reply-To: References: X-Mailer: ASF-Git Admin Mailer Subject: [12/14] zest-java git commit: introduce single() Collector, preferred over Iterables.single() archived-at: Mon, 28 Nov 2016 16:07:48 -0000 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 Authored: Mon Nov 28 12:52:32 2016 +0100 Committer: Paul Merlin 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 Element type + * @return The single element + * @throws IllegalArgumentException if no or more than one element + */ + public static Collector single() + throws IllegalArgumentException + { + Supplier 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 Element type + * @return An optional single element, empty if no or more than one element + */ + public static Collector> 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 toRel()