@Override public JdbiImmutables createCopy() { return new JdbiImmutables(); } }
/** * Register bean arguments and row mapping for an {@code Modifiable*} value class, expecting the default generated class and public nullary constructor. * @param spec the specification interface or abstract class * @param <S> the specification class * @return a plugin that configures type mapping for the given class */ public <S> JdbiImmutables registerModifiable(Class<S> spec) { final Class<? extends S> impl = classByPrefix("Modifiable", spec); return registerModifiable(spec, impl, nullaryMethodOf(impl, "create") .orElseGet(() -> constructorOf(impl))); }
/** * Register bean arguments and row mapping for an {@code Immutable*} value class, expecting the default generated class and builder names. * @param spec the specification interface or abstract class * @param <S> the specification class * @return a plugin that configures type mapping for the given class */ public <S> JdbiImmutables registerImmutable(Class<S> spec) { final Class<? extends S> impl = classByPrefix("Immutable", spec); return registerImmutable(spec, impl, JdbiOptionals.findFirstPresent( () -> nullaryMethodOf(spec, "builder"), () -> nullaryMethodOf(impl, "builder")) .orElseThrow(() -> new IllegalArgumentException("Neither " + spec + " nor " + impl + " have a 'builder' method"))); }
@Test public void simpleTest() { jdbi.getConfig(JdbiImmutables.class).registerImmutable(Train.class); try (Handle handle = jdbi.open()) { handle.execute("create table train (name varchar, carriages int, observation_car boolean)"); assertThat( handle.createUpdate("insert into train(name, carriages, observation_car) values (:name, :carriages, :observationCar)") .bindPojo(ImmutableTrain.builder().name("Zephyr").carriages(8).observationCar(true).build()) .execute()) .isEqualTo(1); assertThat( handle.createQuery("select * from train") .mapTo(Train.class) .findOnly()) .extracting("name", "carriages", "observationCar") .containsExactly("Zephyr", 8, true); } } // end::example[]
/** * Register bean arguments and row mapping for an {@code Immutable*} value class, using a supplied implementation and builder. * @param spec the specification interface or abstract class * @param impl the generated implementation class * @param builder a supplier of new Builder instances * @param <S> the specification class * @param <I> the implementation class * @return a plugin that configures type mapping for the given class */ public <S, I extends S> JdbiImmutables registerImmutable(Class<S> spec, Class<I> impl, Supplier<?> builder) { return register(spec, impl, ImmutablesPropertiesFactory.immutable(spec, builder)); }
/** * Register bean arguments and row mapping for an {@code Modifiable*} value class, using a supplied implementation and constructor. * @param spec the specification interface or abstract class * @param impl the modifiable class * @param constructor a supplier of new Modifiable instances * @param <S> the specification class * @param <M> the modifiable class * @return a plugin that configures type mapping for the given class */ public <S, M extends S> JdbiImmutables registerModifiable(Class<S> spec, Class<M> impl, Supplier<?> constructor) { return register(spec, impl, ImmutablesPropertiesFactory.modifiable(spec, impl, () -> impl.cast(constructor.get()))); }