Refactor ColumnType
[ovsdb.git] / library / impl / src / main / java / org / opendaylight / ovsdb / lib / schema / KeyValuedColumnType.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 org.opendaylight.ovsdb.lib.notation.OvsdbMap;
12
13 final class KeyValuedColumnType extends ColumnType {
14     private final BaseType keyType;
15
16     private KeyValuedColumnType(final BaseType keyType, final BaseType valueType, final long min, final long max) {
17         super(valueType, min, max);
18         this.keyType = keyType;
19     }
20
21     /**
22      * Creates a ColumnType from the JsonNode if the implementation knows how to, returns null otherwise.
23      *
24      * @param json the JSONNode object that needs to converted
25      * @return a valid SubType or Null (if the JsonNode does not represent the subtype)
26      */
27     static KeyValuedColumnType fromJsonNode(final JsonNode json) {
28         if (json.isValueNode() || !json.has("value")) {
29             return null;
30         }
31         BaseType jsonKeyType = BaseType.fromJson(json, "key");
32         BaseType valueType = BaseType.fromJson(json, "value");
33
34         return new KeyValuedColumnType(jsonKeyType, valueType, minFromJson(json), maxFromJson(json));
35     }
36
37     @Override
38     public Object valueFromJson(final JsonNode node) {
39         if (node.isArray() && node.size() == 2) {
40             if (node.get(0).isTextual() && "map".equals(node.get(0).asText())) {
41                 OvsdbMap<Object, Object> map = new OvsdbMap<>();
42                 for (JsonNode pairNode : node.get(1)) {
43                     if (pairNode.isArray() && node.size() == 2) {
44                         Object key = keyType.toValue(pairNode.get(0));
45                         Object value = getBaseType().toValue(pairNode.get(1));
46                         map.put(key, value);
47                     }
48                 }
49                 return map;
50             } else if (node.size() == 0) {
51                 return null;
52             }
53         }
54         return null;
55     }
56
57     @Override
58     public String toString() {
59         return "KeyValuedColumnType [keyType=" + keyType + " " + super.toString() + "]";
60     }
61
62     @Override
63     public int hashCode() {
64         final int prime = 31;
65         int result = super.hashCode();
66         result = prime * result
67                 + (keyType == null ? 0 : keyType.hashCode());
68         return result;
69     }
70
71     @Override
72     public boolean equals(final Object obj) {
73         if (this == obj) {
74             return true;
75         }
76         if (!super.equals(obj)) {
77             return false;
78         }
79         if (getClass() != obj.getClass()) {
80             return false;
81         }
82         KeyValuedColumnType other = (KeyValuedColumnType) obj;
83         if (keyType == null) {
84             if (other.keyType != null) {
85                 return false;
86             }
87         } else if (!keyType.equals(other.keyType)) {
88             return false;
89         }
90         return true;
91     }
92 }