Example usage for com.google.common.reflect TypeToken equals

List of usage examples for com.google.common.reflect TypeToken equals

Introduction

In this page you can find the example usage for com.google.common.reflect TypeToken equals.

Prototype

@Override
public boolean equals(@Nullable Object o) 

Source Link

Document

Returns true if o is another TypeToken that represents the same Type .

Usage

From source file:brooklyn.util.guava.TypeTokens.java

/** returns null if it's raw, else the type token */
@Nullable//w  ww. j  a  v  a  2  s . co  m
public static <T> TypeToken<T> getTypeTokenIfNotRaw(@Nullable TypeToken<T> type) {
    if (type == null || type.equals(TypeToken.of(type.getRawType()))) {
        return null;
    } else {
        return type;
    }
}

From source file:brooklyn.util.guava.TypeTokens.java

/** returns raw type, if it's raw, else null;
 * used e.g. to set only one of the raw type or the type token,
 * for instance to make serialized output nicer */
@Nullable/*from ww  w.  j  av  a 2s.  c  o  m*/
public static <T> Class<? super T> getRawTypeIfRaw(@Nullable TypeToken<T> type) {
    if (type == null || !type.equals(TypeToken.of(type.getRawType()))) {
        return null;
    } else {
        return type.getRawType();
    }
}

From source file:edu.washington.cs.cupid.TypeManager.java

/**
 * Returns <code>true</code> iff an argument of type <code>argumentType</code> can be supplied as the argument
 * for <code>capability</code>.
 * /*  w  w w. j  a  v a2  s  . co m*/
 * @param capability the capability
 * @param argumentType the argument type
 * @return <code>true</code> iff an argument of type <code>argumentType</code> can be supplied as the argument
 * for <code>capability</code>
 */
public static boolean isCompatible(final ICapability.IParameter<?> parameter, final TypeToken<?> argumentType) {

    TypeToken<?> parameterType = parameter.getType();

    if (parameterType.equals(TypeToken.of(Void.class))) {
        // parameter does not expect any input
        return true;

    } else if (isJavaCompatible(parameterType, argumentType)) {
        // Java's standard typing rules work
        return true;
    } else if (parameterType.getType() instanceof ParameterizedType) {
        if (parameterType.getRawType().isAssignableFrom(argumentType.getRawType())) {
            // check if type is all variables (i.e., fully generic)
            for (Type arg : ((ParameterizedType) parameterType.getType()).getActualTypeArguments()) {
                if (!(arg instanceof TypeVariable)) {
                    return parameterType.isAssignableFrom(argumentType);
                }
            }
            return true;
        } else {
            return false;
        }
    } else {
        return ADAPTER_REGISTRY.getTypeAdapter(argumentType, parameterType) != null;
    }
}

From source file:ratpack.form.internal.FormNoOptParser.java

@SuppressWarnings("unchecked")
@Override/*from   www . j a va2s  . com*/
public <T> T parse(Context context, TypedData requestBody, TypeToken<T> type) throws Exception {
    if (type.equals(FORM_TYPE)) {
        return (T) FormDecoder.parseForm(context, requestBody, empty());
    } else {
        return null;
    }
}

From source file:ratpack.jackson.internal.JsonParser.java

@Override
public <T> T parse(Context context, TypedData body, Parse<T, JsonParseOpts> parse) throws IOException {
    JsonParseOpts opts = parse.getOpts();
    TypeToken<T> type = parse.getType();

    ObjectMapper objectMapper = getObjectMapper(opts);
    InputStream inputStream = body.getInputStream();
    if (type.equals(JSON_NODE_TYPE)) {
        return cast(objectMapper.readTree(inputStream));
    } else {//from www .ja  v a  2  s .c  o  m
        return objectMapper.readValue(inputStream, toJavaType(type, objectMapper));
    }
}

From source file:org.jclouds.config.BindApiContextWithWildcardExtendsExplicitAndRawType.java

@SuppressWarnings("unchecked")
@Override//from  w  ww .j av a  2  s . co m
protected void configure() {
    TypeToken<?> concreteType = BaseHttpApiMetadata.contextToken(typeToken(httpApiMetadata.getApi()));
    // bind explicit type
    bind(TypeLiteral.get(concreteType.getType())).to(TypeLiteral.class
            .cast(TypeLiteral.get(Types.newParameterizedType(ApiContextImpl.class, httpApiMetadata.getApi()))));
    // bind potentially wildcard type
    if (!concreteType.equals(httpApiMetadata.getContext())) {
        bind(TypeLiteral.get(httpApiMetadata.getContext().getType())).to(TypeLiteral.class.cast(
                TypeLiteral.get(Types.newParameterizedType(ApiContextImpl.class, httpApiMetadata.getApi()))));
    }
    // bind w/o types
    bind(TypeLiteral.get(ApiContext.class)).to(TypeLiteral.class
            .cast(TypeLiteral.get(Types.newParameterizedType(ApiContextImpl.class, httpApiMetadata.getApi()))));
}

From source file:org.jclouds.config.BindRestContextWithWildcardExtendsExplicitAndRawType.java

@SuppressWarnings("unchecked")
@Override/*from w  w w  .jav  a 2  s.com*/
protected void configure() {
    TypeToken<?> concreteType = BaseRestApiMetadata.contextToken(typeToken(restApiMetadata.getApi()),
            typeToken(restApiMetadata.getAsyncApi()));
    // bind explicit type
    bind(TypeLiteral.get(concreteType.getType()))
            .to(TypeLiteral.class.cast(TypeLiteral.get(Types.newParameterizedType(RestContextImpl.class,
                    restApiMetadata.getApi(), restApiMetadata.getAsyncApi()))));
    // bind potentially wildcard type
    if (!concreteType.equals(restApiMetadata.getContext())) {
        bind(TypeLiteral.get(restApiMetadata.getContext().getType()))
                .to(TypeLiteral.class.cast(TypeLiteral.get(Types.newParameterizedType(RestContextImpl.class,
                        restApiMetadata.getApi(), restApiMetadata.getAsyncApi()))));
    }
    // bind w/o types
    bind(TypeLiteral.get(RestContext.class))
            .to(TypeLiteral.class.cast(TypeLiteral.get(Types.newParameterizedType(RestContextImpl.class,
                    restApiMetadata.getApi(), restApiMetadata.getAsyncApi()))));
}

From source file:com.google.cloud.dataflow.sdk.util.ApiSurface.java

/**
 * Adds a class and all of the types it exposes. The cause
 * of the class being exposed is given, and the cause
 * of everything within the class is that class itself.
 *///from w  ww.j a  v  a2  s.  com
private void addExposedTypes(Class<?> clazz, Class<?> cause) {
    if (pruned(clazz)) {
        return;
    }
    // Even if `clazz` has been visited, the link from `cause` may be new
    boolean alreadyDone = done(clazz);
    visit(clazz);
    recordExposure(clazz, cause);
    if (alreadyDone || pruned(clazz)) {
        return;
    }

    TypeToken<?> token = TypeToken.of(clazz);
    for (TypeToken<?> superType : token.getTypes()) {
        if (!superType.equals(token)) {
            logger.debug("Adding exposed types from {}, which is a super type token on {}", superType, clazz);
            addExposedTypes(superType, clazz);
        }
    }
    for (Class innerClass : clazz.getDeclaredClasses()) {
        if (exposed(innerClass.getModifiers())) {
            logger.debug("Adding exposed types from {}, which is an exposed inner class of {}", innerClass,
                    clazz);
            addExposedTypes(innerClass, clazz);
        }
    }
    for (Field field : clazz.getDeclaredFields()) {
        if (exposed(field.getModifiers())) {
            logger.debug("Adding exposed types from {}, which is an exposed field on {}", field, clazz);
            addExposedTypes(field, clazz);
        }
    }
    for (Invokable invokable : getExposedInvokables(token)) {
        logger.debug("Adding exposed types from {}, which is an exposed invokable on {}", invokable, clazz);
        addExposedTypes(invokable, clazz);
    }
}

From source file:com.google.api.server.spi.config.jsonwriter.JsonConfigWriter.java

@Nullable
private String schemaFormatForType(TypeToken<?> type, ApiConfig apiConfig) {
    TypeToken<?> serializedType = ApiAnnotationIntrospector.getSchemaType(type, apiConfig);
    if (!type.equals(serializedType)) {
        return schemaFormatForType(serializedType, apiConfig);
    }// w ww  .j  a  v  a 2 s. com
    return typeLoader.getSchemaFormat(type);
}

From source file:org.apache.beam.sdk.util.ApiSurface.java

/**
 * Adds a class and all of the types it exposes. The cause of the class being exposed is given,
 * and the cause of everything within the class is that class itself.
 *///ww  w.j a v  a2s. c om
private void addExposedTypes(Class<?> clazz, Class<?> cause) {
    if (pruned(clazz)) {
        return;
    }
    // Even if `clazz` has been visited, the link from `cause` may be new
    boolean alreadyDone = done(clazz);
    visit(clazz);
    recordExposure(clazz, cause);
    if (alreadyDone || pruned(clazz)) {
        return;
    }

    TypeToken<?> token = TypeToken.of(clazz);
    for (TypeToken<?> superType : token.getTypes()) {
        if (!superType.equals(token)) {
            LOG.debug("Adding exposed types from {}, which is a super type token on {}", superType, clazz);
            addExposedTypes(superType, clazz);
        }
    }
    for (Class innerClass : clazz.getDeclaredClasses()) {
        if (exposed(innerClass.getModifiers())) {
            LOG.debug("Adding exposed types from {}, which is an exposed inner class of {}", innerClass, clazz);
            addExposedTypes(innerClass, clazz);
        }
    }
    for (Field field : clazz.getDeclaredFields()) {
        if (exposed(field.getModifiers())) {
            LOG.debug("Adding exposed types from {}, which is an exposed field on {}", field, clazz);
            addExposedTypes(field, clazz);
        }
    }
    for (Invokable invokable : getExposedInvokables(token)) {
        LOG.debug("Adding exposed types from {}, which is an exposed invokable on {}", invokable, clazz);
        addExposedTypes(invokable, clazz);
    }
}