Skip to content

Commit dfbbf11

Browse files
committed
#1: Code improvements.
1 parent aa9da5e commit dfbbf11

22 files changed

+369
-179
lines changed

gradle/libs.versions.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ commons-math = "org.apache.commons:commons-math3:3.6.1"
1010
equalsverifier = "nl.jqno.equalsverifier:equalsverifier:3.15.3"
1111
facilejdbc = "io.jenetics:facilejdbc:2.0.0"
1212
guava = "com.google.guava:guava:32.1.3-jre"
13-
h2 = "com.h2database:h2:2.1.214"
13+
h2 = "com.h2database:h2:2.2.224"
1414
mariadb-java-client = "org.mariadb.jdbc:mariadb-java-client:3.3.0"
1515
mysql-connector-java = "mysql:mysql-connector-java:8.0.33"
1616
postgres = "org.postgresql:postgresql:42.6.0"

jpx.jdbc/src/main/java/io/jenetics/jpx/jdbc/BoundsAccess.java

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Java Genetic Algorithm Library (@__identifier__@).
2+
* Java GPX Library (@__identifier__@).
33
* Copyright (c) @__year__@ Franz Wilhelmstötter
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -37,7 +37,22 @@
3737
* @since !__version__!
3838
*/
3939
public final class BoundsAccess {
40-
private BoundsAccess() {}
40+
private BoundsAccess() {
41+
}
42+
43+
static final RowParser<Bounds> PARSER = (row, conn) -> Bounds.of(
44+
row.getDouble("minlat"),
45+
row.getDouble("minlon"),
46+
row.getDouble("maxlat"),
47+
row.getDouble("maxlon")
48+
);
49+
50+
static final Dctor<Bounds> DCTOR = Dctor.of(
51+
field("minlat", Bounds::getMinLatitude),
52+
field("minlon", Bounds::getMinLongitude),
53+
field("maxlat", Bounds::getMaxLatitude),
54+
field("maxlon", Bounds::getMaxLongitude)
55+
);
4156

4257
private static final Query SELECT = Query.of("""
4358
SELECT minlat, minlon, maxlat, maxlon
@@ -52,20 +67,6 @@ INSERT INTO bounds(minlat, minlon, maxlat, maxlon)
5267
"""
5368
);
5469

55-
private static final RowParser<Bounds> PARSER = (row, conn) -> Bounds.of(
56-
row.getDouble("minlat"),
57-
row.getDouble("minlon"),
58-
row.getDouble("maxlat"),
59-
row.getDouble("maxlon")
60-
);
61-
62-
private static final Dctor<Bounds> DCTOR = Dctor.of(
63-
field("minlat", Bounds::getMinLatitude),
64-
field("minlon", Bounds::getMinLongitude),
65-
field("maxlat", Bounds::getMaxLatitude),
66-
field("maxlon", Bounds::getMaxLongitude)
67-
);
68-
6970
public static Bounds selectById(final Long id, final Connection conn)
7071
throws SQLException
7172
{

jpx.jdbc/src/main/java/io/jenetics/jpx/jdbc/CopyrightAccess.java

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Java Genetic Algorithm Library (@__identifier__@).
2+
* Java GPX Library (@__identifier__@).
33
* Copyright (c) @__year__@ Franz Wilhelmstötter
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -21,6 +21,7 @@
2121

2222
import static io.jenetics.facilejdbc.Dctor.field;
2323
import static io.jenetics.facilejdbc.Param.value;
24+
import static io.jenetics.facilejdbc.Row.map;
2425

2526
import java.net.URI;
2627
import java.sql.Connection;
@@ -39,7 +40,20 @@
3940
* @since !__version__!
4041
*/
4142
public final class CopyrightAccess {
42-
private CopyrightAccess() {}
43+
private CopyrightAccess() {
44+
}
45+
46+
static final RowParser<Copyright> PARSER = (row, conn) -> Copyright.of(
47+
row.getString("author"),
48+
map(row.getInt("year"), Year::of),
49+
map(row.getString("license"), URI::create)
50+
);
51+
52+
static final Dctor<Copyright> DCTOR = Dctor.of(
53+
field("author", Copyright::getAuthor),
54+
field("year", c -> c.getYear().map(Year::getValue)),
55+
field("license", Copyright::getLicense)
56+
);
4357

4458
private static final Query SELECT = Query.of("""
4559
SELECT id, author, year, license
@@ -54,18 +68,6 @@ INSERT INTO copyright(author, year, license)
5468
"""
5569
);
5670

57-
private static final RowParser<Copyright> PARSER = (row, conn) -> Copyright.of(
58-
row.getString("author"),
59-
Year.of(row.getInt("year")),
60-
URI.create(row.getString("license"))
61-
);
62-
63-
private static final Dctor<Copyright> DCTOR = Dctor.of(
64-
field("author", Copyright::getAuthor),
65-
field("year", c -> c.getYear().map(Year::getValue)),
66-
field("license", Copyright::getLicense)
67-
);
68-
6971
public static Copyright selectById(final Long id, final Connection conn)
7072
throws SQLException
7173
{

jpx.jdbc/src/main/java/io/jenetics/jpx/jdbc/GPXAccess.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Java Genetic Algorithm Library (@__identifier__@).
2+
* Java GPX Library (@__identifier__@).
33
* Copyright (c) @__year__@ Franz Wilhelmstötter
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");

jpx.jdbc/src/main/java/io/jenetics/jpx/jdbc/GpxTypeMapper.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Java Genetic Algorithm Library (@__identifier__@).
2+
* Java GPX Library (@__identifier__@).
33
* Copyright (c) @__year__@ Franz Wilhelmstötter
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -54,7 +54,7 @@ public Object convert(final Object value) {
5454
if (value instanceof ZonedDateTime) return ((ZonedDateTime)value).toOffsetDateTime();
5555
if (value instanceof Duration) return ((Duration)value).getSeconds();
5656
if (value instanceof URI) return value.toString();
57-
if (value instanceof URL) return value.toString();
57+
if (value instanceof URL) return value.toString();
5858
return value;
5959
}
6060
}

jpx.jdbc/src/main/java/io/jenetics/jpx/jdbc/LinkAccess.java

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Java Genetic Algorithm Library (@__identifier__@).
2+
* Java GPX Library (@__identifier__@).
33
* Copyright (c) @__year__@ Franz Wilhelmstötter
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -21,6 +21,7 @@
2121

2222
import static io.jenetics.facilejdbc.Dctor.field;
2323
import static io.jenetics.facilejdbc.Param.value;
24+
import static io.jenetics.facilejdbc.Row.map;
2425

2526
import java.net.URI;
2627
import java.sql.Connection;
@@ -38,43 +39,63 @@
3839
* @since !__version__!
3940
*/
4041
public final class LinkAccess {
41-
private LinkAccess() {}
42+
private LinkAccess() {
43+
}
4244

43-
private static final Query SELECT = Query.of("""
45+
static final RowParser<Link> PARSER = (row, conn) -> Link.of(
46+
map(row.getString("href"), URI::create),
47+
row.getString("text"),
48+
row.getString("type")
49+
);
50+
51+
static final Dctor<Link> DCTOR = Dctor.of(
52+
field("href", Link::getHref),
53+
field("text", Link::getText),
54+
field("type", Link::getType)
55+
);
56+
57+
static final Query SELECT_BY_ID = Query.of("""
4458
SELECT id, href, text, type
4559
FROM link
4660
WHERE id = :id
4761
"""
4862
);
4963

50-
private static final Query INSERT = Query.of("""
64+
static final Query INSERT = Query.of("""
5165
INSERT INTO link(href, text, type)
5266
VALUES(:href, :text, :type)
5367
"""
5468
);
5569

56-
private static final RowParser<Link> PARSER = (row, conn) -> Link.of(
57-
URI.create(row.getString("href")),
58-
row.getString("text"),
59-
row.getString("type")
60-
);
61-
62-
private static final Dctor<Link> DCTOR = Dctor.of(
63-
field("href", Link::getHref),
64-
field("text", Link::getText),
65-
field("type", Link::getType)
66-
);
67-
6870
public static Link selectById(final Long id, final Connection conn)
6971
throws SQLException
7072
{
7173
return id != null
72-
? SELECT
74+
? SELECT_BY_ID
7375
.on(value("id", id))
7476
.as(PARSER.singleNull(), conn)
7577
: null;
7678
}
7779

80+
public static Long insertIfMissing(final Link link, final Connection conn)
81+
throws SQLException
82+
{
83+
final var select = Query.of("""
84+
SELECT id FROM link
85+
WHERE href = :href AND text = :text AND type = :type
86+
"""
87+
);
88+
89+
final var id = select
90+
.on(
91+
value("href", link.getHref()),
92+
value("text", link.getText()),
93+
value("type", link.getType()))
94+
.as(RowParser.int64(1).singleNull(), conn);
95+
96+
return 1L;
97+
}
98+
7899
public static Long insert(final Link link, final Connection conn)
79100
throws SQLException
80101
{

jpx.jdbc/src/main/java/io/jenetics/jpx/jdbc/MetadataAccess.java

Lines changed: 30 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Java Genetic Algorithm Library (@__identifier__@).
2+
* Java GPX Library (@__identifier__@).
33
* Copyright (c) @__year__@ Franz Wilhelmstötter
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -22,10 +22,6 @@
2222
import static io.jenetics.facilejdbc.Dctor.field;
2323
import static io.jenetics.facilejdbc.Param.value;
2424

25-
import lombok.Builder;
26-
import lombok.Value;
27-
import lombok.experimental.Accessors;
28-
2925
import java.sql.Connection;
3026
import java.sql.SQLException;
3127
import java.sql.Timestamp;
@@ -48,44 +44,20 @@
4844
* @since !__version__!
4945
*/
5046
public final class MetadataAccess {
51-
private MetadataAccess() {}
52-
53-
@Value
54-
@Builder(builderClassName = "Builder", toBuilder = true)
55-
@Accessors(fluent = true)
56-
private static final class MetadataRow {
57-
private final String name;
58-
private final String desc;
59-
private final Timestamp time;
60-
private final String keyword;
61-
private final Long personId;
62-
private final Long copyrightId;
63-
private final Long boundsId;
47+
private MetadataAccess() {
6448
}
6549

66-
private static final Query SELECT = Query.of("""
67-
SELECT name, dscr, time, keywords, person_id, copyright_id, bounds_id
68-
FROM metadata
69-
WHERE id = :id
70-
"""
71-
);
72-
73-
private static final Query INSERT = Query.of("""
74-
INSERT INTO metadata(name, dscr, time, keywords, person_id, copyright_id, bounds_id)
75-
VALUES(:name, :dscr, :time, :keywords, :person_id, :copyright_id, :bounds_id)
76-
"""
77-
);
50+
record Row(
51+
String name,
52+
String desc,
53+
Timestamp time,
54+
String keywords,
55+
Long personId,
56+
Long copyrightId,
57+
Long boundsId
58+
) {}
7859

79-
private static final RowParser<MetadataRow> ROW_PARSER = (row, conn) ->
80-
MetadataRow.builder()
81-
.name(row.getString("name"))
82-
.desc(row.getString("dscr"))
83-
.time(row.getTimestamp("time"))
84-
.keyword(row.getString("keywords"))
85-
.personId(row.getObject("person_id", Long.class))
86-
.copyrightId(row.getObject("copyright_id", Long.class))
87-
.boundsId(row.getObject("bounds_id", Long.class))
88-
.build();
60+
private static final RowParser<Row> ROW_PARSER = RowParser.record(Row.class);
8961

9062
private static final Dctor<Metadata> DCTOR = Dctor.of(
9163
field("name", Metadata::getName),
@@ -94,24 +66,37 @@ INSERT INTO metadata(name, dscr, time, keywords, person_id, copyright_id, bounds
9466
field("keywords", Metadata::getKeywords),
9567
field(
9668
"person_id",
97-
(m, c) -> PersonAccess.insert(m.getAuthor().orElse(null), c)
69+
(md, conn) -> PersonAccess.insert(md.getAuthor().orElse(null), conn)
9870
),
9971
field(
10072
"copyright_id",
101-
(m, c) -> CopyrightAccess.insert(m.getCopyright().orElse(null), c)
73+
(md, conn) -> CopyrightAccess.insert(md.getCopyright().orElse(null), conn)
10274
),
10375
field(
10476
"bounds_id",
105-
(m, c) -> BoundsAccess.insert(m.getBounds().orElse(null), c)
77+
(md, conn) -> BoundsAccess.insert(md.getBounds().orElse(null), conn)
10678
)
10779
);
10880

81+
private static final Query SELECT = Query.of("""
82+
SELECT name, dscr, time, keywords, person_id, copyright_id, bounds_id
83+
FROM metadata
84+
WHERE id = :id
85+
"""
86+
);
87+
88+
private static final Query INSERT = Query.of("""
89+
INSERT INTO metadata(name, dscr, time, keywords, person_id, copyright_id, bounds_id)
90+
VALUES(:name, :dscr, :time, :keywords, :person_id, :copyright_id, :bounds_id)
91+
"""
92+
);
93+
10994
public static Metadata selectById(final Long id, final Connection conn)
11095
throws SQLException
11196
{
11297
if (id == null) return null;
11398

114-
final MetadataRow row = SELECT
99+
final Row row = SELECT
115100
.on(value("id", id))
116101
.as(ROW_PARSER.singleNull(), conn);
117102

@@ -125,7 +110,7 @@ public static Metadata selectById(final Long id, final Connection conn)
125110
.name(row.name())
126111
.desc(row.desc())
127112
.time(row.time().toInstant())
128-
.keywords(row.keyword())
113+
.keywords(row.keywords())
129114
.author(author)
130115
.copyright(copyright)
131116
.bounds(bounds)

0 commit comments

Comments
 (0)