blob: 617efafb69aff9d3aadc0e50531ec770c26d102a [file] [log] [blame]
Sithara Punnassery9306e6b2017-02-06 15:38:19 -08001/*
2 * Copyright 2017-present Open Networking Laboratory
3 *
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 */
16
17package org.onosproject.config.model;
18
19import java.util.LinkedList;
20import java.util.List;
21import java.util.Objects;
22
23import static com.google.common.base.MoreObjects.toStringHelper;
24
25/**
26 * Represents an entity which identifies a unique branching node
27 * corresponding to a multi instance schema definition.
28 */
29public final class ListKey extends NodeKey<ListKey> implements Comparable<ListKey> {
30
31 private List<KeyLeaf> keyLeafs;
32
33 /**
34 * Create object from builder.
35 *
36 * @param builder initialized builder
37 */
38 private ListKey(ListKeyBuilder builder) {
39 super(builder);
40 keyLeafs = builder.keyLeafs;
41 }
42
43 /**
44 * Returns the list of key leaf nodes of a multi instance node, which
45 * uniquely identifies the branching node entry corresponding to a multi
46 * instance schema definition.
47 *
48 * @return List of key leaf nodes
49 */
50 List<KeyLeaf> keyLeafs() {
51 return keyLeafs;
52 }
53
54 /**
55 * Creates and returns a deep copy of this object.
56 *
57 * @return cloned copy
58 * @throws CloneNotSupportedException if the object's class does not
59 * support the {@code Cloneable} interface
60 */
61 public ListKey clone() throws CloneNotSupportedException {
62 ListKey clonedListKey = (ListKey) super.clone();
63 List<KeyLeaf> clonedKeyLeafs = new LinkedList<>();
64 for (KeyLeaf leaf : keyLeafs) {
65 clonedKeyLeafs.add(leaf.clone());
66 }
67 clonedListKey.keyLeafs = clonedKeyLeafs;
68 return clonedListKey;
69 }
70
71 public int compareTo(ListKey o) {
72 //TODO: implement me
73 return 0;
74 }
75
76 @Override
77 public int hashCode() {
78 return Objects.hash(schemaId, keyLeafs);
79 }
80
81 @Override
82 public boolean equals(Object obj) {
83 if (obj == null) {
84 return false;
85 }
86
87 if (!getClass().equals(obj.getClass())) {
88 return false;
89 }
90
91 ListKey that = (ListKey) obj;
92 List<KeyLeaf> thatList = that.keyLeafs;
93 return keyLeafs.size() == thatList.size() &&
94 keyLeafs.containsAll(thatList) &&
95 Objects.equals(schemaId, that.schemaId);
96 }
97
98 @Override
99 public String toString() {
100 return toStringHelper(getClass())
101 .add("value", keyLeafs)
102 .toString();
103 }
104
105 /**
106 * Represents list key builder.
107 */
108 public static class ListKeyBuilder extends NodeKeyBuilder<ListKeyBuilder> {
109 private List<KeyLeaf> keyLeafs = new LinkedList<>();
110
111 /**
112 * used to construct the key from scratch.
113 */
114 public ListKeyBuilder() {
115 }
116
117 /**
118 * used to construct a key from an existing node key.
119 *
120 * @param base existing node key
121 */
122 public ListKeyBuilder(NodeKeyBuilder base) {
123 super(base);
124 }
125
126 /**
127 * Adds the key leaf for the list resource.
128 *
129 * @param name key leaf name
130 * @param nameSpace key laef namespace
131 * @param val value of key
132 */
133 void addKeyLeaf(String name, String nameSpace, Object val) {
134 KeyLeaf keyLeaf = new KeyLeaf(name, nameSpace, val);
135 keyLeafs.add(keyLeaf);
136 }
137
138 /**
139 * Creates the list key object.
140 *
141 * @return list key
142 */
143 public ListKey build() {
144 return new ListKey(this);
145 }
146 }
147}