From eb559fedc17c756bf32d5800d56a568d455542dc Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Mon, 4 Mar 2024 09:09:30 +0100 Subject: [PATCH] Polishing. Simplify code. Use putIfAbsent instead of custom check. [#636][closes #641] --- src/main/java/io/r2dbc/postgresql/PostgresqlRow.java | 4 +--- .../java/io/r2dbc/postgresql/PostgresqlRowMetadata.java | 8 +++----- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/src/main/java/io/r2dbc/postgresql/PostgresqlRow.java b/src/main/java/io/r2dbc/postgresql/PostgresqlRow.java index 72f6d63c..867db1b5 100644 --- a/src/main/java/io/r2dbc/postgresql/PostgresqlRow.java +++ b/src/main/java/io/r2dbc/postgresql/PostgresqlRow.java @@ -59,9 +59,7 @@ final class PostgresqlRow implements io.r2dbc.postgresql.api.PostgresqlRow { this.data = Assert.requireNonNull(data, "data must not be null"); if (metadata instanceof PostgresqlRowMetadata) { - this.columnNameIndexCacheMap = Assert.requireNonNull( - ((PostgresqlRowMetadata) metadata).getColumnNameIndexMap(), - "columnNameIndexCacheMap must not be null"); + this.columnNameIndexCacheMap = ((PostgresqlRowMetadata) metadata).getColumnNameIndexMap(); } else { this.columnNameIndexCacheMap = createColumnNameIndexMap(this.fields); } diff --git a/src/main/java/io/r2dbc/postgresql/PostgresqlRowMetadata.java b/src/main/java/io/r2dbc/postgresql/PostgresqlRowMetadata.java index 72c965d2..bcfa5f6f 100644 --- a/src/main/java/io/r2dbc/postgresql/PostgresqlRowMetadata.java +++ b/src/main/java/io/r2dbc/postgresql/PostgresqlRowMetadata.java @@ -48,15 +48,13 @@ final class PostgresqlRowMetadata extends AbstractCollection implements PostgresqlRowMetadata(List columnMetadatas) { this.columnMetadatas = Assert.requireNonNull(columnMetadatas, "columnMetadatas must not be null"); - this.nameKeyedColumns = new LinkedHashMap<>(); + this.nameKeyedColumns = new LinkedHashMap<>(columnMetadatas.size(), 1); this.columnNameIndexMap = new HashMap<>(columnMetadatas.size(), 1); int i = 0; for (PostgresqlColumnMetadata columnMetadata : columnMetadatas) { - if (!this.nameKeyedColumns.containsKey(columnMetadata.getName())) { - this.nameKeyedColumns.put(columnMetadata.getName(), columnMetadata); - } - columnNameIndexMap.putIfAbsent(columnMetadata.getName().toLowerCase(Locale.ROOT), i++); + this.nameKeyedColumns.putIfAbsent(columnMetadata.getName(), columnMetadata); + this.columnNameIndexMap.putIfAbsent(columnMetadata.getName().toLowerCase(Locale.ROOT), i++); } }