commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From er...@apache.org
Subject [6/8] commons-numbers git commit: Use named constants.
Date Fri, 30 Nov 2018 14:00:26 GMT
Use named constants.


Project: http://git-wip-us.apache.org/repos/asf/commons-numbers/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-numbers/commit/daa6a061
Tree: http://git-wip-us.apache.org/repos/asf/commons-numbers/tree/daa6a061
Diff: http://git-wip-us.apache.org/repos/asf/commons-numbers/diff/daa6a061

Branch: refs/heads/master
Commit: daa6a0616ac85869a2e078eb276438380643398a
Parents: a058f41
Author: Gilles Sadowski <gilles@harfang.homelinux.org>
Authored: Fri Nov 30 14:01:49 2018 +0100
Committer: Gilles Sadowski <gilles@harfang.homelinux.org>
Committed: Fri Nov 30 14:01:49 2018 +0100

----------------------------------------------------------------------
 .../apache/commons/numbers/complex/Complex.java | 46 +++++++++++++++-----
 .../commons/numbers/quaternion/Quaternion.java  | 34 +++++++++------
 2 files changed, 57 insertions(+), 23 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/daa6a061/commons-numbers-complex/src/main/java/org/apache/commons/numbers/complex/Complex.java
----------------------------------------------------------------------
diff --git a/commons-numbers-complex/src/main/java/org/apache/commons/numbers/complex/Complex.java
b/commons-numbers-complex/src/main/java/org/apache/commons/numbers/complex/Complex.java
index 979c474..90a140c 100644
--- a/commons-numbers-complex/src/main/java/org/apache/commons/numbers/complex/Complex.java
+++ b/commons-numbers-complex/src/main/java/org/apache/commons/numbers/complex/Complex.java
@@ -56,9 +56,17 @@ public final class Complex implements Serializable  {
     public static final Complex ZERO = new Complex(0, 0);
     /** A complex number representing "NaN + NaN i" */
     private static final Complex NAN = new Complex(Double.NaN, Double.NaN);
+
     /** Serializable version identifier. */
     private static final long serialVersionUID = 20180201L;
 
+    /** {@link #toString() String representation}. */
+    private static final String FORMAT_START = "(";
+    /** {@link #toString() String representation}. */
+    private static final String FORMAT_END = ")";
+    /** {@link #toString() String representation}. */
+    private static final String FORMAT_SEP = ",";
+
     /** The imaginary part. */
     private final double imaginary;
     /** The real part. */
@@ -143,21 +151,33 @@ public final class Complex implements Serializable  {
      */
     public static Complex parse(String s) {
         final int len = s.length();
-        final int startParen = s.indexOf("(");
+        final int startParen = s.indexOf(FORMAT_START);
         if (startParen != 0) {
-            throw new ComplexParsingException("Missing start parenthesis");
+            throw new ComplexParsingException("Expected start string: " + FORMAT_START);
         }
-        final int endParen = s.indexOf(")");
+        final int endParen = s.indexOf(FORMAT_END);
         if (endParen != len - 1) {
-            throw new ComplexParsingException("Missing end parenthesis");
+            throw new ComplexParsingException("Expected end string: " + FORMAT_END);
         }
-        final int comma = s.indexOf(",");
-        if (comma == -1) {
-            throw new ComplexParsingException("Missing comma");
+        final String[] elements = s.substring(1, s.length() - 1).split(FORMAT_SEP);
+        if (elements.length != 2) {
+            throw new ComplexParsingException("Incorrect number of parts: Expected 2 but
was " +
+                                              elements.length +
+                                              " (separator is '" + FORMAT_SEP + "')");
         }
 
-        final double re = Double.parseDouble(s.substring(startParen + 1, comma));
-        final double im = Double.parseDouble(s.substring(comma + 1, endParen));
+        final double re;
+        try {
+            re = Double.parseDouble(elements[0]);
+        } catch (NumberFormatException ex) {
+            throw new ComplexParsingException("Could not parse real part" + elements[0]);
+        }
+        final double im;
+        try {
+            im = Double.parseDouble(elements[1]);
+        } catch (NumberFormatException ex) {
+            throw new ComplexParsingException("Could not parse imaginary part" + elements[1]);
+        }
 
         return ofCartesian(re, im);
     }
@@ -1314,7 +1334,13 @@ public final class Complex implements Serializable  {
     /** {@inheritDoc} */
     @Override
     public String toString() {
-        return "(" + real + ", " + imaginary + ")";
+        final StringBuilder s = new StringBuilder();
+        s.append(FORMAT_START)
+            .append(real).append(FORMAT_SEP)
+            .append(imaginary)
+            .append(FORMAT_END);
+
+        return s.toString();
     }
 
     /**

http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/daa6a061/commons-numbers-quaternion/src/main/java/org/apache/commons/numbers/quaternion/Quaternion.java
----------------------------------------------------------------------
diff --git a/commons-numbers-quaternion/src/main/java/org/apache/commons/numbers/quaternion/Quaternion.java
b/commons-numbers-quaternion/src/main/java/org/apache/commons/numbers/quaternion/Quaternion.java
index 4126a85..afd1a63 100644
--- a/commons-numbers-quaternion/src/main/java/org/apache/commons/numbers/quaternion/Quaternion.java
+++ b/commons-numbers-quaternion/src/main/java/org/apache/commons/numbers/quaternion/Quaternion.java
@@ -44,6 +44,13 @@ public final class Quaternion implements Serializable {
     /** Error message. */
     private static final String ZERO_NORM_MSG = "Norm is zero";
 
+    /** {@link #toString() String representation}. */
+    private static final String FORMAT_START = "[";
+    /** {@link #toString() String representation}. */
+    private static final String FORMAT_END = "]";
+    /** {@link #toString() String representation}. */
+    private static final String FORMAT_SEP = " ";
+
     /** First component (scalar part). */
     private final double q0;
     /** Second component (first vector part). */
@@ -308,7 +315,7 @@ public final class Quaternion implements Serializable {
             return ((Double)q0).equals(q.q0) &&
                     ((Double)q1).equals(q.q1) &&
                     ((Double)q2).equals(q.q2) &&
-                    ((Double)q3).equals(q.q3) ;
+                    ((Double)q3).equals(q.q3);
         }
 
         return false;
@@ -498,17 +505,19 @@ public final class Quaternion implements Serializable {
      */
     public static Quaternion parse(String s) {
         final int len = s.length();
-        final int startBracket = s.indexOf("[");
+        final int startBracket = s.indexOf(FORMAT_START);
         if (startBracket != 0) {
-            throw new QuaternionParsingException("Missing opening square bracket");
+            throw new QuaternionParsingException("Expected start string: " + FORMAT_START);
         }
-        final int endBracket = s.indexOf("]");
+        final int endBracket = s.indexOf(FORMAT_END);
         if (endBracket != len - 1) {
-            throw new QuaternionParsingException("Missing closing square bracket");
+            throw new QuaternionParsingException("Expected end string: " + FORMAT_END);
         }
-        String[] elements = s.substring(1, s.length()-1).split(" ");
+        final String[] elements = s.substring(1, s.length() - 1).split(FORMAT_SEP);
         if (elements.length != 4) {
-            throw new QuaternionParsingException("Incorrect number of parts");
+            throw new QuaternionParsingException("Incorrect number of parts: Expected 4 but
was " +
+                                                 elements.length +
+                                                 " (separator is '" + FORMAT_SEP + "')");
         }
 
         final double q1;
@@ -544,14 +553,13 @@ public final class Quaternion implements Serializable {
      */
     @Override
     public String toString() {
-        final String sp = " ";
         final StringBuilder s = new StringBuilder();
-        s.append("[")
-            .append(q0).append(sp)
-            .append(q1).append(sp)
-            .append(q2).append(sp)
+        s.append(FORMAT_START)
+            .append(q0).append(FORMAT_SEP)
+            .append(q1).append(FORMAT_SEP)
+            .append(q2).append(FORMAT_SEP)
             .append(q3)
-            .append("]");
+            .append(FORMAT_END);
 
         return s.toString();
     }


Mime
View raw message