nz.ac.waikato.cms.locator.ClassUtils Maven / Gradle / Ivy
/*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
/*
* ClassUtils.java
* Copyright (C) 2017 University of Waikato, Hamilton, NZ
*/
package nz.ac.waikato.cms.locator;
import java.lang.reflect.Array;
import java.util.List;
/**
* Helper class for class-related operations.
*
* @author FracPete (fracpete at waikato dot ac dot nz)
* @version $Revision$
*/
public class ClassUtils {
/**
* Returns the basic class of an array class (handles multi-dimensional
* arrays).
* @param c the array to inspect
* @return the class of the innermost elements
*/
public static Class getArrayClass(Class c) {
if (c.getComponentType().isArray())
return getArrayClass(c.getComponentType());
else
return c.getComponentType();
}
/**
* Returns the dimensions of the given array. Even though the
* parameter is of type "Object" one can hand over primitve arrays, e.g.
* int[3] or double[2][4].
*
* @param array the array to determine the dimensions for
* @return the dimensions of the array
*/
public static int getArrayDimensions(Class array) {
if (array.getComponentType().isArray())
return 1 + getArrayDimensions(array.getComponentType());
else
return 1;
}
/**
* Returns the dimensions of the given array. Even though the
* parameter is of type "Object" one can hand over primitve arrays, e.g.
* int[3] or double[2][4].
*
* @param array the array to determine the dimensions for
* @return the dimensions of the array
*/
public static int getArrayDimensions(Object array) {
return getArrayDimensions(array.getClass());
}
/**
* Returns the given Array in a string representation. Even though the
* parameter is of type "Object" one can hand over primitve arrays, e.g.
* int[3] or double[2][4].
*
* @param array the array to return in a string representation
* @param outputClass whether to output the class name instead of calling
* the object's "toString()" method
* @return the array as string
*/
public static String arrayToString(Object array, boolean outputClass) {
StringBuilder result;
int dimensions;
int i;
Object obj;
result = new StringBuilder();
dimensions = getArrayDimensions(array);
if (dimensions == 0) {
result.append("null");
}
else if (dimensions == 1) {
for (i = 0; i < Array.getLength(array); i++) {
if (i > 0)
result.append(",");
if (Array.get(array, i) == null) {
result.append("null");
}
else {
obj = Array.get(array, i);
if (outputClass) {
if (obj instanceof Class)
result.append(((Class) obj).getName());
else
result.append(obj.getClass().getName());
}
else {
result.append(obj.toString());
}
}
}
}
else {
for (i = 0; i < Array.getLength(array); i++) {
if (i > 0)
result.append(",");
result.append("[" + arrayToString(Array.get(array, i)) + "]");
}
}
return result.toString();
}
/**
* Returns the given Array in a string representation. Even though the
* parameter is of type "Object" one can hand over primitve arrays, e.g.
* int[3] or double[2][4].
*
* @param array the array to return in a string representation
* @return the array as string
*/
public static String arrayToString(Object array) {
return arrayToString(array, false);
}
/**
* Flattens the list into a single, long string. The separator string gets
* added between the objects, but not after the last one.
*
* @param lines the lines to flatten
* @param sep the separator
* @return the generated string
*/
public static String flatten(List lines, String sep) {
return flatten(lines.toArray(new Object[lines.size()]), sep);
}
/**
* Flattens the array into a single, long string. The separator string gets
* added between the objects, but not after the last one. Uses the "toString()"
* method of the objects to turn them into a string.
*
* @param lines the lines to flatten
* @param sep the separator
* @return the generated string
*/
public static String flatten(Object[] lines, String sep) {
StringBuilder result;
int i;
result = new StringBuilder();
for (i = 0; i < lines.length; i++) {
if (i > 0)
result.append(sep);
result.append(lines[i].toString());
}
return result.toString();
}
}