001package org.javasimon.jdbc4;
002
003import java.sql.SQLException;
004import java.sql.Wrapper;
005
006/**
007 * Helper class for implementing {@link Wrapper} on wrappers.
008 *
009 * @param <D> delegate type
010 * @author gquintana
011 */
012public final class WrapperSupport<D extends Wrapper> implements Wrapper {
013        /**
014         * Delegate instance.
015         */
016        private final D delegate;
017
018        /**
019         * Interface implemented by delegate.
020         */
021        private final Class<D> delegateType;
022
023        public WrapperSupport(D delegate, Class<D> delegateType) {
024                this.delegate = delegate;
025                this.delegateType = delegateType;
026        }
027
028        public boolean isWrapperFor(Class<?> iface) throws SQLException {
029                return delegateType.equals(iface) || delegate.isWrapperFor(iface);
030        }
031
032        public <T> T unwrap(Class<T> iface) throws SQLException {
033                if (delegateType.equals(iface)) {
034                        return delegate.isWrapperFor(iface) ? delegate.unwrap(iface) : iface.cast(delegate);
035                } else {
036                        return delegate.unwrap(iface);
037                }
038        }
039}