2
I have the following class:
package br.com.xti.ouvidoria.helper;
import java.util.Collection;
/**
* @author Samuel Correia Guimarães
*/
public class ValidacaoHelper {
/**
* @param obj
* objeto a ser validado
* @return TRUE se o objeto passado por parâmetro for NULL, VAZIO
* (Collections, Arrays, Strings) ou menor/igual a zero para tipos
* númericos
*/
public static boolean isEmpty(Object obj) {
boolean vazio = false;
if (obj == null) {
vazio = true;
} else {
if (obj instanceof String) {
vazio = ((String) obj).replaceAll("\\s+", " ").trim().isEmpty();
} else if (obj instanceof Number) {
if (((Number) obj).longValue() == 0)
vazio = true;
} else if (obj instanceof Collection<?>) {
Collection<?> col = (Collection<?>) obj;
vazio = (col == null || col.isEmpty());
} else if (obj instanceof Object[]) {
vazio = (((Object[]) obj).length == 0);
}
}
return vazio;
}
/**
* @param objs
* objetos a serem validados
* @return TRUE se algum dos objetos passados por parâmetro for NULL ou
* VAZIO
*/
public static boolean isEmpty(Object... objs) {
boolean isEmpty = false;
for (Object obj : objs) {
isEmpty = isEmpty(obj);
if (isEmpty) {
break;
}
}
return isEmpty;
}
/**
* @param obj
* objeto a ser validado
* @return TRUE se o objeto passado por parâmetro for diferente de NULL,
* VAZIO (Collections, Arrays, Strings) e maior que zero para tipos
* númericos
*/
public static boolean isNotEmpty(Object obj) {
return !isEmpty(obj);
}
I wanted to add to it the following function:
public static boolean saoIguais(Integer obj, Integer obj2) {
boolean iguais = false;
if (obj == obj2) {
iguais = true;
}
return iguais;
}
But how is the system nor compiles!
If I put as Object instead of Integer it compiles:
public static boolean saoIguais(Object obj, Object obj2) {
boolean iguais = false;
if (obj == obj2) {
iguais = true;
}
return iguais;
}
But in that case the comparison never returns as true.
What error and compilation it produces?
– Victor Stafusa
Because you don’t use the method
Object.equals(Object, Object)
which is already ready, is already in the standard library and already does what you want?– Victor Stafusa
@Victorstafusa I was mistaken in my data entry so I thought something was wrong
– Mateus
But the main of my question still not understood that is the pq using the
(Integer obj, Integer obj2)
the system does not compile– Mateus
And what is the build error in this case?
– Victor Stafusa