Move isPrimitiveOrString() method from ObjectNavigator to JsonPrimitive.

This commit is contained in:
Joel Leitch 2008-11-27 00:40:31 +00:00
parent d6d1daeedd
commit b506183d94
2 changed files with 19 additions and 20 deletions

View File

@ -28,6 +28,10 @@ import java.math.BigInteger;
*/ */
public final class JsonPrimitive extends JsonElement { public final class JsonPrimitive extends JsonElement {
private static final Class<?>[] PRIMITIVE_TYPES = { int.class, long.class, short.class,
float.class, double.class, byte.class, boolean.class, char.class, Integer.class, Long.class,
Short.class, Float.class, Double.class, Byte.class, Boolean.class, Character.class };
private Object value; private Object value;
/** /**
@ -95,7 +99,7 @@ public final class JsonPrimitive extends JsonElement {
this.value = String.valueOf(c); this.value = String.valueOf(c);
} else { } else {
Preconditions.checkArgument(primitive instanceof Number Preconditions.checkArgument(primitive instanceof Number
|| ObjectNavigator.isPrimitiveOrString(primitive)); || isPrimitiveOrString(primitive));
this.value = primitive; this.value = primitive;
} }
} }
@ -317,4 +321,18 @@ public final class JsonPrimitive extends JsonElement {
} }
} }
} }
private static boolean isPrimitiveOrString(Object target) {
if (target instanceof String) {
return true;
}
Class<?> classOfPrimitive = target.getClass();
for (Class<?> standardPrimitive : PRIMITIVE_TYPES) {
if (standardPrimitive.isAssignableFrom(classOfPrimitive)) {
return true;
}
}
return false;
}
} }

View File

@ -150,23 +150,4 @@ final class ObjectNavigator {
} }
} }
} }
@SuppressWarnings("unchecked")
private static final Class[] PRIMITIVE_TYPES = { int.class, long.class, short.class, float.class,
double.class, byte.class, boolean.class, Integer.class, Long.class, Short.class, Float.class,
Double.class, Byte.class, Boolean.class };
@SuppressWarnings("unchecked")
static boolean isPrimitiveOrString(Object target) {
if (target instanceof String) {
return true;
}
Class<?> classOfPrimitive = target.getClass();
for (Class standardPrimitive : PRIMITIVE_TYPES) {
if (standardPrimitive.isAssignableFrom(classOfPrimitive)) {
return true;
}
}
return false;
}
} }