Use ImmutableMap instead of Collections.emptyMap()
[mdsal.git] / dom / mdsal-dom-inmemory-datastore / src / main / java / org / opendaylight / mdsal / dom / store / inmemory / ModificationContextNodeBuilder.java
1 /*
2  * Copyright (c) 2016 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
9 package org.opendaylight.mdsal.dom.store.inmemory;
10
11 import java.util.HashMap;
12 import java.util.Map;
13 import java.util.Map.Entry;
14 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
15
16 abstract class ModificationContextNodeBuilder<T extends WriteableModificationNode> {
17
18     private final Map<PathArgument, InteriorNodeBuilder> interiorChildren = new HashMap<>();
19     private final Map<PathArgument, WriteableSubshardBoundaryNode> boundaryChildren = new HashMap<>();
20
21     protected InteriorNodeBuilder getInterior(final PathArgument arg) {
22         InteriorNodeBuilder potential = interiorChildren.get(arg);
23         if (potential == null) {
24             potential = new InteriorNodeBuilder(arg);
25             interiorChildren.put(arg, potential);
26         }
27         return potential;
28     }
29
30     protected void addBoundary(final PathArgument arg, final WriteableSubshardBoundaryNode subshardNode) {
31         boundaryChildren.put(arg, subshardNode);
32     }
33
34     final T build() {
35         HashMap<PathArgument, WriteableModificationNode> builtChildren =
36                 new HashMap<PathArgument, WriteableModificationNode>(boundaryChildren);
37         for (Entry<PathArgument, InteriorNodeBuilder> interiorNode : interiorChildren.entrySet()) {
38             WriteableModificationNode builded = interiorNode.getValue().build();
39             builtChildren.put(builded.getIdentifier(), builded);
40         }
41
42         return build(builtChildren);
43     }
44
45     abstract T build(Map<PathArgument, WriteableModificationNode> children);
46
47     private static class InteriorNodeBuilder extends ModificationContextNodeBuilder<WritableInteriorNode> {
48
49         private final PathArgument arg;
50
51         InteriorNodeBuilder(final PathArgument arg) {
52             this.arg = arg;
53         }
54
55         @Override
56         WritableInteriorNode build(final Map<PathArgument, WriteableModificationNode> children) {
57             return new WritableInteriorNode(arg, children);
58         }
59     }
60
61 }