Adapting methods which return true/false

Posted by James P. on Stack Overflow See other posts from Stack Overflow or by James P.
Published on 2010-04-04T21:36:09Z Indexed on 2010/04/04 21:43 UTC
Read the original article Hit count: 209

Filed under:
|
|
|

What's the best practise when adapting C-style functions which return a true/false to Java?

Here's a simple method to illustrate where the problem lies.

public static boolean fileNameEndsWithExtension( String filename, String fileExtension)  { 
    return filename.endsWith( fileExtension );
}

Note that there's probably a more elegant way of filtering files (feel free to comment on this). Anyway, if filename is a null value, does one:

  1. Return a false if filename is null? If so, how does one go about distinguishing between the case where filename is null and the case where the String or file name doesn't end with a given file extension?
  2. Change the return type to the wrapper class Boolean which allows a null value.
  3. Throw an Exception and force the programmer to make sure that a null value is never passed to the method?
  4. Use another solution?

© Stack Overflow or respective owner

Related posts about java

Related posts about true