Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save RicardoRFaria/8812e478a4fd3a3d8f5ba82a3b4a5cc0 to your computer and use it in GitHub Desktop.
Save RicardoRFaria/8812e478a4fd3a3d8f5ba82a3b4a5cc0 to your computer and use it in GitHub Desktop.
SetUtil
import java.util.function.Consumer;
import java.util.function.Supplier;
import org.apache.commons.lang3.StringUtils;
/**
* Classe responsavel por fornecer metodos utilitarios para transferencia do
* valor de um objeto para outro
*
* @author Ricardo Faria
*
*/
public final class SetUtil {
private SetUtil() {
// Classe utilitaria...
}
/**
* Obtém o valor do método get e caso ele nao seja nulo transfere para o método set de destino
*
* @param getMethod o método get da origem
* @param setMethod o método set do destino
* @return {@code true} caso seja transferido o valor, {@code false} caso contrário.
*/
public static <T> boolean seNaoForNuloSet(Supplier<T> getMethod, Consumer<T> setMethod) {
if (getMethod.get() != null) {
setMethod.accept(getMethod.get());
return true;
}
return false;
}
/**
* Obt&eacute;m a string do m&eacute;todo get e caso ela nao seja nem nula e nem vazia transfere para o
* m&eacute;todo set de destino.
*
* @param getMethod o m&eacute;todo get da origem
* @param setMethod o m&eacute;todo set do destino
* @return {@code true} caso seja transferido o valor, {@code false} caso contr&aacute;rio.
*/
public static boolean seStringNaoForNulaSet(Supplier<String> getMethod, Consumer<String> setMethod) {
if (StringUtils.isNotEmpty(getMethod.get())) {
setMethod.accept(getMethod.get());
return true;
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment