directory-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Emmanuel Lecharny <elecha...@gmail.com>
Subject Re: How strict must we check in util methods
Date Fri, 04 Jun 2010 11:51:16 GMT
On 6/4/10 11:28 AM, Felix Knecht wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Do we leave it up to third party developers to take care that only
> correct values are passed or do we have to take care that otherwise an
> exception is thrown? To give an example from shared AttributUtils:
> We never check if the object to clone is a String, we just presume that
> any other developer using this class respects the convention given by
> javadoc. Can we do so or should an exception be thrown in such cases?
>
> This question will appear for sure for other methods as well.
>
>     /**
>       * Clone the value. An attribute value is supposed to be either a String
>       * or a byte array. If it's a String, then we just return it ( as String
>       * is immutable, we don't need to copy it). If it's a bu=yte array, we
>       * create a new byte array and copy the bytes into it.
>       *
>       * @param value The value to clone
>       * @return The cloned value
>       */
>      public static Object cloneValue( Object value )
>      {
>          // First copy the value
>          Object newValue = null;
>
>          if ( value instanceof byte[] )
>          {
>              newValue = ( ( byte[] ) value ).clone();
>          }
>          else
>          {
>              newValue = value;
>          }
>
>          return newValue;
>      }
>    
Hi Felix,

I think I replied by sending another mail (I wrote it 2 hours ago while 
I ddn't have internet connection ...)

I think that this is exacty what 'assert' is good for :

     public static Object cloneValue( Object value )
     {
         assert ( value != null );
         assert ( ( value instanceof String ) || ( value instanceof byte[] ) )

         // First copy the value
         Object newValue = null;

           ...

-- 
Regards,
Cordialement,
Emmanuel L├ęcharny
www.nextury.com



Mime
View raw message