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)
* terms of the Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*/
* 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;
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;
import java.util.Set;
import org.opendaylight.ovsdb.lib.error.BadSchemaException;
import org.opendaylight.ovsdb.lib.notation.Condition;
import org.opendaylight.ovsdb.lib.notation.OvsdbMap;
import org.opendaylight.ovsdb.lib.notation.OvsdbSet;
import org.opendaylight.ovsdb.lib.notation.OvsdbMap;
import org.opendaylight.ovsdb.lib.notation.OvsdbSet;
public class ColumnSchema<E extends TableSchema<E>, D> {
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;
}
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");
}
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() {
}
public String getName() {
// --- Operations on the column ----------//
// --- Operations on the column ----------//
- public Condition opEqual(D data) {
+ public Condition opEqual(final D data) {
return new Condition(this.getName(), Function.EQUALS, 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);
}
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);
}
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);
}
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);
}
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);
}
return new Condition(this.getName(), Function.EXCLUDES, data);
}
public int hashCode() {
final int prime = 31;
int result = 1;
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
return result;
}
@Override
- public boolean equals(Object obj) {
+ public boolean equals(final Object obj) {
if (this == obj) {
return true;
}
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()) {
+ 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.
*/
}
/**
* 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;
//todo(type check and validate based on constraints set)
this.type.validate(value);
return (D) value;
* Verifies if this Column if of the specified type.
* @param typeClass the type to check for
*/
* 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);
}
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);
Object untypedValue;
if (value instanceof Set) {
untypedValue = OvsdbSet.fromSet((Set) value);