2 * Copyright © 2014, 2017 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 java.util.HashSet;
12 import java.util.Optional;
15 final class StringBaseType extends BaseType<StringBaseType> {
16 private int minLength = Integer.MIN_VALUE;
17 private int maxLength = Integer.MAX_VALUE;
18 private Set<String> enums;
21 void fillConstraints(final JsonNode type) {
22 JsonNode typeMaxNode = type.get("maxLength");
23 if (typeMaxNode != null) {
24 setMaxLength(typeMaxNode.asInt());
26 JsonNode typeMinNode = type.get("minLength");
27 if (typeMinNode != null) {
28 setMinLength(typeMinNode.asInt());
30 Optional<Set<String>> typeEnumsOpt = populateEnum(type);
31 if (typeEnumsOpt.isPresent()) {
32 setEnums(typeEnumsOpt.get());
37 public Object toValue(final JsonNode value) {
38 return value.asText();
42 public void validate(final Object value) {
46 private static Optional<Set<String>> populateEnum(final JsonNode node) {
47 if (node.has("enum")) {
48 Set<String> nodesEnums = new HashSet<>();
49 JsonNode enumVal = node.get("enum");
50 if (enumVal.isArray()) {
51 JsonNode anEnum = enumVal.get(1);
52 for (JsonNode enm : anEnum) {
53 nodesEnums.add(enm.asText());
55 } else if (enumVal.isTextual()) {
56 nodesEnums.add(enumVal.asText());
58 return Optional.of(nodesEnums);
60 return Optional.empty();
64 public int getMinLength() {
68 public void setMinLength(final int minLength) {
69 this.minLength = minLength;
72 public int getMaxLength() {
76 public void setMaxLength(final int maxLength) {
77 this.maxLength = maxLength;
80 public Set<String> getEnums() {
84 public void setEnums(final Set<String> enums) {
89 public String toString() {
90 return "StringBaseType";
94 public int hashCode() {
97 result = prime * result + (enums == null ? 0 : enums.hashCode());
98 result = prime * result + maxLength;
99 result = prime * result + minLength;
104 public boolean equals(final Object obj) {
111 if (getClass() != obj.getClass()) {
114 StringBaseType other = (StringBaseType) obj;
116 if (other.enums != null) {
119 } else if (!enums.equals(other.enums)) {
122 if (maxLength != other.maxLength) {
125 if (minLength != other.minLength) {