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 com.google.common.collect.ImmutableList;
12 import java.util.function.Function;
13 import org.opendaylight.ovsdb.lib.error.TyperException;
14 import org.opendaylight.ovsdb.lib.jsonrpc.JsonUtils;
16 public abstract class ColumnType {
17 private static final ImmutableList<Function<JsonNode, ColumnType>> FACTORIES = ImmutableList.of(
18 AtomicColumnType::fromJsonNode, KeyValuedColumnType::fromJsonNode);
20 private final BaseType baseType;
21 private final long min;
22 private final long max;
24 ColumnType(final BaseType baseType, final long min, final long max) {
25 this.baseType = baseType;
35 "maxInteger": 4294967295,
47 public static ColumnType fromJson(final JsonNode json) {
48 for (Function<JsonNode, ColumnType> factory : FACTORIES) {
49 ColumnType columnType = factory.apply(json);
50 if (null != columnType) {
54 //todo move to speicfic typed exception
55 throw new TyperException(String.format("could not find the right column type %s",
56 JsonUtils.prettyString(json)));
59 public BaseType getBaseType() {
63 public long getMin() {
67 public long getMax() {
72 * Per RFC 7047, Section 3.2 <type> :
73 * If "min" or "max" is not specified, each defaults to 1. If "max" is specified as "unlimited",
74 * then there is no specified maximum number of elements, although the implementation will
75 * enforce some limit. After considering defaults, "min" must be exactly 0 or
76 * exactly 1, "max" must be at least 1, and "max" must be greater than or equal to "min".
78 * If "min" and "max" are both 1 and "value" is not specified, the
79 * type is the scalar type specified by "key".
81 public boolean isMultiValued() {
82 return this.min != this.max;
85 public abstract Object valueFromJson(JsonNode value);
87 public void validate(final Object value) {
88 baseType.validate(value);
92 public String toString() {
94 + "baseType=" + baseType
101 public int hashCode() {
102 final int prime = 31;
104 result = prime * result
105 + (baseType == null ? 0 : baseType.hashCode());
106 result = prime * result + (int) (max ^ max >>> 32);
107 result = prime * result + (int) (min ^ min >>> 32);
112 public boolean equals(final Object obj) {
119 if (getClass() != obj.getClass()) {
122 ColumnType other = (ColumnType) obj;
123 if (baseType == null) {
124 if (other.baseType != null) {
127 } else if (!baseType.equals(other.baseType)) {
130 if (max != other.max) {
133 if (min != other.min) {
139 static long maxFromJson(final JsonNode json) {
140 final JsonNode maxNode = json.get("max");
141 if (maxNode != null) {
142 if (maxNode.isLong()) {
143 return maxNode.asLong();
145 if (maxNode.isTextual() && "unlimited".equals(maxNode.asText())) {
146 return Long.MAX_VALUE;
152 static long minFromJson(final JsonNode json) {
153 final JsonNode minNode = json.get("min");
154 return minNode == null ? 1 : minNode.asLong();