Bug 5019: Add QName param to NormalizedNodeWriter#leafSetEntryNode
[yangtools.git] / yang / yang-data-api / src / main / java / org / opendaylight / yangtools / yang / data / api / schema / stream / DataSchemaNodeAwareAdaptor.java
1 /*
2  * Copyright (c) 2015 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.api.schema.stream;
9
10 import com.google.common.annotations.Beta;
11 import com.google.common.base.Preconditions;
12 import javax.annotation.Nonnull;
13 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
14
15 /**
16  * Utility class for adapting {@link NormalizedNodeStreamWriter}s to their {@link DataSchemaNodeAware} counterparts
17  * which ignore the provided schema node.
18  */
19 @Beta
20 public final class DataSchemaNodeAwareAdaptor {
21     private DataSchemaNodeAwareAdaptor() {
22         throw new UnsupportedOperationException();
23     }
24
25     public static SchemaAwareNormalizedNodeStreamAttributeWriter forAttributeWriter(
26             @Nonnull final NormalizedNodeStreamAttributeWriter writer) {
27         Preconditions.checkNotNull(writer, "Writer must not be null");
28
29         if (writer instanceof SchemaAwareNormalizedNodeStreamAttributeWriter) {
30             return (SchemaAwareNormalizedNodeStreamAttributeWriter) writer;
31         }
32
33         return new AttributeWriter() {
34             @Override
35             protected NormalizedNodeStreamAttributeWriter delegate() {
36                 return writer;
37             }
38         };
39     }
40
41     public static SchemaAwareNormalizedNodeStreamWriter forWriter(@Nonnull final NormalizedNodeStreamWriter writer) {
42         Preconditions.checkNotNull(writer, "Writer must not be null");
43
44         if (writer instanceof SchemaAwareNormalizedNodeStreamWriter) {
45             return (SchemaAwareNormalizedNodeStreamWriter) writer;
46         }
47         if (writer instanceof NormalizedNodeStreamAttributeWriter) {
48             return forAttributeWriter((NormalizedNodeStreamAttributeWriter) writer);
49         }
50
51         return new Writer() {
52             @Override
53             protected NormalizedNodeStreamWriter delegate() {
54                 return writer;
55             }
56         };
57     }
58
59     private static abstract class AttributeWriter extends ForwardingNormalizedNodeStreamAttributeWriter
60             implements SchemaAwareNormalizedNodeStreamAttributeWriter {
61         @Override
62         public void nextDataSchemaNode(final DataSchemaNode schema) {
63             // Intentional no-op
64         }
65     }
66
67     private static abstract class Writer extends ForwardingNormalizedNodeStreamWriter
68             implements SchemaAwareNormalizedNodeStreamWriter {
69         @Override
70         public void nextDataSchemaNode(final DataSchemaNode schema) {
71             // Intentional no-op
72         }
73     }
74 }