This is an automated email from the ASF dual-hosted git repository.

btellier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git

commit 67e2b38fddb697f575b794fd719673b5a3c20145
Author: RĂ©mi KOWALSKI <rkowal...@linagora.com>
AuthorDate: Thu Nov 7 14:11:04 2019 +0100

    JAMES-2964 simplify quota usage domain object to better represent the domain
---
 .../apache/james/core/quota/QuotaCountUsage.java   | 37 ++++----------------
 .../apache/james/core/quota/QuotaSizeUsage.java    | 39 ++++------------------
 .../apache/james/core/quota/QuotaUsageValue.java   | 14 --------
 .../james/core/quota/QuotaUsageValueTest.java      | 15 ---------
 .../mailbox/model/SerializableQuotaUsageValue.java | 24 +++----------
 .../apache/james/event/json/EventSerializer.scala  |  4 +--
 .../james/event/json/dtos/QuotaCountTest.java      | 12 -------
 .../james/event/json/dtos/QuotaSizeTest.java       | 12 -------
 .../java/org/apache/james/cli/ServerCmdTest.java   |  4 +--
 9 files changed, 21 insertions(+), 140 deletions(-)

diff --git 
a/core/src/main/java/org/apache/james/core/quota/QuotaCountUsage.java 
b/core/src/main/java/org/apache/james/core/quota/QuotaCountUsage.java
index 7e67f1b..10e2818 100644
--- a/core/src/main/java/org/apache/james/core/quota/QuotaCountUsage.java
+++ b/core/src/main/java/org/apache/james/core/quota/QuotaCountUsage.java
@@ -18,63 +18,40 @@
  ****************************************************************/
 package org.apache.james.core.quota;
 
-import java.util.Optional;
-
 import com.google.common.base.MoreObjects;
 import com.google.common.base.Objects;
 
 public class QuotaCountUsage implements QuotaUsageValue<QuotaCountUsage, 
QuotaCountLimit> {
 
-    public static QuotaCountUsage unlimited() {
-        return new QuotaCountUsage(Optional.empty());
-    }
-
     public static QuotaCountUsage count(long value) {
-        return count(Optional.of(value));
-    }
-
-    public static QuotaCountUsage count(Optional<Long> value) {
         return new QuotaCountUsage(value);
     }
 
-    private final Optional<Long> value;
+    private final Long value;
 
-    private QuotaCountUsage(Optional<Long> value) {
+    private QuotaCountUsage(Long value) {
         this.value = value;
     }
 
     @Override
     public long asLong() {
-        return value.orElseThrow(IllegalStateException::new);
-    }
-
-    @Override
-    public boolean isLimited() {
-        return value.isPresent();
+        return value;
     }
 
     @Override
     public QuotaCountUsage add(long additionalValue) {
-        return new QuotaCountUsage(value.map(x -> x + additionalValue));
+        return new QuotaCountUsage(value + additionalValue);
     }
 
     @Override
     public QuotaCountUsage add(QuotaCountUsage additionalValue) {
-        if (additionalValue.isUnlimited()) {
-            return unlimited();
-        }
-        return new QuotaCountUsage(value.map(x -> x + 
additionalValue.asLong()));
-    }
-
-    @Override
-    public boolean greaterThan(QuotaCountUsage other) {
-        return value.orElse(Long.MAX_VALUE) > 
other.value.orElse(Long.MAX_VALUE);
+        return new QuotaCountUsage(value + additionalValue.asLong());
     }
 
     @Override
     public boolean exceedLimit(QuotaCountLimit limit) {
         if (limit.isLimited()) {
-            return value.orElse(Long.MAX_VALUE) > limit.asLong();
+            return value > limit.asLong();
         } else {
             return false;
         }
@@ -83,7 +60,7 @@ public class QuotaCountUsage implements 
QuotaUsageValue<QuotaCountUsage, QuotaCo
     @Override
     public String toString() {
         return MoreObjects.toStringHelper(this)
-            .add("value", value.map(String::valueOf).orElse("unlimited"))
+            .add("value", value.toString())
             .toString();
     }
 
diff --git a/core/src/main/java/org/apache/james/core/quota/QuotaSizeUsage.java 
b/core/src/main/java/org/apache/james/core/quota/QuotaSizeUsage.java
index 7bc74fb..d3ccfc5 100644
--- a/core/src/main/java/org/apache/james/core/quota/QuotaSizeUsage.java
+++ b/core/src/main/java/org/apache/james/core/quota/QuotaSizeUsage.java
@@ -18,65 +18,40 @@
  ****************************************************************/
 package org.apache.james.core.quota;
 
-import java.util.Optional;
-
 import com.google.common.base.MoreObjects;
 import com.google.common.base.Objects;
 
 public class QuotaSizeUsage implements QuotaUsageValue<QuotaSizeUsage, 
QuotaSizeLimit> {
 
-    public static final QuotaSizeUsage QUOTA_SIZE = new 
QuotaSizeUsage(Optional.empty());
-
-    public static QuotaSizeUsage unlimited() {
-        return QUOTA_SIZE;
-    }
-
     public static QuotaSizeUsage size(long value) {
-        return size(Optional.of(value));
-    }
-
-    public static QuotaSizeUsage size(Optional<Long> value) {
         return new QuotaSizeUsage(value);
     }
 
-    private final Optional<Long> value;
+    private final Long value;
 
-    private QuotaSizeUsage(Optional<Long> value) {
+    private QuotaSizeUsage(Long value) {
         this.value = value;
     }
 
     @Override
     public long asLong() {
-        return value.orElseThrow(IllegalStateException::new);
-    }
-
-    @Override
-    public boolean isLimited() {
-        return value.isPresent();
+        return value;
     }
 
     @Override
     public QuotaSizeUsage add(long additionalValue) {
-        return new QuotaSizeUsage(value.map(x -> x + additionalValue));
+        return new QuotaSizeUsage(value + additionalValue);
     }
 
     @Override
     public QuotaSizeUsage add(QuotaSizeUsage additionalValue) {
-        if (additionalValue.isUnlimited()) {
-            return unlimited();
-        }
-        return new QuotaSizeUsage(value.map(x -> x + 
additionalValue.asLong()));
-    }
-
-    @Override
-    public boolean greaterThan(QuotaSizeUsage other) {
-        return value.orElse(Long.MAX_VALUE) > 
other.value.orElse(Long.MAX_VALUE);
+        return new QuotaSizeUsage(value + additionalValue.asLong());
     }
 
     @Override
     public boolean exceedLimit(QuotaSizeLimit limit) {
         if (limit.isLimited()) {
-            return value.orElse(Long.MAX_VALUE) > limit.asLong();
+            return value > limit.asLong();
         } else {
             return false;
         }
@@ -85,7 +60,7 @@ public class QuotaSizeUsage implements 
QuotaUsageValue<QuotaSizeUsage, QuotaSize
     @Override
     public String toString() {
         return MoreObjects.toStringHelper(this)
-            .add("value", value.map(String::valueOf).orElse("unlimited"))
+            .add("value", value.toString())
             .toString();
     }
 
diff --git 
a/core/src/main/java/org/apache/james/core/quota/QuotaUsageValue.java 
b/core/src/main/java/org/apache/james/core/quota/QuotaUsageValue.java
index d96d55d..1b28868 100644
--- a/core/src/main/java/org/apache/james/core/quota/QuotaUsageValue.java
+++ b/core/src/main/java/org/apache/james/core/quota/QuotaUsageValue.java
@@ -18,27 +18,13 @@
  ****************************************************************/
 package org.apache.james.core.quota;
 
-import java.util.Optional;
-
 public interface QuotaUsageValue<T extends QuotaUsageValue<T, U>, U extends 
QuotaLimitValue<U>> {
 
-    static boolean isValidValue(Optional<Long> value) {
-        return !value.isPresent() || value.get() >= -1;
-    }
-
     long asLong();
 
-    boolean isLimited();
-
-    default boolean isUnlimited() {
-        return !isLimited();
-    }
-
     T add(long additionalValue);
 
     T add(T additionalValue);
 
-    boolean greaterThan(T other);
-
     boolean exceedLimit(U limit);
 }
diff --git 
a/core/src/test/java/org/apache/james/core/quota/QuotaUsageValueTest.java 
b/core/src/test/java/org/apache/james/core/quota/QuotaUsageValueTest.java
index 8e4ddce..4deb73b 100644
--- a/core/src/test/java/org/apache/james/core/quota/QuotaUsageValueTest.java
+++ b/core/src/test/java/org/apache/james/core/quota/QuotaUsageValueTest.java
@@ -31,21 +31,6 @@ public interface QuotaUsageValueTest<T extends 
QuotaLimitValue<T>, U extends Quo
     T unlimited();
 
     @Test
-    default void greaterThanShouldReturnFalseWhenFirstEqualToSecond() {
-        assertThat(usageInstance(1).greaterThan(usageInstance(1))).isFalse();
-    }
-
-    @Test
-    default void greaterThanShouldReturnFalseWhenFirstSmallerThanSecond() {
-        assertThat(usageInstance(1).greaterThan(usageInstance(2))).isFalse();
-    }
-
-    @Test
-    default void greaterThanShouldReturnTrueWhenFirstGreaterThanSecond() {
-        assertThat(usageInstance(2).greaterThan(usageInstance(1))).isTrue();
-    }
-
-    @Test
     default void greaterThanShouldReturnFalseWhenUsageEqualToLimit() {
         assertThat(usageInstance(1).exceedLimit(limitInstance(1))).isFalse();
     }
diff --git 
a/mailbox/api/src/main/java/org/apache/james/mailbox/model/SerializableQuotaUsageValue.java
 
b/mailbox/api/src/main/java/org/apache/james/mailbox/model/SerializableQuotaUsageValue.java
index d62bc71..eb8f444 100644
--- 
a/mailbox/api/src/main/java/org/apache/james/mailbox/model/SerializableQuotaUsageValue.java
+++ 
b/mailbox/api/src/main/java/org/apache/james/mailbox/model/SerializableQuotaUsageValue.java
@@ -22,7 +22,6 @@ package org.apache.james.mailbox.model;
 import java.io.Serializable;
 import java.util.Objects;
 import java.util.Optional;
-import java.util.function.Function;
 
 import org.apache.james.core.quota.QuotaLimitValue;
 import org.apache.james.core.quota.QuotaUsageValue;
@@ -32,16 +31,11 @@ import com.google.common.base.MoreObjects;
 public class SerializableQuotaUsageValue<T extends QuotaLimitValue<T>, U 
extends QuotaUsageValue<U, T>> implements Serializable {
 
     public static <T extends QuotaLimitValue<T>, U extends QuotaUsageValue<U, 
T>> SerializableQuotaUsageValue<T, U> valueOf(Optional<U> input) {
-        return new SerializableQuotaUsageValue<T, U>(input.orElse(null));
+        return new SerializableQuotaUsageValue<>(input.orElse(null));
     }
 
-    public static final long UNLIMITED = -1;
-
     private static <T extends QuotaLimitValue<T>, U extends QuotaUsageValue<U, 
T>> Long encodeAsLong(U quota) {
-        if (quota.isLimited()) {
-            return quota.asLong();
-        }
-        return UNLIMITED;
+        return quota.asLong();
     }
 
     private final Long value;
@@ -58,21 +52,10 @@ public class SerializableQuotaUsageValue<T extends 
QuotaLimitValue<T>, U extends
         return value;
     }
 
-    public Optional<U> toValue(Function<Long, U> factory, U unlimited) {
-        Long longValue = encodeAsLong();
-        if (longValue == null) {
-            return Optional.empty();
-        }
-        if (longValue == UNLIMITED) {
-            return Optional.of(unlimited);
-        }
-        return Optional.of(factory.apply(longValue));
-    }
-
     @Override
     public boolean equals(Object o) {
         if (o instanceof SerializableQuotaUsageValue<?, ?>) {
-            SerializableQuotaUsageValue<?, ?> that = 
(SerializableQuotaUsageValue<?,?>) o;
+            SerializableQuotaUsageValue<?, ?> that = 
(SerializableQuotaUsageValue<?, ?>) o;
             return Objects.equals(value, that.value);
         }
         return false;
@@ -89,4 +72,5 @@ public class SerializableQuotaUsageValue<T extends 
QuotaLimitValue<T>, U extends
             .add("value", value)
             .toString();
     }
+
 }
diff --git 
a/mailbox/event/json/src/main/scala/org/apache/james/event/json/EventSerializer.scala
 
b/mailbox/event/json/src/main/scala/org/apache/james/event/json/EventSerializer.scala
index be52b08..87b6e21 100644
--- 
a/mailbox/event/json/src/main/scala/org/apache/james/event/json/EventSerializer.scala
+++ 
b/mailbox/event/json/src/main/scala/org/apache/james/event/json/EventSerializer.scala
@@ -205,7 +205,7 @@ class JsonSerialize(mailboxIdFactory: MailboxId.Factory, 
messageIdFactory: Messa
   implicit val systemFlagsWrites: Writes[SystemFlag] = Writes.enumNameWrites
   implicit val userWriters: Writes[Username] = (user: Username) => 
JsString(user.asString)
   implicit val quotaRootWrites: Writes[QuotaRoot] = quotaRoot => 
JsString(quotaRoot.getValue)
-  implicit val quotaUsageValueWrites: Writes[QuotaUsageValue[_, _]] = value => 
if (value.isUnlimited) JsNull else JsNumber(value.asLong())
+  implicit val quotaUsageValueWrites: Writes[QuotaUsageValue[_, _]] = value => 
JsNumber(value.asLong())
   implicit val quotaLimitValueWrites: Writes[QuotaLimitValue[_]] = value => if 
(value.isUnlimited) JsNull else JsNumber(value.asLong())
   implicit val quotaScopeWrites: Writes[JavaQuota.Scope] = value => 
JsString(value.name)
   implicit val quotaCountWrites: Writes[Quota[QuotaCountLimit, 
QuotaCountUsage]] = Json.writes[Quota[QuotaCountLimit, QuotaCountUsage]]
@@ -246,7 +246,6 @@ class JsonSerialize(mailboxIdFactory: MailboxId.Factory, 
messageIdFactory: Messa
   }
   implicit val quotaCountUsageReads: Reads[QuotaCountUsage] = {
     case JsNumber(count) => JsSuccess(QuotaCountUsage.count(count.toLong))
-    case JsNull => JsSuccess(QuotaCountUsage.unlimited())
     case _ => JsError()
   }
   implicit val quotaRootReads: Reads[QuotaRoot] = {
@@ -264,7 +263,6 @@ class JsonSerialize(mailboxIdFactory: MailboxId.Factory, 
messageIdFactory: Messa
   }
   implicit val quotaSizeUsageReads: Reads[QuotaSizeUsage] = {
     case JsNumber(size) => JsSuccess(QuotaSizeUsage.size(size.toLong))
-    case JsNull => JsSuccess(QuotaSizeUsage.unlimited())
     case _ => JsError()
   }
   implicit val sessionIdReads: Reads[SessionId] = {
diff --git 
a/mailbox/event/json/src/test/java/org/apache/james/event/json/dtos/QuotaCountTest.java
 
b/mailbox/event/json/src/test/java/org/apache/james/event/json/dtos/QuotaCountTest.java
index 80191db..43cb574 100644
--- 
a/mailbox/event/json/src/test/java/org/apache/james/event/json/dtos/QuotaCountTest.java
+++ 
b/mailbox/event/json/src/test/java/org/apache/james/event/json/dtos/QuotaCountTest.java
@@ -82,18 +82,6 @@ class QuotaCountTest {
     }
 
     @Test
-    void quotaCountUsageShouldBeWellSerializedWhenUnlimited() {
-        
assertThat(DTO_JSON_SERIALIZE.quotaUsageValueWrites().writes(QuotaCountUsage.unlimited()))
-            .isEqualTo(JsNull$.MODULE$);
-    }
-
-    @Test
-    void quotaCountUsageShouldBeWellDeSerializedWhenUnUnlimited() {
-        
assertThat(DTO_JSON_SERIALIZE.quotaCountUsageReads().reads(JsNull$.MODULE$).get())
-            .isEqualTo(QuotaCountUsage.unlimited());
-    }
-
-    @Test
     void quotaCountUsageShouldReturnErrorWhenString() {
         assertThat(DTO_JSON_SERIALIZE.quotaCountUsageReads().reads(new 
JsString("18")))
             .isInstanceOf(JsError.class);
diff --git 
a/mailbox/event/json/src/test/java/org/apache/james/event/json/dtos/QuotaSizeTest.java
 
b/mailbox/event/json/src/test/java/org/apache/james/event/json/dtos/QuotaSizeTest.java
index ad2d54a..cdb800b 100644
--- 
a/mailbox/event/json/src/test/java/org/apache/james/event/json/dtos/QuotaSizeTest.java
+++ 
b/mailbox/event/json/src/test/java/org/apache/james/event/json/dtos/QuotaSizeTest.java
@@ -81,18 +81,6 @@ class QuotaSizeTest {
     }
 
     @Test
-    void quotaSizeUsageShouldBeWellSerializedWhenUnlimited() {
-        
assertThat(DTO_JSON_SERIALIZE.quotaUsageValueWrites().writes(QuotaSizeUsage.unlimited()))
-            .isEqualTo(JsNull$.MODULE$);
-    }
-
-    @Test
-    void quotaSizeUsageShouldBeWellDeSerializedWhenUnlimited() {
-        
assertThat(DTO_JSON_SERIALIZE.quotaSizeUsageReads().reads(JsNull$.MODULE$).get())
-            .isEqualTo(QuotaSizeUsage.unlimited());
-    }
-
-    @Test
     void quotaSizeUsageShouldReturnErrorWhenString() {
         assertThat(DTO_JSON_SERIALIZE.quotaSizeUsageReads().reads(new 
JsString("18")))
             .isInstanceOf(JsError.class);
diff --git 
a/server/container/cli/src/test/java/org/apache/james/cli/ServerCmdTest.java 
b/server/container/cli/src/test/java/org/apache/james/cli/ServerCmdTest.java
index ce70e3b..ddba90a 100644
--- a/server/container/cli/src/test/java/org/apache/james/cli/ServerCmdTest.java
+++ b/server/container/cli/src/test/java/org/apache/james/cli/ServerCmdTest.java
@@ -491,7 +491,7 @@ public class ServerCmdTest {
         String[] arguments = { "-h", "127.0.0.1", "-p", "9999", 
CmdType.GETSTORAGEQUOTA.getCommand(), quotaroot};
         CommandLine commandLine = ServerCmd.parseCommandLine(arguments);
 
-        
when(quotaProbe.getStorageQuota(quotaroot)).thenReturn(SerializableQuota.newInstance(QuotaSizeUsage.unlimited(),
 QuotaSizeLimit.size(12)));
+        
when(quotaProbe.getStorageQuota(quotaroot)).thenReturn(SerializableQuota.newInstance(QuotaSizeUsage.size(12),
 QuotaSizeLimit.unlimited()));
 
         testee.executeCommandLine(commandLine);
     }
@@ -502,7 +502,7 @@ public class ServerCmdTest {
         String[] arguments = { "-h", "127.0.0.1", "-p", "9999", 
CmdType.GETMESSAGECOUNTQUOTA.getCommand(), quotaroot};
         CommandLine commandLine = ServerCmd.parseCommandLine(arguments);
 
-        
when(quotaProbe.getMessageCountQuota(quotaroot)).thenReturn(SerializableQuota.newInstance(QuotaCountUsage.unlimited(),
 QuotaCountLimit.count(12)));
+        
when(quotaProbe.getMessageCountQuota(quotaroot)).thenReturn(SerializableQuota.newInstance(QuotaCountUsage.count(12),
 QuotaCountLimit.unlimited()));
 
         testee.executeCommandLine(commandLine);
     }


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org
For additional commands, e-mail: server-dev-h...@james.apache.org

Reply via email to