Merge branch 'master' of ../controller
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / builder / impl / ImmutableOrderedLeafSetNodeBuilder.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.Iterables;
11 import java.util.Collection;
12 import java.util.Collections;
13 import java.util.LinkedHashMap;
14 import java.util.Map;
15 import java.util.Optional;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.opendaylight.yangtools.util.UnmodifiableCollection;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
21 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
22 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
23 import org.opendaylight.yangtools.yang.data.api.schema.OrderedLeafSetNode;
24 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.ListNodeBuilder;
25 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeContainerBuilder;
26 import org.opendaylight.yangtools.yang.data.impl.schema.nodes.AbstractImmutableNormalizedNode;
27
28 public class ImmutableOrderedLeafSetNodeBuilder<T> implements ListNodeBuilder<T, LeafSetEntryNode<T>> {
29     private Map<NodeWithValue, LeafSetEntryNode<T>> value;
30     private NodeIdentifier nodeIdentifier;
31     private boolean dirty;
32
33     protected ImmutableOrderedLeafSetNodeBuilder() {
34         value = new LinkedHashMap<>();
35         dirty = false;
36     }
37
38     protected ImmutableOrderedLeafSetNodeBuilder(final ImmutableOrderedLeafSetNode<T> node) {
39         nodeIdentifier = node.getIdentifier();
40         value = node.getChildren();
41         dirty = true;
42     }
43
44     public static <T> @NonNull ListNodeBuilder<T, LeafSetEntryNode<T>> create() {
45         return new ImmutableOrderedLeafSetNodeBuilder<>();
46     }
47
48     public static <T> @NonNull ListNodeBuilder<T, LeafSetEntryNode<T>> create(final LeafSetNode<T> node) {
49         if (!(node instanceof ImmutableOrderedLeafSetNode<?>)) {
50             throw new UnsupportedOperationException(String.format("Cannot initialize from class %s", node.getClass()));
51         }
52
53         return new ImmutableOrderedLeafSetNodeBuilder<>((ImmutableOrderedLeafSetNode<T>) node);
54     }
55
56     private void checkDirty() {
57         if (dirty) {
58             value = new LinkedHashMap<>(value);
59             dirty = false;
60         }
61     }
62
63     @Override
64     public ListNodeBuilder<T, LeafSetEntryNode<T>> withChild(final LeafSetEntryNode<T> child) {
65         checkDirty();
66         this.value.put(child.getIdentifier(), child);
67         return this;
68     }
69
70     @Override
71     public ListNodeBuilder<T, LeafSetEntryNode<T>> withoutChild(final PathArgument key) {
72         checkDirty();
73         this.value.remove(key);
74         return this;
75     }
76
77     @Override
78     public OrderedLeafSetNode<T> build() {
79         dirty = true;
80         return new ImmutableOrderedLeafSetNode<>(nodeIdentifier, value);
81     }
82
83     @Override
84     public ListNodeBuilder<T, LeafSetEntryNode<T>> withNodeIdentifier(final NodeIdentifier withNodeIdentifier) {
85         this.nodeIdentifier = withNodeIdentifier;
86         return this;
87     }
88
89     @Override
90     public ListNodeBuilder<T, LeafSetEntryNode<T>> withValue(final Collection<LeafSetEntryNode<T>> withValue) {
91         checkDirty();
92         for (final LeafSetEntryNode<T> leafSetEntry : withValue) {
93             withChild(leafSetEntry);
94         }
95         return this;
96     }
97
98     @Override
99     public ListNodeBuilder<T, LeafSetEntryNode<T>> withChildValue(final T childValue) {
100         return withChild(ImmutableLeafSetEntryNodeBuilder.<T>create()
101             .withNodeIdentifier(new NodeWithValue<>(nodeIdentifier.getNodeType(), childValue))
102             .withValue(childValue).build());
103     }
104
105     protected static final class ImmutableOrderedLeafSetNode<T> extends
106             AbstractImmutableNormalizedNode<NodeIdentifier, Collection<LeafSetEntryNode<T>>> implements
107             OrderedLeafSetNode<T> {
108
109         private final Map<NodeWithValue, LeafSetEntryNode<T>> children;
110
111         ImmutableOrderedLeafSetNode(final NodeIdentifier nodeIdentifier,
112                 final Map<NodeWithValue, LeafSetEntryNode<T>> children) {
113             super(nodeIdentifier);
114             this.children = children;
115         }
116
117         @Override
118         public Optional<LeafSetEntryNode<T>> getChild(final NodeWithValue child) {
119             return Optional.ofNullable(children.get(child));
120         }
121
122         @Override
123         public LeafSetEntryNode<T> getChild(final int position) {
124             return Iterables.get(children.values(), position);
125         }
126
127         @Override
128         protected int valueHashCode() {
129             return children.hashCode();
130         }
131
132         private Map<NodeWithValue, LeafSetEntryNode<T>> getChildren() {
133             return Collections.unmodifiableMap(children);
134         }
135
136         @Override
137         protected boolean valueEquals(final AbstractImmutableNormalizedNode<?, ?> other) {
138             return children.equals(((ImmutableOrderedLeafSetNode<?>) other).children);
139         }
140
141         @Override
142         public int getSize() {
143             return children.size();
144         }
145
146         @Override
147         public Collection<LeafSetEntryNode<T>> getValue() {
148             return UnmodifiableCollection.create(children.values());
149         }
150     }
151
152     @Override
153     public NormalizedNodeContainerBuilder<NodeIdentifier, PathArgument, LeafSetEntryNode<T>, LeafSetNode<T>> addChild(
154             final LeafSetEntryNode<T> child) {
155         return withChild(child);
156     }
157
158     @Override
159     public NormalizedNodeContainerBuilder<NodeIdentifier, PathArgument, LeafSetEntryNode<T>, LeafSetNode<T>>
160             removeChild(final PathArgument key) {
161         return withoutChild(key);
162     }
163 }