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 static final StringBaseType SINGLETON = new StringBaseType();
18 private int minLength = Integer.MIN_VALUE;
19 private int maxLength = Integer.MAX_VALUE;
20 private Set<String> enums;
23 void fillConstraints(final JsonNode type) {
24 JsonNode typeMaxNode = type.get("maxLength");
25 if (typeMaxNode != null) {
26 maxLength = typeMaxNode.asInt();
28 JsonNode typeMinNode = type.get("minLength");
29 if (typeMinNode != null) {
30 minLength = typeMinNode.asInt();
32 Optional<Set<String>> typeEnumsOpt = populateEnum(type);
33 if (typeEnumsOpt.isPresent()) {
34 enums = typeEnumsOpt.get();
39 public Object toValue(final JsonNode value) {
40 return value.asText();
44 public void validate(final Object value) {
48 private static Optional<Set<String>> populateEnum(final JsonNode node) {
49 if (node.has("enum")) {
50 Set<String> nodesEnums = new HashSet<>();
51 JsonNode enumVal = node.get("enum");
52 if (enumVal.isArray()) {
53 JsonNode anEnum = enumVal.get(1);
54 for (JsonNode enm : anEnum) {
55 nodesEnums.add(enm.asText());
57 } else if (enumVal.isTextual()) {
58 nodesEnums.add(enumVal.asText());
60 return Optional.of(nodesEnums);
62 return Optional.empty();
66 public int getMinLength() {
70 public int getMaxLength() {
74 public Set<String> getEnums() {
79 public String toString() {
80 return "StringBaseType";
84 public int hashCode() {
87 result = prime * result + (enums == null ? 0 : enums.hashCode());
88 result = prime * result + maxLength;
89 result = prime * result + minLength;
94 public boolean equals(final Object obj) {
101 if (getClass() != obj.getClass()) {
104 StringBaseType other = (StringBaseType) obj;
106 if (other.enums != null) {
109 } else if (!enums.equals(other.enums)) {
112 if (maxLength != other.maxLength) {
115 if (minLength != other.minLength) {