From 9e91f36438cdc09101eabfd6d7782adf04a5b08c Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Fri, 29 Nov 2019 10:27:52 +0100 Subject: [PATCH] Make GenericTableSchema.fromJson() a factory method This method is called from precisely one place, which is creating a new object for the call. Turn allocation around, taking a step towards immutable schemas. Change-Id: I97c44da72bc3b63260941bcf7d480a9c0c9e3a4a Signed-off-by: Robert Varga (cherry picked from commit f74cb36d4df75f1fcf20014166061e627849a9b0) --- .../ovsdb/lib/schema/DatabaseSchema.java | 20 +++++------ .../ovsdb/lib/schema/GenericTableSchema.java | 18 ++++------ .../ovsdb/lib/schema/TableSchema.java | 36 ++++++++----------- 3 files changed, 32 insertions(+), 42 deletions(-) diff --git a/library/impl/src/main/java/org/opendaylight/ovsdb/lib/schema/DatabaseSchema.java b/library/impl/src/main/java/org/opendaylight/ovsdb/lib/schema/DatabaseSchema.java index af07c092b..1868da636 100644 --- a/library/impl/src/main/java/org/opendaylight/ovsdb/lib/schema/DatabaseSchema.java +++ b/library/impl/src/main/java/org/opendaylight/ovsdb/lib/schema/DatabaseSchema.java @@ -31,13 +31,13 @@ public class DatabaseSchema { private String name; private Version version; - private Map tables; + private final Map tables; - public DatabaseSchema(Map tables) { + public DatabaseSchema(final Map tables) { this.tables = tables; } - public DatabaseSchema(String name, Version version, Map tables) { + public DatabaseSchema(final String name, final Version version, final Map tables) { this.name = name; this.version = version; this.tables = tables; @@ -47,11 +47,11 @@ public class DatabaseSchema { return this.tables.keySet(); } - public boolean hasTable(String table) { + public boolean hasTable(final String table) { return this.getTables().contains(table); } - public > E table(String tableName, Class clazz) { + public > E table(final String tableName, final Class clazz) { TableSchema table = tables.get(tableName); if (clazz.isInstance(table)) { @@ -61,7 +61,7 @@ public class DatabaseSchema { return createTableSchema(clazz, table); } - protected > E createTableSchema(Class clazz, TableSchema table) { + protected > E createTableSchema(final Class clazz, final TableSchema table) { Constructor declaredConstructor; try { declaredConstructor = clazz.getDeclaredConstructor(TableSchema.class); @@ -81,7 +81,7 @@ public class DatabaseSchema { } //todo : this needs to move to a custom factory - public static DatabaseSchema fromJson(String dbName, JsonNode json) { + public static DatabaseSchema fromJson(final String dbName, final JsonNode json) { if (!json.isObject() || !json.has("tables")) { throw new ParsingException("bad DatabaseSchema root, expected \"tables\" as child but was not found"); } @@ -97,7 +97,7 @@ public class DatabaseSchema { LOG.trace("Read schema for table[{}]:{}", table.getKey(), table.getValue()); //todo : this needs to done by a factory - tables.put(table.getKey(), new GenericTableSchema().fromJson(table.getKey(), table.getValue())); + tables.put(table.getKey(), GenericTableSchema.fromJson(table.getKey(), table.getValue())); } return new DatabaseSchema(dbName, dbVersion, tables); @@ -107,7 +107,7 @@ public class DatabaseSchema { return name; } - public void setName(String name) { + public void setName(final String name) { this.name = name; } @@ -115,7 +115,7 @@ public class DatabaseSchema { return version; } - public void setVersion(Version version) { + public void setVersion(final Version version) { this.version = version; } diff --git a/library/impl/src/main/java/org/opendaylight/ovsdb/lib/schema/GenericTableSchema.java b/library/impl/src/main/java/org/opendaylight/ovsdb/lib/schema/GenericTableSchema.java index f32978b1b..47a0d9a12 100644 --- a/library/impl/src/main/java/org/opendaylight/ovsdb/lib/schema/GenericTableSchema.java +++ b/library/impl/src/main/java/org/opendaylight/ovsdb/lib/schema/GenericTableSchema.java @@ -5,7 +5,6 @@ * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ - package org.opendaylight.ovsdb.lib.schema; import com.fasterxml.jackson.databind.JsonNode; @@ -17,22 +16,21 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class GenericTableSchema extends TableSchema { - private static final Logger LOG = LoggerFactory.getLogger(GenericTableSchema.class); - public GenericTableSchema() { + public GenericTableSchema(final String name) { + super(name); } - public GenericTableSchema(String tableName) { - super(tableName); + public GenericTableSchema(final String name, final Map columns) { + super(name, columns); } - public GenericTableSchema(TableSchema tableSchema) { + public GenericTableSchema(final TableSchema tableSchema) { super(tableSchema.getName(), tableSchema.getColumnSchemas()); } - public GenericTableSchema fromJson(String tableName, JsonNode json) { - + public static GenericTableSchema fromJson(final String tableName, final JsonNode json) { if (!json.isObject() || !json.has("columns")) { throw new BadSchemaException("bad tableschema root, expected \"columns\" as child"); } @@ -44,8 +42,6 @@ public class GenericTableSchema extends TableSchema { columns.put(column.getKey(), ColumnSchema.fromJson(column.getKey(), column.getValue())); } - this.setName(tableName); - this.setColumns(columns); - return this; + return new GenericTableSchema(tableName, columns); } } diff --git a/library/impl/src/main/java/org/opendaylight/ovsdb/lib/schema/TableSchema.java b/library/impl/src/main/java/org/opendaylight/ovsdb/lib/schema/TableSchema.java index 55a2e70ca..a41fbe31a 100644 --- a/library/impl/src/main/java/org/opendaylight/ovsdb/lib/schema/TableSchema.java +++ b/library/impl/src/main/java/org/opendaylight/ovsdb/lib/schema/TableSchema.java @@ -5,7 +5,6 @@ * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ - package org.opendaylight.ovsdb.lib.schema; import com.fasterxml.jackson.databind.JsonNode; @@ -26,20 +25,16 @@ import org.opendaylight.ovsdb.lib.operations.Insert; import org.opendaylight.ovsdb.lib.schema.BaseType.UuidBaseType; import org.opendaylight.ovsdb.lib.schema.ColumnType.AtomicColumnType; - public abstract class TableSchema> { private String name; private Map columns; - public TableSchema() { - } - - protected TableSchema(String name) { + protected TableSchema(final String name) { this.name = name; } - public TableSchema(String name, Map columns) { + protected TableSchema(final String name, final Map columns) { this.name = name; this.columns = columns; } @@ -52,16 +47,15 @@ public abstract class TableSchema> { return columns; } - public boolean hasColumn(String column) { + public boolean hasColumn(final String column) { return this.getColumns().contains(column); } - - public ColumnType getColumnType(String column) { + public ColumnType getColumnType(final String column) { return this.columns.get(column).getType(); } - public > E as(Class clazz) { + public > E as(final Class clazz) { try { Constructor instance = clazz.getConstructor(TableSchema.class); return instance.newInstance(this); @@ -75,7 +69,7 @@ public abstract class TableSchema> { return new Insert<>(this); } - public ColumnSchema> multiValuedColumn(String column, Class type) { + public ColumnSchema> multiValuedColumn(final String column, final Class type) { //todo exception handling ColumnSchema> columnSchema = columns.get(column); @@ -83,7 +77,8 @@ public abstract class TableSchema> { return columnSchema; } - public ColumnSchema> multiValuedColumn(String column, Class keyType, Class valueType) { + public ColumnSchema> multiValuedColumn(final String column, final Class keyType, + final Class valueType) { //todo exception handling ColumnSchema> columnSchema = columns.get(column); @@ -91,7 +86,7 @@ public abstract class TableSchema> { return columnSchema; } - public ColumnSchema column(String column, Class type) { + public ColumnSchema column(final String column, final Class type) { //todo exception handling ColumnSchema columnSchema = columns.get(column); @@ -101,24 +96,23 @@ public abstract class TableSchema> { return columnSchema; } - public ColumnSchema column(String column) { + public ColumnSchema column(final String column) { return this.columns.get(column); } - public String getName() { return name; } - protected void setName(String name) { + protected void setName(final String name) { this.name = name; } - protected void setColumns(Map columns) { + protected void setColumns(final Map columns) { this.columns = columns; } - public TableUpdate updatesFromJson(JsonNode value) { + public TableUpdate updatesFromJson(final JsonNode value) { TableUpdate tableUpdate = new TableUpdate<>(); Iterator> fields = value.fields(); while (fields.hasNext()) { @@ -136,7 +130,7 @@ public abstract class TableSchema> { return tableUpdate; } - public Row createRow(ObjectNode rowNode) { + public Row createRow(final ObjectNode rowNode) { List> newColumns = new ArrayList<>(); for (Iterator> iter = rowNode.fields(); iter.hasNext();) { Map.Entry next = iter.next(); @@ -155,7 +149,7 @@ public abstract class TableSchema> { return new Row<>(this, newColumns); } - public List> createRows(JsonNode rowsNode) { + public List> createRows(final JsonNode rowsNode) { List> rows = new ArrayList<>(); for (JsonNode rowNode : rowsNode.get("rows")) { rows.add(createRow((ObjectNode)rowNode)); -- 2.36.6