java - generics method for closing resource files -
i have cursors, inputstreams, outputstreams instead of doing .close() each one, call method on them closes them.
i tried :
private static void closeresource ( object<t> item ) { try { if ( item != null ) { item.close(); } } catch ( exception e ) { throw new runtimeexception( e ); } }
it doesn't work.. object not generic.
object
not generic type can't useobject<t>
.- the
object
class has noclose
method defined in it.
the generics-oriented solution define generic method type parameter bound closeable
interface declares close
method:
private static <t extends closeable> void closeresource ( t item ) { try { if ( item != null ) { item.close(); } } catch ( exception e ) { throw new runtimeexception( e ); } }
Comments
Post a Comment