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

He-Pin pushed a commit to branch fix/metric-java-math-number-support
in repository https://gitbox.apache.org/repos/asf/pekko.git

commit 6ccabbe6f8caed7a1c9bb9a6f9ae8f41b0e16a5c
Author: 虎鸣 <[email protected]>
AuthorDate: Sat Jul 25 23:53:58 2026 +0800

    fix: support java.math.BigInteger/BigDecimal in MetricNumericConverter
    
    Motivation:
    MetricNumericConverter.convertNumber only handled scala.math.BigInt and
    scala.math.BigDecimal but not java.math.BigInteger and java.math.BigDecimal.
    Since Metric.value is typed as java.lang.Number, Java users passing
    java.math.BigInteger or java.math.BigDecimal would get an
    IllegalArgumentException. The NumberInputStream deserializer also explicitly
    allows java.math classes through, so deserialized values could also fail.
    
    Modification:
    Add pattern match cases for java.math.BigInteger and java.math.BigDecimal in
    convertNumber, placed before the Scala BigInt/BigDecimal cases.
    
    Result:
    Java users can now use java.math.BigInteger and java.math.BigDecimal as
    Metric values without errors.
    
    Tests:
    - sbt "cluster-metrics / Test / testOnly 
org.apache.pekko.cluster.metrics.MetricNumericConverterSpec"
    - sbt "cluster-metrics / Test / testOnly 
org.apache.pekko.cluster.metrics.protobuf.MessageSerializerSpec"
    - All tests passed.
    
    References:
    None - discovered during Java API audit for Scala number type usage
---
 .../scala/org/apache/pekko/cluster/metrics/Metric.scala  | 16 +++++++++-------
 .../org/apache/pekko/cluster/metrics/MetricSpec.scala    | 15 +++++++++++++++
 2 files changed, 24 insertions(+), 7 deletions(-)

diff --git 
a/cluster-metrics/src/main/scala/org/apache/pekko/cluster/metrics/Metric.scala 
b/cluster-metrics/src/main/scala/org/apache/pekko/cluster/metrics/Metric.scala
index df7ee5d67e..cb964d8e73 100644
--- 
a/cluster-metrics/src/main/scala/org/apache/pekko/cluster/metrics/Metric.scala
+++ 
b/cluster-metrics/src/main/scala/org/apache/pekko/cluster/metrics/Metric.scala
@@ -271,13 +271,15 @@ private[metrics] trait MetricNumericConverter {
    * May involve rounding or truncation.
    */
   def convertNumber(from: Any): Either[Long, Double] = from match {
-    case n: Int        => Left(n)
-    case n: Long       => Left(n)
-    case n: Double     => Right(n)
-    case n: Float      => Right(n)
-    case n: BigInt     => Left(n.longValue)
-    case n: BigDecimal => Right(n.doubleValue)
-    case x             => throw new IllegalArgumentException(s"Not a number 
[$x]")
+    case n: Int                  => Left(n)
+    case n: Long                 => Left(n)
+    case n: Double               => Right(n)
+    case n: Float                => Right(n)
+    case n: java.math.BigInteger => Left(n.longValue)
+    case n: java.math.BigDecimal => Right(n.doubleValue)
+    case n: BigInt               => Left(n.longValue)
+    case n: BigDecimal           => Right(n.doubleValue)
+    case x                       => throw new IllegalArgumentException(s"Not a 
number [$x]")
   }
 
 }
diff --git 
a/cluster-metrics/src/test/scala/org/apache/pekko/cluster/metrics/MetricSpec.scala
 
b/cluster-metrics/src/test/scala/org/apache/pekko/cluster/metrics/MetricSpec.scala
index e8cc708979..f1bca14731 100644
--- 
a/cluster-metrics/src/test/scala/org/apache/pekko/cluster/metrics/MetricSpec.scala
+++ 
b/cluster-metrics/src/test/scala/org/apache/pekko/cluster/metrics/MetricSpec.scala
@@ -39,6 +39,21 @@ class MetricNumericConverterSpec extends AnyWordSpec with 
Matchers with MetricNu
       convertNumber(0.0).isRight should ===(true)
     }
 
+    "convert java.math.BigInteger and java.math.BigDecimal" in {
+      convertNumber(java.math.BigInteger.valueOf(42)).isLeft should ===(true)
+      convertNumber(java.math.BigInteger.valueOf(42)).left.get should ===(42L)
+      convertNumber(new java.math.BigDecimal("3.14")).isRight should ===(true)
+      convertNumber(new java.math.BigDecimal("3.14")).right.get should 
===(3.14 +- 0.0001)
+    }
+
+    "define a metric with java.math.BigInteger and java.math.BigDecimal 
values" in {
+      val Some(intMetric) = Metric.create("big-int", 
java.math.BigInteger.valueOf(1024L), None): @unchecked
+      intMetric.value should ===(java.math.BigInteger.valueOf(1024L))
+
+      val Some(decMetric) = Metric.create("big-dec", new 
java.math.BigDecimal("99.9"), None): @unchecked
+      decMetric.value should ===(new java.math.BigDecimal("99.9"))
+    }
+
     "define a new metric" in {
       val Some(metric) = Metric.create(HeapMemoryUsed, 256L, decayFactor = 
Some(0.18)): @unchecked
       metric.name should ===(HeapMemoryUsed)


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to