List of usage examples for com.google.common.primitives Primitives wrap
public static <T> Class<T> wrap(Class<T> type)
From source file:org.statmantis.util.ConvertUtils.java
@SuppressWarnings({ "unchecked", "rawtypes" }) public static <T> T convertKnownType(Object one, Class<T> to) { //let's be lazy right now if (to.isPrimitive()) { to = Primitives.wrap(to); }//from w w w. j a va2s .c o m if (one == null) { return null; } else if (Number.class.isAssignableFrom(to)) { try { return to.getConstructor(String.class).newInstance(one.toString()); } catch (Exception e) { throw new RuntimeException(e); } } else if (to.equals(String.class)) { return (T) one.toString(); } else if (to.isEnum()) { return (T) Enum.valueOf((Class<Enum>) to, one.toString()); } else if (java.sql.Date.class.equals(to)) { //meh try { return (T) new java.sql.Date(new SimpleDateFormat("yyyy-MM-dd").parse(one.toString()).getTime()); } catch (ParseException e) { throw new RuntimeException(e); } } else { throw new IllegalArgumentException("Unknown class: " + to); } }
From source file:com.facebook.buck.rules.modern.impl.ValueTypeInfos.java
static ValueTypeInfo<?> forSimpleType(Type type) { Preconditions.checkState(type instanceof Class<?>); Class<?> rawClass = Primitives.wrap((Class<?>) type); if (rawClass == String.class) { return StringValueTypeInfo.INSTANCE; } else if (rawClass == Character.class) { return CharacterValueTypeInfo.INSTANCE; } else if (rawClass == Boolean.class) { return BooleanValueTypeInfo.INSTANCE; } else if (rawClass == Byte.class) { return ByteValueTypeInfo.INSTANCE; } else if (rawClass == Short.class) { return ShortValueTypeInfo.INSTANCE; } else if (rawClass == Integer.class) { return IntegerValueTypeInfo.INSTANCE; } else if (rawClass == Long.class) { return LongValueTypeInfo.INSTANCE; } else if (rawClass == Float.class) { return FloatValueTypeInfo.INSTANCE; } else if (rawClass == Double.class) { return DoubleValueTypeInfo.INSTANCE; } else if (rawClass == OptionalInt.class) { return OptionalIntValueTypeInfo.INSTANCE; }/* w w w .ja v a 2 s .c om*/ throw new RuntimeException(); }
From source file:com.mysema.query.types.ConstructorExpression.java
private static Class<?> normalize(Class<?> clazz) { return Primitives.wrap(clazz); }
From source file:com.google.caliper.core.Parameters.java
private static Parser<?> getParser(Field field) { Class<?> type = Primitives.wrap(field.getType()); try {//from w w w .j a v a 2 s . com return Parsers.conventionalParser(type); } catch (NoSuchMethodException e) { throw new InvalidBenchmarkException("Type '%s' of parameter field '%s' has no recognized " + "String-converting method; see <TODO> for details", type.getName(), field.getName()); } }
From source file:com.google.caliper.util.Parsers.java
/** * Parser that tries, in this order:/* w w w. j a v a 2 s. c om*/ * <ul> * <li>ResultType.fromString(String) * <li>ResultType.decode(String) * <li>ResultType.valueOf(String) * <li>new ResultType(String) * </ul> */ public static <T> Parser<T> conventionalParser(Class<T> resultType) throws NoSuchMethodException { if (resultType == String.class) { @SuppressWarnings("unchecked") // T == String Parser<T> identity = (Parser<T>) IDENTITY; return identity; } final Class<T> wrappedResultType = Primitives.wrap(resultType); for (String methodName : CONVERSION_METHOD_NAMES) { try { final Method method = wrappedResultType.getDeclaredMethod(methodName, String.class); if (Util.isStatic(method) && wrappedResultType.isAssignableFrom(method.getReturnType())) { method.setAccessible(true); // to permit inner enums, etc. return new InvokingParser<T>() { @Override protected T invoke(String input) throws Exception { return wrappedResultType.cast(method.invoke(null, input)); } }; } } catch (Exception tryAgain) { } } final Constructor<T> constr = wrappedResultType.getDeclaredConstructor(String.class); constr.setAccessible(true); return new InvokingParser<T>() { @Override protected T invoke(String input) throws Exception { return wrappedResultType.cast(constr.newInstance(input)); } }; }
From source file:com.facebook.buck.util.Types.java
/** * Determine the "base type" of a field. That is, the following will be returned: * <ul>/* w w w. jav a 2s . co m*/ * <li>{@code String} -> {@code String.class} * <li>{@code Optional<String>} -> {@code String.class} * <li>{@code Set<String>} -> {@code String.class} * <li>{@code Collection<? extends Comparable>} -> {@code Comparable.class} * <li>{@code Collection<? super Comparable} -> {@code Object.class} * </ul> */ public static Type getBaseType(Field field) { Type type = getFirstNonOptionalType(field); if (type instanceof ParameterizedType) { type = ((ParameterizedType) type).getActualTypeArguments()[0]; } if (type instanceof WildcardType) { type = ((WildcardType) type).getUpperBounds()[0]; } return Primitives.wrap((Class<?>) type); }
From source file:at.ac.tuwien.infosys.jcloudscale.datastore.mapping.type.AbstractTypeAdapterFactory.java
@Override public TypeAdapter<?, T> get(Class<?> clazz) { Class<?> wrappedClass = Primitives.wrap(clazz); for (TypeToken<?, T> typeToken : typeTokens) { if (typeToken.isAssignableFrom(wrappedClass)) { return typeToken.getTypeAdapter(); }//from w w w .j a v a 2 s . c o m } return null; }
From source file:com.facebook.presto.raptor.systemtables.ValueBuffer.java
private static Object normalizeValue(Type type, @Nullable Object value) { if (value == null) { return value; }/*from ww w . j a va2 s .c o m*/ Class<?> javaType = type.getJavaType(); if (javaType.isPrimitive()) { checkArgument(Primitives.wrap(javaType).isInstance(value), "Type %s incompatible with %s", type, value); return value; } if (javaType == Slice.class) { if (value instanceof Slice) { return value; } if (value instanceof String) { return utf8Slice(((String) value)); } if (value instanceof byte[]) { return wrappedBuffer((byte[]) value); } } throw new IllegalArgumentException(format("Type %s incompatible with %s", type, value)); }
From source file:org.openqa.selenium.json.NumberCoercer.java
@Override public boolean test(Class<?> type) { return stereotype.isAssignableFrom(Primitives.wrap(type)); }
From source file:com.querydsl.core.types.OperationImpl.java
protected OperationImpl(Class<? extends T> type, Operator operator, ImmutableList<Expression<?>> args) { super(type);/*from w ww .j a va2 s .c om*/ Class<?> wrapped = Primitives.wrap(type); Preconditions.checkArgument(operator.getType().isAssignableFrom(wrapped), operator.name()); this.operator = operator; this.args = args; }