Rajan
Rajan

Reputation: 322

Best programming practice to get a value from a list

I am developing and android application using Java. I need my application to get the phone prefix from Country code.

Say for example, if the country code is US, it should return back prefix as "+1" in case of code IN, it should return back "+91", so on and so forth.

This can be achieved by having a function with if-else block as follows:

String getPrefix(String iso){
    String prefix = "";
    if(iso.equalsIgnoreCase("AD"))          prefix = "376";
    else if(iso.equalsIgnoreCase("AE"))     prefix = "971";
    else if(iso.equalsIgnoreCase("AF"))     prefix = "93";
    else if(iso.equalsIgnoreCase("AG"))     prefix = "268";
    else if(iso.equalsIgnoreCase("AI"))     prefix = "264";
    ....
    ....
    return prefix;
    }

Or we can have a big vector object with all the key value pair, and the prefix can be retrieved by calling the get method on that object.

I need this function to be called once for the program life cycle. Please suggest me the best logic to implement this.

Regards.

Upvotes: 0

Views: 332

Answers (8)

Marcelo
Marcelo

Reputation: 4608

You could use an enum.

public enum Code {
   AD("376"),
   AE("971")
   ...

   private String prefix;

   private Code(String prefix) { this.prefix = prefix; }

   public String getPrefix() { return prefix; }
}

I would imagine you have a quite limited number of prefixes (perhaps a few hundreds), so performance would not be an issue.

As suggested, you can use Code#valueOf() witch will give you a better performance when searching:

//get codes
Code c = Code.valueOf("AD");
// remember to check for null, if getting the code from user input
String prefix = c.getPrefix();

You'll have to benchmark to see if it's much slower then using a HashMap, but it does a lot for readability...

Upvotes: 2

Robᵩ
Robᵩ

Reputation: 168636

I can't speak to the relative speed of different solutions, but using an enum seems more usable and maintainable:

public class EnumTest {
  public enum DialPlan {
    US("+1"),
    AD("+376"),
    AE("+971"),
    AF("+93"),
    AG("+268"),
    AI("+264");

    final String m_code;
    private DialPlan(String code) { m_code = code; }
  }
  public static void main(String[] args) {
    for(String arg : args) {
      System.out.println(arg + ": " + DialPlan.valueOf(arg).m_code);
    }
  }
}

Upvotes: 0

David Grant
David Grant

Reputation: 14223

I would use a distinct type, backed by a static HashMap, e.g.

public class CallingCode {
    private static HashMap<String, CallingCode> callingCodes;

    static {
        callingCodes.put("AD", new CallingCode("AD", 376));
        ...
    }

    private final String country;
    private final int code;

    public CallingCode(String country, int code) {
       this.country = country;
       this.code = code;
    }

    public static CallingCode getInstance(country) {
        if (country == null) {
            return null;
        }
        return callingCodes.get(country.toUpperCase());
    }

    // implement equals / hashCode
}

Each calling code will be created at class load time, so you might wish to adapt to make it instantiate each instance on demand.

Upvotes: 0

&#211;scar L&#243;pez
&#211;scar L&#243;pez

Reputation: 236014

You should use a Map for that, first define an attribute (possibly static) with the prefixes:

 private Map<String, String> prefixes = new HashMap<String, String>();

 // fill the elements in the constructor, or in a static block
 prefixes.put("AD", "376");
 prefixes.put("AE", "971");
 prefixes.put("AF", "93");
 prefixes.put("AG", "268");
 prefixes.put("AI", "264");

Then your getPrefix method becomes an O(1) operation:

 String getPrefix(String iso) {
     return prefixes.get(iso.toUpperCase());
 }

Upvotes: 0

mtsz
mtsz

Reputation: 2855

Use a switch statement with enums, it is the fastest solution.

First you have to convert your iso-string to an enum. And then you switch on the enum and return the right code.

Upvotes: -1

Azodious
Azodious

Reputation: 13872

You can use HashMap collection for this problem:

Country Code becomes Key and it's prefix value becomes its Value

You can put all these name-value pairs in a configuration file (.txt OR xml etc.) and fill HashMap using put(key, value) method. then using get(Key) method it's value can be retrieved.

Upvotes: 1

Matt
Matt

Reputation: 17629

I suggest you store it in a HashMap:

import java.util.HashMap;
import java.util.Map;

class YourActivity {
    // ....

    private Map<String, String> prefixes = new HashMap<String, String>();
    {
        prefixes.put("AD", "376");
        //... and the rest of them
    }

    public String getPrefix(String state) {
       if(prefixes.containsKey(state)) {
           return prefixes.get(state);
       }
       // handle the case for unkown states
       return "";
    }
}

Upvotes: 1

user1096188
user1096188

Reputation: 1839

Not sure if there exists such "programming practice", but there is a data structure just for this occasion. HashMap

Upvotes: 3

Related Questions