org.babyfish.jimmer.sql.cache.chain.ChainCacheBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jimmer-sql Show documentation
Show all versions of jimmer-sql Show documentation
A revolutionary ORM framework for both java and kotlin
package org.babyfish.jimmer.sql.cache.chain;
import org.babyfish.jimmer.sql.cache.Cache;
import java.util.ArrayList;
import java.util.List;
public class ChainCacheBuilder {
private final List> binders = new ArrayList<>();
private Boolean hasParameterizedBinder = null;
private boolean hasLockableBinder = false;
private Binder.TrackingMode trackingMode = Binder.TrackingMode.NONE;
public ChainCacheBuilder add(LoadingBinder binder) {
if (binder != null) {
if (Boolean.TRUE.equals(hasParameterizedBinder)) {
throw new IllegalStateException(
"Parameterized binder and normal binder cannot be mixed"
);
}
hasParameterizedBinder = false;
validateNewBinder(binder);
binders.add(binder);
}
return this;
}
public ChainCacheBuilder add(LoadingBinder.Parameterized binder) {
if (binder != null) {
if (Boolean.FALSE.equals(hasParameterizedBinder)) {
throw new IllegalStateException(
"Parameterized binder and normal binder cannot be mixed"
);
}
hasParameterizedBinder = true;
validateNewBinder(binder);
binders.add(binder);
}
return this;
}
public ChainCacheBuilder add(SimpleBinder binder) {
if (binder != null) {
boolean isParameterized = binder instanceof SimpleBinder.Parameterized, ?>;
if (hasParameterizedBinder != null && !hasParameterizedBinder.equals(isParameterized)) {
throw new IllegalStateException(
"Parameterized binder and normal binder cannot be mixed"
);
}
hasParameterizedBinder = isParameterized;
validateNewBinder(binder);
binders.add(binder);
}
return this;
}
private void validateNewBinder(Binder binder) {
if (binder instanceof LockedBinder) {
this.hasLockableBinder = true;
} else if (this.hasLockableBinder) {
throw new IllegalStateException(
"Non-lockable binder cannot be added after lockable binder"
);
}
switch (binder.tracingMode()) {
case NONE:
case CONSUMER:
if (trackingMode != Binder.TrackingMode.NONE) {
throw new IllegalStateException(
"The binder with the tracking mode \"" +
binder.tracingMode() +
"\" cannot be added " +
"after lockable binder with the tracking mode \"" +
trackingMode.name() +
"\""
);
}
break;
case PRODUCER:
if (trackingMode != Binder.TrackingMode.CONSUMER && trackingMode != Binder.TrackingMode.NONE) {
throw new IllegalStateException(
"The binder with the tracking mode \"" +
binder.tracingMode() +
"\" cannot be added " +
"after lockable binder with the tracking mode \"" +
trackingMode.name() +
"\""
);
}
break;
}
}
public Cache build() {
List> binders = this.binders;
if (binders.isEmpty()) {
return null;
}
if (hasParameterizedBinder) {
return new ParameterizedChainCacheImpl<>(binders);
}
return new ChainCacheImpl<>(binders);
}
}