Use singleton BaseType instances for simple definitions
[ovsdb.git] / library / impl / src / main / java / org / opendaylight / ovsdb / lib / schema / UuidBaseType.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.notation.ReferencedRow;
12 import org.opendaylight.ovsdb.lib.notation.UUID;
13
14 final class UuidBaseType extends BaseType<UuidBaseType> {
15     // These enum types correspond to JSON values and need to be in lower-case currently
16     public enum RefType { strong, weak }
17
18     static final UuidBaseType SINGLETON = new UuidBaseType();
19
20     private String refTable;
21     private UuidBaseType.RefType refType;
22
23     @Override
24     void fillConstraints(final JsonNode node) {
25         JsonNode refTableNode = node.get("refTable");
26         refTable = refTableNode != null ? refTableNode.asText() : null;
27
28         JsonNode refTypeJson = node.get("refType");
29         refType = refTypeJson != null ? RefType.valueOf(refTypeJson.asText()) : RefType.strong;
30     }
31
32     @Override
33     public Object toValue(final JsonNode value) {
34         if (value.isArray()) {
35             if (value.size() == 2 && value.get(0).isTextual() && "uuid".equals(value.get(0).asText())) {
36                 return new UUID(value.get(1).asText());
37             }
38         } else {
39             /*
40              * UUIDBaseType used by RefTable from SouthBound will always be an Array of ["uuid", <uuid>].
41              * But there are some cases from northbound where the RefTable type can be expanded to a Row
42              * with contents. In those scenarios, just retain the content and return a ReferencedRow for
43              * the upper layer functions to process it.
44              */
45             return new ReferencedRow(refTable, value);
46         }
47         return null;
48     }
49
50     @Override
51     public void validate(final Object value) {
52
53     }
54
55     public String getRefTable() {
56         return refTable;
57     }
58
59     public UuidBaseType.RefType getRefType() {
60         return refType;
61     }
62
63     @Override
64     public String toString() {
65         return "UuidBaseType";
66     }
67
68     @Override
69     public int hashCode() {
70         final int prime = 31;
71         int result = 1;
72         result = prime * result
73                 + (refTable == null ? 0 : refTable.hashCode());
74         result = prime * result
75                 + (refType == null ? 0 : refType.hashCode());
76         return result;
77     }
78
79     @Override
80     public boolean equals(final Object obj) {
81         if (this == obj) {
82             return true;
83         }
84         if (obj == null) {
85             return false;
86         }
87         if (getClass() != obj.getClass()) {
88             return false;
89         }
90         UuidBaseType other = (UuidBaseType) obj;
91         if (refTable == null) {
92             if (other.refTable != null) {
93                 return false;
94             }
95         } else if (!refTable.equals(other.refTable)) {
96             return false;
97         }
98         if (refType != other.refType) {
99             return false;
100         }
101         return true;
102     }
103 }