Avoiding multiple If statements in Java

后端 未结 13 784
北恋
北恋 2020-12-30 07:52

I\'ve coded a method something like this. But I guess this should undergo refactoring. Can any one suggest the best approach to avoid using this multiple if statements?

相关标签:
13条回答
  • 2020-12-30 08:12

    You can use a Map to hold your solutions:

    Map<String,String> extensionToMimeType = new HashMap<String,String>();
    extensionToMimeType.put("pdf", "application/pdf");
    extensionToMimeType.put("doc", "application/msword");
    // and the rest
    
    int lastDot = fileName.lastIndexOf(".");
    String mimeType;
    if (lastDot == -1) {
        mimeType = NO_EXTENSION_MIME_TYPE;
    } else {
        String extension = fileName.substring(lastDot+1);
        mimeType = extensionToMimeType.getOrDefault(extension, 
                                                    UNKNOWN_EXTENSION_MIME_TYPE);
    }
    

    For this code to work you'll need to have defined NO_EXTENSION_MIME_TYPE and UNKNOWN_EXTENSION_MIME_TYPE as in your class, somewhat like this:

    private static final String NO_EXTENSION_MIME_TYPE = "application/octet-stream";
    private static final String UNKNOWN_EXTENSION_MIME_TYPE = "text/plain";
    
    0 讨论(0)
  • 2020-12-30 08:13

    Personally I don't have problems with the if statements. The code is readable, it took just milliseconds to understand what you're doing. It's a private method anyway and if the list of mime types is static then there's no urgent need to move the mapping to a properties file and use a lookup table (map). Map would reduce lines of code, but to understand the code, then you're forced to read the code and the implementation of the mapping - either a static initializer or an external file.

    You could change the code a bit and use an enum:

    private enum FileExtension { NONE, DEFAULT, PDF, DOC, XLS /* ... */ }
    
    private String getMimeType(String fileName){
      String mimeType = null;
    
      FileExtension fileNameExtension = getFileNameExtension(fileName);
    
      switch(fileNameExtension) {
        case NONE:
          return "";
        case PDF:
          return "application/pdf";
    
        // ...
    
        case DEFAULT:
          return "txt/plain";   
      }
    
      throw new RuntimeException("Unhandled FileExtension detected");
    } 
    

    The getFileNameExtension(String fileName) method will just return the fitting enum value for the fileName, FileExtension.NONE if fileName is empty (or null?) and FileExtension.DEFAULT if the file extension is not mapped to a mime type.

    0 讨论(0)
  • 2020-12-30 08:16

    I would do this by putting the associations in a map, and then using the map for lookup:

    Map<String, String> map = new HashMap<String, String>();
    
    map.put(".pdf", "application/pdf");
    map.put(".doc", "application/msword");
    // ... etc.
    
    // For lookup:
    private String getMimeType(String fileName) {
        if (fileName == null || fileName.length() < 4) {
            return null;
        }
    
        return map.get(fileName.substring(fileName.length() - 4));
    }
    

    Note that using the switch statements on strings is one of the proposed new features for the next version of Java; see this page for more details and an example of how that would look in Java 7:

    switch (fileName.substring(fileName.length() - 4)) {
        case ".pdf": return "application/pdf";
        case ".doc": return "application/msword";
        // ...
        default: return null;
    

    (edit: My solution assumes the file extension is always 3 letters; you'd have to change it slightly if it can be longer or shorter).

    0 讨论(0)
  • 2020-12-30 08:16

    Create an enum called MimeType with 2 String variables: extension and type. Create an appropriate constructor and pass in the ".xxx" and the "application/xxx" values. Create a method to do the lookup. You can use enums in switch.

    0 讨论(0)
  • 2020-12-30 08:21

    I consider your approach to be the best overall. This comes after having tested with a number of different approaches myself.

    I see a number of huge benefits in your current approach, namely:

    1. Easily readable and understandable by anyone (in my experience, medium-level programmers often underestimate this and usually prefer going with fancy-patterns which, in the end are not readable at all for the vast majority of programmers who do not know that specific pattern)
    2. All the information is in one single place. As Andreas_D pointed out, hunting around files or classes is not a good option for someone that needs to fix a bug while you are on holiday!
    3. Easily maintainable: I could "F3" (if you are Eclipse-ing) on the method and add a new content type in seconds without any worries of introducing bugs!

    I can suggest a few things anyway:

    1. This method is very general purpose: Why should it be private?! This is a public method of some utility/helper class! Moreover it should be a static method!! You don't need anything from the Object itself to perform your job!
    2. You could use indenting to make things prettier and compact. I know that indenting is some kind of religion for the most of us, but I think it should not be a strict rule; it should be properly used to make our code more readable and compact. If this would be a config file you would probably have something like:
    pdf=application/pdf
    doc=application/msword
    

    You could have a very similar result with:

        public static String getMimeType(String fileName){
           if(fileName == null) return "";
           if(fileName.endsWith(".pdf")) return "application/pdf";
           if(fileName.endsWith(".doc")) return "application/msword";
           if(fileName.endsWith(".xls")) return "application/vnd.ms-excel"; 
           return "txt/plain"; 
       }
    

    This is also what a lot of the Map based implementations look like.

    0 讨论(0)
  • 2020-12-30 08:25

    what about using a MIME detection library instead?

    • mime-util
    • mime4j
    • JMimeMagic library - Free. Uses file extension and magic headers to determine MIME type.
    • mime-util - Free. Uses file extension and magic headers to determine MIME type.
    • DROID (Digital Record Object Identification) - Free. Uses batch automation to detect MIME types.
    • Aperture Framework - Free. A framework for crawling external sources to identify MIME types.

    (feel free to add more, there so many libraries..)

    0 讨论(0)
提交回复
热议问题