Use singleton BaseType instances for simple definitions
[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     static final StringBaseType SINGLETON = new StringBaseType();
17
18     private int minLength = Integer.MIN_VALUE;
19     private int maxLength = Integer.MAX_VALUE;
20     private Set<String> enums;
21
22     @Override
23     void fillConstraints(final JsonNode type) {
24         JsonNode typeMaxNode = type.get("maxLength");
25         if (typeMaxNode != null) {
26             maxLength = typeMaxNode.asInt();
27         }
28         JsonNode typeMinNode = type.get("minLength");
29         if (typeMinNode != null) {
30             minLength = typeMinNode.asInt();
31         }
32         Optional<Set<String>> typeEnumsOpt = populateEnum(type);
33         if (typeEnumsOpt.isPresent()) {
34             enums = typeEnumsOpt.get();
35         }
36     }
37
38     @Override
39     public Object toValue(final JsonNode value) {
40         return value.asText();
41     }
42
43     @Override
44     public void validate(final Object value) {
45
46     }
47
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());
56                 }
57             } else if (enumVal.isTextual()) {
58                 nodesEnums.add(enumVal.asText());
59             }
60             return Optional.of(nodesEnums);
61         } else {
62             return Optional.empty();
63         }
64     }
65
66     public int getMinLength() {
67         return minLength;
68     }
69
70     public int getMaxLength() {
71         return maxLength;
72     }
73
74     public Set<String> getEnums() {
75         return enums;
76     }
77
78     @Override
79     public String toString() {
80         return "StringBaseType";
81     }
82
83     @Override
84     public int hashCode() {
85         final int prime = 31;
86         int result = 1;
87         result = prime * result + (enums == null ? 0 : enums.hashCode());
88         result = prime * result + maxLength;
89         result = prime * result + minLength;
90         return result;
91     }
92
93     @Override
94     public boolean equals(final Object obj) {
95         if (this == obj) {
96             return true;
97         }
98         if (obj == null) {
99             return false;
100         }
101         if (getClass() != obj.getClass()) {
102             return false;
103         }
104         StringBaseType other = (StringBaseType) obj;
105         if (enums == null) {
106             if (other.enums != null) {
107                 return false;
108             }
109         } else if (!enums.equals(other.enums)) {
110             return false;
111         }
112         if (maxLength != other.maxLength) {
113             return false;
114         }
115         if (minLength != other.minLength) {
116             return false;
117         }
118         return true;
119     }
120 }