5171748eced06d9a7448e0eea22c6f2f1685166c
[ovsdb.git] / library / impl / src / main / java / org / opendaylight / ovsdb / lib / schema / ColumnSchema.java
1 /*
2  * Copyright (c) 2014, 2015 EBay Software Foundation and others. All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.ovsdb.lib.schema;
10
11 import com.fasterxml.jackson.databind.JsonNode;
12 import java.util.Map;
13 import java.util.Set;
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;
19
20
21 public class ColumnSchema<E extends TableSchema<E>, D> {
22     String name;
23     ColumnType type;
24
25     public ColumnSchema(String name, ColumnType columnType) {
26         this.name = name;
27         this.type = columnType;
28     }
29
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");
33         }
34
35         return new ColumnSchema(name, ColumnType.fromJson(json.get("type")));
36     }
37
38     public String getName() {
39         return name;
40     }
41
42     public ColumnType getType() {
43         return type;
44     }
45
46     // --- Operations on the column ----------//
47
48     public Condition opEqual(D data) {
49         return new Condition(this.getName(), Function.EQUALS, data);
50     }
51
52     public Condition opGreaterThan(D data) {
53         return new Condition(this.getName(), Function.GREATER_THAN, data);
54     }
55
56     public Condition opLesserThan(D data) {
57         return new Condition(this.getName(), Function.GREATER_THAN, data);
58     }
59
60     public Condition opLesserThanOrEquals(D data) {
61         return new Condition(this.getName(), Function.LESS_THAN_OR_EQUALS, data);
62     }
63
64     public Condition opIncludes(D data) {
65         return new Condition(this.getName(), Function.INCLUDES, data);
66     }
67
68     public Condition opExcludes(D data) {
69         return new Condition(this.getName(), Function.EXCLUDES, data);
70     }
71
72     // --- Operations on the column ----------//:w
73
74
75     @Override
76     public String toString() {
77         return "ColumnSchema{"
78                 + "name='" + name + '\''
79                 + ", type=" + type
80                 + '}';
81     }
82
83     @Override
84     public int hashCode() {
85         final int prime = 31;
86         int result = 1;
87         result = prime * result + ((name == null) ? 0 : name.hashCode());
88         result = prime * result + ((type == null) ? 0 : type.hashCode());
89         return result;
90     }
91
92     @Override
93     public boolean equals(Object obj) {
94         if (this == obj) {
95             return true;
96         }
97         if (obj == null) {
98             return false;
99         }
100         if (getClass() != obj.getClass()) {
101             return false;
102         }
103         ColumnSchema other = (ColumnSchema) obj;
104         if (name == null) {
105             if (other.name != null) {
106                 return false;
107             }
108         } else if (!name.equals(other.name)) {
109             return false;
110         }
111         if (type == null) {
112             if (other.type != null) {
113                 return false;
114             }
115         } else if (!type.equals(other.type)) {
116             return false;
117         }
118         return true;
119     }
120
121     /**
122      * Validates the passed in value against the constraints set for this ColumnSchema.
123      */
124     public D validate(Object value) {
125         //todo(type check and validate based on constraints set)
126         this.type.validate(value);
127         return (D) value;
128     }
129
130     /**
131      * Verifies if this Column if of the specified type.
132      * @param typeClass the type to check for
133      */
134     public void validateType(Class<?> typeClass) {
135
136     }
137
138     public D valueFromJson(JsonNode value) {
139         return (D) this.getType().valueFromJson(value);
140     }
141
142     public Object getNormalizeData(D value) {
143         Object untypedValue;
144         if (value instanceof Set) {
145             untypedValue = OvsdbSet.fromSet((Set) value);
146         } else if (value instanceof Map) {
147             untypedValue = OvsdbMap.fromMap((Map) value);
148         } else {
149             untypedValue = value;
150         }
151         return untypedValue;
152     }
153 }