Enhancement: toBoolean() and empty String - Mailing list pgsql-jdbc

From Eberhard Schulte
Subject Enhancement: toBoolean() and empty String
Date
Msg-id 44080934.4030605@pixelboxx.de
Whole thread Raw
Responses Re: Enhancement: toBoolean() and empty String
List pgsql-jdbc
Hello,

I habe two enhancements. I found the problems even in older versions.

1. Encoding.decode() creates for each empty String a new String object. Memory is wasted unnecessary.

Index: Encoding.java
===================================================================
RCS file: /usr/local/cvsroot/pgjdbc/pgjdbc/org/postgresql/core/Encoding.java,v
retrieving revision 1.21
diff -u -r1.21 Encoding.java
--- Encoding.java       4 Jul 2005 02:18:32 -0000       1.21
+++ Encoding.java       3 Mar 2006 08:45:12 -0000
@@ -175,6 +175,10 @@
       */
      public String decode(byte[] encodedString, int offset, int length) throws IOException
      {
+        if(encodedString == null || encodedString.length == 0) {
+            return ""; // do not create new String() use constant String
+        }
+
          if (encoding == null)
              return new String(encodedString, offset, length);



2. AbstractJdbc2ResultSet.toBoolean(): there is no explicite false check. A false is produced by an
NumberFormatException.


Index: AbstractJdbc2ResultSet.java
===================================================================
RCS file: /usr/local/cvsroot/pgjdbc/pgjdbc/org/postgresql/jdbc2/AbstractJdbc2ResultSet.java,v
retrieving revision 1.83
diff -u -r1.83 AbstractJdbc2ResultSet.java
--- AbstractJdbc2ResultSet.java 4 Dec 2005 21:40:33 -0000       1.83
+++ AbstractJdbc2ResultSet.java 3 Mar 2006 08:54:53 -0000
@@ -2434,22 +2434,28 @@

      //----------------- Formatting Methods -------------------

-    public static boolean toBoolean(String s)
+    public static boolean toBoolean(String s) throws PSQLException
      {
          if (s != null)
          {
              s = s.trim();

-            if (s.equalsIgnoreCase("true") || s.equalsIgnoreCase("t"))
-                return true;
-
+            if (s.equalsIgnoreCase("t") || s.equalsIgnoreCase("true")) { // examine the most frequent case first
+                return true;
+            } else if (s.equalsIgnoreCase("f") || s.equalsIgnoreCase("false")) { // explicite false check
+                return false;
+            }
+
              try
              {
                  if (Double.valueOf(s).doubleValue() == 1)
                      return true;
+                // TODO / FIXME: explicite false check
              }
              catch (NumberFormatException e)
              {
+                // throw exception if occurs
+                throw new PSQLException ("postgresql.res.baddouble", PSQLState.NUMERIC_VALUE_OUT_OF_RANGE, e);
              }
          }
          return false;  // SQL NULL





Schönen Gruß

Eberhard Schulte


pgsql-jdbc by date:

Previous
From: Hugo Sacramento
Date:
Subject: Re: Retrieve Query
Next
From: "sathish kumar shanmugavelu"
Date:
Subject: Aggregate function for a text column