Search code examples
javaandroidoptimizationcode-duplication

How do I reduce this code duplication


I'm stuck on how I can reduce this code duplication, I'm using the TextToSpeech engine and using locales so the user can select their language.

language is a Spinner.

language.setOnItemSelectedListener(new OnItemSelectedListener() {

    public void onItemSelected(AdapterView<?> parent, View arg1,
        int pos, long id) {
    System.out.println(parent.getItemAtPosition(pos).toString());
    if (parent.getItemAtPosition(pos).toString().equals("UK")) {
        textToSpeech = new TextToSpeech(MainActivity.this,
            new TextToSpeech.OnInitListener() {

            @Override
            public void onInit(int status) {
                if (status != TextToSpeech.ERROR) {
                textToSpeech.setLanguage(Locale.UK);
                }
            }
            });
    } else if (parent.getItemAtPosition(pos).toString()
        .equals("US")) {
        textToSpeech = new TextToSpeech(MainActivity.this,
            new TextToSpeech.OnInitListener() {

            @Override
            public void onInit(int status) {
                if (status != TextToSpeech.ERROR) {
                textToSpeech.setLanguage(Locale.US);
                }
            }
            });

    } else if (parent.getItemAtPosition(pos).toString()
        .equals("French")) {
        textToSpeech = new TextToSpeech(MainActivity.this,
            new TextToSpeech.OnInitListener() {

            @Override
            public void onInit(int status) {
                if (status != TextToSpeech.ERROR) {
                textToSpeech.setLanguage(Locale.FRANCE);
                }
            }
            });

    } else if (parent.getItemAtPosition(pos).toString()
        .equals("Italian")) {
        textToSpeech = new TextToSpeech(MainActivity.this,
            new TextToSpeech.OnInitListener() {

            @Override
            public void onInit(int status) {
                if (status != TextToSpeech.ERROR) {
                textToSpeech
                    .setLanguage(Locale.ITALIAN);
                }
            }
            });

    } else if (parent.getItemAtPosition(pos).toString()
        .equals("German")) {
        textToSpeech = new TextToSpeech(MainActivity.this,
            new TextToSpeech.OnInitListener() {

            @Override
            public void onInit(int status) {
                if (status != TextToSpeech.ERROR) {
                textToSpeech
                    .setLanguage(Locale.GERMAN);
                }
            }
            });

    }

    }

    public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub

    }
});
}

Solution

  • You can create a Map.

    private static final Map<String, Locale> LOCALES = new LinkedHashMap<String, Locale>() {{
       put("US", Locale.US);
       // many more
    }
    
    
    final Locale locale = LOCALES.get(parent.getItemAtPosition(pos).toString());
    if(locale != null)
        textToSpeech = new TextToSpeech(MainActivity.this,
            new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                if (status != TextToSpeech.ERROR) 
                   textToSpeech.setLanguage(locale);
            }
        });