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