Skip to content

Commit a388c90

Browse files
authored
Merge pull request #63 from jenetics/issues/FACILEJDBC-58-code_snippets
#58: Code snippets
2 parents c88f9bf + 72bc6a2 commit a388c90

File tree

15 files changed

+157
-205
lines changed

15 files changed

+157
-205
lines changed

facilejdbc/src/main/java/io/jenetics/facilejdbc/Batch.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,11 @@
3232
* essentially an {@link Iterable} of records or row-creation functions. The
3333
* available factory functions make it easy to create a batch from a given
3434
* list of records or parameters.
35-
*
36-
* <pre>{@code
37-
* final List<Person> persons = ...;
38-
* final Dctor<Person> dctor = ...;
35+
* {@snippet lang="java":
36+
* final List<Person> persons = null; // @replace substring='null' replacement="..."
37+
* final Dctor<Person> dctor = null; // @replace substring='null' replacement="..."
3938
* final Batch batch = Batch.of(persons, dctor);
40-
* }</pre>
39+
* }
4140
*
4241
* @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
4342
* @version 1.0

facilejdbc/src/main/java/io/jenetics/facilejdbc/Dctor.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,19 @@
3838
* This interface is responsible for <em>deconstructing</em> a given record, of
3939
* type {@code T}, to a DB-<em>row</em> ({@link ParamValues}).
4040
*
41-
* <pre>{@code
41+
* {@snippet lang="java":
4242
* final Dctor<Book> dctor = Dctor.of(
4343
* Dctor.field("title", Book::title),
4444
* Dctor.field("isbn", Book::isbn),
4545
* Dctor.field("published_at", Book::publishedAt),
4646
* Dctor.field("pages", Book::pages)
4747
* );
48-
* }</pre>
48+
* }
4949
*
5050
* If the {@code Book} class is a record, you can just write
51-
* <pre>{@code
51+
* {@snippet lang="java":
5252
* final Dctor<Book> dctor = Dctor.of(Book.class);
53-
* }</pre>
53+
* }
5454
*
5555
* @apiNote
5656
* A {@code Dctor} (deconstructor) is responsible for splitting a given record
@@ -199,8 +199,7 @@ static <T> Dctor<T> of(final Field<? super T>... fields) {
199199

200200
/**
201201
* Create a new deconstructor for the given record type.
202-
*
203-
* <pre>{@code
202+
* {@snippet lang="java":
204203
* // Matching column names, with book columns:
205204
* // [title, author, isbn, pages, published_at]
206205
* final Dctor<Book> dctor = Dctor.of(Book.class);
@@ -219,7 +218,7 @@ static <T> Dctor<T> of(final Field<? super T>... fields) {
219218
* field("pages", book -> book.pages()*3),
220219
* field("title_hash", book -> book.title().hashCode())
221220
* );
222-
* }</pre>
221+
* }
223222
*
224223
* @see Records#dctor(Class, Field[])
225224
*

facilejdbc/src/main/java/io/jenetics/facilejdbc/Lifecycle.java

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,7 @@ default void silentClose(final Throwable previousError) {
232232
* interface but needs some cleanup work to do after usage. In the following
233233
* example the created {@code file} is automatically deleted when leaving the
234234
* {@code try} block.
235-
*
236-
* <pre>{@code
235+
* {@snippet lang="java":
237236
* // Create the closeable file.
238237
* final Value<Path, IOException> file = Value.of(
239238
* Files.createFile(Path.of("some_file")),
@@ -246,7 +245,7 @@ default void silentClose(final Throwable previousError) {
246245
* final var writtenText = Files.readString(file.get());
247246
* assert "foo".equals(writtenText);
248247
* }
249-
* }</pre>
248+
* }
250249
*
251250
* @see #of(Object, ThrowingConsumer)
252251
* @see #build(ThrowingFunction)
@@ -290,8 +289,7 @@ public String toString() {
290289
* released, by calling the defined <em>release</em> method. The typical
291290
* use case for this method is when additional initialization of the
292291
* value is needed.
293-
*
294-
* <pre>{@code
292+
* {@snippet lang="java":
295293
* final var file = CloseableValue.of(
296294
* Files.createFile(Path.of("some_file")),
297295
* Files::deleteIfExists
@@ -303,7 +301,7 @@ public String toString() {
303301
* try (file) {
304302
* // Do something with temp file.
305303
* }
306-
* }</pre>
304+
* }
307305
*
308306
* @param <E> the exception type
309307
* @param block the codec block which is applied to the value
@@ -352,8 +350,7 @@ public static <T, E extends Exception> Value<T, E> of(
352350
* in the case of an error. If the <em>value</em> could be created, the
353351
* caller is responsible for closing the opened <em>resources</em> by
354352
* calling the {@link Value#close()} method.
355-
*
356-
* <pre>{@code
353+
* {@snippet lang="java":
357354
* final Value<Stream<Object>, IOException> result = Value.build(resources -> {
358355
* final var fin = resources.add(new FileInputStream(file.toFile()), Closeable::close);
359356
* final var bin = resources.add(new BufferedInputStream(fin), Closeable::close);
@@ -366,7 +363,7 @@ public static <T, E extends Exception> Value<T, E> of(
366363
* try (result) {
367364
* result.get().forEach(System.out::println);
368365
* }
369-
* }</pre>
366+
* }
370367
*
371368
* @see Resources
372369
*
@@ -414,8 +411,7 @@ public static <T, E extends Exception> Value<T, E> of(
414411
* Using the {@code Resources} class can simplify the creation of
415412
* dependent input streams, where it might be otherwise necessary to create
416413
* nested {@code try-with-resources} blocks.
417-
*
418-
* <pre>{@code
414+
* {@snippet lang="java":
419415
* try (var resources = new Resources<IOException>()) {
420416
* final var fin = resources.add(new FileInputStream(file), Closeable::close);
421417
* if (fin.read() != -1) {
@@ -424,7 +420,7 @@ public static <T, E extends Exception> Value<T, E> of(
424420
* final var oin = resources.add(new ObjectInputStream(fin), Closeable::close);
425421
* // ...
426422
* }
427-
* }</pre>
423+
* }
428424
*
429425
* @see Value#build(ThrowingFunction)
430426
*
@@ -509,15 +505,14 @@ private Lifecycle() {
509505
* of the method invocations throws an exception. The first exception thrown
510506
* is rethrown after invoking the method on the remaining objects, all other
511507
* exceptions are swallowed.
512-
*
513-
* <pre>{@code
508+
* {@snippet lang="java":
514509
* final var streams = new ArrayList<InputStream>();
515510
* streams.add(new FileInputStream(file1));
516511
* streams.add(new FileInputStream(file2));
517512
* streams.add(new FileInputStream(file3));
518513
* // ...
519514
* invokeAll(Closeable::close, streams);
520-
* }</pre>
515+
* }
521516
*
522517
* @param <A> the closeable object type
523518
* @param <E> the exception type

facilejdbc/src/main/java/io/jenetics/facilejdbc/MultiParam.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,9 @@
2828
* Represents a query parameter with <em>name</em> and one or more <em>values</em>.
2929
* The parameter values are evaluated lazily. But it is also possible to create
3030
* {@code MultiParam} objects with eagerly evaluated values.
31-
*
32-
* <pre>{@code
31+
* {@snippet lang="java":
3332
* SELECT_QUERY.on(Param.values("ids", 1, 2, 3, 4))
34-
* }</pre>
33+
* }
3534
*
3635
* @see SingleParam
3736
* @see SingleParam#values(String, Iterable)

facilejdbc/src/main/java/io/jenetics/facilejdbc/Param.java

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,18 @@
3434
* class.
3535
* <p>
3636
* Creating single-valued parameters:
37-
* <pre>{@code
37+
* {@snippet lang="java":
3838
* INSERT_QUERY.on(
3939
* Param.value("forename", "Werner"),
4040
* Param.value("birthday", LocalDate.now()),
4141
* Param.value("email", "some.email@gmail.com"))
42-
* }</pre>
42+
* }
4343
*
4444
* Creating multivalued parameters:
45-
* <pre>{@code
45+
* {@snippet lang="java":
4646
* Query.of("SELECT * FROM table WHERE id = IN(:ids);")
4747
* .on(Param.values("ids", 1, 2, 3, 4))
48-
* }</pre>
48+
* }
4949
*
5050
* @see SingleParam
5151
* @see MultiParam
@@ -70,12 +70,11 @@ public sealed interface Param permits SingleParam, MultiParam {
7070
/**
7171
* Create a new query parameter object for the given {@code name} and
7272
* {@code value}.
73-
*
74-
* <pre>{@code
73+
* {@snippet lang="java":
7574
* final var result = Query.of("SELECT * FROM table WHERE id = :id;")
7675
* .on(Param.value("id", 43245)
7776
* .as(PARSER.singleOpt(), conn);
78-
* }</pre>
77+
* }
7978
*
8079
* @param name the parameter name
8180
* @param value the parameter values, which may be {@code null}
@@ -93,12 +92,11 @@ static SingleParam value(final String name, final Object value) {
9392
/**
9493
* Create a new (multi) query parameter object for the given {@code name}
9594
* and the given {@code values}.
96-
*
97-
* <pre>{@code
95+
* {@snippet lang="java":
9896
* final var result = Query.of("SELECT * FROM table WHERE id = IN(:ids);")
9997
* .on(Param.values("ids", List.of(43245, 434, 23, 987, 1239))
10098
* .as(PARSER.list(), conn);
101-
* }</pre>
99+
* }
102100
*
103101
* @since 1.3
104102
*
@@ -130,12 +128,11 @@ private static <T> Stream<T> stream(final Iterable<? extends T> values) {
130128
/**
131129
* Create a new (multi) query parameter object for the given {@code name}
132130
* and the given {@code values}.
133-
*
134-
* <pre>{@code
131+
* {@snippet lang="java":
135132
* final var result = Query.of("SELECT * FROM table WHERE id = IN(:ids);")
136133
* .on(Param.values("ids", 43245, 434, 23, 987, 1239)
137134
* .as(PARSER.list(), conn);
138-
* }</pre>
135+
* }
139136
*
140137
* @since 1.3
141138
*
@@ -155,12 +152,11 @@ static MultiParam values(final String name, final Object... values) {
155152
/**
156153
* Create a new query parameter object from the given {@code name} and
157154
* lazily evaluated {@code value}.
158-
*
159-
* <pre>{@code
155+
* {@snippet lang="java":
160156
* final var result = Query.of("SELECT * FROM table WHERE date < :date;")
161157
* .on(Param.lazyValue("date", LocalDate::now)
162158
* .as(PARSER.singleOpt(), conn);
163-
* }</pre>
159+
* }
164160
*
165161
* @param name the parameter name
166162
* @param value the lazily evaluated parameter value
@@ -178,14 +174,13 @@ static SingleParam lazyValue(final String name, final SqlSupplier<?> value) {
178174
/**
179175
* Create a new query parameter object for the given {@code name} and
180176
* lazily evaluated {@code values}.
181-
*
182-
* <pre>{@code
177+
* {@snippet lang="java":
183178
* final SqlSupplier<Integer> id1 = ...;
184179
* final SqlSupplier<Integer> id2 = ...;
185180
* final var result = Query.of("SELECT * FROM table WHERE id = IN(:ids);")
186181
* .on(Param.lazyValues("id", List.of(id1, id2))
187182
* .as(PARSER.list(), conn);
188-
* }</pre>
183+
* }
189184
*
190185
* @param name the parameter name
191186
* @param values the parameter values
@@ -209,12 +204,11 @@ static MultiParam lazyValues(
209204
/**
210205
* Create a new query parameter object for the given {@code name} and
211206
* lazily evaluated {@code values}.
212-
*
213-
* <pre>{@code
207+
* {@snippet lang="java":
214208
* final var result = Query.of("SELECT * FROM table WHERE id = IN(:ids);")
215209
* .on(Param.lazyValues("id", () -> 324, () -> 9967))
216210
* .as(PARSER.list(), conn);
217-
* }</pre>
211+
* }
218212
*
219213
* @param name the parameter name
220214
* @param values the parameter values

facilejdbc/src/main/java/io/jenetics/facilejdbc/Query.java

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@
5252

5353
/**
5454
* A {@code Query} represents an executable piece of SQL text.
55-
*
56-
* <pre>{@code
55+
* {@snippet lang="java":
5756
* private static final Query SELECT = Query.of("""
5857
* SELECT * FROM person
5958
* WHERE forename like :forename
@@ -66,7 +65,7 @@
6665
* VALUES(:forename, :surname, :birthday, :email);
6766
* """
6867
* );
69-
* }</pre>
68+
* }
7069
*
7170
* @apiNote
7271
* This class is immutable and thread-safe.
@@ -111,11 +110,11 @@ public String sql() {
111110
/**
112111
* Return the original SQL string, this object is created with. So the
113112
* following assertion holds for every possible SQL string;
114-
* <pre>{@code
113+
* {@snippet lang="java":
115114
* final String sql = "SELECT * FROM table WHERE id = :id;";
116115
* final Query query = Query.of(sql);
117116
* assert sql.equals(query.rawSql());
118-
* }</pre>
117+
* }
119118
*
120119
* @since 1.1
121120
*
@@ -209,12 +208,11 @@ public Query withTimeout(final Duration timeout) {
209208

210209
/**
211210
* Return a new query object with the given query parameter values.
212-
*
213-
* <pre>{@code
211+
* {@snippet lang="java":
214212
* final var result = Query.of("SELECT * FROM table WHERE id = :id;")
215-
* .on(List.of(Param.value("id", 43245))
213+
* .on(List.of(Param.value("id", 43245)))
216214
* .as(PARSER.singleOpt(), conn);
217-
* }</pre>
215+
* }
218216
*
219217
* @see #on(Param...)
220218
* @see #on(Map)
@@ -279,12 +277,11 @@ private static Stream<SingleParam> toParams(final MultiParam param) {
279277

280278
/**
281279
* Return a new query object with the given query parameter values.
282-
*
283-
* <pre>{@code
280+
* {@snippet lang="java":
284281
* final var result = Query.of("SELECT * FROM table WHERE id = :id;")
285-
* .on(Param.value("id", 43245)
282+
* .on(Param.value("id", 43245))
286283
* .as(PARSER.singleOpt(), conn);
287-
* }</pre>
284+
* }
288285
*
289286
* @param params the query parameters
290287
* @return a new query object with the set parameters
@@ -298,12 +295,11 @@ public Query on(final Param... params) {
298295

299296
/**
300297
* Return a new query object with the given query parameter values.
301-
*
302-
* <pre>{@code
298+
* {@snippet lang="java":
303299
* final var result = Query.of("SELECT * FROM table WHERE id = :id;")
304300
* .on(Map.of("id", 43245))
305301
* .as(PARSER.singleOpt(), conn);
306-
* }</pre>
302+
* }
307303
*
308304
* @param params the query parameters
309305
* @return a new query object with the set parameters
@@ -624,7 +620,7 @@ public String toString() {
624620

625621
/**
626622
* Create a new query object from the given SQL string.
627-
* <pre>{@code
623+
* {@snippet lang="java":
628624
* private static final Query SELECT = Query.of("""
629625
* SELECT * FROM person
630626
* WHERE forename like :forename
@@ -637,7 +633,7 @@ public String toString() {
637633
* VALUES(:forename, :surname, :birthday, :email);
638634
* """
639635
* );
640-
* }</pre>
636+
* }
641637
*
642638
* @param sql the SQL string of the created query
643639
* @return a new query object from the given SQL string

0 commit comments

Comments
 (0)