Make sure we optimize DOMDataTreeIdentifier
[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 org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
14
15 abstract class ModificationContextNodeBuilder<T extends WriteableModificationNode> {
16
17     private final Map<PathArgument, InteriorNodeBuilder> interiorChildren = new HashMap<>();
18     private final Map<PathArgument, WriteableSubshardBoundaryNode> boundaryChildren = new HashMap<>();
19
20     protected InteriorNodeBuilder getInterior(final PathArgument arg) {
21         InteriorNodeBuilder potential = interiorChildren.get(arg);
22         if (potential == null) {
23             potential = new InteriorNodeBuilder(arg);
24             interiorChildren.put(arg, potential);
25         }
26         return potential;
27     }
28
29     protected void addBoundary(final PathArgument arg, final WriteableSubshardBoundaryNode subshardNode) {
30         boundaryChildren.put(arg, subshardNode);
31     }
32
33     final T build() {
34         final Map<PathArgument, WriteableModificationNode> builtChildren = new HashMap<>(boundaryChildren);
35         for (InteriorNodeBuilder interiorNode : interiorChildren.values()) {
36             WriteableModificationNode builded = interiorNode.build();
37             builtChildren.put(builded.getIdentifier(), builded);
38         }
39
40         return build(builtChildren);
41     }
42
43     abstract T build(Map<PathArgument, WriteableModificationNode> children);
44
45     private static class InteriorNodeBuilder extends ModificationContextNodeBuilder<WritableInteriorNode> {
46
47         private final PathArgument arg;
48
49         InteriorNodeBuilder(final PathArgument arg) {
50             this.arg = arg;
51         }
52
53         @Override
54         WritableInteriorNode build(final Map<PathArgument, WriteableModificationNode> children) {
55             return new WritableInteriorNode(arg, children);
56         }
57     }
58
59 }