Java Pearls: assertNotNull() – the expert version

Recently a colleague stumbled upon this pearl doing a production code review:

/**
 * Assert not null.
 * @param object the object
 * @param  element type
 * @return the not null object
 */
public static  T assertNotNull(T object) {
	if (object == null) {
		assertNotNull(object, "");
		throw thisLineShouldNeverBeReached();
	} 
	return object;
}
/**
 * Assert not null.
 * @param object the object
 * @param msg the msg
 */
public static void assertNotNull(Object object, String msg) {
	if (object == null) {
		throwRuntimeException("assertion: object is null but was expected not to be null. \n"
				+ msg);
	}
}

well at least it is a generic method 🙂

The commenting is also from the production code… Hell of a expert developer.

Advertisements


Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s