Use singleton BaseType instances for simple definitions
[ovsdb.git] / library / impl / src / main / java / org / opendaylight / ovsdb / lib / schema / BaseType.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 org.opendaylight.ovsdb.lib.error.TyperException;
12 import org.slf4j.Logger;
13
14 public abstract class BaseType<E extends BaseType<E>> {
15     private static final Logger LOG = org.slf4j.LoggerFactory.getLogger(BaseType.class);
16
17     BaseType() {
18         // Prevent external instantiation
19     }
20
21     public static BaseType fromJson(final JsonNode json, final String keyorval) {
22         if (json.isValueNode()) {
23             return singletonFor(json.asText().trim());
24         }
25
26         final JsonNode type = json.get(keyorval);
27         if (type == null) {
28             throw new TyperException("Not a type");
29         }
30         if (type.isTextual()) {
31             //json like  "string"
32             return singletonFor(type.asText());
33         }
34         if (type.isObject()) {
35             //json like  {"type" : "string", "enum": ["set", ["access", "native-tagged"]]}" for key or value
36             final JsonNode nestedType = type.get("type");
37             if (nestedType != null) {
38                 final BaseType ret = builderFor(nestedType.asText());
39                 if (ret != null) {
40                     ret.fillConstraints(type);
41                     return ret;
42                 }
43             }
44         }
45
46         return null;
47     }
48
49     abstract void fillConstraints(JsonNode type);
50
51     public abstract Object toValue(JsonNode value);
52
53     public abstract void validate(Object value);
54
55     // Find a simple singleton instance
56     private static BaseType singletonFor(final String type) {
57         switch (type) {
58             case "boolean":
59                 return BooleanBaseType.SINGLETON;
60             case "integer":
61                 return IntegerBaseType.SINGLETON;
62             case "real":
63                 return RealBaseType.SINGLETON;
64             case "string":
65                 return StringBaseType.SINGLETON;
66             case "uuid":
67                 return UuidBaseType.SINGLETON;
68             default:
69                 LOG.debug("Unknown base type {}", type);
70                 return null;
71         }
72     }
73
74     // Create a new instance for customization
75     private static BaseType builderFor(final String type) {
76         switch (type) {
77             case "boolean":
78                 return new BooleanBaseType();
79             case "integer":
80                 return new IntegerBaseType();
81             case "real":
82                 return new RealBaseType();
83             case "string":
84                 return new StringBaseType();
85             case "uuid":
86                 return new UuidBaseType();
87             default:
88                 LOG.debug("Unknown base type {}", type);
89                 return null;
90         }
91     }
92
93 }