use of org.jdbi.v3.core.collector.JdbiCollectors in project jdbi by jdbi.
the class RegisterCollectorFactoryImpl method configureForType.
@Override
public void configureForType(ConfigRegistry registry, Annotation annotation, Class<?> sqlObjectType) {
RegisterCollectorFactory registerCollectorFactory = (RegisterCollectorFactory) annotation;
JdbiCollectors collectors = registry.get(JdbiCollectors.class);
Class<? extends CollectorFactory> type = registerCollectorFactory.value();
try {
collectors.register(type.newInstance());
} catch (InstantiationException | IllegalAccessException e) {
throw new IllegalStateException("Unable to instantiate container factory", e);
}
}
use of org.jdbi.v3.core.collector.JdbiCollectors in project jdbi by jdbi.
the class SqlArrayMapperFactory method build.
@Override
@SuppressWarnings("unchecked")
public Optional<ColumnMapper<?>> build(Type type, ConfigRegistry config) {
final Class<?> erasedType = GenericTypes.getErasedType(type);
if (erasedType.isArray()) {
Class<?> elementType = erasedType.getComponentType();
return elementTypeMapper(elementType, config).map(elementMapper -> new ArrayColumnMapper(elementMapper, elementType));
}
JdbiCollectors collectorRegistry = config.get(JdbiCollectors.class);
return collectorRegistry.findFor(type).flatMap(collector -> collectorRegistry.findElementTypeFor(type).flatMap(elementType -> elementTypeMapper(elementType, config)).map(elementMapper -> new CollectorColumnMapper(elementMapper, collector)));
}
use of org.jdbi.v3.core.collector.JdbiCollectors in project jdbi by jdbi.
the class TestGuavaCollectors method testMapCollector.
@SuppressWarnings("unchecked")
private <M extends Map<Long, String>> void testMapCollector(Class<? extends Map> erasedType, GenericType<M> genericType) {
JdbiCollectors registry = dbRule.getJdbi().getConfig(JdbiCollectors.class);
assertThat(registry.findElementTypeFor(genericType.getType())).contains(new GenericType<Map.Entry<Long, String>>() {
}.getType());
Collector<Map.Entry<Long, String>, ?, M> collector = (Collector<Map.Entry<Long, String>, ?, M>) registry.findFor(genericType.getType()).orElseThrow(() -> new IllegalStateException("Missing collector for " + genericType));
M map = Stream.of(entry(1L, "foo"), entry(2L, "bar")).collect(collector);
assertThat(map).isInstanceOf(erasedType).containsExactly(entry(1L, "foo"), entry(2L, "bar"));
}
use of org.jdbi.v3.core.collector.JdbiCollectors in project jdbi by jdbi.
the class TestGuavaCollectors method testMultimapCollector.
@SuppressWarnings("unchecked")
private <M extends Multimap<Long, String>> void testMultimapCollector(Class<? extends Multimap> erasedType, GenericType<M> genericType) {
JdbiCollectors registry = dbRule.getJdbi().getConfig(JdbiCollectors.class);
assertThat(registry.findElementTypeFor(genericType.getType())).contains(new GenericType<Map.Entry<Long, String>>() {
}.getType());
Collector<Map.Entry<Long, String>, ?, M> collector = (Collector<Map.Entry<Long, String>, ?, M>) registry.findFor(genericType.getType()).orElseThrow(() -> new IllegalStateException("Missing collector for " + genericType));
M map = Stream.of(entry(1L, "foo"), entry(2L, "bar")).collect(collector);
assertThat(map).isInstanceOf(erasedType).containsAllEntriesOf(ImmutableMultimap.of(1L, "foo", 2L, "bar"));
}
Aggregations