001package org.javasimon.callback.async;
002
003import org.javasimon.proxy.DelegatingMethodInvocation;
004import org.javasimon.proxy.DelegatingProxyFactory;
005
006/**
007 * Proxy factory which can be used make any class asynchronous.
008 *
009 * @param <T>
010 * @author gerald
011 */
012public class ExecutorProxyFactory<T> extends DelegatingProxyFactory<T> {
013
014        /** Executor used for invoking methods on delegate object. */
015        private Executor executor;
016
017        /**
018         * Constructor.
019         *
020         * @param delegate Delegate object
021         */
022        public ExecutorProxyFactory(T delegate) {
023                this(delegate, Executors.async());
024        }
025
026        /**
027         * Constructor.
028         *
029         * @param delegate Delegate object
030         * @param executor Executor used, see {@link Executors}
031         */
032        public ExecutorProxyFactory(T delegate, Executor executor) {
033                super(delegate);
034                this.executor = executor;
035        }
036
037        /**
038         * Returns used executor.
039         *
040         * @return Executor
041         */
042        public Executor getExecutor() {
043                return executor;
044        }
045
046        /**
047         * Sets used executor.
048         *
049         * @param executor Executor
050         */
051        public void setExecutor(Executor executor) {
052                this.executor = executor;
053        }
054
055        @Override
056        protected Object invoke(DelegatingMethodInvocation<T> delegatingMethodInvocation) throws Throwable {
057                return executor.execute(delegatingMethodInvocation);
058        }
059}