defaulttypetransformation.java
来自「Groovy动态语言 运行在JVM中的动态语言 可以方便的处理业务逻辑变化大的业」· Java 代码 · 共 823 行 · 第 1/2 页
JAVA
823 行
try {
return DefaultGroovyMethods.readLines((File) value);
}
catch (IOException e) {
throw new GroovyRuntimeException("Error reading file: " + value, e);
}
}
else {
// lets assume its a collection of 1
return Collections.singletonList(value);
}
}
/**
* Allows conversion of arrays into a mutable List
*
* @return the array as a List
*/
public static List primitiveArrayToList(Object array) {
int size = Array.getLength(array);
List list = new ArrayList(size);
for (int i = 0; i < size; i++) {
list.add(Array.get(array, i));
}
return list;
}
/**
* Compares the two objects handling nulls gracefully and performing numeric type coercion if required
*/
public static int compareTo(Object left, Object right) {
if (left == right) {
return 0;
}
if (left == null) {
return -1;
}
else if (right == null) {
return 1;
}
if (left instanceof Comparable) {
if (left instanceof Number) {
if (isValidCharacterString(right)) {
return castToChar(left) - castToChar(right);
} else if (right instanceof Character || right instanceof Number) {
return DefaultGroovyMethods.compareTo((Number) left, castToNumber(right));
}
}
else if (left instanceof Character) {
if (isValidCharacterString(right)) {
return castToChar(left) - castToChar(right);
}
else if (right instanceof Number) {
return castToChar(left) - castToChar(right);
}
}
else if (right instanceof Number) {
if (isValidCharacterString(left)) {
return castToChar(left) - castToChar(right);
}
}
else if (left instanceof String && right instanceof Character) {
return ((String) left).compareTo(right.toString());
}
else if (left instanceof String && right instanceof GString) {
return ((String) left).compareTo(right.toString());
}
Comparable comparable = (Comparable) left;
return comparable.compareTo(right);
}
if (left.getClass().isArray()) {
Collection leftList = asCollection(left);
if (right.getClass().isArray()) {
right = asCollection(right);
}
return ((Comparable) leftList).compareTo(right);
}
throw new GroovyRuntimeException("Cannot compare values: " + left + " and " + right);
}
public static boolean compareEqual(Object left, Object right) {
if (left == right) return true;
if (left == null || right == null) return false;
if (left instanceof Comparable) {
return compareTo(left, right) == 0;
} else if (left instanceof List && right instanceof List) {
return DefaultGroovyMethods.equals((List) left, (List) right);
} else {
return left.equals(right);
}
}
/**
* @return true if the given value is a valid character string (i.e. has length of 1)
*/
private static boolean isValidCharacterString(Object value) {
if (value instanceof String) {
String s = (String) value;
if (s.length() == 1) {
return true;
}
}
return false;
}
/**
* @param a array of primitives
* @param type component type of the array
* @return
*/
public static Object[] convertPrimitiveArray(Object a, Class type) {
// System.out.println("a.getClass() = " + a.getClass());
Object[] ans = null;
String elemType = type.getName();
if (elemType.equals("int")) {
// conservative coding
if (a.getClass().getName().equals("[Ljava.lang.Integer;")) {
ans = (Integer[]) a;
}
else {
int[] ia = (int[]) a;
ans = new Integer[ia.length];
for (int i = 0; i < ia.length; i++) {
int e = ia[i];
ans[i] = IntegerCache.integerValue(e);
}
}
}
else if (elemType.equals("char")) {
if (a.getClass().getName().equals("[Ljava.lang.Character;")) {
ans = (Character[]) a;
}
else {
char[] ia = (char[]) a;
ans = new Character[ia.length];
for (int i = 0; i < ia.length; i++) {
char e = ia[i];
ans[i] = new Character(e);
}
}
}
else if (elemType.equals("boolean")) {
if (a.getClass().getName().equals("[Ljava.lang.Boolean;")) {
ans = (Boolean[]) a;
}
else {
boolean[] ia = (boolean[]) a;
ans = new Boolean[ia.length];
for (int i = 0; i < ia.length; i++) {
boolean e = ia[i];
ans[i] = new Boolean(e);
}
}
}
else if (elemType.equals("byte")) {
if (a.getClass().getName().equals("[Ljava.lang.Byte;")) {
ans = (Byte[]) a;
}
else {
byte[] ia = (byte[]) a;
ans = new Byte[ia.length];
for (int i = 0; i < ia.length; i++) {
byte e = ia[i];
ans[i] = new Byte(e);
}
}
}
else if (elemType.equals("short")) {
if (a.getClass().getName().equals("[Ljava.lang.Short;")) {
ans = (Short[]) a;
}
else {
short[] ia = (short[]) a;
ans = new Short[ia.length];
for (int i = 0; i < ia.length; i++) {
short e = ia[i];
ans[i] = new Short(e);
}
}
}
else if (elemType.equals("float")) {
if (a.getClass().getName().equals("[Ljava.lang.Float;")) {
ans = (Float[]) a;
}
else {
float[] ia = (float[]) a;
ans = new Float[ia.length];
for (int i = 0; i < ia.length; i++) {
float e = ia[i];
ans[i] = new Float(e);
}
}
}
else if (elemType.equals("long")) {
if (a.getClass().getName().equals("[Ljava.lang.Long;")) {
ans = (Long[]) a;
}
else {
long[] ia = (long[]) a;
ans = new Long[ia.length];
for (int i = 0; i < ia.length; i++) {
long e = ia[i];
ans[i] = new Long(e);
}
}
}
else if (elemType.equals("double")) {
if (a.getClass().getName().equals("[Ljava.lang.Double;")) {
ans = (Double[]) a;
}
else {
double[] ia = (double[]) a;
ans = new Double[ia.length];
for (int i = 0; i < ia.length; i++) {
double e = ia[i];
ans[i] = new Double(e);
}
}
}
return ans;
}
public static int[] convertToIntArray(Object a) {
int[] ans = null;
// conservative coding
if (a.getClass().getName().equals("[I")) {
ans = (int[]) a;
}
else {
Object[] ia = (Object[]) a;
ans = new int[ia.length];
for (int i = 0; i < ia.length; i++) {
if (ia[i] == null) {
continue;
}
ans[i] = ((Number) ia[i]).intValue();
}
}
return ans;
}
public static boolean[] convertToBooleanArray(Object a) {
boolean[] ans = null;
// conservative coding
if (a.getClass().getName().equals("[Z")) {
ans = (boolean[]) a;
}
else {
Object[] ia = (Object[]) a;
ans = new boolean[ia.length];
for (int i = 0; i < ia.length; i++) {
if (ia[i] == null) {
continue;
}
ans[i] = ((Boolean) ia[i]).booleanValue();
}
}
return ans;
}
public static byte[] convertToByteArray(Object a) {
byte[] ans = null;
// conservative coding
if (a.getClass().getName().equals("[B")) {
ans = (byte[]) a;
}
else {
Object[] ia = (Object[]) a;
ans = new byte[ia.length];
for (int i = 0; i < ia.length; i++) {
if (ia[i] != null) {
ans[i] = ((Number) ia[i]).byteValue();
}
}
}
return ans;
}
public static short[] convertToShortArray(Object a) {
short[] ans = null;
// conservative coding
if (a.getClass().getName().equals("[S")) {
ans = (short[]) a;
}
else {
Object[] ia = (Object[]) a;
ans = new short[ia.length];
for (int i = 0; i < ia.length; i++) {
ans[i] = ((Number) ia[i]).shortValue();
}
}
return ans;
}
public static char[] convertToCharArray(Object a) {
char[] ans = null;
// conservative coding
if (a.getClass().getName().equals("[C")) {
ans = (char[]) a;
}
else {
Object[] ia = (Object[]) a;
ans = new char[ia.length];
for (int i = 0; i < ia.length; i++) {
if (ia[i] == null) {
continue;
}
ans[i] = ((Character) ia[i]).charValue();
}
}
return ans;
}
public static long[] convertToLongArray(Object a) {
long[] ans = null;
// conservative coding
if (a.getClass().getName().equals("[J")) {
ans = (long[]) a;
}
else {
Object[] ia = (Object[]) a;
ans = new long[ia.length];
for (int i = 0; i < ia.length; i++) {
if (ia[i] == null) {
continue;
}
ans[i] = ((Number) ia[i]).longValue();
}
}
return ans;
}
public static float[] convertToFloatArray(Object a) {
float[] ans = null;
// conservative coding
if (a.getClass().getName().equals("[F")) {
ans = (float[]) a;
}
else {
Object[] ia = (Object[]) a;
ans = new float[ia.length];
for (int i = 0; i < ia.length; i++) {
if (ia[i] == null) {
continue;
}
ans[i] = ((Number) ia[i]).floatValue();
}
}
return ans;
}
public static double[] convertToDoubleArray(Object a) {
double[] ans = null;
// conservative coding
if (a.getClass().getName().equals("[D")) {
ans = (double[]) a;
}
else {
Object[] ia = (Object[]) a;
ans = new double[ia.length];
for (int i = 0; i < ia.length; i++) {
if (ia[i] == null) {
continue;
}
ans[i] = ((Number) ia[i]).doubleValue();
}
}
return ans;
}
public static Object convertToPrimitiveArray(Object a, Class type) {
if (type == Byte.TYPE) {
return convertToByteArray(a);
}
if (type == Boolean.TYPE) {
return convertToBooleanArray(a);
}
if (type == Short.TYPE) {
return convertToShortArray(a);
}
if (type == Character.TYPE) {
return convertToCharArray(a);
}
if (type == Integer.TYPE) {
return convertToIntArray(a);
}
if (type == Long.TYPE) {
return convertToLongArray(a);
}
if (type == Float.TYPE) {
return convertToFloatArray(a);
}
if (type == Double.TYPE) {
return convertToDoubleArray(a);
}
else {
return a;
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?