13b4406f1cb2df0efc30a4878d7404ca4b549554
[mdsal.git] / binding / mdsal-binding-dom-codec / src / main / java / org / opendaylight / mdsal / binding / dom / codec / impl / ValueNodeCodecContext.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 static java.util.Objects.requireNonNull;
11
12 import org.eclipse.jdt.annotation.NonNull;
13 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
14 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
15
16 /**
17  * Abstract base class for atomic nodes. These are nodes which are not decomposed in the Binding Specification, such
18  * as LeafNodes and LeafSetNodes.
19  */
20 abstract class ValueNodeCodecContext extends NodeCodecContext implements NodeContextSupplier {
21     abstract static class WithCodec extends ValueNodeCodecContext {
22         private final @NonNull ValueCodec<Object, Object> valueCodec;
23
24         WithCodec(final DataSchemaNode schema, final ValueCodec<Object, Object> codec, final String getterName,
25                 final Object defaultObject) {
26             super(schema, getterName, defaultObject);
27             valueCodec = requireNonNull(codec);
28         }
29
30         @Override
31         final ValueCodec<Object, Object> getValueCodec() {
32             return valueCodec;
33         }
34     }
35
36     private final @NonNull NodeIdentifier yangIdentifier;
37     private final @NonNull String getterName;
38     private final @NonNull DataSchemaNode schema;
39     private final Object defaultObject;
40
41     ValueNodeCodecContext(final DataSchemaNode schema, final String getterName, final Object defaultObject) {
42         yangIdentifier = NodeIdentifier.create(schema.getQName());
43         this.getterName = requireNonNull(getterName);
44         this.schema = requireNonNull(schema);
45         this.defaultObject = defaultObject;
46     }
47
48     @Override
49     protected final NodeIdentifier getDomPathArgument() {
50         return yangIdentifier;
51     }
52
53     @Override
54     public final NodeCodecContext get() {
55         return this;
56     }
57
58     final String getGetterName() {
59         return getterName;
60     }
61
62     abstract ValueCodec<Object, Object> getValueCodec();
63
64     @Override
65     public final DataSchemaNode getSchema() {
66         return schema;
67     }
68
69     @Override
70     final Object defaultObject() {
71         return defaultObject;
72     }
73 }