Rename Any{Data,Xml}SchemaNode to Any{data,xml}SchemaNode
[yangtools.git] / yang / yang-data-util / src / main / java / org / opendaylight / yangtools / yang / data / util / AnydataNodeDataWithSchema.java
1 /*
2  * Copyright (c) 2019 PANTHEON.tech, s.r.o. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.yang.data.util;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static com.google.common.base.Preconditions.checkState;
12 import static java.util.Objects.requireNonNull;
13
14 import com.google.common.annotations.Beta;
15 import java.io.IOException;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.opendaylight.yangtools.rfc7952.data.api.NormalizedMetadataStreamWriter;
18 import org.opendaylight.yangtools.yang.data.api.schema.stream.AnydataExtension;
19 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
20 import org.opendaylight.yangtools.yang.model.api.AnydataSchemaNode;
21
22 @Beta
23 public class AnydataNodeDataWithSchema extends SimpleNodeDataWithSchema<AnydataSchemaNode> {
24     private Class<?> objectModel;
25
26     public AnydataNodeDataWithSchema(final AnydataSchemaNode dataSchemaNode) {
27         super(dataSchemaNode);
28     }
29
30     public AnydataNodeDataWithSchema(final AnydataSchemaNode dataSchemaNode, final Class<?> objectModel) {
31         super(dataSchemaNode);
32         this.objectModel = requireNonNull(objectModel);
33     }
34
35     @Override
36     public Object getValue() {
37         return getObjectModel().cast(super.getValue());
38     }
39
40     @Override
41     public void setValue(final Object value) {
42         final Class<?> clazz = getObjectModel();
43         checkArgument(clazz.isInstance(value), "Value %s is not compatible with %s", clazz);
44         super.setValue(value);
45     }
46
47     @Override
48     protected void write(final NormalizedNodeStreamWriter writer, final NormalizedMetadataStreamWriter metaWriter)
49             throws IOException {
50         final AnydataExtension ext = writer.getExtensions().getInstance(AnydataExtension.class);
51         if (ext != null) {
52             writer.nextDataSchemaNode(getSchema());
53             if (ext.startAnydataNode(provideNodeIdentifier(), getObjectModel())) {
54                 writer.scalarValue(getValue());
55                 writer.endNode();
56             }
57         }
58     }
59
60     public final @NonNull Class<?> getObjectModel() {
61         checkState(objectModel != null, "Object model not set");
62         return objectModel;
63     }
64
65     public void setObjectModel(final Class<?> newObjectModel) {
66         checkState(objectModel == null, "Object model already set to %s", objectModel);
67         objectModel = requireNonNull(newObjectModel);
68     }
69 }