orange
orange

Reputation: 5405

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

    }
});
}

Upvotes: 1

Views: 223

Answers (5)

vincenzo iafelice
vincenzo iafelice

Reputation: 270

public class TextToSpeechFactory {

private static final Map<String, Locale> LOCALES = new HashMap<String, Locale>() {{
       put("US", Locale.US);
       // many more
    }
};

public static TextToSpeech createInstance(String language){
    Locale l = LOCALES.get(language);
    if(l == null)
        throw new Exception("Languange "+ language + "is not valid!");
    else{
        return new TextToSpeech(MainActivity.this,
            new TextToSpeech.OnInitListener() {

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

}

Upvotes: 1

user902383
user902383

Reputation: 8640

from top of my head, make a Map<String,Locale> where key will be name of country and value will be locale
then just do

textToSpeech = new TextToSpeech(MainActivity.this,
    new TextToSpeech.OnInitListener() {

    @Override
    public void onInit(int status) {
        if (status != TextToSpeech.ERROR) {
            textToSpeech
              .setLanguage(localeMap.get(parent.getItemAtPosition(pos).toString()));
        }
    }
});

Upvotes: 1

Peter Lawrey
Peter Lawrey

Reputation: 533930

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);
        }
    });

Upvotes: 3

FIG-GHD742
FIG-GHD742

Reputation: 2486

Try to use a enum as

public enum LANGLIST {
   UK("uk", Locale.UK),
   UK("swe", Locale.SWEIDHS);

   public String lang;
   public Locale loc;
   private LANGLIST(String lang, Locale loc) {
      this.lang = lang;
      this.loc = loc;
   }
}

And then loop through all elements in the enum.

Upvotes: 0

Heinzi
Heinzi

Reputation: 172478

Extract creation of the TextToSpeech object into a separate function:

private TextToSpeech createTextToSpeech(final Locale loc) {
    return new TextToSpeech(MainActivity.this,  
        new TextToSpeech.OnInitListener() {  

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

Note that the argument loc must be declared final so that it can be used inside the anonymous class.

Usage:

...
} else if (parent.getItemAtPosition(pos).toString().equals("French")) {   
    textToSpeech = createTextToSpeech(Locale.FRANCE);
} ...

Upvotes: 5

Related Questions