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

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

Introduction

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

Prototype

public static TypeToken<?> of(Type type) 

Source Link

Document

Returns an instance of type token that wraps type .

Usage

From source file:com.theoriginalbit.peripheral.converter.inbound.ConverterListInbound.java

@Override
public Object toJava(IConversionRegistry registry, Object obj, Type expected) throws TypeConversionException {
    if (obj instanceof Map) {
        final TypeToken<?> type = TypeToken.of(expected);
        if (type.getRawType() == List.class) {
            final Type valueType = type.resolveType(TYPE_PARAM).getType();

            final Map<?, ?> m = (Map<?, ?>) obj;

            if (m.isEmpty())
                return ImmutableList.of();

            int indexMin = Integer.MAX_VALUE;
            int indexMax = Integer.MIN_VALUE;

            Map<Integer, Object> tmp = Maps.newHashMap();
            for (Map.Entry<?, ?> e : m.entrySet()) {
                Object k = e.getKey();
                if (!(k instanceof Number))
                    return null;
                int index = ((Number) k).intValue();
                if (index < indexMin)
                    indexMin = index;// ww w  .  jav a 2s. co m
                if (index > indexMax)
                    indexMax = index;
                tmp.put(index, e.getValue());
            }

            if (indexMin != 0 && indexMin != 1)
                return null;

            List<Object> result = Lists.newArrayList();

            for (int index = indexMin; index <= indexMax; index++) {
                Object o = tmp.get(index);
                final Object converted = registry.toJava(o, valueType);
                result.add(converted);
            }

            return result;
        }
    }

    return null;
}

From source file:org.apache.aurora.common.args.parsers.MultimapParser.java

@SuppressWarnings("unchecked")
@Override/*from   w ww . j a va 2 s. c o m*/
Multimap<?, ?> doParse(ParserOracle parserOracle, String raw, List<Type> typeParams) {
    Type keyType = typeParams.get(0);
    Parser<?> keyParser = parserOracle.get(TypeToken.of(keyType));

    Type valueType = typeParams.get(1);
    Parser<?> valueParser = parserOracle.get(TypeToken.of(valueType));

    ImmutableMultimap.Builder<Object, Object> multimapBuilder = ImmutableMultimap.builder();
    for (String keyAndValue : Parsers.MULTI_VALUE_SPLITTER.split(raw)) {
        List<String> fields = ImmutableList.copyOf(KEY_VALUE_SPLITTER.split(keyAndValue));
        checkArgument(fields.size() == 2, "Failed to parse key/value pair: " + keyAndValue);
        multimapBuilder.put(keyParser.parse(parserOracle, keyType, fields.get(0)),
                valueParser.parse(parserOracle, valueType, fields.get(1)));
    }

    return multimapBuilder.build();
}

From source file:com.opengamma.collect.id.LinkResolver.java

/**
 * Resolves the supplied link, returning the realized target of the link.
 * <p>/*  w ww  . j  a  v  a 2 s.  c o m*/
 * The implementation of this interface may perform any thread-safe action to obtain
 * the link target. Typically this will involve accessing an underlying data store.
 * If the link cannot be resolved then a {@code LinkResolutionException} will be thrown.
 * <p>
 * The type is expressed as a standard {@link Class} object.
 *
 * @param <T>  the type of the target of the link
 * @param identifier  the identifier to be resolved
 * @param targetType  the target type of the link
 * @return the resolved target of the link
 * @throws LinkResolutionException if the link cannot be resolved
 */
public default <T extends IdentifiableBean> T resolve(StandardId identifier, Class<T> targetType) {
    return resolve(identifier, TypeToken.of(targetType));
}

From source file:com.datastore_android_sdk.reflect.Types.java

/**
 * Returns the parameterized type that is or extends the given type that
 * matches the given super class.// www . j a  v  a2s . c  om
 * 
 * <p>
 * For example, if the input type is {@code HashMap<String,Integer>} and the
 * input super class is {@code Map.class}, it will return the extended
 * parameterized type {@link Map}, but which retains the actual type
 * information from the original {@code HashMap}.
 * </p>
 * 
 * @param type
 *            class or parameterized type
 * @param superClass
 *            super class
 * @return matching parameterized type or {@code null}
 */
@SuppressWarnings({ "rawtypes" })
public static ParameterizedType getSuperParameterizedType(Type type, Class<?> superClass) {
    if (type instanceof Class<?> || type instanceof ParameterizedType) {
        TypeToken<?> typeToken = TypeToken.of(type);
        TypeSet types = typeToken.getTypes();

        if (types != null) {
            TypeSet set = superClass.isInterface() ? types.interfaces() : types.classes();
            Iterator iterator = set.iterator();

            while (iterator.hasNext()) {
                TypeToken tt = (TypeToken) iterator.next();
                if ((tt.getRawType() == superClass || superClass.isAssignableFrom(tt.getRawType()))
                        && tt.getType() instanceof ParameterizedType) {
                    return (ParameterizedType) tt.getType();
                }
            }
        }
    }
    return null;
}

From source file:co.cask.cdap.internal.app.deploy.pipeline.adapter.DeployAdapterDatasetModulesStage.java

public DeployAdapterDatasetModulesStage(CConfiguration configuration, Id.Namespace namespace,
        Location templateJarLocation, DatasetFramework datasetFramework,
        DatasetFramework inMemoryDatasetFramework) {
    super(TypeToken.of(AdapterDefinition.class));
    this.datasetModulesDeployer = new DatasetModulesDeployer(datasetFramework, inMemoryDatasetFramework,
            namespace, configuration);//from   w  ww  .  j av a 2  s.  c o m
    this.templateJarLocation = templateJarLocation;
}

From source file:io.appform.nautilus.funnel.model.session.StateTransition.java

@Override
protected TypeToken<StateTransition> token() {
    return TypeToken.of(StateTransition.class);
}

From source file:org.jclouds.privatechef.config.PrivateChefRestClientModule.java

public PrivateChefRestClientModule() {
    super(TypeToken.of(PrivateChefApi.class), TypeToken.of(PrivateChefAsyncApi.class), DELEGATE_MAP);
}

From source file:io.appform.nautilus.funnel.model.session.Session.java

@Override
protected TypeToken<Session> token() {
    return TypeToken.of(Session.class);
}

From source file:co.cask.cdap.internal.app.deploy.pipeline.ApplicationRegistrationStage.java

public ApplicationRegistrationStage(Store store, UsageRegistry usageRegistry) {
    super(TypeToken.of(ApplicationWithPrograms.class));
    this.store = store;
    this.usageRegistry = usageRegistry;
}

From source file:org.darkware.wpman.ContextManager.java

public <T> T getContextualInstance(T example) {
    return this.getContextualInstance(TypeToken.of((Class<? extends T>) example.getClass()));
}