Example usage for com.google.common.base Predicates assignableFrom

List of usage examples for com.google.common.base Predicates assignableFrom

Introduction

In this page you can find the example usage for com.google.common.base Predicates assignableFrom.

Prototype

@GwtIncompatible("Class.isAssignableFrom")
@Beta
public static Predicate<Class<?>> assignableFrom(Class<?> clazz) 

Source Link

Document

Returns a predicate that evaluates to true if the class being tested is assignable from the given class.

Usage

From source file:com.kolich.curacao.mappers.MapperTable.java

private final ImmutableMultimap<Class<?>, ControllerArgumentMapper<?>> buildArgumentMapperTable(
        final Set<Class<?>> mapperSet) {
    // Using a LinkedHashMultimap internally because insertion order is
    // very important in this case.
    final Multimap<Class<?>, ControllerArgumentMapper<?>> mappers = LinkedHashMultimap.create(); // Preserves order
    // Filter the incoming mapper set to only argument mappers.
    final Set<Class<?>> filtered = Sets.filter(mapperSet,
            Predicates.assignableFrom(ControllerArgumentMapper.class));
    logger__.debug("Found {} argument mappers annotated with @{}", filtered.size(), MAPPER_ANNOTATION_SN);
    // For each discovered mapper class...
    for (final Class<?> mapper : filtered) {
        logger__.debug("Found @{}: argument mapper {}", MAPPER_ANNOTATION_SN, mapper.getCanonicalName());
        try {/*  w  w  w  . j av  a2s  .c  o  m*/
            // Locate a single constructor worthy of injecting with
            // components, if any.  May be null.
            final Constructor<?> ctor = getInjectableConstructor(mapper);
            ControllerArgumentMapper<?> instance = null;
            if (ctor == null) {
                // Class.newInstance() is evil, so we do the ~right~ thing
                // here to instantiate a new instance of the mapper using
                // the preferred getConstructor() idiom.
                instance = (ControllerArgumentMapper<?>) mapper.getConstructor().newInstance();
            } else {
                final Class<?>[] types = ctor.getParameterTypes();
                final Object[] params = new Object[types.length];
                for (int i = 0, l = types.length; i < l; i++) {
                    params[i] = componentTable_.getComponentForType(types[i]);
                }
                instance = (ControllerArgumentMapper<?>) ctor.newInstance(params);
            }
            // Note the key in the map is the parameterized generic type
            // hanging off the mapper.
            mappers.put(getGenericType(mapper), instance);
        } catch (Exception e) {
            logger__.error("Failed to instantiate mapper instance: {}", mapper.getCanonicalName(), e);
        }
    }
    // Add the "default" mappers to the ~end~ of the immutable hash multi map.
    // This essentially means that default argument mappers (the ones
    // provided by this library) are found & called after any user registered
    // mappers.
    mappers.putAll(defaultArgMappers__);
    return ImmutableMultimap.copyOf(mappers);
}

From source file:com.kolich.curacao.mappers.MapperTable.java

private final ImmutableMap<Class<?>, ControllerReturnTypeMapper<?>> buildReturnTypeMapperTable(
        final Set<Class<?>> mapperSet) {
    // Using a LinkedHashMap internally because insertion order is
    // very important in this case.
    final Map<Class<?>, ControllerReturnTypeMapper<?>> mappers = Maps.newLinkedHashMap(); // Preserves insertion order.
    // Filter the incoming mapper set to only return type mappers.
    final Set<Class<?>> filtered = Sets.filter(mapperSet,
            Predicates.assignableFrom(ControllerReturnTypeMapper.class));
    logger__.debug("Found {} return type mappers annotated with @{}", filtered.size(), MAPPER_ANNOTATION_SN);
    // For each discovered mapper class...
    for (final Class<?> mapper : filtered) {
        logger__.debug("Found @{}: return type mapper {}", MAPPER_ANNOTATION_SN, mapper.getCanonicalName());
        try {/*from   w ww . j  av a2  s .  co  m*/
            // Locate a single constructor worthy of injecting with
            // components, if any.  May be null.
            final Constructor<?> ctor = getInjectableConstructor(mapper);
            ControllerReturnTypeMapper<?> instance = null;
            if (ctor == null) {
                // Class.newInstance() is evil, so we do the ~right~ thing
                // here to instantiate a new instance of the mapper using
                // the preferred getConstructor() idiom.
                instance = (ControllerReturnTypeMapper<?>) mapper.getConstructor().newInstance();
            } else {
                final Class<?>[] types = ctor.getParameterTypes();
                final Object[] params = new Object[types.length];
                for (int i = 0, l = types.length; i < l; i++) {
                    params[i] = componentTable_.getComponentForType(types[i]);
                }
                instance = (ControllerReturnTypeMapper<?>) ctor.newInstance(params);
            }
            // Note the key in the map is the parameterized generic type
            // hanging off the mapper.
            mappers.put(getGenericType(mapper), instance);
        } catch (Exception e) {
            logger__.error("Failed to instantiate mapper instance: {}", mapper.getCanonicalName(), e);
        }
    }
    // Add the "default" mappers to the ~end~ of the linked hash map, being
    // careful not to overwrite any user-defined mappers.  That is, if a
    // user has declared their own mappers for one of our default types,
    // we should not blindly "putAll" and overwrite them.
    // <https://github.com/markkolich/curacao/issues/9>
    for (final Map.Entry<Class<?>, ControllerReturnTypeMapper<?>> entry : defaultReturnTypeMappers__
            .entrySet()) {
        // Only add the default mapper if a user-defined one does not exist.
        if (!mappers.containsKey(entry.getKey())) {
            mappers.put(entry.getKey(), entry.getValue());
        }
    }
    return ImmutableMap.copyOf(mappers);
}

From source file:org.apache.brooklyn.cloudfoundry.location.CloudFoundryLocation.java

private boolean implementsInterface(Entity entity, Class<?> type) {
    return Iterables.tryFind(Arrays.asList(entity.getClass().getInterfaces()), Predicates.assignableFrom(type))
            .isPresent();/*from   w  ww  .ja va2  s  . co  m*/
}

From source file:org.apache.brooklyn.container.location.kubernetes.KubernetesLocation.java

public boolean implementsInterface(Entity entity, Class<?> type) {
    return Iterables.tryFind(Arrays.asList(entity.getClass().getInterfaces()), Predicates.assignableFrom(type))
            .isPresent();/*from   ww w.ja v a2  s .c  o  m*/
}