org.simpleflatmapper.jdbc.spring.DynamicPlaceHolderValueGetterSource Maven / Gradle / Ivy
package org.simpleflatmapper.jdbc.spring;
import org.simpleflatmapper.jdbc.JdbcColumnKey;
import org.simpleflatmapper.map.MapperConfig;
import org.simpleflatmapper.reflect.meta.ClassMeta;
import java.util.concurrent.atomic.AtomicReference;
public class DynamicPlaceHolderValueGetterSource implements PlaceHolderValueGetterSource {
private final AtomicReference> delegate;
private final ClassMeta classMeta;
private final MapperConfig mapperConfig;
@SuppressWarnings("unchecked")
public DynamicPlaceHolderValueGetterSource(ClassMeta classMeta, MapperConfig mapperConfig) {
this.classMeta = classMeta;
this.mapperConfig = mapperConfig;
this.delegate = new AtomicReference>(new ArrayPlaceHolderValueGetterSource(new PlaceHolderValueGetter[0]));
}
@Override
public PlaceHolderValueGetter getPlaceHolderValueGetter(String column) {
do {
PlaceHolderValueGetterSource ssp = delegate.get();
PlaceHolderValueGetter parameter = ssp.getPlaceHolderValueGetter(column);
if (parameter != null) {
return parameter;
} else {
PlaceHolderValueGetterSource nssp = addColumn(ssp, column);
if (delegate.compareAndSet(ssp, nssp)) {
return nssp.getPlaceHolderValueGetter(column);
}
}
} while(true);
}
@Override
public Iterable> getParameters() {
return delegate.get().getParameters();
}
private PlaceHolderValueGetterSource addColumn(PlaceHolderValueGetterSource ssp, String column) {
SqlParameterSourceBuilder builder = new SqlParameterSourceBuilder(classMeta, mapperConfig);
for(PlaceHolderValueGetter ph : ssp.getParameters()) {
builder.add(ph.getColumn());
}
builder.add(column);
return builder.buildSource();
}
}