Cleanup ColumnSchema 32/86132/2
authorRobert Varga <robert.varga@pantheon.tech>
Fri, 29 Nov 2019 10:24:05 +0000 (11:24 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Tue, 3 Dec 2019 10:19:29 +0000 (11:19 +0100)
Fields should be private (making it obvious they are final) and
the fromJson() method should be properly generic.

Change-Id: Ic38e4179f1108f41882bd336b3a03b339d435f9c
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
(cherry picked from commit 347d025f789d5a53f0d24d114905cd6fefd6c311)

library/impl/src/main/java/org/opendaylight/ovsdb/lib/schema/ColumnSchema.java

index 5171748eced06d9a7448e0eea22c6f2f1685166c..9dc03a2c3e36aa322da6ab58fc35ec30f99e0137 100644 (file)
@@ -5,11 +5,11 @@
  * 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;
 import java.util.Map;
+import java.util.Objects;
 import java.util.Set;
 import org.opendaylight.ovsdb.lib.error.BadSchemaException;
 import org.opendaylight.ovsdb.lib.notation.Condition;
@@ -17,22 +17,24 @@ import org.opendaylight.ovsdb.lib.notation.Function;
 import org.opendaylight.ovsdb.lib.notation.OvsdbMap;
 import org.opendaylight.ovsdb.lib.notation.OvsdbSet;
 
-
 public class ColumnSchema<E extends TableSchema<E>, D> {
-    String name;
-    ColumnType type;
+    private final String name;
+    private final ColumnType type;
 
-    public ColumnSchema(String name, ColumnType columnType) {
+    public ColumnSchema(final String name, final ColumnType columnType) {
         this.name = name;
         this.type = columnType;
     }
 
-    public static ColumnSchema fromJson(String name, JsonNode json) {
-        if (!json.isObject() || !json.has("type")) {
+    public static <E extends TableSchema<E>, D> ColumnSchema<E, D> fromJson(final String name, final JsonNode json) {
+        if (!json.isObject()) {
+            throw new BadSchemaException("bad column schema root, expected an object");
+        }
+        final JsonNode type = json.get("type");
+        if (type == null) {
             throw new BadSchemaException("bad column schema root, expected \"type\" as child");
         }
-
-        return new ColumnSchema(name, ColumnType.fromJson(json.get("type")));
+        return new ColumnSchema<>(name, ColumnType.fromJson(type));
     }
 
     public String getName() {
@@ -45,27 +47,27 @@ public class ColumnSchema<E extends TableSchema<E>, D> {
 
     // --- Operations on the column ----------//
 
-    public Condition opEqual(D data) {
+    public Condition opEqual(final D data) {
         return new Condition(this.getName(), Function.EQUALS, data);
     }
 
-    public Condition opGreaterThan(D data) {
+    public Condition opGreaterThan(final D data) {
         return new Condition(this.getName(), Function.GREATER_THAN, data);
     }
 
-    public Condition opLesserThan(D data) {
+    public Condition opLesserThan(final D data) {
         return new Condition(this.getName(), Function.GREATER_THAN, data);
     }
 
-    public Condition opLesserThanOrEquals(D data) {
+    public Condition opLesserThanOrEquals(final D data) {
         return new Condition(this.getName(), Function.LESS_THAN_OR_EQUALS, data);
     }
 
-    public Condition opIncludes(D data) {
+    public Condition opIncludes(final D data) {
         return new Condition(this.getName(), Function.INCLUDES, data);
     }
 
-    public Condition opExcludes(D data) {
+    public Condition opExcludes(final D data) {
         return new Condition(this.getName(), Function.EXCLUDES, data);
     }
 
@@ -84,44 +86,27 @@ public class ColumnSchema<E extends TableSchema<E>, D> {
     public int hashCode() {
         final int prime = 31;
         int result = 1;
-        result = prime * result + ((name == null) ? 0 : name.hashCode());
-        result = prime * result + ((type == null) ? 0 : type.hashCode());
+        result = prime * result + (name == null ? 0 : name.hashCode());
+        result = prime * result + (type == null ? 0 : type.hashCode());
         return result;
     }
 
     @Override
-    public boolean equals(Object obj) {
+    public boolean equals(final Object obj) {
         if (this == obj) {
             return true;
         }
-        if (obj == null) {
-            return false;
-        }
-        if (getClass() != obj.getClass()) {
-            return false;
-        }
-        ColumnSchema other = (ColumnSchema) obj;
-        if (name == null) {
-            if (other.name != null) {
-                return false;
-            }
-        } else if (!name.equals(other.name)) {
-            return false;
-        }
-        if (type == null) {
-            if (other.type != null) {
-                return false;
-            }
-        } else if (!type.equals(other.type)) {
+        if (obj == null || getClass() != obj.getClass()) {
             return false;
         }
-        return true;
+        final ColumnSchema<?, ?> other = (ColumnSchema<?, ?>) obj;
+        return Objects.equals(name, other.name) && Objects.equals(type, other.type);
     }
 
     /**
      * Validates the passed in value against the constraints set for this ColumnSchema.
      */
-    public D validate(Object value) {
+    public D validate(final Object value) {
         //todo(type check and validate based on constraints set)
         this.type.validate(value);
         return (D) value;
@@ -131,15 +116,15 @@ public class ColumnSchema<E extends TableSchema<E>, D> {
      * Verifies if this Column if of the specified type.
      * @param typeClass the type to check for
      */
-    public void validateType(Class<?> typeClass) {
+    public void validateType(final Class<?> typeClass) {
 
     }
 
-    public D valueFromJson(JsonNode value) {
+    public D valueFromJson(final JsonNode value) {
         return (D) this.getType().valueFromJson(value);
     }
 
-    public Object getNormalizeData(D value) {
+    public Object getNormalizeData(final D value) {
         Object untypedValue;
         if (value instanceof Set) {
             untypedValue = OvsdbSet.fromSet((Set) value);