Approach with Map
Since your purpose is to set a different color for each region, then:
private static final Map<String, String> regioes = new HashMap<>(27);
private static final Map<String, Color> cores = new HashMap<>(27);
static {
String[] norte = {"AM", "AP", "AC", "RO", "RR", "PA", "TO"};
String[] sul = {"PR", "SC", "RS"};
String[] sudeste = {"SP", "MG", "RJ", "ES"};
String[] nordeste = {"BA", "SE", "AL", "PE", "PB", "RN", "CE", "PI", "MA"};
String[] centroOeste = {"MS", "MT", "GO", "DF"};
for (String s : norte) regioes.put(s, "norte");
for (String s : sul) regioes.put(s, "sul");
for (String s : sudeste ) regioes.put(s, "sudeste");
for (String s : nordeste ) regioes.put(s, "nordeste");
for (String s : centroOeste ) regioes.put(s, "centro-oeste");
cores.put("norte", Color.RED);
cores.put("sul", Color.GREEN);
cores.put("centro-oeste", Color.YELLOW);
cores.put("nordeste", Color.BLUE);
cores.put("sudeste", Color.ORANGE);
}
private static Color corDoEstado(String sigla) {
String regiao = regioes.get(sigla);
return cores.get(regiao);
}
And then in your code, you just have to do this:
algumaCoisa.setCor(corDoEstado(estado));
The advantages of this above approach is that:
- Mapping is built only once in class loading.
- The mapping is reusable elsewhere, and you get rid of having to repeat this
switch
appalling whenever dealing with states.
Object-oriented approach
However, this approach still has its problems. The main problem is that this is all about string-oriented programming, by treating these things as strings and not as objects that should be.
So the ideal is to do something like this:
package com.example;
import java.awt.Color;
public enum RegiaoBrasileira {
SUL(Color.GREEN),
SUDESTE(Color.ORANGE),
CENTRO_OESTE(Color.YELLOW),
NORTE(Color.RED),
NORDESTE(Color.BLUE);
private final Color cor;
private RegiaoBrasileira(Color cor) {
this.cor = cor;
}
public Color getCor() {
return cor;
}
public static List<EstadoBrasileiro> getEstados() {
return LocalizaEstados.porRegiao(this);
}
}
package com.example;
import java.util.Locale;
import static com.example.RegiaoBrasileira.*;
import com.example.util.StringUtils;
public enum EstadoBrasileiro {
RIO_GRANDE_DO_SUL("RS", SUL),
SANTA_CATARINA("SC", SUL),
PARANÁ("PR", SUL),
SÃO_PAULO("SP", SUDESTE),
RIO_DE_JANEIRO("RJ", SUDESTE),
MINAS_GERAIS("MG", SUDESTE),
ESPÍRITO_SANTO("ES", SUDESTE),
BAHIA("BA", NORDESTE),
SERGIPE("SE", NORDESTE),
ALAGOAS("AL", NORDESTE),
PERNAMBUCO("PE", NORDESTE),
PARAÍBA("PB", NORDESTE),
RIO_GRANDE_DO_NORTE("RN", NORDESTE),
CEARÁ("CE", NORDESTE),
PIAUÍ("PI", NORDESTE),
MARANHÃO("MA", NORDESTE),
PARÁ("PA", NORTE),
AMAPÁ("AP", NORTE),
ACRE("AC", NORTE),
AMAZONAS("AM", NORTE),
RONDÔNIA("RO", NORTE),
RORAIMA("RR", NORTE),
TOCANTINS("TO", NORTE),
MATO_GROSSO("MT", CENTRO_OESTE),
MATO_GROSSO_DO_SUL("MS", CENTRO_OESTE),
GOIÁS("GO", CENTRO_OESTE),
DISTRITO_FEDERAL("DF", CENTRO_OESTE),
private final String nome;
private final String sigla;
private final RegiaoBrasileira regiao;
private EstadoBrasileiro(String sigla, RegiaoBrasileira regiao) {
this.sigla = sigla;
this.regiao = regiao;
this.nome = StringUtils.toTitleCase(name().toLowerCase(Locale.ROOT).replace("_", " "));
}
public String getNome() {
return nome;
}
public String getSigla() {
return sigla;
}
public RegiaoBrasileira getRegiao() {
return regiao;
}
}
package com.example;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
class LocalizaEstados {
private static final Map<String, EstadoBrasileiro> SIGLAS_POR_ESTADO = new HashMap<>(27);
private static final Map<RegiaoBrasileira, List<EstadoBrasileiro>> ESTADO_POR_REGIAO = new HashMap<>(27);
static {
for (RegiaoBrasileira s : RegiaoBrasileira.values()) {
ESTADO_POR_REGIAO.put(s, new ArrayList<>(9));
}
for (EstadoBrasileiro s : EstadoBrasileiro.values()) {
SIGLAS_POR_ESTADO.put(s.sigla, s);
ESTADO_POR_REGIAO.get(s.getRegiao()).add(s);
}
for (RegiaoBrasileira s : RegiaoBrasileira.values()) {
ESTADO_POR_REGIAO.put(s, Collections.unmodifiableList(ESTADO_POR_REGIAO.get(s));
}
}
private LocalizaEstados() {}
static EstadoBrasileiro porSigla(String sigla) {
return SIGLAS_POR_ESTADO.get(sigla);
}
static List<EstadoBrasileiro> porRegiao(RegiaoBrasileira regiao) {
return ESTADO_POR_REGIAO.get(regiao);
}
}
package com.example.util;
class StringUtils {
// Fonte: https://stackoverflow.com/a/1086134/540552
public static String toTitleCase(String input) {
StringBuilder titleCase = new StringBuilder();
boolean nextTitleCase = true;
for (char c : input.toCharArray()) {
if (Character.isSpaceChar(c)) {
nextTitleCase = true;
} else if (nextTitleCase) {
c = Character.toTitleCase(c);
nextTitleCase = false;
}
titleCase.append(c);
}
return titleCase.toString();
}
}
It may seem like a great job, but:
You separate the concepts of Brazilian states and regions from other parts of the code.
All this is reusable.
If you need to change concepts of Brazilian states and regions (for example, add the concept of state capitals), it is not difficult to change the Enum.
Concepts of states, Brazilian regions and corresponding colors no longer pollute other parts of the code.
Completion
Use the switch
is an awful thing. It tends to pollute the code leaving it confused and difficult to maintain. Polymorphism and proper data tabulation in suitable data structures are better alternatives to it in most cases.
It’s true that this all seems more complicated than using one switch
. The problem is that you rarely end up using only one and yes you end up using two, three, five, twenty times those switch
es scattered in a lot of code places.
So stick to the case of XY problem.
So né... haha ai depends, I find it aesthetically ugly hehe so in my case I prefer to use if and even comparisons, but it goes of the taste of each one =)
– Dev
Usually the
switch
is a bad choice. But to know what would be best in your case, you would have to say what you want to do with those states.– Victor Stafusa
set a different color for each region. That’s all.
– Aline
It’s not really an answer, but it seems to me you’re modeling regions implicitly through your switch statement. An alternative to its solution would be to include the regions in the model (e. g.,
enum Regiao
withNORDESTE
,SUDESTE
, etc). It seems to me cleaner to treat 5 regions than oneswitch
with all states + DF.– Anthony Accioly
@Matheus but in question of many validations, use
if
would not make the execution slower than the functionswitch
? Theswitch
has the possibility to brake (break) the code if the value corresponds, already theif
would cause unnecessary processing as if I found what I needed I would not need to continue processing. Orif
also has a break internally?– Tiago Boeing
For simple cases you can use that approach is valid for any language with some adaptations. For more complex uses see the other answers ;)
– rray
In programming, as in any other aspect of life, one thing is only wrong if there is a reason why it is wrong. Good BTW question.
– Oralista de Sistemas
@Tiagoboeing yes this is true, so I quoted that it goes from the taste of each =), if we use Else if we will also stop going through unnecessary codes
– Dev