Hi Romain,

https://github.com/apache/incubator-johnzon/compare/master...danielsoro:numbers?expand=1
WDYT!?

patch attached.


On Thu, Mar 12, 2015 at 6:37 PM, Romain Manni-Bucau
<[email protected]> wrote:
> Hmm seems you do too much conversions in some cases (big decimal for
> instance)
>
> That said i m not sure it brings (readability) more than it destroy (perf).
> Any figures to proove me i am wrong?
>  Le 12 mars 2015 21:50, "Daniel Cunha" <[email protected]> a écrit :
>
>> Hi folks,
>>
>>
>> https://github.com/apache/incubator-johnzon/compare/master...danielsoro:numbers?expand=1
>>
>> I have this change in johnzon, maybe this patch can be apply.
>> patch attached.
>>
>> --
>> Best regard,
>> Daniel Cunha (soro)
>>



-- 
Best regard,
Daniel Cunha (soro)
From 7b9ae7c632e063250f104fc8c3beb3e03f181fb0 Mon Sep 17 00:00:00 2001
From: "Daniel Cunha (soro)" <[email protected]>
Date: Thu, 12 Mar 2015 17:40:30 -0300
Subject: [PATCH] Adjusted JsonNumbers

---
 .../apache/johnzon/core/AbstractJsonNumber.java    | 76 ++++++++++++++++++++++
 .../apache/johnzon/core/JsonArrayBuilderImpl.java  |  6 +-
 .../org/apache/johnzon/core/JsonDoubleImpl.java    | 58 ++---------------
 .../java/org/apache/johnzon/core/JsonLongImpl.java | 56 ++--------------
 .../org/apache/johnzon/core/JsonNumberImpl.java    | 35 ++--------
 5 files changed, 95 insertions(+), 136 deletions(-)
 create mode 100644 johnzon-core/src/main/java/org/apache/johnzon/core/AbstractJsonNumber.java

diff --git a/johnzon-core/src/main/java/org/apache/johnzon/core/AbstractJsonNumber.java b/johnzon-core/src/main/java/org/apache/johnzon/core/AbstractJsonNumber.java
new file mode 100644
index 0000000..4566595
--- /dev/null
+++ b/johnzon-core/src/main/java/org/apache/johnzon/core/AbstractJsonNumber.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.johnzon.core;
+
+import javax.json.JsonNumber;
+import java.math.BigDecimal;
+import java.math.BigInteger;
+
+public abstract class AbstractJsonNumber<T extends Number> implements JsonNumber {
+    protected final BigDecimal value;
+
+    public AbstractJsonNumber(T value) {
+        this.value =  new BigDecimal(value.toString());
+    }
+
+    @Override
+    public int intValue() {
+        return value.intValue();
+    }
+
+    @Override
+    public long longValue() {
+        return value.longValue();
+    }
+
+    @Override
+    public BigInteger bigIntegerValue() {
+        return value.toBigInteger();
+    }
+
+    @Override
+    public BigInteger bigIntegerValueExact() {
+        return value.toBigIntegerExact();
+    }
+
+    @Override
+    public double doubleValue() {
+        return value.doubleValue();
+    }
+
+    @Override
+    public BigDecimal bigDecimalValue() {
+        return value;
+    }
+
+    @Override
+    public ValueType getValueType() {
+        return ValueType.NUMBER;
+    }
+
+    @Override
+    public int hashCode() {
+        return value.hashCode();
+    }
+
+    @Override
+    public String toString() {
+        return value.toString();
+    }
+}
diff --git a/johnzon-core/src/main/java/org/apache/johnzon/core/JsonArrayBuilderImpl.java b/johnzon-core/src/main/java/org/apache/johnzon/core/JsonArrayBuilderImpl.java
index 5dfd839..1313565 100644
--- a/johnzon-core/src/main/java/org/apache/johnzon/core/JsonArrayBuilderImpl.java
+++ b/johnzon-core/src/main/java/org/apache/johnzon/core/JsonArrayBuilderImpl.java
@@ -101,7 +101,7 @@ class JsonArrayBuilderImpl implements JsonArrayBuilder, Serializable {
     
     private void addValue(JsonValue value){
         if (value == null) {
-            throw npe();
+           throw new NullPointerException("value/builder must not be null");
         }
         
         if(tmpList==null){
@@ -123,8 +123,4 @@ class JsonArrayBuilderImpl implements JsonArrayBuilder, Serializable {
         }
         
     }
-
-    private static NullPointerException npe() {
-        throw new NullPointerException("value/builder must not be null");
-    }
 }
diff --git a/johnzon-core/src/main/java/org/apache/johnzon/core/JsonDoubleImpl.java b/johnzon-core/src/main/java/org/apache/johnzon/core/JsonDoubleImpl.java
index bb51b36..8e76048 100644
--- a/johnzon-core/src/main/java/org/apache/johnzon/core/JsonDoubleImpl.java
+++ b/johnzon-core/src/main/java/org/apache/johnzon/core/JsonDoubleImpl.java
@@ -18,21 +18,15 @@
  */
 package org.apache.johnzon.core;
 
-import java.math.BigDecimal;
-import java.math.BigInteger;
-
 import javax.json.JsonNumber;
 
-final class JsonDoubleImpl implements JsonNumber {
-    private final double value;
+final class JsonDoubleImpl extends AbstractJsonNumber<Double> {
 
     JsonDoubleImpl(final double value) {
-        
-        if(Double.isInfinite(value) || Double.isNaN(value)) {
+        super(value);
+        if (Double.isInfinite(value) || Double.isNaN(value)) {
             throw new NumberFormatException("double value must not be NaN or Infinite");
         }
-        
-        this.value = value;
     }
 
     @Override
@@ -41,62 +35,22 @@ final class JsonDoubleImpl implements JsonNumber {
     }
 
     @Override
-    public int intValue() {
-        return (int) value;
-    }
-
-    @Override
     public int intValueExact() {
         return intValue();
     }
 
     @Override
-    public long longValue() {
-        return (long) value;
-    }
-
-    @Override
     public long longValueExact() {
-        return (long) value;
-    }
-
-    @Override
-    public BigInteger bigIntegerValue() {
-        return new BigDecimal(toString()).toBigInteger();
-    }
-
-    @Override
-    public BigInteger bigIntegerValueExact() {
-        return new BigDecimal(toString()).toBigIntegerExact();
-    }
-
-    @Override
-    public double doubleValue() {
-        return value;
-    }
-
-    @Override
-    public BigDecimal bigDecimalValue() {
-        return new BigDecimal(toString());
-    }
-
-    @Override
-    public ValueType getValueType() {
-        return ValueType.NUMBER;
-    }
-
-    @Override
-    public String toString() {
-        return Double.toString(value);
+        return longValue();
     }
 
     @Override
     public int hashCode() {
-        return Double.valueOf(value).hashCode();
+        return super.hashCode();
     }
 
     @Override
     public boolean equals(final Object obj) {
-        return JsonNumber.class.isInstance(obj) && JsonNumber.class.cast(obj).doubleValue() == value;
+        return JsonNumber.class.isInstance(obj) && JsonNumber.class.cast(obj).doubleValue() == value.doubleValue();
     }
 }
diff --git a/johnzon-core/src/main/java/org/apache/johnzon/core/JsonLongImpl.java b/johnzon-core/src/main/java/org/apache/johnzon/core/JsonLongImpl.java
index 2f013f7..2579aad 100644
--- a/johnzon-core/src/main/java/org/apache/johnzon/core/JsonLongImpl.java
+++ b/johnzon-core/src/main/java/org/apache/johnzon/core/JsonLongImpl.java
@@ -18,16 +18,12 @@
  */
 package org.apache.johnzon.core;
 
-import java.math.BigDecimal;
-import java.math.BigInteger;
-
 import javax.json.JsonNumber;
 
-final class JsonLongImpl implements JsonNumber {
-    private final long value;
+final class JsonLongImpl extends AbstractJsonNumber<Long> {
 
-    JsonLongImpl(final long value) {
-        this.value = value;
+    public JsonLongImpl(final long value) {
+        super(value);
     }
 
     @Override
@@ -36,62 +32,22 @@ final class JsonLongImpl implements JsonNumber {
     }
 
     @Override
-    public int intValue() {
-        return (int) value;
-    }
-
-    @Override
     public int intValueExact() {
         return intValue();
     }
 
     @Override
-    public long longValue() {
-        return value;
-    }
-
-    @Override
     public long longValueExact() {
-        return value;
-    }
-
-    @Override
-    public BigInteger bigIntegerValue() {
-        return new BigInteger(toString());
-    }
-
-    @Override
-    public BigInteger bigIntegerValueExact() {
-        return bigIntegerValue();
-    }
-
-    @Override
-    public double doubleValue() {
-        return value;
-    }
-
-    @Override
-    public BigDecimal bigDecimalValue() {
-        return new BigDecimal(toString());
-    }
-
-    @Override
-    public ValueType getValueType() {
-        return ValueType.NUMBER;
-    }
-
-    @Override
-    public String toString() {
-        return Long.toString(value);
+        return longValue();
     }
 
     @Override
     public int hashCode() {
-        return (int) value;
+        return super.hashCode();
     }
 
     @Override
     public boolean equals(final Object obj) {
-        return JsonNumber.class.isInstance(obj) && JsonNumber.class.cast(obj).longValue() == value;
+        return JsonNumber.class.isInstance(obj) && JsonNumber.class.cast(obj).longValue() == value.longValue();
     }
 }
diff --git a/johnzon-core/src/main/java/org/apache/johnzon/core/JsonNumberImpl.java b/johnzon-core/src/main/java/org/apache/johnzon/core/JsonNumberImpl.java
index 8b560ed..0d974e8 100644
--- a/johnzon-core/src/main/java/org/apache/johnzon/core/JsonNumberImpl.java
+++ b/johnzon-core/src/main/java/org/apache/johnzon/core/JsonNumberImpl.java
@@ -18,21 +18,18 @@
  */
 package org.apache.johnzon.core;
 
+import javax.json.JsonNumber;
 import java.math.BigDecimal;
 import java.math.BigInteger;
 
-import javax.json.JsonNumber;
-
-final class JsonNumberImpl implements JsonNumber {
-    private final BigDecimal value;
+final class JsonNumberImpl extends AbstractJsonNumber<BigDecimal> implements JsonNumber {
     private Integer hashCode = null;
 
     JsonNumberImpl(final BigDecimal decimal) {
-        if(decimal == null) {
+        super(decimal);
+        if (decimal == null) {
             throw new NullPointerException("decimal must not be null");
         }
-        
-        this.value = decimal;
     }
 
     @Override
@@ -41,11 +38,6 @@ final class JsonNumberImpl implements JsonNumber {
     }
 
     @Override
-    public int intValue() {
-        return value.intValue();
-    }
-
-    @Override
     public int intValueExact() {
         return value.intValueExact();
     }
@@ -71,31 +63,16 @@ final class JsonNumberImpl implements JsonNumber {
     }
 
     @Override
-    public double doubleValue() {
-        return value.doubleValue();
-    }
-
-    @Override
     public BigDecimal bigDecimalValue() {
         return value;
     }
 
     @Override
-    public ValueType getValueType() {
-        return ValueType.NUMBER;
-    }
-
-    @Override
-    public String toString() {
-        return value.toString();
-    }
-
-    @Override
     public int hashCode() {
-        Integer h=hashCode;
+        Integer h = hashCode;
         if (h == null) {
             h = value.hashCode();
-            hashCode=h;
+            hashCode = h;
         }
         return h;
     }
-- 
2.1.0

Reply via email to