I'd like to refactor a Java method I've created, making it cleaner and more beautiful. The method concatenates some strings, which may be empty or not. I'm new on java so i'm not sure how to do that, especially because between the strings I may have separators.
Here is my method:
private String createString(String modeloVotacao, String tipoProposicao, String ordemNumero, String ordemAno) {
String modeloDescricao = "";
if(modeloVotacao != null) {
modeloDescricao = modeloVotacao + " - ";
}
if(tipoProposicao != null) {
modeloDescricao += tipoProposicao + " ";
}
modeloDescricao += ordemNumero != null?ordemNumero:"";
if(ordemNumero != null && ordemAno != null) {
modeloDescricao += "/"+ordemAno;
} else {
if(ordemAno != null) {
modeloDescricao += ordemAno;
}
}
return modeloDescricao;
}
Here is some results that this method returns:
- All filled: 1ª DISCUSSÃO - PROJETO DE LEI COMPLEMENTAR Nº 16/2018
- modeloVotacao = null: PROJETO DE LEI COMPLEMENTAR Nº 16/2018
- tipoProposicao = null: 1ª DISCUSSÃO - Nº 16/2018
- ordemNumero = null: 1ª DISCUSSÃO - PROJETO DE LEI COMPLEMENTAR 2018
- ordemAno = null: 1ª DISCUSSÃO - PROJETO DE LEI COMPLEMENTAR Nº 16
- all null: empty string
- modeloVotacao and tipoProposicao = null: Nº 16/2018
- ordemNumero and ordemAno = null: 1ª DISCUSSÃO - PROJETO DE LEI COMPLEMENTAR