本文整理匯總了Java中com.google.common.primitives.Primitives類的典型用法代碼示例。如果您正苦於以下問題:Java Primitives類的具體用法?Java Primitives怎麽用?Java Primitives使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
Primitives類屬於com.google.common.primitives包,在下文中一共展示了Primitives類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: forJavaType
import com.google.common.primitives.Primitives; //導入依賴的package包/類
/**
* Gets the data type most relevant to a Java class.
*/
@SuppressWarnings("unchecked")
public <T> Optional<DataType<T>> forJavaType(Class<T> type) {
if (type.isPrimitive()) {
return forJavaType((Class) Primitives.wrap(type));
}
return (Optional) typeCache.computeIfAbsent(type, __ -> {
if (DataType.class.isAssignableFrom(type)) {
return forType((Class<DataType>) type);
}
Optional<DataType> naive = dataTypes.values().stream()
.filter(t -> type.equals(t.getJavaClass()))
.findAny();
if (naive.isPresent()) {
return naive;
} else {
// Check Java class hierarchy
Comparator<Class<?>> classComparator = closestTo(type);
// This stream MUST be sequential to work correctly
return dataTypes.values().stream()
.filter(t -> t != All)
.sorted((t1, t2) -> classComparator.reversed().compare(t1.getJavaClass(), t2.getJavaClass()))
.findFirst();
}
});
}
示例2: findMethod
import com.google.common.primitives.Primitives; //導入依賴的package包/類
public static final Method findMethod(Class<?> clazz, InvocationParametersBean invocation) throws NoSuchMethodException {
Method ret = null;
Class<?>[] types = getParameterTypes(invocation);
try {
ret = clazz.getMethod(invocation.getMethodName(), types);
} catch (NoSuchMethodException e) {
}
if (ret == null && types != null) {
int max = types.length;
for (int i = 0; i < max; i++) {
if (Primitives.isWrapperType(types[i])) {
types[i] = Primitives.unwrap(types[i]);
}
}
ret = clazz.getMethod(invocation.getMethodName(), types);
}
return ret;
}
示例3: cast
import com.google.common.primitives.Primitives; //導入依賴的package包/類
private static <T> T cast(final Object value, final Class<T> expectedType) {
if (value == null) {
return null;
}
if (expectedType.isPrimitive() && Primitives.wrap(expectedType).equals(value.getClass())) {
@SuppressWarnings("unchecked")
final T castValue = (T) value;
return castValue;
}
if (!expectedType.isAssignableFrom(value.getClass())) {
throw new IllegalArgumentException(String.format(
"Expected %s, but was: %s", expectedType.getName(), value.getClass().getName()));
}
return expectedType.cast(value);
}
示例4: getValueOfMethodFrom
import com.google.common.primitives.Primitives; //導入依賴的package包/類
public static Method getValueOfMethodFrom(Class returnClass) {
val existsMethod = valueOfMethodCache.get(returnClass);
if (existsMethod != null) return existsMethod.orNull();
val clazz = Primitives.wrap(returnClass);
try {
val valueOfMethod = clazz.getMethod("valueOf", String.class);
if (isPublicStatic(clazz, valueOfMethod)) {
valueOfMethodCache.put(returnClass, Optional.of(valueOfMethod));
return valueOfMethod;
}
} catch (Exception e) {
valueOfMethodCache.put(returnClass, Optional.<Method>absent());
}
return null;
}
示例5: isTrue
import com.google.common.primitives.Primitives; //導入依賴的package包/類
/**
* Determines whether a value is 'true', where true is interpreted depending on the values
* type.
*/
@SuppressWarnings("rawtypes")
static boolean isTrue(Object v1) {
if (v1 instanceof Number) {
// For numbers, compare with the default value (zero), which we obtain by
// creating an array.
return !Array.get(Array.newInstance(Primitives.unwrap(v1.getClass()), 1), 0).equals(v1);
}
if (v1 instanceof Boolean) {
return (Boolean) v1;
}
if (v1 instanceof Doc) {
return !((Doc) v1).isWhitespace();
}
if (v1 instanceof String) {
return !Strings.isNullOrEmpty((String) v1);
}
if (v1 instanceof Iterable) {
return ((Iterable) v1).iterator().hasNext();
}
return false;
}
示例6: reAssignContainers
import com.google.common.primitives.Primitives; //導入依賴的package包/類
static String reAssignContainers(Class<?>[] paramTypes) {
StringBuilder stringBuilder = new StringBuilder();
int i = 0;
for (Class<?> c : paramTypes) {
stringBuilder.append("param").append(i).append(" = (");
if (c.isPrimitive()) {
Class<?> wrapped = Primitives.wrap(c);
stringBuilder
.append("(").append(wrapped.getCanonicalName()).append(") $mArgs[").append(i).append("]).")
.append(c.getCanonicalName()).append("Value();\n");// <-- This feels so cheaty
} else {
stringBuilder.append(c.getCanonicalName()).append(") $mArgs[").append(i).append("];\n");
}
i++;
}
return stringBuilder.toString();
}
示例7: itemFromString
import com.google.common.primitives.Primitives; //導入依賴的package包/類
@SuppressWarnings("unchecked")
protected static <X> X itemFromString(String value, Class<X> clazz, Flag flag)
throws FlagException {
// In case we get a primitive type (e.g. from Scala), get the boxed version to know
// how to serialize.
clazz = Primitives.wrap(clazz);
if (Number.class.isAssignableFrom(clazz)) {
return clazz.cast(PrimitiveFlagField.NumberFlagField.fromString(value, clazz, flag));
} else if (Boolean.class.isAssignableFrom(clazz)) {
return clazz.cast(Boolean.valueOf(value));
} else if (String.class.isAssignableFrom(clazz)) {
return clazz.cast(value);
} else if (clazz.isEnum()) {
return (X) PrimitiveFlagField.EnumFlagField.fromString(value, (Class<Enum>) clazz, flag);
}
throw new FlagException.UnsupportedType(flag, clazz);
}
示例8: testBoxedPrimitives
import com.google.common.primitives.Primitives; //導入依賴的package包/類
@Test
public void testBoxedPrimitives() {
// boxed primitives cannot be null otherwise we get NPE when unboxing
ImmutableMap<Class<?>, String> defaultValueMap = ImmutableMap.<Class<?>, String> builder()
.put(Byte.class, "0").put(Short.class, "0").put(Integer.class, "0")
.put(Long.class, "0L").put(Float.class, "0.0F").put(Double.class, "0.0D")
.put(Character.class, "\'a\'").put(Boolean.class, "false").build();
RetainedTestField[] fields = defaultValueMap.entrySet()
.stream().map(ent -> new RetainedTestField(ent.getKey(),
"_" + ent.getKey().getSimpleName(), ent.getValue()))
.toArray(RetainedTestField[]::new);
// comparison between primitives and boxed primitives does not work,
// manually wrap it
testTypes(ALWAYS, (field, type, arguments) -> type.isPrimitive()
&& field.clazz == Primitives.wrap(type), f->1, fields);
}
示例9: setLocalConverter
import com.google.common.primitives.Primitives; //導入依賴的package包/類
private void setLocalConverter(FieldConverterMapper converterMapper) {
// it is not good for wrap clazz in this place
Class<?> keyFieldType = converterMapper.isPrimitive() ? Primitives
.wrap(converterMapper.getFieldClass()) : converterMapper
.getFieldClass();
FieldValueConverter fieldValueConverter = defaultLocalConverterHandler
.getLocalConverter(keyFieldType);
if (fieldValueConverter == null) {
throw new IllegalEntityException(clazz,
"can find the converter for fieldType ["
+ converterMapper.getFieldClass() + "]");
}
converterMapper.setFieldConverter(fieldValueConverter);
}
示例10: findMethod
import com.google.common.primitives.Primitives; //導入依賴的package包/類
private static Method findMethod( Class type, String name, Object... args ) {
for( Method m : type.getDeclaredMethods() ) {
if( !Objects.equals(m.getName(), name) ) {
continue;
}
Class[] paramTypes = m.getParameterTypes();
if( paramTypes.length != args.length ) {
continue;
}
int matches = 0;
for( int i = 0; i < args.length; i++ ) {
if( paramTypes[i].isInstance(args[i])
|| Primitives.wrap(paramTypes[i]).isInstance(args[i]) ) {
matches++;
}
}
if( matches == args.length ) {
return m;
}
}
if( type.getSuperclass() != null ) {
return findMethod(type.getSuperclass(), name, args);
}
return null;
}
示例11: get
import com.google.common.primitives.Primitives; //導入依賴的package包/類
/**
* Retrieves the {@link ColumnType} for specified {@link EntityColumn}.
*
* <p>By default, this method returns the {@link ColumnType} for the
* {@linkplain EntityColumn column's} {@linkplain EntityColumn#getType() type}.
*
* <p>If the {@link ColumnType} was not found by the {@code class} of the {@link EntityColumn},
* its superclasses are checked one by one until a {@link ColumnType} is found or until
* the look up reaches the class representing {@link Object}.
*
* <p>If no {@link ColumnType} is found, an {@link IllegalStateException} is thrown.
*
* @param field the {@link EntityColumn} to get type conversion strategy for
* @return the {@link ColumnType} for the given {@link EntityColumn}
*/
public C get(EntityColumn field) {
checkNotNull(field);
Class javaType = field.getType();
javaType = Primitives.wrap(javaType);
C type = null;
while (type == null && javaType != null) {
type = columnTypeMap.get(javaType);
javaType = javaType.getSuperclass();
}
checkState(type != null,
"Could not find storage type for %s.",
field.getType());
return type;
}
示例12: convertInputString
import com.google.common.primitives.Primitives; //導入依賴的package包/類
private Object convertInputString(final String s, final Class<?> type) {
if (type == String.class) {
return s;
} else if (type.isPrimitive() || Primitives.isWrapperType(type)) {
final Class<?> wrapperClass = Primitives.wrap(type);
try {
final Method valueOf = wrapperClass.getMethod("valueOf", String.class);
return valueOf.invoke(wrapperClass, s);
} catch (Exception e) {
final String message = "Failed to convert String "
+ s + " into type " + wrapperClass
+ ".\nCaused by: " + e;
throw new AssertionError(message);
}
} else {
final ExpressionFactory expressionFactory = expressionFactoryResolver.apply(s);
if (expressionFactory != null) {
final Expression expression = expressionFactory.compile(s);
return expression.evaluate().get();
} else {
throw new UnsupportedOperationException("No rule implemented to convert type String to type " + type);
}
}
}
示例13: doWrap
import com.google.common.primitives.Primitives; //導入依賴的package包/類
private static <T> T[] doWrap(Exchanging<T> exch, Object target) {
if (null == target) {
return null;
}
@SuppressWarnings("unchecked")
T[] r = newArray((Class<T>) Primitives.wrap(target.getClass().getComponentType()), Array.getLength(target));
if (0 == r.length) {
return r;
}
for (int i = 0; i < r.length; i++) {
r[i] = exch.wrap(Array.get(target, i));
}
return r;
}
示例14: doUnwrap
import com.google.common.primitives.Primitives; //導入依賴的package包/類
private static <T> Object doUnwrap(Exchanging<T> exch, T... target) {
if (null == target) {
return null;
}
Class<?> clazz = Primitives.unwrap(target.getClass().getComponentType());
Object r = Array.newInstance(clazz, target.length);
if (0 == target.length) {
return r;
}
T el = null;
Object defaultVal = Defaults.defaultValue(clazz);
for (int i = 0; i < target.length; i++) {
Array.set(r, i, (null == (el = target[i])) ? defaultVal : exch.unwraps(el));
}
return r;
}
示例15: isAssignmentCompatible
import com.google.common.primitives.Primitives; //導入依賴的package包/類
static boolean isAssignmentCompatible(Class<?> a, Class<?> b) {
if (a == b)
return true;
// Wrap primitive type
a = Primitives.wrap(a);
// Check primitive type assignment compatibility
if (b.isPrimitive()) {
b = Primitives.wrap(b);
float distance = calculateDistancePrimitive(a, b);
return distance >= 0;
} else {
return b.isInstance(b);
}
}