Fix SnapshotBackedWriteTransaction error message
[mdsal.git] / dom / mdsal-dom-spi / src / main / java / org / opendaylight / mdsal / dom / spi / shard / 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 package org.opendaylight.mdsal.dom.spi.shard;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.annotations.Beta;
13 import java.util.HashMap;
14 import java.util.LinkedHashMap;
15 import java.util.Map;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
17
18 @Beta
19 public abstract class ModificationContextNodeBuilder {
20
21     private final Map<PathArgument, InteriorNodeBuilder> interiorChildren = new LinkedHashMap<>();
22     private final Map<PathArgument, WriteableSubshardBoundaryNode> boundaryChildren = new HashMap<>();
23
24     public ModificationContextNodeBuilder getInterior(final PathArgument arg) {
25         InteriorNodeBuilder potential = interiorChildren.get(arg);
26         if (potential == null) {
27             potential = new InteriorNodeBuilder(arg);
28             interiorChildren.put(arg, potential);
29         }
30         return potential;
31     }
32
33     public void addBoundary(final PathArgument arg, final WriteableSubshardBoundaryNode subshardNode) {
34         boundaryChildren.put(arg, subshardNode);
35     }
36
37     public final Map<PathArgument, WriteableModificationNode> buildChildren() {
38         final Map<PathArgument, WriteableModificationNode> builtChildren = new HashMap<>(boundaryChildren);
39         for (InteriorNodeBuilder interiorNode : interiorChildren.values()) {
40             WriteableModificationNode builded = interiorNode.build();
41             builtChildren.put(builded.getIdentifier(), builded);
42         }
43
44         return builtChildren;
45     }
46
47     public static final class InteriorNodeBuilder extends ModificationContextNodeBuilder {
48         private final PathArgument arg;
49
50         InteriorNodeBuilder(final PathArgument arg) {
51             this.arg = requireNonNull(arg);
52         }
53
54         WritableInteriorNode build() {
55             return new WritableInteriorNode(arg, buildChildren());
56         }
57     }
58 }