Skip to content

Commit

Permalink
HHH-11944 Make the AbstractDelegatingSessionBuilder hierarchy generic
Browse files Browse the repository at this point in the history
  • Loading branch information
gsmet committed Sep 1, 2017
1 parent 3620e1e commit 123ef45
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 78 deletions.
Expand Up @@ -40,7 +40,7 @@
* to a specialization of {@link MetadataBuilderImplementor} * to a specialization of {@link MetadataBuilderImplementor}
*/ */
@SuppressWarnings("unused") @SuppressWarnings("unused")
public abstract class AbstractDelegatingMetadataBuilderImplementor<T extends AbstractDelegatingMetadataBuilderImplementor<T>> implements MetadataBuilderImplementor { public abstract class AbstractDelegatingMetadataBuilderImplementor<T extends MetadataBuilderImplementor> implements MetadataBuilderImplementor {


private final MetadataBuilderImplementor delegate; private final MetadataBuilderImplementor delegate;


Expand Down
Expand Up @@ -39,7 +39,7 @@
* @param <T> The type of a specific sub-class; Allows sub-classes to narrow down the return-type of the contract methods * @param <T> The type of a specific sub-class; Allows sub-classes to narrow down the return-type of the contract methods
* to a specialization of {@link SessionFactoryBuilder} * to a specialization of {@link SessionFactoryBuilder}
*/ */
public abstract class AbstractDelegatingSessionFactoryBuilder<T extends AbstractDelegatingSessionFactoryBuilder<T>> implements SessionFactoryBuilder { public abstract class AbstractDelegatingSessionFactoryBuilder<T extends SessionFactoryBuilder> implements SessionFactoryBuilder {
private final SessionFactoryBuilder delegate; private final SessionFactoryBuilder delegate;


public AbstractDelegatingSessionFactoryBuilder(SessionFactoryBuilder delegate) { public AbstractDelegatingSessionFactoryBuilder(SessionFactoryBuilder delegate) {
Expand Down
Expand Up @@ -10,106 +10,138 @@
import java.util.TimeZone; import java.util.TimeZone;


import org.hibernate.ConnectionReleaseMode; import org.hibernate.ConnectionReleaseMode;
import org.hibernate.FlushMode;
import org.hibernate.Interceptor; import org.hibernate.Interceptor;
import org.hibernate.Session; import org.hibernate.Session;
import org.hibernate.SessionBuilder; import org.hibernate.SessionBuilder;
import org.hibernate.SessionEventListener; import org.hibernate.SessionEventListener;
import org.hibernate.resource.jdbc.spi.PhysicalConnectionHandlingMode;
import org.hibernate.resource.jdbc.spi.StatementInspector; import org.hibernate.resource.jdbc.spi.StatementInspector;


/** /**
* Base class for {@link SessionBuilder} implementations that wish to implement only parts of that contract themselves * Base class for {@link SessionBuilder} implementations that wish to implement only parts of that contract themselves
* while forwarding other method invocations to a delegate instance. * while forwarding other method invocations to a delegate instance.
* *
* @author Gunnar Morling * @author Gunnar Morling
* @author Guillaume Smet
*/ */
public abstract class AbstractDelegatingSessionBuilder implements SessionBuilder { public abstract class AbstractDelegatingSessionBuilder<T extends SessionBuilder> implements SessionBuilder<T> {


private final SessionBuilder delegate; private final SessionBuilder delegate;


public AbstractDelegatingSessionBuilder(SessionBuilder delegate) { public AbstractDelegatingSessionBuilder(SessionBuilder delegate) {
this.delegate = delegate; this.delegate = delegate;
} }


@SuppressWarnings("unchecked")
protected T getThis() {
return (T) this;
}

protected SessionBuilder getDelegate() {
return delegate;
}

@Override @Override
public Session openSession() { public Session openSession() {
return delegate.openSession(); return delegate.openSession();
} }


@Override @Override
public SessionBuilder interceptor(Interceptor interceptor) { public T interceptor(Interceptor interceptor) {
delegate.interceptor( interceptor ); delegate.interceptor( interceptor );
return this; return getThis();
} }


@Override @Override
public SessionBuilder noInterceptor() { public T noInterceptor() {
delegate.noInterceptor(); delegate.noInterceptor();
return this; return getThis();
} }


@Override @Override
public SessionBuilder statementInspector(StatementInspector statementInspector) { public T statementInspector(StatementInspector statementInspector) {
delegate.statementInspector( statementInspector ); delegate.statementInspector( statementInspector );
return this; return getThis();
} }


@Override @Override
public SessionBuilder connection(Connection connection) { public T connection(Connection connection) {
delegate.connection( connection ); delegate.connection( connection );
return this; return getThis();
} }


@SuppressWarnings("deprecation")
@Override @Override
public SessionBuilder connectionReleaseMode(ConnectionReleaseMode connectionReleaseMode) { public T connectionReleaseMode(ConnectionReleaseMode connectionReleaseMode) {
delegate.connectionReleaseMode( connectionReleaseMode ); delegate.connectionReleaseMode( connectionReleaseMode );
return this; return getThis();
} }


@Override @Override
public SessionBuilder autoJoinTransactions(boolean autoJoinTransactions) { public T autoJoinTransactions(boolean autoJoinTransactions) {
delegate.autoJoinTransactions( autoJoinTransactions ); delegate.autoJoinTransactions( autoJoinTransactions );
return this; return getThis();
} }


@Override @Override
public SessionBuilder autoClose(boolean autoClose) { public T autoClose(boolean autoClose) {
delegate.autoClose( autoClose ); delegate.autoClose( autoClose );
return this; return getThis();
} }


@SuppressWarnings("deprecation")
@Override @Override
public SessionBuilder flushBeforeCompletion(boolean flushBeforeCompletion) { public T flushBeforeCompletion(boolean flushBeforeCompletion) {
delegate.flushBeforeCompletion( flushBeforeCompletion ); delegate.flushBeforeCompletion( flushBeforeCompletion );
return this; return getThis();
} }


@Override @Override
public SessionBuilder tenantIdentifier(String tenantIdentifier) { public T tenantIdentifier(String tenantIdentifier) {
delegate.tenantIdentifier( tenantIdentifier ); delegate.tenantIdentifier( tenantIdentifier );
return this; return getThis();
} }


@Override @Override
public SessionBuilder eventListeners(SessionEventListener... listeners) { public T eventListeners(SessionEventListener... listeners) {
delegate.eventListeners( listeners ); delegate.eventListeners( listeners );
return this; return getThis();
} }


@Override @Override
public SessionBuilder clearEventListeners() { public T clearEventListeners() {
delegate.clearEventListeners(); delegate.clearEventListeners();
return this; return getThis();
} }


@Override @Override
public SessionBuilder jdbcTimeZone(TimeZone timeZone) { public T jdbcTimeZone(TimeZone timeZone) {
delegate.jdbcTimeZone(timeZone); delegate.jdbcTimeZone(timeZone);
return this; return getThis();
} }


@Override @Override
public SessionBuilder setQueryParameterValidation(boolean enabled) { public T setQueryParameterValidation(boolean enabled) {
delegate.setQueryParameterValidation( enabled ); delegate.setQueryParameterValidation( enabled );
return this; return getThis();
}

@Override
public T connectionHandlingMode(PhysicalConnectionHandlingMode mode) {
delegate.connectionHandlingMode( mode );
return getThis();
}

@Override
public T autoClear(boolean autoClear) {
delegate.autoClear( autoClear );
return getThis();
}

@Override
public T flushMode(FlushMode flushMode) {
delegate.flushMode( flushMode );
return getThis();
} }
} }
Expand Up @@ -6,29 +6,28 @@
*/ */
package org.hibernate.engine.spi; package org.hibernate.engine.spi;


import org.hibernate.SessionBuilder;

/** /**
* Base class for {@link SessionBuilderImplementor} implementations that wish to implement only parts of that contract * Base class for {@link SessionBuilderImplementor} implementations that wish to implement only parts of that contract
* themselves while forwarding other method invocations to a delegate instance. * themselves while forwarding other method invocations to a delegate instance.
* *
* @author Gunnar Morling * @author Gunnar Morling
*/ */
@SuppressWarnings("unused") public abstract class AbstractDelegatingSessionBuilderImplementor<T extends SessionBuilderImplementor>
public abstract class AbstractDelegatingSessionBuilderImplementor extends AbstractDelegatingSessionBuilder<T>
extends AbstractDelegatingSessionBuilder implements SessionBuilderImplementor<T> {
implements SessionBuilderImplementor {

private final SessionBuilderImplementor delegate;


public AbstractDelegatingSessionBuilderImplementor(SessionBuilderImplementor delegate) { public AbstractDelegatingSessionBuilderImplementor(SessionBuilderImplementor delegate) {
super( delegate ); super( delegate );
this.delegate = delegate;
} }


protected SessionBuilderImplementor getDelegate() {
return (SessionBuilderImplementor) super.getDelegate();
}

@SuppressWarnings({ "unchecked", "deprecation" })
@Override @Override
public SessionBuilder owner(SessionOwner sessionOwner) { public T owner(SessionOwner sessionOwner) {
delegate.owner( sessionOwner ); getDelegate().owner( sessionOwner );
return this; return (T) this;
} }
} }

0 comments on commit 123ef45

Please sign in to comment.