com.github.napp.util.UClose Maven / Gradle / Ivy
/**
*
*/
package com.github.napp.util;
import java.io.Closeable;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import org.apache.commons.logging.LogFactory;
import com.github.napp.util.mass.VoidEvaluator;
/**
* @author Alexandru Bledea
* @since Sep 22, 2013
*/
public class UClose {
public static VoidEvaluator CLOSE_CLOSEABLE = new CloseEvaluator() {
/* (non-Javadoc)
* @see com.github.napp.util.UClose.CloseEvaluator#close(java.lang.Object)
*/
@Override
protected void close(Closeable object) throws Exception {
object.close();
}
};
public static VoidEvaluator CLOSE_RESULT_SET = new CloseEvaluator() {
/* (non-Javadoc)
* @see com.github.napp.util.UClose.CloseEvaluator#close(java.lang.Object)
*/
@Override
protected void close(ResultSet object) throws Exception {
if (!object.isClosed()) {
object.close();
}
}
};
public static VoidEvaluator CLOSE_STATEMENT = new CloseEvaluator() {
/* (non-Javadoc)
* @see com.github.napp.util.UClose.CloseEvaluator#close(java.lang.Object)
*/
@Override
protected void close(Statement object) throws Exception {
if (!object.isClosed()) {
object.close();
}
}
};
public static VoidEvaluator CLOSE_CONNECTION = new CloseEvaluator() {
/* (non-Javadoc)
* @see com.github.napp.util.UClose.CloseEvaluator#close(java.lang.Object)
*/
@Override
protected void close(Connection object) throws Exception {
if (!object.isClosed()) {
object.close();
}
}
};
/**
* @author Alexandru Bledea
* @since Sep 22, 2013
* @param
*/
private static abstract class CloseEvaluator extends VoidEvaluator {
/* (non-Javadoc)
* @see com.github.napp.util.VoidEvaluator#evaluateNoResult(java.lang.Object)
*/
@Override
public void evaluateNoResult(O obj) {
if (obj != null) {
try {
close(obj);
} catch (Throwable t) {
LogFactory.getLog(getClass()).error("Failed to close", t);
}
}
};
/**
* @param object
*/
protected abstract void close(O object) throws Exception;
}
}