Make GenericTableSchema.fromJson() a factory method 30/86130/2
authorRobert Varga <robert.varga@pantheon.tech>
Fri, 29 Nov 2019 09:27:52 +0000 (10:27 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Tue, 3 Dec 2019 10:19:29 +0000 (11:19 +0100)
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 <robert.varga@pantheon.tech>
(cherry picked from commit f74cb36d4df75f1fcf20014166061e627849a9b0)

library/impl/src/main/java/org/opendaylight/ovsdb/lib/schema/DatabaseSchema.java
library/impl/src/main/java/org/opendaylight/ovsdb/lib/schema/GenericTableSchema.java
library/impl/src/main/java/org/opendaylight/ovsdb/lib/schema/TableSchema.java

index af07c092bdf6f3753c8720595b24d1ab9ce8906f..1868da63625fbeb2fb74e8f0cd7161514d79bfae 100644 (file)
@@ -31,13 +31,13 @@ public class DatabaseSchema {
     private String name;
 
     private Version version;
-    private Map<String, TableSchema> tables;
+    private final Map<String, TableSchema> tables;
 
-    public DatabaseSchema(Map<String, TableSchema> tables) {
+    public DatabaseSchema(final Map<String, TableSchema> tables) {
         this.tables = tables;
     }
 
-    public DatabaseSchema(String name, Version version, Map<String, TableSchema> tables) {
+    public DatabaseSchema(final String name, final Version version, final Map<String, TableSchema> 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 extends TableSchema<E>> E table(String tableName, Class<E> clazz) {
+    public <E extends TableSchema<E>> E table(final String tableName, final Class<E> clazz) {
         TableSchema<E> table = tables.get(tableName);
 
         if (clazz.isInstance(table)) {
@@ -61,7 +61,7 @@ public class DatabaseSchema {
         return createTableSchema(clazz, table);
     }
 
-    protected <E extends TableSchema<E>> E createTableSchema(Class<E> clazz, TableSchema<E> table) {
+    protected <E extends TableSchema<E>> E createTableSchema(final Class<E> clazz, final TableSchema<E> table) {
         Constructor<E> 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;
     }
 
index f32978b1b14a5ccddade4ca3532fc24626bba74c..47a0d9a12c833a52cde700ac71053055629b4cf2 100644 (file)
@@ -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<GenericTableSchema> {
-
     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<String, ColumnSchema> 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<GenericTableSchema> {
             columns.put(column.getKey(), ColumnSchema.fromJson(column.getKey(), column.getValue()));
         }
 
-        this.setName(tableName);
-        this.setColumns(columns);
-        return this;
+        return new GenericTableSchema(tableName, columns);
     }
 }
index 55a2e70ca626cb39e3d95ec74febe93e6f6fd819..a41fbe31a9b3c67e9b552b5930157370a7ff3882 100644 (file)
@@ -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<E extends TableSchema<E>> {
 
     private String name;
     private Map<String, ColumnSchema> columns;
 
-    public TableSchema() {
-    }
-
-    protected TableSchema(String name) {
+    protected TableSchema(final String name) {
         this.name = name;
     }
 
-    public TableSchema(String name, Map<String, ColumnSchema> columns) {
+    protected TableSchema(final String name, final Map<String, ColumnSchema> columns) {
         this.name = name;
         this.columns = columns;
     }
@@ -52,16 +47,15 @@ public abstract class TableSchema<E extends TableSchema<E>> {
         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 extends TableSchema<E>> E as(Class<E> clazz) {
+    public <E extends TableSchema<E>> E as(final Class<E> clazz) {
         try {
             Constructor<E> instance = clazz.getConstructor(TableSchema.class);
             return instance.newInstance(this);
@@ -75,7 +69,7 @@ public abstract class TableSchema<E extends TableSchema<E>> {
         return new Insert<>(this);
     }
 
-    public <D> ColumnSchema<E, Set<D>> multiValuedColumn(String column, Class<D> type) {
+    public <D> ColumnSchema<E, Set<D>> multiValuedColumn(final String column, final Class<D> type) {
         //todo exception handling
 
         ColumnSchema<E, Set<D>> columnSchema = columns.get(column);
@@ -83,7 +77,8 @@ public abstract class TableSchema<E extends TableSchema<E>> {
         return columnSchema;
     }
 
-    public <K,V> ColumnSchema<E, Map<K,V>> multiValuedColumn(String column, Class<K> keyType, Class<V> valueType) {
+    public <K,V> ColumnSchema<E, Map<K,V>> multiValuedColumn(final String column, final Class<K> keyType,
+            final Class<V> valueType) {
         //todo exception handling
 
         ColumnSchema<E, Map<K, V>> columnSchema = columns.get(column);
@@ -91,7 +86,7 @@ public abstract class TableSchema<E extends TableSchema<E>> {
         return columnSchema;
     }
 
-    public <D> ColumnSchema<E, D> column(String column, Class<D> type) {
+    public <D> ColumnSchema<E, D> column(final String column, final Class<D> type) {
         //todo exception handling
 
         ColumnSchema<E, D> columnSchema = columns.get(column);
@@ -101,24 +96,23 @@ public abstract class TableSchema<E extends TableSchema<E>> {
         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<String, ColumnSchema> columns) {
+    protected void setColumns(final Map<String, ColumnSchema> columns) {
         this.columns = columns;
     }
 
-    public TableUpdate<E> updatesFromJson(JsonNode value) {
+    public TableUpdate<E> updatesFromJson(final JsonNode value) {
         TableUpdate<E> tableUpdate = new TableUpdate<>();
         Iterator<Entry<String, JsonNode>> fields = value.fields();
         while (fields.hasNext()) {
@@ -136,7 +130,7 @@ public abstract class TableSchema<E extends TableSchema<E>> {
         return tableUpdate;
     }
 
-    public Row<E> createRow(ObjectNode rowNode) {
+    public Row<E> createRow(final ObjectNode rowNode) {
         List<Column<E, ?>> newColumns = new ArrayList<>();
         for (Iterator<Map.Entry<String, JsonNode>> iter = rowNode.fields(); iter.hasNext();) {
             Map.Entry<String, JsonNode> next = iter.next();
@@ -155,7 +149,7 @@ public abstract class TableSchema<E extends TableSchema<E>> {
         return new Row<>(this, newColumns);
     }
 
-    public List<Row<E>> createRows(JsonNode rowsNode) {
+    public List<Row<E>> createRows(final JsonNode rowsNode) {
         List<Row<E>> rows = new ArrayList<>();
         for (JsonNode rowNode : rowsNode.get("rows")) {
             rows.add(createRow((ObjectNode)rowNode));