Clean-up and minor changes in core classes.

Adapted new code style.
No or few functional changes.
This commit is contained in:
Harald Kuhr
2011-02-16 22:29:23 +01:00
parent df0d3f90e8
commit 191643a36c
16 changed files with 298 additions and 127 deletions
@@ -276,19 +276,19 @@ public final class BeanUtil {
}
try {
// If this does not throw an excption, it works
// If this does not throw an exception, it works
method = pObject.getClass().getMethod(pName, pParams);
}
catch (Throwable t) {
// Ignore
}
// 2: Try any supertypes of paramType, to see if we have a match
// 2: Try any super-types of paramType, to see if we have a match
if (method == null) {
while ((paramType = paramType.getSuperclass()) != null) {
pParams[0] = paramType;
try {
// If this does not throw an excption, it works
// If this does not throw an exception, it works
method = pObject.getClass().getMethod(pName, pParams);
}
catch (Throwable t) {
@@ -365,6 +365,9 @@ public final class BeanUtil {
}
}
// TODO: Convert value to single-value array if needed
// TODO: Convert CSV String to string array (or potentially any type of array)
// TODO: Convert other types
if (pValue instanceof String) {
Converter converter = Converter.getInstance();
@@ -596,8 +599,7 @@ public final class BeanUtil {
catch (NoSuchMethodException ignore) {
// If invocation failed, convert lisp-style and try again
if (pLispToCamel && property.indexOf('-') > 0) {
setPropertyValue(pBean, StringUtil.lispToCamel(property, false),
entry.getValue());
setPropertyValue(pBean, StringUtil.lispToCamel(property, false), entry.getValue());
}
}
}
@@ -5,6 +5,8 @@ import java.io.IOException;
import java.lang.reflect.UndeclaredThrowableException;
import java.sql.SQLException;
import static com.twelvemonkeys.lang.Validate.notNull;
/**
* ExceptionUtil
*
@@ -14,6 +16,15 @@ import java.sql.SQLException;
*/
public final class ExceptionUtil {
/**
* Re-throws an exception, either as-is if the exception was already unchecked, otherwise wrapped in
* a {@link RuntimeException}.
* "Expected" exception types are wrapped in {@link RuntimeException}s, while
* "unexpected" exception types are wrapped in {@link java.lang.reflect.UndeclaredThrowableException}s.
*
* @param pThrowable the exception to launder
* @param pExpectedTypes the types of exception the code is expected to throw
*/
/*public*/ static void launder(final Throwable pThrowable, Class<? extends Throwable>... pExpectedTypes) {
if (pThrowable instanceof Error) {
throw (Error) pThrowable;
@@ -40,36 +51,38 @@ public final class ExceptionUtil {
throwAs(RuntimeException.class, pThrowable);
}
/*public*/ static void handle(final Throwable pThrowable, final ThrowableHandler<? extends Throwable>... pHandler) {
handleImpl(pThrowable, pHandler);
@SuppressWarnings({"unchecked"})
/*public*/ static void handle(final Throwable pThrowable, final ThrowableHandler<? extends Throwable>... pHandlers) {
handleImpl(pThrowable, (ThrowableHandler<Throwable>[]) pHandlers);
}
@SuppressWarnings({"unchecked"})
private static <T extends Throwable> void handleImpl(final Throwable pThrowable, final ThrowableHandler<T>... pHandler) {
private static void handleImpl(final Throwable pThrowable, final ThrowableHandler<Throwable>... pHandlers) {
// TODO: Sort more specific throwable handlers before less specific?
for (ThrowableHandler<T> handler : pHandler) {
for (ThrowableHandler<Throwable> handler : pHandlers) {
if (handler.handles(pThrowable)) {
handler.handle((T) pThrowable);
handler.handle(pThrowable);
return;
}
}
// Not handled, re-throw
throwUnchecked(pThrowable);
}
public static abstract class ThrowableHandler<T extends Throwable> {
private Class<? extends T>[] mThrowables;
private final Class<? extends T>[] throwables;
protected ThrowableHandler(final Class<? extends T>... pThrowables) {
// TODO: Assert not null
mThrowables = pThrowables.clone();
throwables = notNull(pThrowables).clone();
}
final public boolean handles(final Throwable pThrowable) {
for (Class<? extends T> throwable : mThrowables) {
for (Class<? extends T> throwable : throwables) {
if (throwable.isAssignableFrom(pThrowable.getClass())) {
return true;
}
}
return false;
}
@@ -39,24 +39,24 @@ public final class Platform {
/**
* Normalized operating system constant
*/
final OperatingSystem mOS;
final OperatingSystem os;
/**
* Unormalized operating system version constant (for completeness)
*/
final String mVersion;
final String version;
/**
* Normalized system architecture constant
*/
final Architecture mArchitecture;
final Architecture architecture;
static final private Platform INSTANCE = new Platform();
private Platform() {
mOS = normalizeOperatingSystem();
mVersion = System.getProperty("os.version");
mArchitecture = normalizeArchitecture(mOS);
os = normalizeOperatingSystem();
version = System.getProperty("os.version");
architecture = normalizeArchitecture(os);
}
private static OperatingSystem normalizeOperatingSystem() {
@@ -138,21 +138,21 @@ public final class Platform {
* @return this platform's OS.
*/
public OperatingSystem getOS() {
return mOS;
return os;
}
/**
* @return this platform's OS version.
*/
public String getVersion() {
return mVersion;
return version;
}
/**
* @return this platform's architecture.
*/
public Architecture getArchitecture() {
return mArchitecture;
return architecture;
}
/**
@@ -160,7 +160,7 @@ public final class Platform {
* @return the current {@code OperatingSystem}.
*/
public static OperatingSystem os() {
return INSTANCE.mOS;
return INSTANCE.os;
}
/**
@@ -168,7 +168,7 @@ public final class Platform {
* @return the current OS version.
*/
public static String version() {
return INSTANCE.mVersion;
return INSTANCE.version;
}
/**
@@ -176,7 +176,7 @@ public final class Platform {
* @return the current {@code Architecture}.
*/
public static Architecture arch() {
return INSTANCE.mArchitecture;
return INSTANCE.architecture;
}
/**
@@ -197,14 +197,14 @@ public final class Platform {
Unknown(System.getProperty("os.arch"));
final String mName;// for debug only
final String name;// for debug only
private Architecture(String pName) {
mName = pName;
name = pName;
}
public String toString() {
return mName;
return name;
}
}
@@ -225,20 +225,20 @@ public final class Platform {
Unknown(System.getProperty("os.name"), "");
final String mId;
final String mName;// for debug only
final String id;
final String name;// for debug only
private OperatingSystem(String pName, String pId) {
mName = pName;
mId = pId;
name = pName;
id = pId;
}
public String getName() {
return mName;
return name;
}
public String toString() {
return mId;
return id;
}
}
}
@@ -77,7 +77,7 @@ public final class StringUtil {
}
/**
* Constructs a new {@link String} by decoding the specified subarray of bytes using the specified charset.
* Constructs a new {@link String} by decoding the specified sub array of bytes using the specified charset.
* Replacement for {@link String#String(byte[], int, int, String) new String(byte[], int, int, String)}, that does
* not throw the checked {@link UnsupportedEncodingException},
* but instead the unchecked {@link UnsupportedCharsetException} if the character set is not supported.
@@ -1580,7 +1580,7 @@ public final class StringUtil {
* Converts a string array to a string separated by the given delimiter.
*
* @param pStringArray the string array
* @param pDelimiterString the delimter string
* @param pDelimiterString the delimiter string
* @return string of delimiter separated values
* @throws IllegalArgumentException if {@code pDelimiterString == null}
*/
@@ -16,6 +16,8 @@ import java.util.Map;
* @version $Id: //depot/branches/personal/haraldk/twelvemonkeys/release-2/twelvemonkeys-core/src/main/java/com/twelvemonkeys/lang/Validate.java#1 $
*/
public final class Validate {
// TODO: Make it possible to throw IllegalStateException instead of IllegalArgumentException?
private static final String UNSPECIFIED_PARAM_NAME = "method parameter";
private Validate() {}
@@ -121,4 +123,16 @@ public final class Validate {
return pParameter;
}
public static boolean isTrue(final boolean pExpression, final String pMessage) {
return isTrue(pExpression, pExpression, pMessage);
}
public static <T> T isTrue(final boolean condition, final T value, final String message) {
if (!condition) {
throw new IllegalArgumentException(String.format(message, value));
}
return value;
}
}