001package net.sf.hajdbc.invocation; 002 003import java.lang.reflect.Method; 004 005import net.sf.hajdbc.Database; 006import net.sf.hajdbc.ExceptionFactory; 007import net.sf.hajdbc.logging.Level; 008import net.sf.hajdbc.logging.Logger; 009import net.sf.hajdbc.logging.LoggerFactory; 010import net.sf.hajdbc.util.reflect.Methods; 011 012public class SimpleInvoker<Z, D extends Database<Z>, T, R, E extends Exception> implements Invoker<Z, D, T, R, E> 013{ 014 private static final Logger logger = LoggerFactory.getLogger(SimpleInvoker.class); 015 016 private final Method method; 017 private final Object[] parameters; 018 private final ExceptionFactory<E> exceptionFactory; 019 020 /** 021 * @param method 022 * @param parameters 023 */ 024 public SimpleInvoker(Method method, Object[] parameters, ExceptionFactory<E> exceptionFactory) 025 { 026 this.method = method; 027 this.parameters = parameters; 028 this.exceptionFactory = exceptionFactory; 029 } 030 031 public Method getMethod() 032 { 033 return this.method; 034 } 035 036 public Object[] getParameters() 037 { 038 return this.parameters; 039 } 040 041 public ExceptionFactory<E> getExceptionFactory() 042 { 043 return this.exceptionFactory; 044 } 045 046 /** 047 * @see net.sf.hajdbc.invocation.Invoker#invoke(net.sf.hajdbc.Database, java.lang.Object) 048 */ 049 @Override 050 public R invoke(D database, T object) throws E 051 { 052 logger.log(Level.TRACE, "Invoking {0} against {1}", this.method, database); 053 return Methods.<R, E>invoke(this.method, this.exceptionFactory, object, this.parameters); 054 } 055 056 @Override 057 public int hashCode() 058 { 059 return this.method.hashCode(); 060 } 061 062 @Override 063 public boolean equals(Object object) 064 { 065 if ((object != null) && (object instanceof SimpleInvoker)) 066 { 067 @SuppressWarnings("unchecked") 068 SimpleInvoker<Z, D, T, R, E> invoker = (SimpleInvoker<Z, D, T, R, E>) object; 069 return this.method.equals(invoker.method); 070 } 071 return false; 072 } 073 074 /** 075 * {@inheritDoc} 076 * @see java.lang.Object#toString() 077 */ 078 @Override 079 public String toString() 080 { 081 return this.method.toString(); 082 } 083}