package net.sourceforge.pain.logic.fn.util;
import net.sourceforge.pain.data.type.*;
/**
* PAiN Date: 05.06.2003 Time: 1:57:10
*/
public final class Utils {
private final static String[] spaces = new String[20];
static {
StringBuffer buf = new StringBuffer(spaces.length);
for (int i = 0; i < spaces.length; i++) {
buf.append(' ');
spaces[i] = buf.toString();
}
}
public static String getStackTrace(Exception e) {
final StackTraceElement[] stack = e.getStackTrace();
StringBuffer result = new StringBuffer(2048);
for (int i = 0; i < stack.length; i++) {
result.append("\t");
result.append(stack[i].toString());
result.append("\n");
}
return result.toString();
}
public static String whiteSpaceChars(int n) {
if (n < 10) {
return spaces[n];
} else {
StringBuffer s = new StringBuffer(n);
while (--n >= 0) {
s.append(' ');
}
return s.toString();
}
}
public static int numStrLen(int num) {
int strLen = num < 0 ? 2 : 1;
while (Math.abs(num) > 9) {
strLen++;
num = num / 10;
}
return strLen;
}
public static String classNameWithoutPackage(Class clazz) {
String name = clazz.getName();
int i = name.lastIndexOf('.');
return name.substring(i + 1);
}
public static String formatName(String name) {
return Character.toUpperCase(name.charAt(0)) + name.substring(1);
}
public static int parseDirection(final char direction) throws IllegalArgumentException {
int dir;
switch (direction) {
case 'n':
case 'N':
dir = LinkedSpace.DIR_NORTH;
break;
case 'e':
case 'E':
dir = LinkedSpace.DIR_EAST;
break;
case 's':
case 'S':
dir = LinkedSpace.DIR_SOUTH;
break;
case 'w':
case 'W':
dir = LinkedSpace.DIR_WEST;
break;
case 'u':
case 'U':
dir = LinkedSpace.DIR_UP;
break;
case 'd':
case 'D':
dir = LinkedSpace.DIR_DOWN;
break;
default:
throw new IllegalArgumentException("Illegal Direction:" + direction);
}
return dir;
}
public static String formattedStacktrace(Exception e) {
return "{RError!: " + e.getClass().getName() + ":" + e.getMessage() + "\n{w" + getStackTrace(e) + "{x";
}
}