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