package org.opendaylight.ovsdb.lib.schema.typed;
+import com.google.common.base.Preconditions;
+import com.google.common.reflect.Reflection;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
-
import org.opendaylight.ovsdb.lib.error.ColumnSchemaNotFoundException;
import org.opendaylight.ovsdb.lib.error.SchemaVersionMismatchException;
import org.opendaylight.ovsdb.lib.error.TableSchemaNotFoundException;
import org.opendaylight.ovsdb.lib.schema.GenericTableSchema;
import org.opendaylight.ovsdb.lib.schema.TableSchema;
-import com.google.common.base.Preconditions;
-import com.google.common.reflect.Reflection;
-
public class TyperUtils {
+
private static final String GET_STARTS_WITH = "get";
private static final String SET_STARTS_WITH = "set";
private static final String GETCOLUMN_ENDS_WITH = "Column";
private static final String GETROW_ENDS_WITH = "Row";
+ private TyperUtils() {
+ // Prevent instantiating a utility class
+ }
+
private static <T> String getTableName(Class<T> klazz) {
TypedTable typedTable = klazz.getAnnotation(TypedTable.class);
if (typedTable != null) {
private static boolean isGetTableSchema(Method method) {
TypedColumn typedColumn = method.getAnnotation(TypedColumn.class);
- if (typedColumn != null) {
- return typedColumn.method().equals(MethodType.GETTABLESCHEMA) ? true : false;
- }
- return false;
+ return typedColumn != null && typedColumn.method().equals(MethodType.GETTABLESCHEMA);
}
private static boolean isGetRow(Method method) {
TypedColumn typedColumn = method.getAnnotation(TypedColumn.class);
if (typedColumn != null) {
- return typedColumn.method().equals(MethodType.GETROW) ? true : false;
+ return typedColumn.method().equals(MethodType.GETROW);
}
- if (method.getName().startsWith(GET_STARTS_WITH) && method.getName().endsWith(GETROW_ENDS_WITH)) {
- return true;
- }
- return false;
+ return method.getName().startsWith(GET_STARTS_WITH) && method.getName().endsWith(GETROW_ENDS_WITH);
}
private static boolean isGetColumn(Method method) {
TypedColumn typedColumn = method.getAnnotation(TypedColumn.class);
if (typedColumn != null) {
- return typedColumn.method().equals(MethodType.GETCOLUMN) ? true : false;
+ return typedColumn.method().equals(MethodType.GETCOLUMN);
}
- if (method.getName().startsWith(GET_STARTS_WITH) && method.getName().endsWith(GETCOLUMN_ENDS_WITH)) {
- return true;
- }
- return false;
+ return method.getName().startsWith(GET_STARTS_WITH) && method.getName().endsWith(GETCOLUMN_ENDS_WITH);
}
private static boolean isGetData(Method method) {
TypedColumn typedColumn = method.getAnnotation(TypedColumn.class);
if (typedColumn != null) {
- return typedColumn.method().equals(MethodType.GETDATA) ? true : false;
+ return typedColumn.method().equals(MethodType.GETDATA);
}
- if (method.getName().startsWith(GET_STARTS_WITH) && !method.getName().endsWith(GETCOLUMN_ENDS_WITH)) {
- return true;
- }
- return false;
+ return method.getName().startsWith(GET_STARTS_WITH) && !method.getName().endsWith(GETCOLUMN_ENDS_WITH);
}
private static boolean isSetData(Method method) {
TypedColumn typedColumn = method.getAnnotation(TypedColumn.class);
if (typedColumn != null) {
- return typedColumn.method().equals(MethodType.SETDATA) ? true : false;
+ return typedColumn.method().equals(MethodType.SETDATA);
}
- if (method.getName().startsWith(SET_STARTS_WITH)) {
- return true;
- }
- return false;
+ return method.getName().startsWith(SET_STARTS_WITH);
}
public static Version getColumnFromVersion(Method method) {
}
TypedTable typedTable = klazz.getAnnotation(TypedTable.class);
- if (typedTable != null) {
- if (!dbSchema.getName().equalsIgnoreCase(typedTable.database())) {
- return false;
- }
+ if (typedTable != null && !dbSchema.getName().equalsIgnoreCase(typedTable.database())) {
+ return false;
}
checkTableSchemaVersion(dbSchema, klazz);
}
private static void checkVersion(Version schemaVersion, Version fromVersion, Version untilVersion) {
- if (!fromVersion.equals(Version.NULL)) {
- if (schemaVersion.compareTo(fromVersion) < 0) {
- String message = SchemaVersionMismatchException.createMessage(schemaVersion, fromVersion);
- throw new SchemaVersionMismatchException(message);
- }
- }
- if (!untilVersion.equals(Version.NULL)) {
- if (schemaVersion.compareTo(untilVersion) > 0) {
- String message = SchemaVersionMismatchException.createMessage(schemaVersion, untilVersion);
- throw new SchemaVersionMismatchException(message);
- }
+ if ((!fromVersion.equals(Version.NULL) && schemaVersion.compareTo(fromVersion) < 0) || (!untilVersion.equals(
+ Version.NULL) && schemaVersion.compareTo(untilVersion) > 0)) {
+ throw new SchemaVersionMismatchException(schemaVersion, fromVersion, untilVersion);
}
}
/**
- * This method returns a Typed Proxy implementation for the klazz passed as a parameter.
+ * Returns a Typed Proxy implementation for the klazz passed as a parameter.
* Per design choice, the Typed Proxy implementation is just a Wrapper on top of the actual
* Row which is untyped.
- * Being just a wrapper, it is state-less and more of a convenience functionality to
+ *
+ * <p>Being just a wrapper, it is state-less and more of a convenience functionality to
* provide a type-safe infrastructure for the applications to built on top of.
* And this Typed infra is completely optional.
*
- * It is the applications responsibility to pass on the raw Row parameter and this method will
+ * <p>It is the applications responsibility to pass on the raw Row parameter and this method will
* return the appropriate Proxy wrapper for the passed klazz Type.
* The raw Row parameter may be null if the caller is interested in just the ColumnSchema.
* But that is not a very common use-case.
*
* @param dbSchema DatabaseSchema as learnt from a OVSDB connection
* @param klazz Typed Class that represents a Table
- * @return
*/
public static <T> T getTypedRowWrapper(final DatabaseSchema dbSchema, final Class<T> klazz) {
return getTypedRowWrapper(dbSchema, klazz,new Row<GenericTableSchema>());
}
/**
- * This method returns a Typed Proxy implementation for the klazz passed as a parameter.
+ * Returns a Typed Proxy implementation for the klazz passed as a parameter.
* Per design choice, the Typed Proxy implementation is just a Wrapper on top of the actual
* Row which is untyped.
- * Being just a wrapper, it is state-less and more of a convenience functionality
+ *
+ * <p>Being just a wrapper, it is state-less and more of a convenience functionality
* to provide a type-safe infrastructure for the applications to built on top of.
* And this Typed infra is completely optional.
*
- * It is the applications responsibility to pass on the raw Row parameter and this method
+ * <p>It is the applications responsibility to pass on the raw Row parameter and this method
* will return the appropriate Proxy wrapper for the passed klazz Type.
* The raw Row parameter may be null if the caller is interested in just the
* ColumnSchema. But that is not a very common use-case.
* @param klazz Typed Class that represents a Table
* @param row The actual Row that the wrapper is operating on. It can be null if the caller
* is just interested in getting ColumnSchema.
- * @return
*/
public static <T> T getTypedRowWrapper(final DatabaseSchema dbSchema, final Class<T> klazz,
final Row<GenericTableSchema> row) {
row.setTableSchema(getTableSchema(dbSchema, klazz));
}
return Reflection.newProxy(klazz, new InvocationHandler() {
- private Object processGetData(Method method) throws Throwable {
+ private Object processGetData(Method method) {
String columnName = getColumnName(method);
checkColumnSchemaVersion(dbSchema, method);
if (columnName == null) {
return row.getColumn(columnSchema).getData();
}
- private Object processGetRow() throws Throwable {
+ private Object processGetRow() {
return row;
}
- private Object processGetColumn(Method method) throws Throwable {
+ private Object processGetColumn(Method method) {
String columnName = getColumnName(method);
checkColumnSchemaVersion(dbSchema, method);
if (columnName == null) {
// When the row is null, that might indicate that the user maybe interested
// only in the ColumnSchema and not on the Data.
if (row == null) {
- return new Column<GenericTableSchema, Object>(columnSchema, null);
+ return new Column<>(columnSchema, null);
}
return row.getColumn(columnSchema);
}
- private Object processSetData(Object proxy, Method method, Object[] args) throws Throwable {
+ private Object processSetData(Object proxy, Method method, Object[] args) {
if (args == null || args.length != 1) {
throw new TyperException("Setter method : " + method.getName() + " requires 1 argument");
}
ColumnSchema<GenericTableSchema, Object> columnSchema =
getColumnSchema(tableSchema, columnName, (Class<Object>) args[0].getClass());
Column<GenericTableSchema, Object> column =
- new Column<GenericTableSchema, Object>(columnSchema, args[0]);
+ new Column<>(columnSchema, args[0]);
row.addColumn(columnName, column);
return proxy;
}
private Boolean isHashCodeMethod(Method method, Object[] args) {
return (args == null || args.length == 0) && method.getName().equals("hashCode");
}
+
private Boolean isEqualsMethod(Method method, Object[] args) {
return (args != null
&& args.length == 1
&& method.getName().equals("equals")
- && method.getParameterTypes()[0] == Object.class);
+ && Object.class.equals(method.getParameterTypes()[0]));
}
+
private Boolean isToStringMethod(Method method, Object[] args) {
return (args == null || args.length == 0) && method.getName().equals("toString");
}
+
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (isGetTableSchema(method)) {
}
@Override public String toString() {
- String tableName = null;
+ String tableName;
try {
TableSchema<?> schema = (TableSchema<?>)processGetTableSchema();
tableName = schema.getName();
Preconditions.checkNotNull(klazz);
Preconditions.checkNotNull(updates);
Preconditions.checkNotNull(dbSchema);
- Map<UUID,T> result = new HashMap<UUID,T>();
+ Map<UUID,T> result = new HashMap<>();
Map<UUID,TableUpdate<GenericTableSchema>.RowUpdate<GenericTableSchema>> rowUpdates =
extractRowUpdates(klazz,updates,dbSchema);
for (TableUpdate<GenericTableSchema>.RowUpdate<GenericTableSchema> rowUpdate : rowUpdates.values()) {
- if (rowUpdate != null) {
- if (rowUpdate.getNew() != null) {
- Row<GenericTableSchema> row = rowUpdate.getNew();
- result.put(rowUpdate.getUuid(),TyperUtils.getTypedRowWrapper(dbSchema,klazz,row));
- }
+ if (rowUpdate != null && rowUpdate.getNew() != null) {
+ Row<GenericTableSchema> row = rowUpdate.getNew();
+ result.put(rowUpdate.getUuid(),TyperUtils.getTypedRowWrapper(dbSchema,klazz,row));
}
}
return result;
/**
* This method extracts all row updates of Class<T> klazz from a TableUpdates
- * that correspond to old version of rows of type klazz that have been updated
+ * that correspond to old version of rows of type klazz that have been updated.
* Example:
* <code>
* Map<UUID,Bridge> oldBridges = extractRowsOld(Bridge.class,updates,dbSchema)
* @param dbSchema Dbschema for the TableUpdates
* @return Map<UUID,T> for the type of things being sought
*/
- public static <T> Map<UUID,T> extractRowsOld(Class<T> klazz,TableUpdates updates,DatabaseSchema dbSchema) {
+ public static <T> Map<UUID, T> extractRowsOld(Class<T> klazz, TableUpdates updates, DatabaseSchema dbSchema) {
Preconditions.checkNotNull(klazz);
Preconditions.checkNotNull(updates);
Preconditions.checkNotNull(dbSchema);
- Map<UUID,T> result = new HashMap<UUID,T>();
+ Map<UUID,T> result = new HashMap<>();
Map<UUID,TableUpdate<GenericTableSchema>.RowUpdate<GenericTableSchema>> rowUpdates =
extractRowUpdates(klazz,updates,dbSchema);
for (TableUpdate<GenericTableSchema>.RowUpdate<GenericTableSchema> rowUpdate : rowUpdates.values()) {
- if (rowUpdate != null) {
- if (rowUpdate.getOld() != null) {
- Row<GenericTableSchema> row = rowUpdate.getOld();
- result.put(rowUpdate.getUuid(),TyperUtils.getTypedRowWrapper(dbSchema,klazz,row));
- }
+ if (rowUpdate != null && rowUpdate.getOld() != null) {
+ Row<GenericTableSchema> row = rowUpdate.getOld();
+ result.put(rowUpdate.getUuid(),TyperUtils.getTypedRowWrapper(dbSchema,klazz,row));
}
}
return result;
Preconditions.checkNotNull(klazz);
Preconditions.checkNotNull(updates);
Preconditions.checkNotNull(dbSchema);
- Map<UUID,T> result = new HashMap<UUID,T>();
+ Map<UUID,T> result = new HashMap<>();
Map<UUID,TableUpdate<GenericTableSchema>.RowUpdate<GenericTableSchema>> rowUpdates =
extractRowUpdates(klazz,updates,dbSchema);
for (TableUpdate<GenericTableSchema>.RowUpdate<GenericTableSchema> rowUpdate : rowUpdates.values()) {
- if (rowUpdate != null) {
- if (rowUpdate.getNew() == null && rowUpdate.getOld() != null) {
- Row<GenericTableSchema> row = rowUpdate.getOld();
- result.put(rowUpdate.getUuid(),TyperUtils.getTypedRowWrapper(dbSchema,klazz,row));
- }
+ if (rowUpdate != null && rowUpdate.getNew() == null && rowUpdate.getOld() != null) {
+ Row<GenericTableSchema> row = rowUpdate.getOld();
+ result.put(rowUpdate.getUuid(),TyperUtils.getTypedRowWrapper(dbSchema,klazz,row));
}
}
return result;
Preconditions.checkNotNull(updates);
Preconditions.checkNotNull(dbSchema);
Map<UUID, TableUpdate<GenericTableSchema>.RowUpdate<GenericTableSchema>> result =
- new HashMap<UUID,TableUpdate<GenericTableSchema>.RowUpdate<GenericTableSchema>>();
+ new HashMap<>();
TableUpdate<GenericTableSchema> update = updates.getUpdate(TyperUtils.getTableSchema(dbSchema, klazz));
if (update != null) {
Map<UUID, TableUpdate<GenericTableSchema>.RowUpdate<GenericTableSchema>> rows = update.getRows();