blob: 18919e32f96f122260fe116590a67efd96efb30e [file] [log] [blame]
Sithara Punnasseryff114552017-01-10 11:40:55 -08001/*
Sithara Punnassery9306e6b2017-02-06 15:38:19 -08002 * Copyright 2017-present Open Networking Laboratory
Sithara Punnasseryff114552017-01-10 11:40:55 -08003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080016
Sithara Punnasseryff114552017-01-10 11:40:55 -080017package org.onosproject.config.model;
18
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080019import java.util.Objects;
Sithara Punnasseryff114552017-01-10 11:40:55 -080020
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080021import static com.google.common.base.MoreObjects.toStringHelper;
22import static com.google.common.base.Preconditions.checkNotNull;
23import static java.util.Objects.hash;
24import static org.onosproject.config.model.ModelConstants.INCOMPLETE_SCHEMA_INFO;
25
26/**
27 * Abstraction of an entity which identifies a node uniquely among its
28 * siblings.
29 */
30public class NodeKey<E extends NodeKey> implements Comparable<E>, Cloneable {
31
32 protected SchemaId schemaId;
33
34 /**
35 * Create object from builder.
36 *
37 * @param builder initialized builder
38 */
39 protected NodeKey(NodeKeyBuilder builder) {
40 schemaId = builder.schemaId;
41 }
42
43 /**
44 * Returns node key builder.
45 *
46 * @return node key builder
47 */
48 public static NodeKeyBuilder builder() {
49 return new NodeKeyBuilder();
50 }
51
52 /**
53 * Returns the schema identifier as minimal key required to identify a
54 * branching node.
55 *
56 * @return schema identifier of a key
57 */
58 public SchemaId schemaId() {
59 return schemaId;
60 }
61
62 @Override
63 public int compareTo(NodeKey o) {
64 checkNotNull(o);
65 return schemaId.compareTo(o.schemaId());
66 }
67
68 @Override
69 public int hashCode() {
70 return hash(schemaId);
71 }
72
73 @Override
74 public boolean equals(Object obj) {
75 if (obj == null) {
76 return false;
77 }
78
79 if (!getClass().equals(obj.getClass())) {
80 return false;
81 }
82
83 NodeKey that = (NodeKey) obj;
84 return Objects.equals(schemaId, that.schemaId);
85 }
86
87 @Override
88 public String toString() {
89 return toStringHelper(getClass())
90 .add("schemaId", schemaId)
91 .toString();
92 }
93
94 /**
95 * Creates and returns a deep copy of this object.
96 *
97 * @return cloned copy
98 * @throws CloneNotSupportedException if the object's class does not
99 * support the {@code Cloneable} interface
100 */
101 public NodeKey clone() throws CloneNotSupportedException {
102 NodeKey clonedKey = (NodeKey) super.clone();
103 clonedKey.schemaId = schemaId.clone();
104 return clonedKey;
105 }
106
107 /**
108 * Builder for node key.
109 *
110 * @param <B> node key type
111 */
112 public static class NodeKeyBuilder<B extends NodeKeyBuilder<B>> {
113 private SchemaId schemaId;
114
115 /**
116 * Create the node key from scratch.
117 */
118 public NodeKeyBuilder() {
119 }
120
121 /**
122 * Support the derived object to inherit from existing node key builder.
123 *
124 * @param base existing node key builder
125 */
126 protected NodeKeyBuilder(NodeKeyBuilder base) {
127 checkNotNull(base.schemaId, INCOMPLETE_SCHEMA_INFO);
128 schemaId = base.schemaId;
129 }
130
131 /**
132 * set the schema identifier.
133 *
134 * @param schema schema identifier
135 * @return current builder
136 */
137 public B schemaId(SchemaId schema) {
138 schemaId = schema;
139 return (B) this;
140 }
141
142 /**
143 * set the schema identifier.
144 *
145 * @param name name of the node
146 * @param nameSpace name space of the node
147 * @return current builder
148 */
149 public B schemaId(String name, String nameSpace) {
150 schemaId = new SchemaId(name, nameSpace);
151 return (B) this;
152 }
153
154 /**
155 * construct the node key.
156 *
157 * @return node key
158 */
159 public NodeKey build() {
160 checkNotNull(schemaId.name(), INCOMPLETE_SCHEMA_INFO);
161 checkNotNull(schemaId.namespace(), INCOMPLETE_SCHEMA_INFO);
162 return new NodeKey(this);
163 }
Sithara Punnasseryff114552017-01-10 11:40:55 -0800164 }
165}
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800166