From 347d025f789d5a53f0d24d114905cd6fefd6c311 Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Fri, 29 Nov 2019 11:24:05 +0100 Subject: [PATCH] Cleanup ColumnSchema 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 --- .../ovsdb/lib/schema/ColumnSchema.java | 69 ++++++++----------- 1 file changed, 27 insertions(+), 42 deletions(-) diff --git a/library/impl/src/main/java/org/opendaylight/ovsdb/lib/schema/ColumnSchema.java b/library/impl/src/main/java/org/opendaylight/ovsdb/lib/schema/ColumnSchema.java index 5171748ec..9dc03a2c3 100644 --- a/library/impl/src/main/java/org/opendaylight/ovsdb/lib/schema/ColumnSchema.java +++ b/library/impl/src/main/java/org/opendaylight/ovsdb/lib/schema/ColumnSchema.java @@ -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, 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 , D> ColumnSchema 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, 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, 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, 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); -- 2.36.6