Do not pretty-print body class
[yangtools.git] / data / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / builder / impl / ImmutableLeafSetNodeBuilder.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.collect.Maps;
11 import java.util.Collection;
12 import java.util.HashMap;
13 import java.util.Map;
14 import org.eclipse.jdt.annotation.NonNull;
15 import org.opendaylight.yangtools.util.MapAdaptor;
16 import org.opendaylight.yangtools.util.UnmodifiableCollection;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
20 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
21 import org.opendaylight.yangtools.yang.data.api.schema.SystemLeafSetNode;
22 import org.opendaylight.yangtools.yang.data.api.schema.builder.ListNodeBuilder;
23 import org.opendaylight.yangtools.yang.data.impl.schema.nodes.AbstractImmutableNormalizedValueNode;
24
25 public class ImmutableLeafSetNodeBuilder<T> implements ListNodeBuilder<T, SystemLeafSetNode<T>> {
26     private static final int DEFAULT_CAPACITY = 4;
27
28     private final Map<NodeWithValue<?>, LeafSetEntryNode<T>> value;
29
30     private NodeIdentifier nodeIdentifier;
31
32     protected ImmutableLeafSetNodeBuilder() {
33         value = new HashMap<>(DEFAULT_CAPACITY);
34     }
35
36     protected ImmutableLeafSetNodeBuilder(final int sizeHint) {
37         if (sizeHint >= 0) {
38             value = Maps.newHashMapWithExpectedSize(sizeHint);
39         } else {
40             value = new HashMap<>(DEFAULT_CAPACITY);
41         }
42     }
43
44     protected ImmutableLeafSetNodeBuilder(final ImmutableLeafSetNode<T> node) {
45         nodeIdentifier = node.getIdentifier();
46         value = MapAdaptor.getDefaultInstance().takeSnapshot(node.children);
47     }
48
49     public static <T> @NonNull ListNodeBuilder<T, SystemLeafSetNode<T>> create() {
50         return new ImmutableLeafSetNodeBuilder<>();
51     }
52
53     public static <T> @NonNull ListNodeBuilder<T, SystemLeafSetNode<T>> create(final int sizeHint) {
54         return new ImmutableLeafSetNodeBuilder<>(sizeHint);
55     }
56
57     public static <T> @NonNull ListNodeBuilder<T, SystemLeafSetNode<T>> create(final SystemLeafSetNode<T> node) {
58         if (!(node instanceof ImmutableLeafSetNode<?>)) {
59             throw new UnsupportedOperationException(String.format("Cannot initialize from class %s", node.getClass()));
60         }
61
62         return new ImmutableLeafSetNodeBuilder<>((ImmutableLeafSetNode<T>) node);
63     }
64
65     @Override
66     public ImmutableLeafSetNodeBuilder<T> withChild(final LeafSetEntryNode<T> child) {
67         this.value.put(child.getIdentifier(), child);
68         return this;
69     }
70
71     @Override
72     public ImmutableLeafSetNodeBuilder<T> withoutChild(final PathArgument key) {
73         this.value.remove(key);
74         return this;
75     }
76
77     @Override
78     public SystemLeafSetNode<T> build() {
79         return new ImmutableLeafSetNode<>(nodeIdentifier, MapAdaptor.getDefaultInstance().optimize(value));
80     }
81
82     @Override
83     public ImmutableLeafSetNodeBuilder<T> withNodeIdentifier(final NodeIdentifier withNodeIdentifier) {
84         this.nodeIdentifier = withNodeIdentifier;
85         return this;
86     }
87
88     @Override
89     public ImmutableLeafSetNodeBuilder<T> withValue(final Collection<LeafSetEntryNode<T>> withValue) {
90         for (final LeafSetEntryNode<T> leafSetEntry : withValue) {
91             withChild(leafSetEntry);
92         }
93         return this;
94     }
95
96     @Override
97     public ImmutableLeafSetNodeBuilder<T> withChildValue(final T childValue) {
98         return withChild(ImmutableLeafSetEntryNodeBuilder.<T>create()
99             .withNodeIdentifier(new NodeWithValue<>(nodeIdentifier.getNodeType(), childValue))
100             .withValue(childValue).build());
101     }
102
103
104     @Override
105     public ImmutableLeafSetNodeBuilder<T> addChild(final LeafSetEntryNode<T> child) {
106         return withChild(child);
107     }
108
109     @Override
110     public ImmutableLeafSetNodeBuilder<T> removeChild(final PathArgument key) {
111         return withoutChild(key);
112     }
113
114     protected static final class ImmutableLeafSetNode<T>
115             extends AbstractImmutableNormalizedValueNode<NodeIdentifier, SystemLeafSetNode<?>,
116                 Collection<@NonNull LeafSetEntryNode<T>>>
117             implements SystemLeafSetNode<T> {
118
119         private final Map<NodeWithValue<?>, LeafSetEntryNode<T>> children;
120
121         ImmutableLeafSetNode(final NodeIdentifier nodeIdentifier,
122                 final Map<NodeWithValue<?>, LeafSetEntryNode<T>> children) {
123             super(nodeIdentifier, UnmodifiableCollection.create(children.values()));
124             this.children = children;
125         }
126
127         @Override
128         public LeafSetEntryNode<T> childByArg(final NodeWithValue<?> child) {
129             return children.get(child);
130         }
131
132         @Override
133         public int size() {
134             return children.size();
135         }
136
137         @Override
138         protected Class<SystemLeafSetNode<?>> implementedType() {
139             return (Class) SystemLeafSetNode.class;
140         }
141
142         @Override
143         protected int valueHashCode() {
144             return children.hashCode();
145         }
146
147         @Override
148         protected boolean valueEquals(final SystemLeafSetNode<?> other) {
149             return children.equals(((ImmutableLeafSetNode<?>) other).children);
150         }
151     }
152 }