You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ba...@apache.org on 2009/10/20 07:46:43 UTC

svn commit: r826960 - /commons/proper/lang/trunk/src/java/org/apache/commons/lang/ObjectUtils.java

Author: bayard
Date: Tue Oct 20 05:46:43 2009
New Revision: 826960

URL: http://svn.apache.org/viewvc?rev=826960&view=rev
Log:
Moved identityToString(StringBuffer, Object) to identityToString(Appendable, Object) per LANG-542

Modified:
    commons/proper/lang/trunk/src/java/org/apache/commons/lang/ObjectUtils.java

Modified: commons/proper/lang/trunk/src/java/org/apache/commons/lang/ObjectUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/java/org/apache/commons/lang/ObjectUtils.java?rev=826960&r1=826959&r2=826960&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/java/org/apache/commons/lang/ObjectUtils.java (original)
+++ commons/proper/lang/trunk/src/java/org/apache/commons/lang/ObjectUtils.java Tue Oct 20 05:46:43 2009
@@ -158,7 +158,7 @@
         if (object == null) {
             return null;
         }
-        StringBuffer buffer = new StringBuffer();
+        StringBuilder buffer = new StringBuilder();
         identityToString(buffer, object);
         return buffer.toString();
     }
@@ -178,13 +178,17 @@
      * @param object  the object to create a toString for
      * @since 2.4
      */
-    public static void identityToString(StringBuffer buffer, Object object) {
+    public static void identityToString(Appendable buffer, Object object) {
         if (object == null) {
             throw new NullPointerException("Cannot get the toString of a null identity");
         }
-        buffer.append(object.getClass().getName())
-              .append('@')
-              .append(Integer.toHexString(System.identityHashCode(object)));
+        try {
+            buffer.append(object.getClass().getName())
+                  .append('@')
+                  .append(Integer.toHexString(System.identityHashCode(object)));
+        } catch(java.io.IOException ioe) {
+            // can't happen - Appendable API forces it upon us
+        }
     }
 
     // ToString



Re: svn commit: r826960 - /commons/proper/lang/trunk/src/java/org/apache/commons/lang/ObjectUtils.java

Posted by sebb <se...@gmail.com>.
On 25/10/2009, Henri Yandell <fl...@gmail.com> wrote:
> On Sun, Oct 25, 2009 at 4:02 AM, sebb <se...@gmail.com> wrote:
>  > On 25/10/2009, Henri Yandell <fl...@gmail.com> wrote:
>  >> On Sat, Oct 24, 2009 at 4:05 AM, Stephen Colebourne
>  >>  <sc...@btopenworld.com> wrote:
>  >>  > bayard@apache.org wrote:
>  >>  >>
>  >>  >> URL: http://svn.apache.org/viewvc?rev=826960&view=rev
>  >>  >> Log:
>  >>  >> Moved identityToString(StringBuffer, Object) to
>  >>  >> identityToString(Appendable, Object) per LANG-542
>  >>  >> @@ -178,13 +178,17 @@
>  >>  >>      * @param object  the object to create a toString for
>  >>  >>      * @since 2.4
>  >>  >>      */
>  >>  >> -    public static void identityToString(StringBuffer buffer, Object
>  >>  >> object) {
>  >>  >> +    public static void identityToString(Appendable buffer, Object object)
>  >>  >> {
>  >>  >>         if (object == null) {
>  >>  >>             throw new NullPointerException("Cannot get the toString of a
>  >>  >> null identity");
>  >>  >>         }
>  >>  >> -        buffer.append(object.getClass().getName())
>  >>  >> -              .append('@')
>  >>  >> -
>  >>  >>  .append(Integer.toHexString(System.identityHashCode(object)));
>  >>  >> +        try {
>  >>  >> +            buffer.append(object.getClass().getName())
>  >>  >> +                  .append('@')
>  >>  >> +
>  >>  >>  .append(Integer.toHexString(System.identityHashCode(object)));
>  >>  >> +        } catch(java.io.IOException ioe) {
>  >>  >> +            // can't happen - Appendable API forces it upon us
>  >>  >> +        }
>  >>  >>     }
>  >>  >
>  >>  > This change is invalid.
>  >>  >
>  >>  > IO classes such as Writer implement Appendable, and appending to those can
>  >>  > throw an IOException.
>  >>
>  >>
>  >> Yup.  I remember telling myself it was fine because we passed a
>  >>  StringBuilder in so the IOException wouldn't happen.... 'cept that's
>  >>  only if someone calls identityToString(Object) :)
>  >>
>  >>  I've rolled back the change for now. I don't like IOException being
>  >>  added to the API. I'm tempted by the somewhat lame solution of a
>  >>  private Appendable method, and explicitly supporting StringBuffer,
>  >>  StringBuilder and StrBuilder.
>  >>
>  >
>  > Unfortunately StringBuilder & StringBuffer don't have a common
>  > accessible parent.
>  > [Seems like a design fault to me]
>  >
>  > Rather than swallowing the Exception, why not convert the IOException
>  > to an unchecked Exception? (and document the restriction)
>  >
>  > The only reason for using Appendable is to support
>  > StringBuilder/Buffer, so if it is used with any other Appendable
>  > classes, let the user beware.
>
>
> Why not just do (StringBuilder, Object); (StringBuffer, Object) and
>  (StrBuilder, Object) if that's the intent?
>

Either will do, but neither is ideal.

However using Appendable + rethrown Exception allows code to be shared
and so generates less code. It's also easier to maintain if there is a
single copy of each method.
Not so important for a short method, but some potential candidates are long.

>  Hen
>
>  ---------------------------------------------------------------------
>  To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
>  For additional commands, e-mail: dev-help@commons.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Re: svn commit: r826960 - /commons/proper/lang/trunk/src/java/org/apache/commons/lang/ObjectUtils.java

Posted by Henri Yandell <fl...@gmail.com>.
On Sun, Oct 25, 2009 at 4:02 AM, sebb <se...@gmail.com> wrote:
> On 25/10/2009, Henri Yandell <fl...@gmail.com> wrote:
>> On Sat, Oct 24, 2009 at 4:05 AM, Stephen Colebourne
>>  <sc...@btopenworld.com> wrote:
>>  > bayard@apache.org wrote:
>>  >>
>>  >> URL: http://svn.apache.org/viewvc?rev=826960&view=rev
>>  >> Log:
>>  >> Moved identityToString(StringBuffer, Object) to
>>  >> identityToString(Appendable, Object) per LANG-542
>>  >> @@ -178,13 +178,17 @@
>>  >>      * @param object  the object to create a toString for
>>  >>      * @since 2.4
>>  >>      */
>>  >> -    public static void identityToString(StringBuffer buffer, Object
>>  >> object) {
>>  >> +    public static void identityToString(Appendable buffer, Object object)
>>  >> {
>>  >>         if (object == null) {
>>  >>             throw new NullPointerException("Cannot get the toString of a
>>  >> null identity");
>>  >>         }
>>  >> -        buffer.append(object.getClass().getName())
>>  >> -              .append('@')
>>  >> -
>>  >>  .append(Integer.toHexString(System.identityHashCode(object)));
>>  >> +        try {
>>  >> +            buffer.append(object.getClass().getName())
>>  >> +                  .append('@')
>>  >> +
>>  >>  .append(Integer.toHexString(System.identityHashCode(object)));
>>  >> +        } catch(java.io.IOException ioe) {
>>  >> +            // can't happen - Appendable API forces it upon us
>>  >> +        }
>>  >>     }
>>  >
>>  > This change is invalid.
>>  >
>>  > IO classes such as Writer implement Appendable, and appending to those can
>>  > throw an IOException.
>>
>>
>> Yup.  I remember telling myself it was fine because we passed a
>>  StringBuilder in so the IOException wouldn't happen.... 'cept that's
>>  only if someone calls identityToString(Object) :)
>>
>>  I've rolled back the change for now. I don't like IOException being
>>  added to the API. I'm tempted by the somewhat lame solution of a
>>  private Appendable method, and explicitly supporting StringBuffer,
>>  StringBuilder and StrBuilder.
>>
>
> Unfortunately StringBuilder & StringBuffer don't have a common
> accessible parent.
> [Seems like a design fault to me]
>
> Rather than swallowing the Exception, why not convert the IOException
> to an unchecked Exception? (and document the restriction)
>
> The only reason for using Appendable is to support
> StringBuilder/Buffer, so if it is used with any other Appendable
> classes, let the user beware.

Why not just do (StringBuilder, Object); (StringBuffer, Object) and
(StrBuilder, Object) if that's the intent?

Hen

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Re: svn commit: r826960 - /commons/proper/lang/trunk/src/java/org/apache/commons/lang/ObjectUtils.java

Posted by sebb <se...@gmail.com>.
On 25/10/2009, Henri Yandell <fl...@gmail.com> wrote:
> On Sat, Oct 24, 2009 at 4:05 AM, Stephen Colebourne
>  <sc...@btopenworld.com> wrote:
>  > bayard@apache.org wrote:
>  >>
>  >> URL: http://svn.apache.org/viewvc?rev=826960&view=rev
>  >> Log:
>  >> Moved identityToString(StringBuffer, Object) to
>  >> identityToString(Appendable, Object) per LANG-542
>  >> @@ -178,13 +178,17 @@
>  >>      * @param object  the object to create a toString for
>  >>      * @since 2.4
>  >>      */
>  >> -    public static void identityToString(StringBuffer buffer, Object
>  >> object) {
>  >> +    public static void identityToString(Appendable buffer, Object object)
>  >> {
>  >>         if (object == null) {
>  >>             throw new NullPointerException("Cannot get the toString of a
>  >> null identity");
>  >>         }
>  >> -        buffer.append(object.getClass().getName())
>  >> -              .append('@')
>  >> -
>  >>  .append(Integer.toHexString(System.identityHashCode(object)));
>  >> +        try {
>  >> +            buffer.append(object.getClass().getName())
>  >> +                  .append('@')
>  >> +
>  >>  .append(Integer.toHexString(System.identityHashCode(object)));
>  >> +        } catch(java.io.IOException ioe) {
>  >> +            // can't happen - Appendable API forces it upon us
>  >> +        }
>  >>     }
>  >
>  > This change is invalid.
>  >
>  > IO classes such as Writer implement Appendable, and appending to those can
>  > throw an IOException.
>
>
> Yup.  I remember telling myself it was fine because we passed a
>  StringBuilder in so the IOException wouldn't happen.... 'cept that's
>  only if someone calls identityToString(Object) :)
>
>  I've rolled back the change for now. I don't like IOException being
>  added to the API. I'm tempted by the somewhat lame solution of a
>  private Appendable method, and explicitly supporting StringBuffer,
>  StringBuilder and StrBuilder.
>

Unfortunately StringBuilder & StringBuffer don't have a common
accessible parent.
[Seems like a design fault to me]

Rather than swallowing the Exception, why not convert the IOException
to an unchecked Exception? (and document the restriction)

The only reason for using Appendable is to support
StringBuilder/Buffer, so if it is used with any other Appendable
classes, let the user beware.


>  Hen
>
>  ---------------------------------------------------------------------
>  To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
>  For additional commands, e-mail: dev-help@commons.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Re: svn commit: r826960 - /commons/proper/lang/trunk/src/java/org/apache/commons/lang/ObjectUtils.java

Posted by Henri Yandell <fl...@gmail.com>.
On Sat, Oct 24, 2009 at 4:05 AM, Stephen Colebourne
<sc...@btopenworld.com> wrote:
> bayard@apache.org wrote:
>>
>> URL: http://svn.apache.org/viewvc?rev=826960&view=rev
>> Log:
>> Moved identityToString(StringBuffer, Object) to
>> identityToString(Appendable, Object) per LANG-542
>> @@ -178,13 +178,17 @@
>>      * @param object  the object to create a toString for
>>      * @since 2.4
>>      */
>> -    public static void identityToString(StringBuffer buffer, Object
>> object) {
>> +    public static void identityToString(Appendable buffer, Object object)
>> {
>>         if (object == null) {
>>             throw new NullPointerException("Cannot get the toString of a
>> null identity");
>>         }
>> -        buffer.append(object.getClass().getName())
>> -              .append('@')
>> -
>>  .append(Integer.toHexString(System.identityHashCode(object)));
>> +        try {
>> +            buffer.append(object.getClass().getName())
>> +                  .append('@')
>> +
>>  .append(Integer.toHexString(System.identityHashCode(object)));
>> +        } catch(java.io.IOException ioe) {
>> +            // can't happen - Appendable API forces it upon us
>> +        }
>>     }
>
> This change is invalid.
>
> IO classes such as Writer implement Appendable, and appending to those can
> throw an IOException.

Yup.  I remember telling myself it was fine because we passed a
StringBuilder in so the IOException wouldn't happen.... 'cept that's
only if someone calls identityToString(Object) :)

I've rolled back the change for now. I don't like IOException being
added to the API. I'm tempted by the somewhat lame solution of a
private Appendable method, and explicitly supporting StringBuffer,
StringBuilder and StrBuilder.

Hen

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Re: svn commit: r826960 - /commons/proper/lang/trunk/src/java/org/apache/commons/lang/ObjectUtils.java

Posted by Stephen Colebourne <sc...@btopenworld.com>.
bayard@apache.org wrote:
> URL: http://svn.apache.org/viewvc?rev=826960&view=rev
> Log:
> Moved identityToString(StringBuffer, Object) to identityToString(Appendable, Object) per LANG-542
> @@ -178,13 +178,17 @@
>       * @param object  the object to create a toString for
>       * @since 2.4
>       */
> -    public static void identityToString(StringBuffer buffer, Object object) {
> +    public static void identityToString(Appendable buffer, Object object) {
>          if (object == null) {
>              throw new NullPointerException("Cannot get the toString of a null identity");
>          }
> -        buffer.append(object.getClass().getName())
> -              .append('@')
> -              .append(Integer.toHexString(System.identityHashCode(object)));
> +        try {
> +            buffer.append(object.getClass().getName())
> +                  .append('@')
> +                  .append(Integer.toHexString(System.identityHashCode(object)));
> +        } catch(java.io.IOException ioe) {
> +            // can't happen - Appendable API forces it upon us
> +        }
>      }

This change is invalid.

IO classes such as Writer implement Appendable, and appending to those 
can throw an IOException.

Stephen

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org