Remove explicit default super-constructor calls
[mdsal.git] / binding / mdsal-binding-dom-codec / src / main / java / org / opendaylight / mdsal / binding / dom / codec / impl / DataContainerCodecPrototype.java
1 /*
2  * Copyright (c) 2014 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.mdsal.binding.dom.codec.impl;
9
10 import com.google.common.collect.Iterables;
11 import javax.annotation.concurrent.GuardedBy;
12 import org.opendaylight.mdsal.binding.dom.codec.impl.NodeCodecContext.CodecContextFactory;
13 import org.opendaylight.yangtools.yang.binding.DataRoot;
14 import org.opendaylight.yangtools.yang.binding.Identifiable;
15 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
16 import org.opendaylight.yangtools.yang.common.QNameModule;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
21 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
22 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
23 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
28 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
29
30 final class DataContainerCodecPrototype<T> implements NodeContextSupplier {
31
32     private final T schema;
33     private final QNameModule namespace;
34     private final CodecContextFactory factory;
35     private final Class<?> bindingClass;
36     private final InstanceIdentifier.Item<?> bindingArg;
37     private final YangInstanceIdentifier.PathArgument yangArg;
38     private volatile DataContainerCodecContext<?,T> instance = null;
39
40     @SuppressWarnings({"rawtypes", "unchecked"})
41     private DataContainerCodecPrototype(final Class<?> cls, final YangInstanceIdentifier.PathArgument arg, final T nodeSchema,
42             final CodecContextFactory factory) {
43         this.bindingClass = cls;
44         this.yangArg = arg;
45         this.schema = nodeSchema;
46         this.factory = factory;
47         this.bindingArg = new InstanceIdentifier.Item(bindingClass);
48
49         if (arg instanceof AugmentationIdentifier) {
50             this.namespace = Iterables.getFirst(((AugmentationIdentifier) arg).getPossibleChildNames(), null).getModule();
51         } else {
52             this.namespace = arg.getNodeType().getModule();
53         }
54     }
55
56     @SuppressWarnings({ "unchecked", "rawtypes" })
57     static <T extends DataSchemaNode> DataContainerCodecPrototype<T> from(final Class<?> cls, final T schema,
58             final CodecContextFactory factory) {
59         return new DataContainerCodecPrototype(cls, NodeIdentifier.create(schema.getQName()), schema, factory);
60     }
61
62     static DataContainerCodecPrototype<SchemaContext> rootPrototype(final CodecContextFactory factory) {
63         final SchemaContext schema = factory.getRuntimeContext().getSchemaContext();
64         final NodeIdentifier arg = NodeIdentifier.create(schema.getQName());
65         return new DataContainerCodecPrototype<SchemaContext>(DataRoot.class, arg, schema, factory);
66     }
67
68
69     @SuppressWarnings({ "rawtypes", "unchecked" })
70     static DataContainerCodecPrototype<?> from(final Class<?> augClass, final AugmentationIdentifier arg,
71             final AugmentationSchema schema, final CodecContextFactory factory) {
72         return new DataContainerCodecPrototype(augClass, arg, schema, factory);
73     }
74
75     static DataContainerCodecPrototype<NotificationDefinition> from(final Class<?> augClass, final NotificationDefinition schema, final CodecContextFactory factory) {
76         final PathArgument arg = NodeIdentifier.create(schema.getQName());
77         return new DataContainerCodecPrototype<NotificationDefinition>(augClass,arg, schema, factory);
78     }
79
80     protected T getSchema() {
81         return schema;
82     }
83
84     protected QNameModule getNamespace() {
85         return namespace;
86     }
87
88     protected CodecContextFactory getFactory() {
89         return factory;
90     }
91
92     protected Class<?> getBindingClass() {
93         return bindingClass;
94     }
95
96     protected InstanceIdentifier.Item<?> getBindingArg() {
97         return bindingArg;
98     }
99
100     protected YangInstanceIdentifier.PathArgument getYangArg() {
101         return yangArg;
102     }
103
104     @Override
105     public DataContainerCodecContext<?,T> get() {
106         DataContainerCodecContext<?,T> tmp = instance;
107         if (tmp == null) {
108             synchronized (this) {
109                 tmp = instance;
110                 if (tmp == null) {
111                     tmp = createInstance();
112                     instance = tmp;
113                 }
114             }
115         }
116
117         return tmp;
118     }
119
120     @GuardedBy("this")
121     @SuppressWarnings({ "rawtypes", "unchecked" })
122     private DataContainerCodecContext<?,T> createInstance() {
123         // FIXME: make protected abstract
124         if (schema instanceof ContainerSchemaNode) {
125             return new ContainerNodeCodecContext(this);
126         } else if (schema instanceof ListSchemaNode) {
127             if (Identifiable.class.isAssignableFrom(getBindingClass())) {
128                 return new KeyedListNodeCodecContext(this);
129             } else {
130                 return new ListNodeCodecContext(this);
131             }
132         } else if (schema instanceof ChoiceSchemaNode) {
133             return new ChoiceNodeCodecContext(this);
134         } else if (schema instanceof AugmentationSchema) {
135             return new AugmentationNodeContext(this);
136         } else if (schema instanceof ChoiceCaseNode) {
137             return new CaseNodeCodecContext(this);
138         }
139         throw new IllegalArgumentException("Unsupported type " + bindingClass + " " + schema);
140     }
141
142     boolean isChoice() {
143         return schema instanceof ChoiceSchemaNode;
144     }
145 }