Cleanup yang-data-impl nullness annotations
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / builder / impl / ImmutableLeafNodeBuilder.java
1 /*
2  * Copyright (c) 2013 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.impl.schema.builder.impl;
9
10 import java.util.Map;
11 import org.eclipse.jdt.annotation.NonNull;
12 import org.opendaylight.yangtools.yang.common.QName;
13 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
14 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
15 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeAttrBuilder;
16 import org.opendaylight.yangtools.yang.data.impl.schema.nodes.AbstractImmutableNormalizedValueAttrNode;
17
18 public class ImmutableLeafNodeBuilder<T>
19         extends AbstractImmutableNormalizedNodeBuilder<NodeIdentifier, T, LeafNode<T>> {
20
21     public static <T> @NonNull NormalizedNodeAttrBuilder<NodeIdentifier, T, LeafNode<T>> create() {
22         return new ImmutableLeafNodeBuilder<>();
23     }
24
25     @Override
26     @SuppressWarnings("unchecked")
27     public LeafNode<T> build() {
28         final T value = getValue();
29         if (value instanceof byte[]) {
30             return (LeafNode<T>) new ImmutableBinaryLeafNode(getNodeIdentifier(), (byte[]) value, getAttributes());
31         }
32
33         return new ImmutableLeafNode<>(getNodeIdentifier(), value, getAttributes());
34     }
35
36     private static final class ImmutableLeafNode<T>
37             extends AbstractImmutableNormalizedValueAttrNode<NodeIdentifier, T> implements LeafNode<T> {
38         ImmutableLeafNode(final NodeIdentifier nodeIdentifier, final T value, final Map<QName, String> attributes) {
39             super(nodeIdentifier, value, attributes);
40         }
41     }
42
43     private static final class ImmutableBinaryLeafNode
44             extends AbstractImmutableNormalizedValueAttrNode<NodeIdentifier, byte[]> implements LeafNode<byte[]> {
45         ImmutableBinaryLeafNode(final NodeIdentifier nodeIdentifier, final byte[] value,
46             final Map<QName, String> attributes) {
47             super(nodeIdentifier, value, attributes);
48         }
49
50         @Override
51         protected byte[] wrapValue(final byte[] valueToWrap) {
52             return valueToWrap.clone();
53         }
54     }
55 }