Cleanup use of Guava library
[yangtools.git] / yang / yang-data-util / src / main / java / org / opendaylight / yangtools / yang / data / util / ListEntryNodeDataWithSchema.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. 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.checkState;
11
12 import java.io.IOException;
13 import java.util.Collection;
14 import java.util.HashMap;
15 import java.util.LinkedHashMap;
16 import java.util.Map;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
19 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamAttributeWriter;
20 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
21 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
24
25 /**
26  * Utility class used for tracking parser state as needed by a StAX-like parser.
27  * This class is to be used only by respective XML and JSON parsers in yang-data-codec-xml and yang-data-codec-gson.
28  *
29  * <p>
30  * Represents a YANG list entry node.
31  */
32 public class ListEntryNodeDataWithSchema extends CompositeNodeDataWithSchema {
33
34     private final Map<QName, SimpleNodeDataWithSchema> qnameToKeys = new HashMap<>();
35
36     public ListEntryNodeDataWithSchema(final DataSchemaNode schema) {
37         super(schema);
38     }
39
40     @Override
41     public void addChild(final AbstractNodeDataWithSchema newChild) {
42         final DataSchemaNode childSchema = newChild.getSchema();
43         if (childSchema instanceof LeafSchemaNode && isPartOfKey((LeafSchemaNode) childSchema)) {
44             qnameToKeys.put(childSchema.getQName(), (SimpleNodeDataWithSchema)newChild);
45         }
46         super.addChild(newChild);
47     }
48
49     private boolean isPartOfKey(final LeafSchemaNode potentialKey) {
50         for (QName qname : ((ListSchemaNode) getSchema()).getKeyDefinition()) {
51             if (qname.equals(potentialKey.getQName())) {
52                 return true;
53             }
54         }
55         return false;
56     }
57
58     @Override
59     public void write(final NormalizedNodeStreamWriter writer) throws IOException {
60         final Collection<QName> keyDef = ((ListSchemaNode) getSchema()).getKeyDefinition();
61         if (keyDef.isEmpty()) {
62             writer.nextDataSchemaNode(getSchema());
63             writer.startUnkeyedListItem(provideNodeIdentifier(), childSizeHint());
64             super.write(writer);
65             writer.endNode();
66             return;
67         }
68
69         checkState(keyDef.size() == qnameToKeys.size(), "Input is missing some of the keys of %s",
70                 getSchema().getQName());
71
72         // Need to restore schema order...
73         final Map<QName, Object> predicates = new LinkedHashMap<>();
74         for (QName qname : keyDef) {
75             predicates.put(qname, qnameToKeys.get(qname).getValue());
76         }
77
78         writer.nextDataSchemaNode(getSchema());
79
80         if (writer instanceof NormalizedNodeStreamAttributeWriter && getAttributes() != null) {
81             ((NormalizedNodeStreamAttributeWriter) writer).startMapEntryNode(
82                     new NodeIdentifierWithPredicates(getSchema().getQName(), predicates), childSizeHint(),
83                     getAttributes());
84         } else {
85             writer.startMapEntryNode(new NodeIdentifierWithPredicates(getSchema().getQName(), predicates),
86                     childSizeHint());
87         }
88
89         super.write(writer);
90         writer.endNode();
91     }
92 }