JAMES-2082 Add a limit object
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/3eca9472 Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/3eca9472 Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/3eca9472 Branch: refs/heads/master Commit: 3eca9472103df9bb35f12927f0361f0da8df0ce1 Parents: 9c7968b Author: benwa <[email protected]> Authored: Thu Jul 6 16:54:06 2017 +0700 Committer: Antoine Duprat <[email protected]> Committed: Mon Jul 10 14:23:55 2017 +0200 ---------------------------------------------------------------------- .../apache/james/mailbox/cassandra/Limit.java | 76 +++++++++++++ .../james/mailbox/cassandra/LimitTest.java | 114 +++++++++++++++++++ 2 files changed, 190 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/3eca9472/mailbox/cassandra/src/main/java/org/apache/james/mailbox/cassandra/Limit.java ---------------------------------------------------------------------- diff --git a/mailbox/cassandra/src/main/java/org/apache/james/mailbox/cassandra/Limit.java b/mailbox/cassandra/src/main/java/org/apache/james/mailbox/cassandra/Limit.java new file mode 100644 index 0000000..0e982ce --- /dev/null +++ b/mailbox/cassandra/src/main/java/org/apache/james/mailbox/cassandra/Limit.java @@ -0,0 +1,76 @@ +/**************************************************************** + * 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.james.mailbox.cassandra; + +import com.google.common.base.Preconditions; + +import java.util.Objects; +import java.util.Optional; +import java.util.stream.Stream; + +public class Limit { + + public static Limit from(int limit) { + if (limit > 0) { + return new Limit(Optional.of(limit)); + } else { + return unlimited(); + } + } + + public static Limit unlimited() { + return new Limit(Optional.empty()); + } + + public static Limit limit(int limit) { + Preconditions.checkArgument(limit > 0, "limit should be positive"); + return new Limit(Optional.of(limit)); + } + + private final Optional<Integer> limit; + + private Limit(Optional<Integer> limit) { + this.limit = limit; + } + + public Optional<Integer> getLimit() { + return limit; + } + + public <T> Stream<T> applyOnStream(Stream<T> stream) { + return limit + .map(stream::limit) + .orElse(stream); + } + + @Override + public final boolean equals(Object o) { + if (o instanceof Limit) { + Limit other = (Limit) o; + return Objects.equals(limit, other.limit); + } + return false; + } + + @Override + public final int hashCode() { + return Objects.hash(limit); + } +} http://git-wip-us.apache.org/repos/asf/james-project/blob/3eca9472/mailbox/cassandra/src/test/java/org/apache/james/mailbox/cassandra/LimitTest.java ---------------------------------------------------------------------- diff --git a/mailbox/cassandra/src/test/java/org/apache/james/mailbox/cassandra/LimitTest.java b/mailbox/cassandra/src/test/java/org/apache/james/mailbox/cassandra/LimitTest.java new file mode 100644 index 0000000..adaf039 --- /dev/null +++ b/mailbox/cassandra/src/test/java/org/apache/james/mailbox/cassandra/LimitTest.java @@ -0,0 +1,114 @@ +/**************************************************************** + * 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.james.mailbox.cassandra; + +import com.github.steveash.guavate.Guavate; +import com.google.common.collect.ImmutableList; +import nl.jqno.equalsverifier.EqualsVerifier; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + + +import java.util.List; +import java.util.Optional; + +import static org.assertj.core.api.Assertions.assertThat; + +public class LimitTest { + + private final List<Integer> aList = ImmutableList.of(1, 2, 3, 4, 5, 6); + + @Rule + public final ExpectedException expectedException = ExpectedException.none(); + + @Test + public void unlimitedShouldCreateLimitWithNoLimit() { + Limit testee = Limit.unlimited(); + assertThat(testee.getLimit()).isEqualTo(Optional.empty()); + } + + @Test + public void beanShouldRespectBeanContract() { + EqualsVerifier.forClass(Limit.class) + .verify(); + } + + @Test + public void unlimitedShouldCreateLimitThatDoesNotAffectStream() { + + Limit testee = Limit.unlimited(); + assertThat( + testee + .applyOnStream(aList.stream()) + .collect(Guavate.toImmutableList()) + ).isEqualTo(aList); + } + + @Test + public void limitShouldCreateLimitWithNoLimit() { + int expected = 3; + + Limit testee = Limit.limit(expected); + assertThat(testee.getLimit()) + .isEqualTo(Optional.of(expected)); + } + + @Test + public void limitShouldCreateLimitThatCorrectlyTruncateStream() { + Limit testee = Limit.limit(3); + + assertThat(testee + .applyOnStream(aList.stream()) + .collect(Guavate.toImmutableList()) + ).isEqualTo(ImmutableList.of(1, 2, 3)); + } + + @Test + public void limitShouldThrowAnErrorWhenCalledWithZero() { + expectedException.expect(IllegalArgumentException.class); + Limit.limit(0); + } + + + @Test + public void limitShouldThrowAnErrorWhenCalledWithNegativeValue() { + expectedException.expect(IllegalArgumentException.class); + Limit.limit(-1); + } + + @Test + public void ofShouldTakePositiveValueAsLimit() { + assertThat(Limit.from(3)) + .isEqualTo(Limit.limit(3)); + } + + @Test + public void ofShouldTakeNegativeValueAsUnlimited() { + assertThat(Limit.from(-1)) + .isEqualTo(Limit.unlimited()); + } + + @Test + public void ofShouldTakeZeroValueAsUnlimited() { + assertThat(Limit.from(0)) + .isEqualTo(Limit.unlimited()); + } +} \ No newline at end of file --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
