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
8 package org.opendaylight.ovsdb.lib.schema;
10 import com.fasterxml.jackson.databind.JsonNode;
11 import org.opendaylight.ovsdb.lib.notation.OvsdbMap;
13 final class KeyValuedColumnType extends ColumnType {
14 private final BaseType keyType;
16 private KeyValuedColumnType(final BaseType keyType, final BaseType valueType, final long min, final long max) {
17 super(valueType, min, max);
18 this.keyType = keyType;
22 * Creates a ColumnType from the JsonNode if the implementation knows how to, returns null otherwise.
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)
27 static KeyValuedColumnType fromJsonNode(final JsonNode json) {
28 if (json.isValueNode() || !json.has("value")) {
31 BaseType jsonKeyType = BaseType.fromJson(json, "key");
32 BaseType valueType = BaseType.fromJson(json, "value");
34 return new KeyValuedColumnType(jsonKeyType, valueType, minFromJson(json), maxFromJson(json));
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));
50 } else if (node.size() == 0) {
58 public String toString() {
59 return "KeyValuedColumnType [keyType=" + keyType + " " + super.toString() + "]";
63 public int hashCode() {
65 int result = super.hashCode();
66 result = prime * result
67 + (keyType == null ? 0 : keyType.hashCode());
72 public boolean equals(final Object obj) {
76 if (!super.equals(obj)) {
79 if (getClass() != obj.getClass()) {
82 KeyValuedColumnType other = (KeyValuedColumnType) obj;
83 if (keyType == null) {
84 if (other.keyType != null) {
87 } else if (!keyType.equals(other.keyType)) {