2a9c53326666ee607f0aae5d4c8c93e480302c99
[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 sealed class ValueNodeCodecContext extends CodecContext implements CodecContextSupplier
21         permits AbstractOpaqueCodecContext, ValueNodeCodecContext.WithCodec {
22     abstract static sealed class WithCodec extends ValueNodeCodecContext
23             permits LeafNodeCodecContext, LeafSetNodeCodecContext {
24         private final @NonNull ValueCodec<Object, Object> valueCodec;
25
26         WithCodec(final DataSchemaNode schema, final ValueCodec<Object, Object> codec, final String getterName,
27                 final Object defaultObject) {
28             super(schema, getterName, defaultObject);
29             valueCodec = requireNonNull(codec);
30         }
31
32         @Override
33         final ValueCodec<Object, Object> getValueCodec() {
34             return valueCodec;
35         }
36     }
37
38     private final @NonNull NodeIdentifier yangIdentifier;
39     private final @NonNull String getterName;
40     private final @NonNull DataSchemaNode schema;
41     private final Object defaultObject;
42
43     ValueNodeCodecContext(final DataSchemaNode schema, final String getterName, final Object defaultObject) {
44         yangIdentifier = NodeIdentifier.create(schema.getQName());
45         this.getterName = requireNonNull(getterName);
46         this.schema = requireNonNull(schema);
47         this.defaultObject = defaultObject;
48     }
49
50     @Override
51     protected final NodeIdentifier getDomPathArgument() {
52         return yangIdentifier;
53     }
54
55     @Override
56     public final CodecContext get() {
57         return this;
58     }
59
60     final String getGetterName() {
61         return getterName;
62     }
63
64     abstract ValueCodec<Object, Object> getValueCodec();
65
66     @Override
67     public final DataSchemaNode getSchema() {
68         return schema;
69     }
70
71     @Override
72     final Object defaultObject() {
73         return defaultObject;
74     }
75 }