Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 1 | /* |
| 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 | package org.onosproject.config.model; |
| 17 | |
| 18 | import java.util.Objects; |
| 19 | |
| 20 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 21 | import static java.util.Objects.hash; |
| 22 | |
| 23 | /** |
| 24 | * Represents the List's key leaf value. |
| 25 | */ |
| 26 | public class KeyLeaf implements Cloneable { |
| 27 | |
| 28 | private SchemaId leafSchema; |
| 29 | private Object leafVal; |
| 30 | |
| 31 | private KeyLeaf() { |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Constructs a key leaf with all the identifier and value initialized. |
| 36 | * |
| 37 | * @param name name of the leaf |
| 38 | * @param nameSpace namespace of leaf |
| 39 | * @param leafVal value of leaf |
| 40 | */ |
| 41 | public KeyLeaf(String name, String nameSpace, Object leafVal) { |
| 42 | leafSchema = new SchemaId(name, nameSpace); |
| 43 | this.leafVal = leafVal; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Creates and returns a deep copy of this object. |
| 48 | * |
| 49 | * @return cloned copy |
| 50 | * @throws CloneNotSupportedException if the object's class does not |
| 51 | * support the {@code Cloneable} interface |
| 52 | */ |
| 53 | public KeyLeaf clone() throws CloneNotSupportedException { |
| 54 | KeyLeaf clonedLeaf = (KeyLeaf) super.clone(); |
| 55 | clonedLeaf.leafSchema = leafSchema.clone(); |
| 56 | return clonedLeaf; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Returns the node schema schemaId. |
| 61 | * |
| 62 | * @return node schema schemaId |
| 63 | */ |
| 64 | public SchemaId leafSchema() { |
| 65 | return leafSchema; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Returns value contained in leaf node. |
| 70 | * |
| 71 | * @return value contained in leaf node |
| 72 | */ |
| 73 | public Object leafValue() { |
| 74 | return leafVal; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Returns value as string, for usage in serializers. |
| 79 | * |
| 80 | * @return string representation of value |
| 81 | */ |
| 82 | public String leafValAsString() { |
| 83 | return leafVal.toString(); |
| 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public int hashCode() { |
| 88 | return hash(leafSchema, leafVal); |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public boolean equals(Object obj) { |
| 93 | if (obj == null) { |
| 94 | return false; |
| 95 | } |
| 96 | |
| 97 | KeyLeaf that = (KeyLeaf) obj; |
| 98 | return Objects.equals(leafSchema, that.leafSchema) && |
| 99 | Objects.equals(leafVal, that.leafVal); |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public String toString() { |
| 104 | return toStringHelper(getClass()) |
| 105 | .add("schemaId", leafSchema) |
| 106 | .add("leafValue", leafVal) |
| 107 | .toString(); |
| 108 | } |
| 109 | } |