Remove AttributesContainer
[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 org.eclipse.jdt.annotation.NonNull;
11 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
12 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
13 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeBuilder;
14 import org.opendaylight.yangtools.yang.data.impl.schema.nodes.AbstractImmutableNormalizedSimpleValueNode;
15
16 public class ImmutableLeafNodeBuilder<T>
17         extends AbstractImmutableNormalizedNodeBuilder<NodeIdentifier, T, LeafNode<T>> {
18
19     public static <T> @NonNull NormalizedNodeBuilder<NodeIdentifier, T, LeafNode<T>> create() {
20         return new ImmutableLeafNodeBuilder<>();
21     }
22
23     @Override
24     @SuppressWarnings("unchecked")
25     public LeafNode<T> build() {
26         final T value = getValue();
27         if (value instanceof byte[]) {
28             return (LeafNode<T>) new ImmutableBinaryLeafNode(getNodeIdentifier(), (byte[]) value);
29         }
30
31         return new ImmutableLeafNode<>(getNodeIdentifier(), value);
32     }
33
34     private static final class ImmutableLeafNode<T>
35             extends AbstractImmutableNormalizedSimpleValueNode<NodeIdentifier, T> implements LeafNode<T> {
36         ImmutableLeafNode(final NodeIdentifier nodeIdentifier, final T value) {
37             super(nodeIdentifier, value);
38         }
39     }
40
41     private static final class ImmutableBinaryLeafNode
42             extends AbstractImmutableNormalizedSimpleValueNode<NodeIdentifier, byte[]> implements LeafNode<byte[]> {
43         ImmutableBinaryLeafNode(final NodeIdentifier nodeIdentifier, final byte[] value) {
44             super(nodeIdentifier, value);
45         }
46
47         @Override
48         protected byte[] wrapValue(final byte[] valueToWrap) {
49             return valueToWrap.clone();
50         }
51     }
52 }