You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@james.apache.org by ma...@apache.org on 2023/02/22 20:14:05 UTC
[james-project] 08/08: Throwing methods are uncommon in scala, let's throw at scala/java boundary
This is an automated email from the ASF dual-hosted git repository.
matthieu pushed a commit to branch refactorings-5
in repository https://gitbox.apache.org/repos/asf/james-project.git
commit 36fa530069dd6d1f2bed42e0dba1787038db74f4
Author: Matthieu Baechler <ma...@baechler-craftsmanship.fr>
AuthorDate: Wed Feb 22 20:53:55 2023 +0100
Throwing methods are uncommon in scala, let's throw at scala/java boundary
---
.../james/rate/limiter/api/RateLimiter.scala | 24 ++++------------------
.../james/transport/mailets/EntityType.scala | 19 +++++++++++++----
2 files changed, 19 insertions(+), 24 deletions(-)
diff --git a/server/mailet/rate-limiter/src/main/scala/org/apache/james/rate/limiter/api/RateLimiter.scala b/server/mailet/rate-limiter/src/main/scala/org/apache/james/rate/limiter/api/RateLimiter.scala
index c02c1aa9f0..9eda3cc7b7 100644
--- a/server/mailet/rate-limiter/src/main/scala/org/apache/james/rate/limiter/api/RateLimiter.scala
+++ b/server/mailet/rate-limiter/src/main/scala/org/apache/james/rate/limiter/api/RateLimiter.scala
@@ -37,16 +37,8 @@ object AllowedQuantity {
type AllowedQuantity = Long Refined PositiveLongConstraint
def validate(value: Long): Either[NumberFormatException, AllowedQuantity] =
- refined.refineV[PositiveLongConstraint](value) match {
- case Right(value) => Right(value)
- case Left(error) => Left(new NumberFormatException(error))
- }
-
- def liftOrThrow(value: Long): AllowedQuantity =
- validate(value) match {
- case Right(value) => value
- case Left(error) => throw error
- }
+ refined.refineV[PositiveLongConstraint](value)
+ .left.map(error => new NumberFormatException(error))
}
object Increment {
@@ -54,16 +46,8 @@ object Increment {
type Increment = Int Refined PositiveLongConstraint
def validate(value: Int): Either[NumberFormatException, Increment] =
- refined.refineV[PositiveLongConstraint](value) match {
- case Right(value) => Right(value)
- case Left(error) => Left(new NumberFormatException(error))
- }
-
- def liftOrThrow(value: Int): Increment =
- validate(value) match {
- case Right(value) => value
- case Left(error) => throw error
- }
+ refined.refineV[PositiveLongConstraint](value)
+ .left.map(error => new NumberFormatException(error))
}
case class Rule(quantity: AllowedQuantity, duration: Duration)
diff --git a/server/mailet/rate-limiter/src/main/scala/org/apache/james/transport/mailets/EntityType.scala b/server/mailet/rate-limiter/src/main/scala/org/apache/james/transport/mailets/EntityType.scala
index ead8e82b97..29282d65a1 100644
--- a/server/mailet/rate-limiter/src/main/scala/org/apache/james/transport/mailets/EntityType.scala
+++ b/server/mailet/rate-limiter/src/main/scala/org/apache/james/transport/mailets/EntityType.scala
@@ -56,21 +56,32 @@ object ConfigurationOps {
}
object EntityType {
+
+ implicit class EitherOps[E <: Throwable, A](either: Either[E, A]) {
+ def orThrow(message: String): A = either.left.map(cause => new RuntimeException(message, cause)).toTry.get
+ }
+
def extractRules(entityType: EntityType, duration: Duration, mailetConfig: MailetConfig): Option[Rules] = (entityType match {
case Count => mailetConfig.getOptionalLong("count")
case RecipientsType => mailetConfig.getOptionalLong("recipients")
case Size => mailetConfig.getOptionalSize("size").map(_.asBytes())
case TotalSize => mailetConfig.getOptionalSize("totalSize").map(_.asBytes())
- }).map(AllowedQuantity.liftOrThrow)
+ }).map(AllowedQuantity.validate(_).orThrow(s"invalid quantity for ${entityType.asString}"))
.map(quantity => Rules(Seq(Rule(quantity, duration))))
def extractQuantity(entityType: EntityType, mail: Mail): Option[Increment] = entityType match {
case Count => Some(1)
- case RecipientsType => Some(Increment.liftOrThrow(mail.getRecipients.size()))
- case Size => Some(Increment.liftOrThrow(mail.getMessageSize.toInt))
+ case RecipientsType =>
+ Some(Increment
+ .validate(mail.getRecipients.size())
+ .orThrow(s"invalid quantity for ${entityType.asString}"))
+ case Size =>
+ Some(Increment
+ .validate(mail.getMessageSize.toInt)
+ .orThrow(s"invalid quantity for ${entityType.asString}"))
case TotalSize =>
Try(Math.multiplyExact(mail.getMessageSize.toInt, mail.getRecipients.size()))
- .map(Increment.liftOrThrow)
+ .map(Increment.validate(_).orThrow(s"invalid quantity for ${entityType.asString}"))
.toOption
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org