Last active
August 29, 2015 14:10
-
-
Save rafarocha/19c834b4199ebf797408 to your computer and use it in GitHub Desktop.
Example that use FluentIterable of Guava
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
- aquela velha história: | |
"quero iterar uma lista, pegar apenas os itens válidos, | |
transformá-los então em um novo tipo e depois entregar o resultado" | |
*/ | |
enum ParameterTal { | |
nome; // imagine exemplo real com quase 20 parametros | |
public String getValue(Entity entity) { // a medida que mais parametros existem este método cresce | |
switch ( this ) { | |
case nome: return entity.getNome(); | |
default : throws new IllegalStateException(); | |
} | |
} | |
public Iterable<HttpParameter> asHttpParameter(final Entity entity) { | |
return FluentIterable | |
.from( newArrayList(values()) ) | |
.filter( new PredicateTal(entity) ) | |
.transform( new Function<ParameterTal, HttpParameter>() { | |
@Override public HttpParameter apply(ParameterTal parameter) { | |
return asHttpParameter(entity); | |
} | |
}); | |
} | |
public HttpParameter asHttpParameter(Entity entity) { | |
return new HttpParameter(this.name(), this.getValue(entity) ); | |
} | |
private class PredicateTal implements Predicate<ParameterTal> { | |
public Entity entity; | |
public PredicateAlias(Entity entity) { | |
this.entity = entity; | |
} | |
@Override public boolean apply(ParameterTal parameter) { | |
String value = parameter.getValue(entity); | |
return isEmpty( strip(value) ); | |
} | |
} | |
} |
na verdade, quando vc faz o simples e tenta melhorar acaba complicando. o problema é que o simples possuia apenas 2 if's e 1 for
respondendo as perguntas. o código acima não justificaria acima caso fosse utilizado apenas em uma localidade.
portanto a necessidade de refatorar este código, a esta forma, é que existirão várias outras classes que pertencem a um parâmetro, então eu precisaria converter vários valores mapeados por um ParameterTal em Iterable.
estou atualizando o código para um exemplo concreto
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
a pergunta é: você acha complexo ao ponto de incomodar, sem levar em consideração a curva de aprendizado?