14
\$\begingroup\$
String fileId = "CPP420.A2_2012072416244467745";        
String reg1 = "CPP350";        
String reg2 = "CPP420.A1";        
String reg3 = "CPP420.A2";        
String country = "";        
if(fileId.startsWith(reg1)) {        
    country = "CA";         
} else if(fileId.startsWith(reg2)) {        
    country = "US";         
} else if(fileId.startsWith(reg3)) {        
    country = "FR";        
} else {        
    System.out.println("Error invalid file");        
}  

OR

String country = fileId.startsWith(reg1) ? "CA" : "";
       country = fileId.startsWith(reg2) ? "US" : "";
       country = fileId.startsWith(reg3) ? "FR" : "";

if("".equals(country)) {
System.out.println("No country found");
}
\$\endgroup\$
2
  • 1
    \$\begingroup\$ If "CA" is 'California' (the US State), you have a bigger problem - you're mixing your domains. \$\endgroup\$ Aug 24, 2012 at 23:16
  • 1
    \$\begingroup\$ Country CA = Canada \$\endgroup\$ Sep 20, 2012 at 21:25

4 Answers 4

18
\$\begingroup\$

Neither. Note: haven't tried this in and IDE, but you should be able to get the idea.

interface RegionID {
    boolean isRegionMatch(String fileID);
    String getRegionCode();
}

class SimpleMatchRegionID implements RegionID {
    private String regionPrefix;
    private String regionCode;

    public SimpleMatchRegionID(String regionPrefix, String regionCode) {
        this.regionPrefix = regionPrefix;
        this.regionCode = regionCode;
    }

    public boolean isRegionMatch(String fileID) {
        return fileID.startsWith(regeionPrefix);
    }

    public String getRegionCode() {
        return regionCode;
    }
}

RegionID us = new SimpleMatchRegionID("CPP420.A1", "US");
RegionID ca = new SimpleMatchRegionID("CPP350", "CA");
RegionID fr = new SimpleMatchRegionID("CPP350", "FR");
RegionID[] regions = {us, ca, fr};

public String findRegionCode(String fileID) {
    for (i = 0; i < regions.length; i++) {
        if (regions[i].isRegionMatch(fileID)) {
            return regions[i].getRegionCode();
        }
    }

    return "";
}

An implementation like this would allow you to load the regions array from a data file, or with some other run-time mechanism. It would also allow you to implement other strategies for recognizing a region that doesn't follow the same basic pattern.

\$\endgroup\$
13
\$\begingroup\$

You can improve the code even without a separate class in the case that you don't need other detection mechanisms. The following will allow you to easily add new prefixes but nothing more.

String fileId = "CPP420.A2_2012072416244467745";        
Map<String,String> countriesByPrefix = new HashMap<>() {{
    put("CPP350", "CA");
    put("CPP420.A1", "US");
    put("CPP420.A2", "FR");
}};
String country = null;
for (Map.Entry<String,String> entry : countriesByPrefix.entrySet()) {
    if (fileId.startsWith(entry.getKey()) {
        country = entry.getValue();
        break;
    }
}
if (country == null) {
    System.out.println("Error invalid file");
}
\$\endgroup\$
11
\$\begingroup\$

If you can use Java 7, you can put Strings in switch statements (this came with JSR334):

String fileId = "CPP420.A2_2012072416244467745";        
String country = "";  
String fileReg = fileId.split("_")[0];      
switch(fileReg) {
  case "CPP350" : country = "CA"; break;   
  case "CPP420.A1" : country = "US"; break;
  case "CPP420.A2" : country = "FR"; break;
  default : System.out.println("Error invalid file");    
}       
\$\endgroup\$
1
7
\$\begingroup\$

I already see some good/better solutions here but what I am missing is an example which uses the famous(?) java ENUM's

public enum Country {
    CA, US, FR;
}

public enum FileType {
    CPP350("CPP350", Country.CA),
    CPP420_A1("CPP420.A1", Country.US),
    CPP420_A2("CPP420.A2", Country.FR);

    private final String fileId;
    private final Country country;

    private FileType(final String fileId , final Country country) {
        this.fileId = fileId;
        this.country = country;
    }

    public Country getCountry() {
        return country;
    }

    public static FileType match( final String fileId ) {
        for ( final FileType fileType : values() ) {
            if ( fileId.startsWith( fileType.fileId ) ) {
                return fileType;
            }
        }
        return null;
    }
}

And the below code should help you to get your desired country for the given fileId.

final Country country = FileType.match( fileId ).getCountry();

Note: As you can see that there is a possibility for NullPointerException if no match found. But I believe it should be handled as part of the implementation.

\$\endgroup\$
1
  • \$\begingroup\$ +1 The more secure way if you add : XX as "unkoown contry" and return Contry.XX instead of null - Implementation can test if((Contry) return == Contry.XX) {.. - - - change also your enum in an Enum Class docs.oracle.com/javase/6/docs/api/java/lang/Enum.html ; see also samples in /Effective%20Java%20-%20Programming%20Language%20Guide.pdf) \$\endgroup\$
    – cl-r
    Aug 27, 2012 at 9:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.