Example usage for com.google.common.primitives Primitives isWrapperType

List of usage examples for com.google.common.primitives Primitives isWrapperType

Introduction

In this page you can find the example usage for com.google.common.primitives Primitives isWrapperType.

Prototype

public static boolean isWrapperType(Class<?> type) 

Source Link

Document

Returns true if type is one of the nine primitive-wrapper types, such as Integer .

Usage

From source file:com.b2international.commons.CompareUtils.java

public static boolean isEmpty(final Object object) {

    if (null == object) {
        return true;
    }//www  .j a  v  a 2  s  . c o  m

    if (Primitives.isWrapperType(object.getClass())) {
        return false;
    }

    if (object instanceof Enum<?>) {
        return false;
    }

    if (object instanceof String) {
        return StringUtils.isEmpty((String) object);
    }

    if (object instanceof Collection) {
        return ((Collection<?>) object).size() == 0;
    }

    if (object instanceof Iterable) {
        return !((Iterable<?>) object).iterator().hasNext();
    }

    if (object instanceof Iterator<?>) {
        return !((Iterator<?>) object).hasNext();
    }

    if (object instanceof Map) {
        return ((Map<?, ?>) object).size() == 0;
    }

    if (object instanceof Multimap) {
        return ((Multimap<?, ?>) object).size() == 0;
    }

    if (object.getClass().isArray()) {
        return Array.getLength(object) == 0;
    }

    if (object instanceof BitSet) {
        return ((BitSet) object).isEmpty();
    }

    if (object instanceof Pair<?, ?>) {
        return null == ((Pair<?, ?>) object).getA() || null == ((Pair<?, ?>) object).getB();
    }

    if (object instanceof Stack) {
        return ((Stack<?>) object).isEmpty();
    }

    if (object instanceof PrimitiveCollection) {
        return ((PrimitiveCollection) object).isEmpty();
    }

    throw new IllegalArgumentException("Don't know how to check emptiness of " + object.getClass());
}

From source file:org.jooby.internal.reqparam.BeanParamInjector.java

public static Object createAndInject(final Request req, final Class<?> beanType) throws Exception {
    if (beanType.isPrimitive() || Primitives.isWrapperType(beanType)
            || CharSequence.class.isAssignableFrom(beanType)) {
        throw new Err(Status.BAD_REQUEST);
    }/*from   w  w w .  j a  va  2  s .  co m*/
    if (beanType.isInterface()) {
        return newBeanInterface(req, beanType);
    }
    return newBean(req, beanType);
}

From source file:com.microsoft.rest.Validator.java

/**
 * Validates a user provided required parameter to be not null.
 * An {@link IllegalArgumentException} is thrown if a property fails the validation.
 *
 * @param parameter the parameter to validate
 * @throws IllegalArgumentException thrown when the Validator determines the argument is invalid
 *///from   ww  w.  j a v a  2  s.com
public static void validate(Object parameter) throws IllegalArgumentException {
    // Validation of top level payload is done outside
    if (parameter == null) {
        return;
    }

    Class parameterType = parameter.getClass();
    TypeToken<?> parameterToken = TypeToken.of(parameterType);
    if (Primitives.isWrapperType(parameterType)) {
        parameterToken = parameterToken.unwrap();
    }
    if (parameterToken.isPrimitive() || parameterType.isEnum()
            || parameterToken.isAssignableFrom(LocalDate.class)
            || parameterToken.isAssignableFrom(DateTime.class) || parameterToken.isAssignableFrom(String.class)
            || parameterToken.isAssignableFrom(DateTimeRfc1123.class)
            || parameterToken.isAssignableFrom(Period.class)) {
        return;
    }

    for (Class<?> c : parameterToken.getTypes().classes().rawTypes()) {
        // Ignore checks for Object type.
        if (c.isAssignableFrom(Object.class)) {
            continue;
        }
        for (Field field : c.getDeclaredFields()) {
            field.setAccessible(true);
            JsonProperty annotation = field.getAnnotation(JsonProperty.class);
            Object property;
            try {
                property = field.get(parameter);
            } catch (IllegalAccessException e) {
                throw new IllegalArgumentException(e.getMessage(), e);
            }
            if (property == null) {
                if (annotation != null && annotation.required()) {
                    throw new IllegalArgumentException(field.getName() + " is required and cannot be null.");
                }
            } else {
                try {
                    Class<?> propertyType = property.getClass();
                    if (TypeToken.of(List.class).isAssignableFrom(propertyType)) {
                        List<?> items = (List<?>) property;
                        for (Object item : items) {
                            Validator.validate(item);
                        }
                    } else if (TypeToken.of(Map.class).isAssignableFrom(propertyType)) {
                        Map<?, ?> entries = (Map<?, ?>) property;
                        for (Map.Entry<?, ?> entry : entries.entrySet()) {
                            Validator.validate(entry.getKey());
                            Validator.validate(entry.getValue());
                        }
                    } else if (parameterType != propertyType) {
                        Validator.validate(property);
                    }
                } catch (IllegalArgumentException ex) {
                    if (ex.getCause() == null) {
                        // Build property chain
                        throw new IllegalArgumentException(field.getName() + "." + ex.getMessage());
                    } else {
                        throw ex;
                    }
                }
            }
        }
    }
}

From source file:com.temenos.useragent.generic.internal.EntityWrapper.java

static <T> void checkValueforPrimitiveorString(T value) {
    if (null == value || !(Primitives.isWrapperType(value.getClass()) || value instanceof String)) {
        throw new IllegalArgumentException(
                "Invalid value passed. Only string value or primitive value allowed.");
    }//from  w w  w  .  j  a v a2  s.  c om
}

From source file:com.comphenix.detectcme.reflect.BukkitUnwrapper.java

/**
 * Retrieve the underlying NMS object from the given Bukkit object.
 * @param wrappedObject - wrapped Minecraft object.
 * @return The underlying NMS object./*  w w w  . j  a  va 2 s .  co  m*/
 */
@SuppressWarnings("unchecked")
public static Object unwrapItem(Object wrappedObject) {
    // Special case
    if (wrappedObject == null)
        return null;
    Class<?> currentClass = wrappedObject.getClass();

    // Next, check for types that doesn't have a getHandle()
    if (wrappedObject instanceof Collection) {
        return handleCollection((Collection<Object>) wrappedObject);
    } else if (Primitives.isWrapperType(currentClass) || wrappedObject instanceof String) {
        return null;
    }

    Unwrapper specificUnwrapper = getSpecificUnwrapper(currentClass);

    // Retrieve the handle
    if (specificUnwrapper != null)
        return specificUnwrapper.unwrapItem(wrappedObject);
    else
        return null;
}

From source file:com.comphenix.protocol.reflect.cloning.ImmutableDetector.java

/**
 * Determine if the given type is probably immutable.
 * @param type - the type to check./*from   w w w  .ja v  a 2s .  co  m*/
 * @return TRUE if the type is immutable, FALSE otherwise.
 */
public static boolean isImmutable(Class<?> type) {
    // Cases that are definitely not true
    if (type.isArray())
        return false;

    // All primitive types
    if (Primitives.isWrapperType(type) || String.class.equals(type))
        return true;

    // May not be true, but if so, that kind of code is broken anyways
    if (isEnumWorkaround(type))
        return true;

    for (Class<?> clazz : immutableClasses)
        if (clazz.equals(type))
            return true;

    // Check for known immutable classes in 1.7.2
    if (MinecraftReflection.isUsingNetty()) {
        if (type.equals(MinecraftReflection.getGameProfileClass())) {
            return true;
        }
    }

    // Check for known immutable classes in 1.9
    if (MinecraftReflection.watcherObjectExists()) {
        if (type.equals(MinecraftReflection.getDataWatcherSerializerClass())
                || type.equals(MinecraftReflection.getMinecraftClass("SoundEffect"))) {
            return true;
        }
    }

    // Probably not
    return false;
}

From source file:com.logsniffer.fields.FieldBaseTypes.java

public static FieldBaseTypes resolveType(final Object v) {
    if (v != null) {
        for (final FieldBaseTypes t : values()) {
            if (t.serializationType == null) {
                continue;
            }//w ww  .j  av  a  2 s .  c o  m
            Class<?> vc = v.getClass();
            if (Primitives.isWrapperType(vc)) {
                vc = Primitives.unwrap(vc);
            }
            if (t.serializationType.isAssignableFrom(vc)) {
                return t;
            }
        }
    }
    return OBJECT;
}

From source file:edu.mit.streamjit.util.bytecode.types.WrapperType.java

WrapperType(Klass klass) {
    super(klass);
    Class<?> backer = klass.getBackingClass();
    checkArgument(backer != null && Primitives.isWrapperType(backer) && !backer.equals(Void.class),
            "not a wrapper type: %s", klass);
}

From source file:com.comphenix.protocol.reflect.instances.PrimitiveGenerator.java

@Override
public Object create(@Nullable Class<?> type) {
    if (type == null) {
        return null;
    } else if (type.isPrimitive()) {
        return Defaults.defaultValue(type);
    } else if (Primitives.isWrapperType(type)) {
        return Defaults.defaultValue(Primitives.unwrap(type));
    } else if (type.isArray()) {
        Class<?> arrayType = type.getComponentType();
        return Array.newInstance(arrayType, 0);
    } else if (type.isEnum()) {
        Object[] values = type.getEnumConstants();
        if (values != null && values.length > 0)
            return values[0];
    } else if (type.equals(String.class)) {
        return stringDefault;
    }//from   w w w  .j  a v a2  s  .  c  o  m

    // Cannot handle this type
    return null;
}

From source file:com.github.radium226.common.ListenerList.java

public void fire(String methodName, Object... parameters) {
    for (T listener : listeners) {
        try {//from w w  w .  j  a  v  a 2  s .com
            Class<?>[] parameterTypes = Lists.transform(Arrays.asList(parameters), (Object parameter) -> {
                Class<?> type = parameter.getClass();
                return Primitives.isWrapperType(type) ? Primitives.unwrap(type) : type;
            }).toArray(new Class<?>[0]);
            Method method = listenerClass.getMethod(methodName, parameterTypes);
            method.invoke(listener, parameters);
        } catch (IllegalAccessException | SecurityException | NoSuchMethodException | IllegalArgumentException
                | InvocationTargetException e) {
            LOGGER.warn("Unable to invoke {}", methodName, e);
        }
    }
}