7e1e77dfffa9baaed51b2e307cd21f4360c44edf
[ovsdb.git] / library / impl / src / main / java / org / opendaylight / ovsdb / lib / schema / StringBaseType.java
1 /*
2  * Copyright © 2014, 2017 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 java.util.HashSet;
12 import java.util.Optional;
13 import java.util.Set;
14
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;
19
20     @Override
21     void fillConstraints(final JsonNode type) {
22         JsonNode typeMaxNode = type.get("maxLength");
23         if (typeMaxNode != null) {
24             setMaxLength(typeMaxNode.asInt());
25         }
26         JsonNode typeMinNode = type.get("minLength");
27         if (typeMinNode != null) {
28             setMinLength(typeMinNode.asInt());
29         }
30         Optional<Set<String>> typeEnumsOpt = populateEnum(type);
31         if (typeEnumsOpt.isPresent()) {
32             setEnums(typeEnumsOpt.get());
33         }
34     }
35
36     @Override
37     public Object toValue(final JsonNode value) {
38         return value.asText();
39     }
40
41     @Override
42     public void validate(final Object value) {
43
44     }
45
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());
54                 }
55             } else if (enumVal.isTextual()) {
56                 nodesEnums.add(enumVal.asText());
57             }
58             return Optional.of(nodesEnums);
59         } else {
60             return Optional.empty();
61         }
62     }
63
64     public int getMinLength() {
65         return minLength;
66     }
67
68     public void setMinLength(final int minLength) {
69         this.minLength = minLength;
70     }
71
72     public int getMaxLength() {
73         return maxLength;
74     }
75
76     public void setMaxLength(final int maxLength) {
77         this.maxLength = maxLength;
78     }
79
80     public Set<String> getEnums() {
81         return enums;
82     }
83
84     public void setEnums(final Set<String> enums) {
85         this.enums = enums;
86     }
87
88     @Override
89     public String toString() {
90         return "StringBaseType";
91     }
92
93     @Override
94     public int hashCode() {
95         final int prime = 31;
96         int result = 1;
97         result = prime * result + (enums == null ? 0 : enums.hashCode());
98         result = prime * result + maxLength;
99         result = prime * result + minLength;
100         return result;
101     }
102
103     @Override
104     public boolean equals(final Object obj) {
105         if (this == obj) {
106             return true;
107         }
108         if (obj == null) {
109             return false;
110         }
111         if (getClass() != obj.getClass()) {
112             return false;
113         }
114         StringBaseType other = (StringBaseType) obj;
115         if (enums == null) {
116             if (other.enums != null) {
117                 return false;
118             }
119         } else if (!enums.equals(other.enums)) {
120             return false;
121         }
122         if (maxLength != other.maxLength) {
123             return false;
124         }
125         if (minLength != other.minLength) {
126             return false;
127         }
128         return true;
129     }
130 }