What methods should go into a java.util.Objects class in JDK 7?

David M. Lloyd david.lloyd at redhat.com
Wed Sep 16 15:29:08 UTC 2009


On 09/16/2009 10:20 AM, Joel Kamentz wrote:
> It's more for I/O, not general Object, but some methods which I almost ALWAYS use in a project are null-safe canonicalization and null-safe close methods.

Yeah I think that these would belong in an I/O package.  That said...

>     static File canonical(File f) {
>         if (f == null) return null;
>         try { return f.getCanonicalFile(); }
>         catch (IOException e) { return f.getAbsoluteFile(); }
>     }

No comment, other than to check out the NIO list archives and/or the 
current source tree which may already allow you to do this.

>     static void close(Reader s) {
>         if (s == null) return;
>         try { s.close(); }
>         catch (IOException e) {}
>     }

These would be better written as a single "close(Closeable c)".  Also, 
swallowing the exception is the wrong thing to do; at the very least it 
should be logged.

>     ... repeat for other stream types as necessary, like ZipFile, RandomAccessFile, etc. because no common inheritance

These *should* all be fixed now to implement Closeable IIRC.

>         Delete files or sub-trees, catching exceptions and instead just return success / failure.
> 
>         Copy an InputStream to an OutputStream with supplied byte buffer (or allocate one internally).  General file (or stream or ?) copying utility methods might be useful.

Definitely in the I/O realm, ask Alan on the NIO list.  The delete thing 
might already be done :)

- DML



More information about the core-libs-dev mailing list