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