Search in sources :

Example 1 with JdbiCollectors

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);
    }
}
Also used : RegisterCollectorFactory(org.jdbi.v3.sqlobject.config.RegisterCollectorFactory) JdbiCollectors(org.jdbi.v3.core.collector.JdbiCollectors)

Example 2 with JdbiCollectors

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)));
}
Also used : JdbiCollectors(org.jdbi.v3.core.collector.JdbiCollectors) Type(java.lang.reflect.Type) ColumnMappers(org.jdbi.v3.core.mapper.ColumnMappers) ColumnMapper(org.jdbi.v3.core.mapper.ColumnMapper) Optional(java.util.Optional) GenericTypes(org.jdbi.v3.core.generic.GenericTypes) ColumnMapperFactory(org.jdbi.v3.core.mapper.ColumnMapperFactory) ConfigRegistry(org.jdbi.v3.core.config.ConfigRegistry) JdbiCollectors(org.jdbi.v3.core.collector.JdbiCollectors)

Example 3 with JdbiCollectors

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"));
}
Also used : GenericType(org.jdbi.v3.core.generic.GenericType) JdbiCollectors(org.jdbi.v3.core.collector.JdbiCollectors) Collector(java.util.stream.Collector) Map(java.util.Map) BiMap(com.google.common.collect.BiMap) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 4 with JdbiCollectors

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"));
}
Also used : GenericType(org.jdbi.v3.core.generic.GenericType) JdbiCollectors(org.jdbi.v3.core.collector.JdbiCollectors) Collector(java.util.stream.Collector) Map(java.util.Map) BiMap(com.google.common.collect.BiMap) ImmutableMap(com.google.common.collect.ImmutableMap)

Aggregations

JdbiCollectors (org.jdbi.v3.core.collector.JdbiCollectors)4 BiMap (com.google.common.collect.BiMap)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 Map (java.util.Map)2 Collector (java.util.stream.Collector)2 GenericType (org.jdbi.v3.core.generic.GenericType)2 Type (java.lang.reflect.Type)1 Optional (java.util.Optional)1 ConfigRegistry (org.jdbi.v3.core.config.ConfigRegistry)1 GenericTypes (org.jdbi.v3.core.generic.GenericTypes)1 ColumnMapper (org.jdbi.v3.core.mapper.ColumnMapper)1 ColumnMapperFactory (org.jdbi.v3.core.mapper.ColumnMapperFactory)1 ColumnMappers (org.jdbi.v3.core.mapper.ColumnMappers)1 RegisterCollectorFactory (org.jdbi.v3.sqlobject.config.RegisterCollectorFactory)1