Sithara Punnassery | ff11455 | 2017-01-10 11:40:55 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-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 | package org.onosproject.config.model; |
| 17 | |
| 18 | import java.util.LinkedHashMap; |
| 19 | |
| 20 | /** |
| 21 | * Representation of an instance node in the Dynamic config store. |
| 22 | */ |
| 23 | public final class DefaultDataNode implements DataNode { |
| 24 | DataNode.Type type; |
| 25 | NodeKey key; |
| 26 | //Object value; |
| 27 | String value; |
| 28 | LinkedHashMap<NodeKey, DataNode> children; |
| 29 | |
| 30 | /** |
| 31 | * Creates a new DefaultDataNode. |
| 32 | * |
| 33 | * @param key node key |
| 34 | * @param type of the node |
| 35 | * @param value of leaf node |
| 36 | * @param children of the inner node |
| 37 | */ |
| 38 | private DefaultDataNode(NodeKey key, DataNode.Type type, |
| 39 | String value, LinkedHashMap<NodeKey, DataNode> children) { |
| 40 | this.type = type; |
| 41 | this.key = key; |
| 42 | this.value = value; |
| 43 | this.children = children; |
| 44 | } |
| 45 | /** |
| 46 | * |
| 47 | */ |
| 48 | /** |
| 49 | * Creates a new DefaultDataNode. |
| 50 | * |
| 51 | * @param node to be cloned |
| 52 | * @param value of leaf node |
| 53 | */ |
| 54 | private DefaultDataNode(DataNode node, String value) { |
| 55 | this.type = node.type(); |
| 56 | this.key = node.key(); |
| 57 | this.value = value; |
| 58 | this.children = null; |
| 59 | } |
| 60 | /** |
| 61 | * Creates a new DefaultDataNode. |
| 62 | * |
| 63 | * @param node to be cloned |
| 64 | * @param children to be added |
| 65 | */ |
| 66 | private DefaultDataNode(DataNode node, LinkedHashMap<NodeKey, DataNode> children) { |
| 67 | this.type = node.type(); |
| 68 | this.key = node.key(); |
| 69 | this.value = null; |
| 70 | this.children = children; |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public LinkedHashMap<NodeKey, DataNode> children() { |
| 75 | return this.children; |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public String value() { |
| 80 | return value; |
| 81 | //return value.toString(); |
| 82 | } |
| 83 | |
| 84 | |
| 85 | /** |
| 86 | * Creates and returns a new builder instance. |
| 87 | * |
| 88 | * @return new builder |
| 89 | */ |
| 90 | public static Builder builder() { |
| 91 | return new Builder(); |
| 92 | } |
| 93 | |
| 94 | public static final class Builder<V> implements DataNode.Builder { |
| 95 | |
| 96 | private DataNode.Type type; |
| 97 | private NodeKey key; |
| 98 | //Object value; |
| 99 | private String value; |
| 100 | private LinkedHashMap<NodeKey, DataNode> children; |
| 101 | |
| 102 | private Builder() { |
| 103 | this.type = null; |
| 104 | this.key = null; |
| 105 | this.value = null; |
| 106 | this.children = null; |
| 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public Builder addBaseObj(DataNode base) { |
| 111 | this.key = base.key(); |
| 112 | this.type = base.type(); |
| 113 | this.value = base.value(); |
| 114 | this.children = base.children(); |
| 115 | return this; |
| 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public Builder addKey(NodeKey key) { |
| 120 | this.key = key; |
| 121 | return this; |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public Builder addType(DataNode.Type type) { |
| 126 | this.type = type; |
| 127 | return this; |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public Builder addValue(String value) { |
| 132 | this.value = value; |
| 133 | return this; |
| 134 | } |
| 135 | |
| 136 | //@Override |
| 137 | public Builder addChildren(LinkedHashMap<NodeKey, DataNode> children) { |
| 138 | this.children = children; |
| 139 | return this; |
| 140 | } |
| 141 | |
| 142 | @Override |
| 143 | public DataNode build() { |
| 144 | return new DefaultDataNode(this.key, this.type, this.value, this.children); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | |
| 149 | @Override |
| 150 | public SchemaIdentifier identifier() { |
| 151 | return this.key.schemaId; |
| 152 | } |
| 153 | |
| 154 | @Override |
| 155 | public Type type() { |
| 156 | return this.type; |
| 157 | } |
| 158 | |
| 159 | @Override |
| 160 | public NodeKey key() { |
| 161 | return this.key; |
| 162 | } |
| 163 | } |