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> {
27 public ColumnSchema(String name, ColumnType columnType) {
29 this.type = columnType;
32 public static ColumnSchema fromJson(String name, JsonNode json) {
33 if (!json.isObject() || !json.has("type")) {
34 throw new BadSchemaException("bad column schema root, expected \"type\" as child");
37 return new ColumnSchema(name, ColumnType.fromJson(json.get("type")));
40 public String getName() {
44 public ColumnType getType() {
48 // --- Operations on the column ----------//
50 public Condition opEqual(D data) {
51 return new Condition(this.getName(), Function.EQUALS, data);
54 public Condition opGreaterThan(D data) {
55 return new Condition(this.getName(), Function.GREATER_THAN, data);
58 public Condition opLesserThan(D data) {
59 return new Condition(this.getName(), Function.GREATER_THAN, data);
62 public Condition opLesserThanOrEquals(D data) {
63 return new Condition(this.getName(), Function.LESS_THAN_OR_EQUALS, data);
66 public Condition opIncludes(D data) {
67 return new Condition(this.getName(), Function.INCLUDES, data);
70 public Condition opExcludes(D data) {
71 return new Condition(this.getName(), Function.EXCLUDES, data);
74 // --- Operations on the column ----------//:w
78 public String toString() {
79 return "ColumnSchema{"
80 + "name='" + name + '\''
86 public int hashCode() {
89 result = prime * result + ((name == null) ? 0 : name.hashCode());
90 result = prime * result + ((type == null) ? 0 : type.hashCode());
95 public boolean equals(Object obj) {
102 if (getClass() != obj.getClass()) {
105 ColumnSchema other = (ColumnSchema) obj;
107 if (other.name != null) {
110 } else if (!name.equals(other.name)) {
114 if (other.type != null) {
117 } else if (!type.equals(other.type)) {
124 * Validates the passed in value against the constraints set for this ColumnSchema
127 public D validate(Object value) {
128 //todo(type check and validate based on constraints set)
129 this.type.validate(value);
134 * Verifies if this Column if of the specified type
135 * @param type the type to check for
137 public void validateType(Class<?> type) {
141 public D valueFromJson(JsonNode value) {
142 return (D) this.getType().valueFromJson(value);
145 public Object getNormalizeData(D value) {
147 if (value instanceof Set) {
148 untypedValue = OvsdbSet.fromSet((Set) value);
149 } else if (value instanceof Map) {
150 untypedValue = OvsdbMap.fromMap((Map) value);
152 untypedValue = value;