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;
11 import com.fasterxml.jackson.databind.JsonNode;
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;
21 public class ColumnSchema<E extends TableSchema<E>, D> {
25 public ColumnSchema(String name, ColumnType columnType) {
27 this.type = columnType;
30 public static ColumnSchema fromJson(String name, JsonNode json) {
31 if (!json.isObject() || !json.has("type")) {
32 throw new BadSchemaException("bad column schema root, expected \"type\" as child");
35 return new ColumnSchema(name, ColumnType.fromJson(json.get("type")));
38 public String getName() {
42 public ColumnType getType() {
46 // --- Operations on the column ----------//
48 public Condition opEqual(D data) {
49 return new Condition(this.getName(), Function.EQUALS, data);
52 public Condition opGreaterThan(D data) {
53 return new Condition(this.getName(), Function.GREATER_THAN, data);
56 public Condition opLesserThan(D data) {
57 return new Condition(this.getName(), Function.GREATER_THAN, data);
60 public Condition opLesserThanOrEquals(D data) {
61 return new Condition(this.getName(), Function.LESS_THAN_OR_EQUALS, data);
64 public Condition opIncludes(D data) {
65 return new Condition(this.getName(), Function.INCLUDES, data);
68 public Condition opExcludes(D data) {
69 return new Condition(this.getName(), Function.EXCLUDES, data);
72 // --- Operations on the column ----------//:w
76 public String toString() {
77 return "ColumnSchema{"
78 + "name='" + name + '\''
84 public int hashCode() {
87 result = prime * result + ((name == null) ? 0 : name.hashCode());
88 result = prime * result + ((type == null) ? 0 : type.hashCode());
93 public boolean equals(Object obj) {
100 if (getClass() != obj.getClass()) {
103 ColumnSchema other = (ColumnSchema) obj;
105 if (other.name != null) {
108 } else if (!name.equals(other.name)) {
112 if (other.type != null) {
115 } else if (!type.equals(other.type)) {
122 * Validates the passed in value against the constraints set for this ColumnSchema.
124 public D validate(Object value) {
125 //todo(type check and validate based on constraints set)
126 this.type.validate(value);
131 * Verifies if this Column if of the specified type.
132 * @param typeClass the type to check for
134 public void validateType(Class<?> typeClass) {
138 public D valueFromJson(JsonNode value) {
139 return (D) this.getType().valueFromJson(value);
142 public Object getNormalizeData(D value) {
144 if (value instanceof Set) {
145 untypedValue = OvsdbSet.fromSet((Set) value);
146 } else if (value instanceof Map) {
147 untypedValue = OvsdbMap.fromMap((Map) value);
149 untypedValue = value;