@Override public Optional<ColumnMapper<?>> build(Type type, ConfigRegistry config) { return FACTORIES.stream() .flatMap(factory -> JdbiOptionals.stream(factory.build(type, config))) .findFirst(); } }
/** * Register a column mapper for a given explicit {@link Type} * Column mappers may be reused by {@link RowMapper} to map individual columns. * * @param type the type to match with equals. * @param mapper the column mapper * @return this */ public ColumnMappers register(Type type, ColumnMapper<?> mapper) { return this.register(ColumnMapperFactory.of(type, mapper)); }
/** * Adapts a {@link ColumnMapperFactory} into a QualifiedColumnMapperFactory. The returned * factory only matches qualified types with zero qualifiers. * * @param factory the factory to adapt */ static QualifiedColumnMapperFactory adapt(ColumnMapperFactory factory) { Set<Annotation> qualifiers = getQualifiers(factory.getClass()); return (type, config) -> type.getQualifiers().equals(qualifiers) ? factory.build(type.getType(), config) : Optional.empty(); }
/** * Register a column mapper for a given explicit {@link GenericType} * Column mappers may be reused by {@link RowMapper} to map individual columns. * * @param <T> the type * @param type the generic type to match with equals. * @param mapper the column mapper * @return this */ public <T> ColumnMappers register(GenericType<T> type, ColumnMapper<T> mapper) { return this.register(ColumnMapperFactory.of(type.getType(), mapper)); }
/** * Obtain a column mapper for the given type. * * @param type the target type to map to * @return a ColumnMapper for the given type, or empty if no column mapper is registered for the given type. */ public Optional<ColumnMapper<?>> findFor(Type type) { // ConcurrentHashMap can enter an infinite loop on nested computeIfAbsent calls. // Since column mappers can decorate other column mappers, we have to populate the cache the old fashioned way. // See https://bugs.openjdk.java.net/browse/JDK-8062841, https://bugs.openjdk.java.net/browse/JDK-8142175 ColumnMapper<?> cached = cache.get(type); if (cached != null) { return Optional.of(cached); } Optional<ColumnMapper<?>> mapper = factories.stream() .flatMap(factory -> toStream(factory.build(type, registry))) .findFirst(); mapper.ifPresent(m -> cache.put(type, m)); return mapper; }
/** * Register a column mapper for a given explicit {@link Type} * Column mappers may be reused by {@link RowMapper} to map individual columns. * * @param type the type to match with equals. * @param mapper the column mapper * @return this */ public ColumnMappers register(Type type, ColumnMapper<?> mapper) { return this.register(ColumnMapperFactory.of(type, mapper)); }