2 * Copyright (c) 2014, 2015 EBay Software Foundation and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.ovsdb.lib.schema;
14 import org.opendaylight.ovsdb.lib.error.BadSchemaException;
15 import org.opendaylight.ovsdb.lib.notation.Condition;
16 import org.opendaylight.ovsdb.lib.notation.Function;
17 import org.opendaylight.ovsdb.lib.notation.OvsdbMap;
18 import org.opendaylight.ovsdb.lib.notation.OvsdbSet;
20 import com.fasterxml.jackson.databind.JsonNode;
23 public class ColumnSchema<E extends TableSchema<E>, D> {
29 public ColumnSchema(String name, ColumnType columnType) {
31 this.type = columnType;
34 public static ColumnSchema fromJson(String name, JsonNode json) {
35 if (!json.isObject() || !json.has("type")) {
36 throw new BadSchemaException("bad column schema root, expected \"type\" as child");
39 return new ColumnSchema(name, ColumnType.fromJson(json.get("type")));
42 public String getName() {
46 public ColumnType getType() {
50 // --- Operations on the column ----------//
52 public Condition opEqual(D data) {
53 return new Condition(this.getName(), Function.EQUALS, data);
56 public Condition opGreaterThan(D data) {
57 return new Condition(this.getName(), Function.GREATER_THAN, data);
60 public Condition opLesserThan(D data) {
61 return new Condition(this.getName(), Function.GREATER_THAN, data);
64 public Condition opLesserThanOrEquals(D data) {
65 return new Condition(this.getName(), Function.LESS_THAN_OR_EQUALS, data);
68 public Condition opIncludes(D data) {
69 return new Condition(this.getName(), Function.INCLUDES, data);
72 public Condition opExcludes(D data) {
73 return new Condition(this.getName(), Function.EXCLUDES, data);
76 // --- Operations on the column ----------//:w
80 public String toString() {
81 return "ColumnSchema{"
82 + "name='" + name + '\''
88 public int hashCode() {
91 result = prime * result + ((name == null) ? 0 : name.hashCode());
92 result = prime * result + ((type == null) ? 0 : type.hashCode());
97 public boolean equals(Object obj) {
104 if (getClass() != obj.getClass()) {
107 ColumnSchema other = (ColumnSchema) obj;
109 if (other.name != null) {
112 } else if (!name.equals(other.name)) {
116 if (other.type != null) {
119 } else if (!type.equals(other.type)) {
126 * Validates the passed in value against the constraints set for this ColumnSchema
128 * @throws java.lang.RuntimeException (validation exception)
130 public D validate(Object value)throws RuntimeException {
131 //todo(type check and validate based on constraints set)
132 this.type.validate(value);
137 * Verifies if this Column if of the specified type
138 * @param type the type to check for
140 public void validateType(Class<?> type) {
144 public D valueFromJson(JsonNode value) {
145 return (D) this.getType().valueFromJson(value);
148 public Object getNormalizeData(D value) {
149 Object untypedValue = null;
150 if (value instanceof Set) {
151 untypedValue = OvsdbSet.fromSet((Set) value);
152 } else if (value instanceof Map) {
153 untypedValue = OvsdbMap.fromMap((Map) value);
155 untypedValue = value;