package org.opendaylight.ovsdb.lib.schema;
+import static java.util.Objects.requireNonNull;
+
import com.fasterxml.jackson.databind.JsonNode;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.Maps;
import com.google.common.reflect.Invokable;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
private final String name;
private final Version version;
- private final Map<String, TableSchema> tables;
+ private final ImmutableMap<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;
+ this.name = requireNonNull(name);
+ this.version = requireNonNull(version);
+ this.tables = ImmutableMap.copyOf(tables);
}
public Set<String> getTables() {
- return this.tables.keySet();
+ return tables.keySet();
}
public boolean hasTable(final String table) {
- return this.getTables().contains(table);
+ return tables.containsKey(table);
}
public <E extends TableSchema<E>> E table(final String tableName, final Class<E> clazz) {
return version;
}
- public void populateInternallyGeneratedColumns() {
+ public DatabaseSchema withInternallyGeneratedColumns() {
+ return haveInternallyGeneratedColumns() ? this : new DatabaseSchema(name, version,
+ Maps.transformValues(tables, TableSchema::withInternallyGeneratedColumns));
+ }
+
+ protected final boolean haveInternallyGeneratedColumns() {
for (TableSchema tableSchema : tables.values()) {
- tableSchema.populateInternallyGeneratedColumns();
+ if (!tableSchema.haveInternallyGeneratedColumns()) {
+ return false;
+ }
}
+ return true;
}
}
*/
package org.opendaylight.ovsdb.lib.schema;
+import static java.util.Objects.requireNonNull;
+
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
+import com.google.common.collect.ImmutableMap;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
-import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
public abstract class TableSchema<E extends TableSchema<E>> {
private static final AtomicColumnType UUID_COLUMN_TYPE = new AtomicColumnType(UuidBaseType.SINGLETON);
- private static final ColumnSchema UUID_COLUMN_SCHMEMA = new ColumnSchema("_uuid", UUID_COLUMN_TYPE);
- private static final ColumnSchema VERSION_COLUMN_SCHMEMA = new ColumnSchema("_version", UUID_COLUMN_TYPE);
+ protected static final ColumnSchema UUID_COLUMN_SCHMEMA = new ColumnSchema("_uuid", UUID_COLUMN_TYPE);
+ protected static final ColumnSchema VERSION_COLUMN_SCHMEMA = new ColumnSchema("_version", UUID_COLUMN_TYPE);
private final String name;
- private final Map<String, ColumnSchema> columns;
+ private final ImmutableMap<String, ColumnSchema> columns;
protected TableSchema(final String name) {
- this.name = name;
- this.columns = new HashMap<>();
+ this(name, ImmutableMap.of());
}
protected TableSchema(final String name, final Map<String, ColumnSchema> columns) {
- this.name = name;
- this.columns = columns;
+ this.name = requireNonNull(name);
+ this.columns = ImmutableMap.copyOf(columns);
}
public Set<String> getColumns() {
- return this.columns.keySet();
+ return columns.keySet();
}
public Map<String, ColumnSchema> getColumnSchemas() {
}
public boolean hasColumn(final String column) {
- return this.getColumns().contains(column);
+ return columns.containsKey(column);
}
public ColumnType getColumnType(final String column) {
- return this.columns.get(column).getType();
+ return columns.get(column).getType();
}
public <E extends TableSchema<E>> E as(final Class<E> clazz) {
}
public ColumnSchema column(final String column) {
- return this.columns.get(column);
+ return columns.get(column);
}
public String getName() {
* a specific Schema implementation detail & hence adding it by default in the Library
* for better application experience using the library.
*/
- public void populateInternallyGeneratedColumns() {
- columns.put("_uuid", UUID_COLUMN_SCHMEMA);
- columns.put("_version", VERSION_COLUMN_SCHMEMA);
+ public abstract E withInternallyGeneratedColumns();
+
+ protected final boolean haveInternallyGeneratedColumns() {
+ return hasColumn(UUID_COLUMN_SCHMEMA.getName()) && hasColumn(VERSION_COLUMN_SCHMEMA.getName());
}
}