fd4b52d53ab27021ea5f3d7b820bb97b5c1ecd27
[smo/teiv.git] /
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2024 Ericsson
4  *  Modifications Copyright (C) 2024 OpenInfra Foundation Europe
5  *  ================================================================================
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21 package org.oran.smo.yangtools.parser.data.instance;
22
23 import java.util.Objects;
24
25 import org.oran.smo.yangtools.parser.data.dom.YangDataDomNode;
26 import org.oran.smo.yangtools.parser.model.statements.AbstractStatement;
27
28 /**
29  * Represents something that carries data, e.g. a leaf, a leaf-list instance.
30  *
31  * @author Mark Hollmann
32  */
33 public abstract class AbstractContentInstance extends AbstractDataInstance {
34
35     private final Object value;
36
37     private final String cachedToString;
38
39     /**
40      * Constructor for a content instance that carries data.
41      */
42     public AbstractContentInstance(final AbstractStatement schemaNode, final YangDataDomNode dataDomNode,
43             final AbstractStructureInstance parent, final Object value) {
44         super(schemaNode, dataDomNode, parent);
45
46         this.value = Objects.requireNonNull(value);
47         this.cachedToString = "(" + getNamespace() + "):" + getName() + "=" + Objects.toString(value);
48     }
49
50     /**
51      * Constructor for a content instance that carries a default value.
52      */
53     public AbstractContentInstance(final AbstractStatement schemaNode, final AbstractStructureInstance parent,
54             final Object value) {
55         super(schemaNode, parent);
56
57         this.value = value;
58         this.cachedToString = "(" + getNamespace() + "):" + getName() + "=" + Objects.toString(value);
59     }
60
61     /**
62      * The type of the returned value depends on how the value was originally encoded. If it
63      * was encoded in XML, the value will be of type String. If it was encoded in JSON, it
64      * will be of type String, Boolean or Double.
65      */
66     public Object getValue() {
67         return value;
68     }
69
70     @Override
71     public String toString() {
72         return cachedToString;
73     }
74 }