/** * Obtain a column mapper for the given type. * * @param <T> the type to map * @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. */ @SuppressWarnings("unchecked") public <T> Optional<ColumnMapper<T>> findFor(Class<T> type) { ColumnMapper<T> mapper = (ColumnMapper<T>) findFor((Type) type).orElse(null); return Optional.ofNullable(mapper); }
/** * Convenience method for {@code getConfig(ColumnMappers.class).register(type, mapper)} * * @param <T> the type * @param type the generic type to register * @param mapper the mapper to use on that type * @return this */ default <T> This registerColumnMapper(GenericType<T> type, ColumnMapper<T> mapper) { return configure(ColumnMappers.class, c -> c.register(type, mapper)); }
@Override public ColumnMappers createCopy() { return new ColumnMappers(this); } }
/** * 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) { return findFor(QualifiedType.of(type)).map(m -> (ColumnMapper<?>) m); }
/** * Convenience method for {@code getConfig(ColumnMappers.class).register(type, mapper)} * * @param type the type to register * @param mapper the mapper to use on that type * @return this */ default This registerColumnMapper(Type type, ColumnMapper<?> mapper) { return configure(ColumnMappers.class, c -> c.register(type, mapper)); }
@Override public ColumnMappers createCopy() { return new ColumnMappers(this); } }
/** * Obtain a column mapper for the given type. * * @param <T> the type to map * @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. */ @SuppressWarnings("unchecked") public <T> Optional<ColumnMapper<T>> findFor(GenericType<T> type) { ColumnMapper<T> mapper = (ColumnMapper<T>) findFor(type.getType()).orElse(null); return Optional.ofNullable(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)); }
/** * Obtain a column mapper for the given type in this context. * * @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<?>> findColumnMapperFor(Type type) { return getConfig(ColumnMappers.class).findFor(type); }
/** * Register a column mapper factory. * <p> * Column mappers may be reused by {@link RowMapper} to map individual columns. * * @param factory the column mapper factory * @return this */ public ColumnMappers register(ColumnMapperFactory factory) { return register(QualifiedColumnMapperFactory.adapt(factory)); }
private Optional<ColumnMapper<?>> elementTypeMapper(Type elementType, ConfigRegistry config) { Optional<ColumnMapper<?>> mapper = config.get(ColumnMappers.class).findFor(elementType); if (!mapper.isPresent() && elementType == Object.class) { return Optional.of((rs, num, context) -> rs.getObject(num)); } return mapper; } }
/** * Convenience method for {@code getConfig(ColumnMappers.class).register(mapper)} * * @param mapper column mapper * @return this */ default This registerColumnMapper(ColumnMapper<?> mapper) { return configure(ColumnMappers.class, c -> c.register(mapper)); }
/** * Obtain a column mapper for the given type in this context. * * @param <T> the type to map * @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 <T> Optional<ColumnMapper<T>> findColumnMapperFor(Class<T> type) { return getConfig(ColumnMappers.class).findFor(type); }
/** * Convenience method for {@code getConfig(ColumnMappers.class).register(factory)} * * @param factory column mapper factory * @return this */ default This registerColumnMapper(ColumnMapperFactory factory) { return configure(ColumnMappers.class, c -> c.register(factory)); }
/** * Obtain a column mapper for the given type in this context. * * @param <T> the type to map * @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 <T> Optional<ColumnMapper<T>> findColumnMapperFor(GenericType<T> type) { return getConfig(ColumnMappers.class).findFor(type); }
public ColumnMappers() { // TODO move to BuiltInSupportPlugin register(new SqlArrayMapperFactory()); register(new JavaTimeMapperFactory()); register(new SqlTimeMapperFactory()); register(new InternetMapperFactory()); register(new EssentialsMapperFactory()); register(new BoxedMapperFactory()); register(new PrimitiveMapperFactory()); register(new OptionalMapperFactory()); register(new EnumMapperFactory()); register(new NVarcharMapper()); }
@Beta public static <T> RowMapper<Map<String, T>> getMapperForValueType(Class<T> valueType, ConfigRegistry config) { return config.get(ColumnMappers.class) .findFor(valueType) .map(GenericMapMapper::new) .orElseThrow(() -> new RuntimeException("no column mapper found for type " + valueType)); }
/** * Register a column mapper which will have its parameterized type inspected to determine what it maps to. * Column mappers may be reused by {@link RowMapper} to map individual columns. * <p> * The parameter must be concretely parameterized, we use the type argument T to * determine if it applies to a given type. * * @param mapper the column mapper * @return this * @throws UnsupportedOperationException if the ColumnMapper is not a concretely parameterized type */ public ColumnMappers register(ColumnMapper<?> mapper) { return this.register(new InferredColumnMapperFactory(mapper)); }
@Beta public static <T> RowMapper<Map<String, T>> getMapperForValueType(GenericType<T> valueType, ConfigRegistry config) { return config.get(ColumnMappers.class) .findFor(valueType) .map(GenericMapMapper::new) .orElseThrow(() -> new RuntimeException("no column mapper found for type " + valueType)); }
@Override public void configureForType(ConfigRegistry registry, Annotation annotation, Class<?> sqlObjectType) { RegisterColumnMapperFactory registerColumnMapperFactory = (RegisterColumnMapperFactory) annotation; try { ColumnMapperFactory factory = registerColumnMapperFactory.value().newInstance(); registry.get(ColumnMappers.class).register(factory); } catch (Exception e) { throw new IllegalStateException("unable to create a specified column mapper factory", e); } } }