X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;ds=sidebyside;f=library%2Fimpl%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fovsdb%2Flib%2Fschema%2Ftyped%2FTyperUtils.java;h=e3eb23e19eadcaadf0f1679d630157d3ad974829;hb=324828f6ddb7f8e3f514adc2a54d0bd10d48d589;hp=934639f34a0fcbf0fcb1e8b1fd92ae18be568594;hpb=5a094317de462a9efb08f007b806f9c683d29c03;p=ovsdb.git diff --git a/library/impl/src/main/java/org/opendaylight/ovsdb/lib/schema/typed/TyperUtils.java b/library/impl/src/main/java/org/opendaylight/ovsdb/lib/schema/typed/TyperUtils.java index 934639f34..e3eb23e19 100644 --- a/library/impl/src/main/java/org/opendaylight/ovsdb/lib/schema/typed/TyperUtils.java +++ b/library/impl/src/main/java/org/opendaylight/ovsdb/lib/schema/typed/TyperUtils.java @@ -8,11 +8,12 @@ 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; @@ -29,15 +30,17 @@ import org.opendaylight.ovsdb.lib.schema.DatabaseSchema; 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 String getTableName(Class klazz) { TypedTable typedTable = klazz.getAnnotation(TypedTable.class); if (typedTable != null) { @@ -82,58 +85,43 @@ public class TyperUtils { 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) { @@ -183,10 +171,8 @@ public class TyperUtils { } 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); @@ -209,50 +195,43 @@ public class TyperUtils { } 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 + * + *

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 + *

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 getTypedRowWrapper(final DatabaseSchema dbSchema, final Class klazz) { return getTypedRowWrapper(dbSchema, klazz,new Row()); } /** - * 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 + * + *

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 + *

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. @@ -261,7 +240,6 @@ public class TyperUtils { * @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 getTypedRowWrapper(final DatabaseSchema dbSchema, final Class klazz, final Row row) { @@ -272,7 +250,7 @@ public class TyperUtils { 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) { @@ -296,11 +274,11 @@ public class TyperUtils { 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) { @@ -321,12 +299,12 @@ public class TyperUtils { // 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(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"); } @@ -339,7 +317,7 @@ public class TyperUtils { ColumnSchema columnSchema = getColumnSchema(tableSchema, columnName, (Class) args[0].getClass()); Column column = - new Column(columnSchema, args[0]); + new Column<>(columnSchema, args[0]); row.addColumn(columnName, column); return proxy; } @@ -354,15 +332,18 @@ public class TyperUtils { 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)) { @@ -408,7 +389,7 @@ public class TyperUtils { } @Override public String toString() { - String tableName = null; + String tableName; try { TableSchema schema = (TableSchema)processGetTableSchema(); tableName = schema.getName(); @@ -441,15 +422,13 @@ public class TyperUtils { Preconditions.checkNotNull(klazz); Preconditions.checkNotNull(updates); Preconditions.checkNotNull(dbSchema); - Map result = new HashMap(); + Map result = new HashMap<>(); Map.RowUpdate> rowUpdates = extractRowUpdates(klazz,updates,dbSchema); for (TableUpdate.RowUpdate rowUpdate : rowUpdates.values()) { - if (rowUpdate != null) { - if (rowUpdate.getNew() != null) { - Row row = rowUpdate.getNew(); - result.put(rowUpdate.getUuid(),TyperUtils.getTypedRowWrapper(dbSchema,klazz,row)); - } + if (rowUpdate != null && rowUpdate.getNew() != null) { + Row row = rowUpdate.getNew(); + result.put(rowUpdate.getUuid(),TyperUtils.getTypedRowWrapper(dbSchema,klazz,row)); } } return result; @@ -457,7 +436,7 @@ public class TyperUtils { /** * 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: * * Map<UUID,Bridge> oldBridges = extractRowsOld(Bridge.class,updates,dbSchema) @@ -468,19 +447,17 @@ public class TyperUtils { * @param dbSchema Dbschema for the TableUpdates * @return Map<UUID,T> for the type of things being sought */ - public static Map extractRowsOld(Class klazz,TableUpdates updates,DatabaseSchema dbSchema) { + public static Map extractRowsOld(Class klazz, TableUpdates updates, DatabaseSchema dbSchema) { Preconditions.checkNotNull(klazz); Preconditions.checkNotNull(updates); Preconditions.checkNotNull(dbSchema); - Map result = new HashMap(); + Map result = new HashMap<>(); Map.RowUpdate> rowUpdates = extractRowUpdates(klazz,updates,dbSchema); for (TableUpdate.RowUpdate rowUpdate : rowUpdates.values()) { - if (rowUpdate != null) { - if (rowUpdate.getOld() != null) { - Row row = rowUpdate.getOld(); - result.put(rowUpdate.getUuid(),TyperUtils.getTypedRowWrapper(dbSchema,klazz,row)); - } + if (rowUpdate != null && rowUpdate.getOld() != null) { + Row row = rowUpdate.getOld(); + result.put(rowUpdate.getUuid(),TyperUtils.getTypedRowWrapper(dbSchema,klazz,row)); } } return result; @@ -503,15 +480,13 @@ public class TyperUtils { Preconditions.checkNotNull(klazz); Preconditions.checkNotNull(updates); Preconditions.checkNotNull(dbSchema); - Map result = new HashMap(); + Map result = new HashMap<>(); Map.RowUpdate> rowUpdates = extractRowUpdates(klazz,updates,dbSchema); for (TableUpdate.RowUpdate rowUpdate : rowUpdates.values()) { - if (rowUpdate != null) { - if (rowUpdate.getNew() == null && rowUpdate.getOld() != null) { - Row row = rowUpdate.getOld(); - result.put(rowUpdate.getUuid(),TyperUtils.getTypedRowWrapper(dbSchema,klazz,row)); - } + if (rowUpdate != null && rowUpdate.getNew() == null && rowUpdate.getOld() != null) { + Row row = rowUpdate.getOld(); + result.put(rowUpdate.getUuid(),TyperUtils.getTypedRowWrapper(dbSchema,klazz,row)); } } return result; @@ -538,7 +513,7 @@ public class TyperUtils { Preconditions.checkNotNull(updates); Preconditions.checkNotNull(dbSchema); Map.RowUpdate> result = - new HashMap.RowUpdate>(); + new HashMap<>(); TableUpdate update = updates.getUpdate(TyperUtils.getTableSchema(dbSchema, klazz)); if (update != null) { Map.RowUpdate> rows = update.getRows();