001: /*
002: * Apollo - Motion capture and animation system
003: * Copyright (c) 2005 Apollo
004: *
005: * This program is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU General Public License
007: * as published by the Free Software Foundation; either version 2
008: * of the License, or (at your option) any later version.
009: *
010: * This program is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: * GNU General Public License for more details.
014: *
015: * You should have received a copy of the GNU General Public License
016: * along with this program; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
018: *
019: * http://www.gnu.org/copyleft/gpl.html
020: *
021: * @author Giovane.Kuhn - brain@netuno.com.br
022: *
023: */
024: package org.apollo;
025:
026: import java.io.File;
027: import java.lang.reflect.InvocationTargetException;
028: import java.lang.reflect.Method;
029:
030: public final class ApolloUtil {
031:
032: private ApolloUtil() {
033: // nop
034: }
035:
036: /**
037: * Runs garbage collector many times
038: */
039: public static void gc() {
040: System.gc();
041: System.gc();
042: }
043:
044: /**
045: * Indicate if there's at least one <code>null</code> object
046: * @param objects List of objects
047: * @return <code>true</code> if has <code>null</code> object, otherwise <code>false</code>
048: */
049: public static boolean hasNull(Object... objects) {
050: for (Object o : objects) {
051: if (o == null) {
052: return true;
053: }
054: }
055: return false;
056: }
057:
058: /**
059: * Indicate if there's at least one <code>NaN</code> number
060: * @param numbers List of numbers
061: * @return <code>true</code> if has <code>NaN</code> number, otherwise <code>false</code>
062: */
063: public static boolean hasNaN(double... numbers) {
064: for (double n : numbers) {
065: if (Double.isNaN(n)) {
066: return true;
067: }
068: }
069: return false;
070: }
071:
072: /**
073: * Return file name from a complete file name
074: * @param fileName Complete file name
075: * @param withExtension <code>true</code> file name with extension, otherwise <code>false</code>
076: * @return File name
077: */
078: public static String getFileName(String fileName,
079: boolean withExtension) {
080: if (fileName == null) {
081: return null;
082: }
083: fileName = fileName.replace('\\', File.separatorChar).replace(
084: '/', File.separatorChar);
085: int dot = fileName.length();
086: if (!withExtension) {
087: dot = fileName.lastIndexOf('.');
088: if (dot < 0) {
089: dot = fileName.length();
090: }
091: }
092: int path = fileName.lastIndexOf(File.separatorChar);
093: if (path < 0) {
094: path = 0;
095: }
096: return fileName.substring(path + 1, dot);
097: }
098:
099: /**
100: * Return path name missing last separator char
101: * @param fileName Full file name
102: * @return Return path name
103: */
104: public static String getPathName(String fileName) {
105: if (fileName == null) {
106: return null;
107: }
108: fileName = fileName.replace('\\', File.separatorChar).replace(
109: '/', File.separatorChar);
110: int path = fileName.lastIndexOf(File.separatorChar);
111: if (path < 0) {
112: path = fileName.length();
113: }
114: return fileName.substring(0, path);
115: }
116:
117: public static String getRelativeFileName(String pathName,
118: String fileName) {
119: if (fileName == null || pathName == null) {
120: return fileName;
121: }
122: if (!fileName.startsWith(pathName)) {
123: return fileName;
124: }
125: return "./" + fileName.substring(pathName.length() + 1);
126:
127: }
128:
129: /**
130: * Create a new instance of the class informed
131: * @param className Class to have a new instance
132: * @return New instance
133: * @throws ClassNotFoundException
134: * @throws IllegalAccessException
135: * @throws InstantiationException
136: */
137: public static Object newInstance(String className)
138: throws ClassNotFoundException, InstantiationException,
139: IllegalAccessException {
140: Class clazz = Class.forName(className);
141: return clazz.newInstance();
142: }
143:
144: /**
145: * Util method to invoke other methods
146: * @param methodName Methos to invoke
147: * @param obj Object that will be invoked
148: * @param args Method arguments
149: * @return Return method invokes result
150: * @throws SecurityException
151: * @throws NoSuchMethodException
152: * @throws InvocationTargetException
153: * @throws IllegalAccessException
154: * @throws IllegalArgumentException
155: */
156: public static Object invokeMethod(String methodName, Object obj,
157: Object[] args) throws SecurityException,
158: NoSuchMethodException, IllegalArgumentException,
159: IllegalAccessException, InvocationTargetException {
160: Method method = null;
161: if (args == null) {
162: args = new Object[0];
163: }
164: for (Method m : obj.getClass().getMethods()) {
165: if (m.getName().equals(methodName)
166: && m.getParameterTypes().length == args.length) {
167: // same name and parameters length
168: method = m;
169: break;
170: }
171: }
172: if (method == null) {
173: throw new NoSuchMethodException("Method " + methodName
174: + " with " + args.length
175: + " parameters, not exists at " + obj.getClass());
176: }
177: return method.invoke(obj, convertValue(method
178: .getParameterTypes(), args));
179: }
180:
181: /**
182: * Convert the values to correct data type
183: * @param types Correct types
184: * @param values Values to be converted
185: * @return An array with the values converted
186: */
187: public static Object[] convertValue(Class[] types, Object[] values) {
188: assert types.length == values.length;
189: Object ret[] = new Object[types.length];
190: for (int i = 0; i < ret.length; i++) {
191:
192: // TODO do it better
193: if (types[i] == Byte.class) {
194: ret[i] = Byte.valueOf(values[i].toString());
195: } else if (types[i] == Short.class) {
196: ret[i] = Short.valueOf(values[i].toString());
197: } else if (types[i] == Integer.class) {
198: ret[i] = Integer.valueOf(values[i].toString());
199: } else if (types[i] == Long.class) {
200: ret[i] = Long.valueOf(values[i].toString());
201: } else if (types[i] == Float.class) {
202: ret[i] = Float.valueOf(values[i].toString());
203: } else if (types[i] == Double.class) {
204: ret[i] = Double.valueOf(values[i].toString());
205: } else if (types[i] == String.class) {
206: ret[i] = String.valueOf(values[i].toString());
207: } else if (types[i] == Boolean.class) {
208: ret[i] = Boolean.valueOf(values[i].toString());
209: } else {
210: ret[i] = values[i];
211: }
212: }
213: return ret;
214: }
215:
216: /**
217: * Return a repeated <code>quantity</code> string
218: * @param s String to be repeated
219: * @param quantity Quantity to repeat
220: * @return String builded
221: */
222: public static String repeatString(String s, int quantity) {
223: StringBuilder b = new StringBuilder("");
224: for (int i = 0; i < quantity; i++) {
225: b.append(s);
226: }
227: return b.toString();
228: }
229: }
|