
jdbi_modules.internal.CollectorImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of core Show documentation
Show all versions of core Show documentation
An Api for modular SQL-Querys and Mapping in java
The newest version!
package jdbi_modules.internal;
import jdbi_modules.Collector;
import org.jdbi.v3.core.mapper.RowMapper;
import org.jdbi.v3.core.statement.StatementContext;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.function.BiFunction;
import java.util.function.Consumer;
/**
* @param the type of the collection the collector handles
* @param the type of elements in the collection
* @since 14.04.2018
*/
class CollectorImpl, Type> implements Collector {
private final RowView rowView;
private final ResultSet resultSet;
private final StatementContext context;
private CollectionType collection;
private BiFunction comparator = Object::equals;
private List added = new ArrayList<>();
CollectorImpl(final CollectionType collection, final RowView rowView, final ResultSet resultSet, final StatementContext context) {
this.collection = collection;
this.rowView = rowView;
this.resultSet = resultSet;
this.context = context;
}
@Override
public Collector setComparator(@NotNull final BiFunction comparator) {
this.comparator = comparator;
return this;
}
@Override
public CollectionType get() {
return collection;
}
@Override
public CollectorImpl appendUnique(@Nullable final Type type, @NotNull final Consumer applier) {
if (Objects.isNull(type)) {
return this;
}
applier.accept(collection.stream().filter(b -> comparator.apply(b, type)).findFirst().orElseGet(() -> {
collection.add(type);
added.add(type);
return type;
}));
return this;
}
@Override
public CollectorImpl appendUnique(@Nullable final Type type) {
if (Objects.isNull(type)) {
return this;
}
collection.stream().filter(b -> comparator.apply(b, type)).findFirst().orElseGet(() -> {
collection.add(type);
added.add(type);
return type;
});
return this;
}
@Override
public CollectorImpl append(@Nullable final Type type, @NotNull final Consumer applyer) {
if (Objects.isNull(type)) {
return this;
}
collection.add(type);
applyer.accept(type);
added.add(type);
return this;
}
@Override
public CollectorImpl append(@Nullable final Type type) {
if (Objects.isNull(type)) {
return this;
}
collection.add(type);
added.add(type);
return this;
}
@Override
public CollectorImpl appendUniqueWithRowView(@NotNull final Class clazz, @NotNull final Consumer applier) {
final Type type = rowView.getRow(clazz);
return appendUnique(type, applier);
}
@Override
public CollectorImpl appendUniqueWithRowView(@NotNull final Class clazz) {
final Type type = rowView.getRow(clazz);
return appendUnique(type);
}
@Override
public CollectorImpl appendWithRowView(@NotNull final Class clazz, @NotNull final Consumer applier) {
final Type type = rowView.getRow(clazz);
return append(type, applier);
}
@Override
public CollectorImpl appendWithRowView(@NotNull final Class clazz) {
final Type type = rowView.getRow(clazz);
return append(type);
}
@Override
public CollectorImpl appendWithRowMapper(@NotNull final RowMapper rowMapper, @NotNull final Consumer applier) throws SQLException {
final Type type = rowMapper.map(resultSet, context);
return append(type, applier);
}
@Override
public CollectorImpl appendWithRowMapper(@NotNull final RowMapper rowMapper) throws SQLException {
final Type type = rowMapper.map(resultSet, context);
return append(type);
}
@Override
public Collector appendUniqueWithRowMapper(@NotNull final RowMapper rowMapper, @NotNull final Consumer applier) throws SQLException {
final Type type = rowMapper.map(resultSet, context);
return appendUnique(type, applier);
}
@Override
public Collector appendUniqueWithRowMapper(@NotNull final RowMapper rowMapper) throws SQLException {
final Type type = rowMapper.map(resultSet, context);
return appendUnique(type);
}
void useCollection(final CollectionType collection) {
this.collection = collection;
this.added.clear();
}
void applyOnAdded(@NotNull final Consumer consumer) {
added.forEach(consumer);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy