JAMES-2255 JMAP supports Number model which present for all number data from JMAP layer
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/d6760a7d Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/d6760a7d Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/d6760a7d Branch: refs/heads/master Commit: d6760a7d26acaa5c31fc312f7e7e7c8f77ab4463 Parents: ff18af4 Author: quynhn <[email protected]> Authored: Tue Dec 12 13:49:44 2017 +0700 Committer: benwa <[email protected]> Committed: Fri Jan 5 16:10:35 2018 +0700 ---------------------------------------------------------------------- .../org/apache/james/jmap/model/Number.java | 117 +++++++++++++++++++ .../org/apache/james/jmap/model/NumberTest.java | 93 +++++++++++++++ 2 files changed, 210 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/d6760a7d/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/Number.java ---------------------------------------------------------------------- diff --git a/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/Number.java b/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/Number.java new file mode 100644 index 0000000..378eee7 --- /dev/null +++ b/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/Number.java @@ -0,0 +1,117 @@ +/**************************************************************** + * 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.jmap.model; + +import java.util.Objects; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonValue; +import com.google.common.base.MoreObjects; +import com.google.common.base.Preconditions; +import com.google.common.math.LongMath; +import com.google.common.primitives.Ints; + +public class Number { + + public static final Logger LOGGER = LoggerFactory.getLogger(Number.class); + + public static Number fromInt(int value) { + Preconditions.checkState(value >= ZERO_VALUE, + "value should be positive and less than 2^31 or empty"); + return new Number(value); + } + + public static Number fromLong(long value) { + Preconditions.checkState(value >= ZERO_VALUE && value <= MAX_VALUE, + "value should be positive and less than 2^53 or empty"); + return new Number(value); + } + + public static Number fromOutboundInt(int value) { + if (value < ZERO_VALUE) { + LOGGER.warn("Received a negative Number"); + return new Number(ZERO_VALUE); + } + return new Number(value); + } + + public static Number fromOutboundLong(long value) { + if (value < ZERO_VALUE) { + LOGGER.warn("Received a negative Number"); + return new Number(ZERO_VALUE); + } + if (value > MAX_VALUE) { + LOGGER.warn("Received a too big Number"); + return new Number(MAX_VALUE); + } + return new Number(value); + } + + private static final int ZERO_VALUE = 0; + public static final long MAX_VALUE = LongMath.pow(2, 53); + public static final Number ZERO = new Number(ZERO_VALUE); + + private final long value; + + private Number(long value) { + this.value = value; + } + + public Number ensureLessThan(long maxValue) { + Preconditions.checkState(value >= ZERO_VALUE && value <= maxValue, + "value should be positive and less than " + maxValue + " or empty"); + return new Number(value); + } + + @JsonValue + public long asLong() { + return value; + } + + @JsonIgnore + public int asInt() { + return Ints.checkedCast(value); + } + + @Override + public final boolean equals(Object o) { + if (o instanceof Number) { + Number number = (Number) o; + + return Objects.equals(this.value, number.value); + } + return false; + } + + @Override + public final int hashCode() { + return Objects.hash(value); + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(this) + .add("value", value) + .toString(); + } +} http://git-wip-us.apache.org/repos/asf/james-project/blob/d6760a7d/server/protocols/jmap/src/test/java/org/apache/james/jmap/model/NumberTest.java ---------------------------------------------------------------------- diff --git a/server/protocols/jmap/src/test/java/org/apache/james/jmap/model/NumberTest.java b/server/protocols/jmap/src/test/java/org/apache/james/jmap/model/NumberTest.java new file mode 100644 index 0000000..c76b993 --- /dev/null +++ b/server/protocols/jmap/src/test/java/org/apache/james/jmap/model/NumberTest.java @@ -0,0 +1,93 @@ +/**************************************************************** + * 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.jmap.model; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import org.junit.Test; + +public class NumberTest { + @Test + public void fromIntShouldReturnMinValueWhenNegativeValueWithLenient() throws Exception { + assertThat(Number.fromOutboundLong(-1)) + .isEqualTo(Number.ZERO); + } + + @Test + public void fromLongShouldReturnMinValueWhenNegativeValueWithLenient() throws Exception { + assertThat(Number.fromOutboundLong(-1)) + .isEqualTo(Number.ZERO); + } + + @Test + public void fromIntShouldThrowWhenNegativeValueWithStrict() throws Exception { + assertThatThrownBy(() -> + Number.fromInt(-1)) + .isInstanceOf(IllegalStateException.class); + } + + @Test + public void fromIntShouldReturnIntegerValue() throws Exception { + assertThat(Number.fromInt(1).asLong()) + .isEqualTo(1); + } + + @Test + public void fromLongShouldThrowWhenNegativeValue() throws Exception { + assertThatThrownBy(() -> + Number.fromLong(-1)) + .isInstanceOf(IllegalStateException.class); + } + + @Test + public void fromLongShouldThrowWhenOver2Pow53Value() throws Exception { + assertThatThrownBy(() -> + Number.fromLong(Number.MAX_VALUE + 1)) + .isInstanceOf(IllegalStateException.class); + } + + @Test + public void fromLongShouldReturnLongValue() throws Exception { + assertThat(Number.fromLong(1).asLong()) + .isEqualTo(1); + } + + @Test + public void ensureLessThanShouldThrowWhenOverSpecifiedValue() throws Exception { + assertThatThrownBy(() -> + Number.fromInt(11).ensureLessThan(10)) + .isInstanceOf(IllegalStateException.class); + } + + @Test + public void ensureLessThanShouldReturnNumberWhenEqualValue() throws Exception { + Number number = Number.fromInt(10); + assertThat(number.ensureLessThan(10)) + .isEqualTo(number); + } + + @Test + public void ensureLessThanShouldReturnNumberWhenLessThanMaxValue() throws Exception { + Number number = Number.fromInt(10); + assertThat(number.ensureLessThan(11)) + .isEqualTo(number); + } +} \ No newline at end of file --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
