Enhancement: toBoolean() and empty String

From: Eberhard Schulte <esc(at)pixelboxx(dot)de>
To: pgsql-jdbc(at)postgresql(dot)org
Subject: Enhancement: toBoolean() and empty String
Date: 2006-03-03 09:15:32
Message-ID: 44080934.4030605@pixelboxx.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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

Responses

Browse pgsql-jdbc by date

  From Date Subject
Next Message sathish kumar shanmugavelu 2006-03-04 04:45:46 Aggregate function for a text column
Previous Message Hugo Sacramento 2006-03-03 00:30:24 Re: Retrieve Query