2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.data.impl.schema.builder.impl;
10 import com.google.common.base.Optional;
11 import com.google.common.collect.Iterables;
12 import java.util.LinkedHashMap;
13 import java.util.List;
15 import org.opendaylight.yangtools.concepts.Immutable;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
19 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
20 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
21 import org.opendaylight.yangtools.yang.data.api.schema.OrderedMapNode;
22 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder;
23 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeContainerBuilder;
24 import org.opendaylight.yangtools.yang.data.impl.schema.nodes.AbstractImmutableNormalizedNode;
26 public class ImmutableOrderedMapNodeBuilder implements CollectionNodeBuilder<MapEntryNode, OrderedMapNode> {
27 private static final int DEFAULT_CAPACITY = 4;
28 private Map<YangInstanceIdentifier.NodeIdentifierWithPredicates, MapEntryNode> value;
29 private YangInstanceIdentifier.NodeIdentifier nodeIdentifier;
30 private boolean dirty;
32 protected ImmutableOrderedMapNodeBuilder() {
33 this.value = new LinkedHashMap<>(DEFAULT_CAPACITY);
37 protected ImmutableOrderedMapNodeBuilder(final int sizeHint) {
38 this.value = new LinkedHashMap<>(DEFAULT_CAPACITY);
42 protected ImmutableOrderedMapNodeBuilder(final ImmutableOrderedMapNode node) {
43 this.nodeIdentifier = node.getIdentifier();
44 this.value = node.children;
48 public static CollectionNodeBuilder<MapEntryNode, OrderedMapNode> create() {
49 return new ImmutableOrderedMapNodeBuilder();
52 public static CollectionNodeBuilder<MapEntryNode, OrderedMapNode> create(final int sizeHint) {
53 return new ImmutableOrderedMapNodeBuilder(sizeHint);
56 public static CollectionNodeBuilder<MapEntryNode, OrderedMapNode> create(final MapNode node) {
57 if (!(node instanceof ImmutableOrderedMapNode)) {
58 throw new UnsupportedOperationException(String.format("Cannot initialize from class %s", node.getClass()));
61 return new ImmutableOrderedMapNodeBuilder((ImmutableOrderedMapNode) node);
64 private void checkDirty() {
66 value = new LinkedHashMap<>(value);
72 public CollectionNodeBuilder<MapEntryNode, OrderedMapNode> withChild(final MapEntryNode child) {
74 this.value.put(child.getIdentifier(), child);
79 public CollectionNodeBuilder<MapEntryNode, OrderedMapNode> withoutChild(final YangInstanceIdentifier.PathArgument key) {
81 this.value.remove(key);
86 public CollectionNodeBuilder<MapEntryNode, OrderedMapNode> withValue(final List<MapEntryNode> value) {
87 // TODO replace or putAll ?
88 for (final MapEntryNode mapEntryNode : value) {
89 withChild(mapEntryNode);
96 public CollectionNodeBuilder<MapEntryNode, OrderedMapNode> withNodeIdentifier(final YangInstanceIdentifier.NodeIdentifier nodeIdentifier) {
97 this.nodeIdentifier = nodeIdentifier;
102 public OrderedMapNode build() {
104 return new ImmutableOrderedMapNode(nodeIdentifier, value);
108 public CollectionNodeBuilder<MapEntryNode, OrderedMapNode> addChild(
109 final MapEntryNode child) {
110 return withChild(child);
115 public NormalizedNodeContainerBuilder<NodeIdentifier, PathArgument, MapEntryNode, OrderedMapNode> removeChild(
116 final PathArgument key) {
117 return withoutChild(key);
120 protected static final class ImmutableOrderedMapNode extends AbstractImmutableNormalizedNode<YangInstanceIdentifier.NodeIdentifier, Iterable<MapEntryNode>> implements Immutable,OrderedMapNode {
122 private final Map<YangInstanceIdentifier.NodeIdentifierWithPredicates, MapEntryNode> children;
124 ImmutableOrderedMapNode(final YangInstanceIdentifier.NodeIdentifier nodeIdentifier,
125 final Map<YangInstanceIdentifier.NodeIdentifierWithPredicates, MapEntryNode> children) {
126 super(nodeIdentifier);
127 this.children = children;
131 public Optional<MapEntryNode> getChild(final YangInstanceIdentifier.NodeIdentifierWithPredicates child) {
132 return Optional.fromNullable(children.get(child));
136 protected int valueHashCode() {
137 return children.hashCode();
141 protected boolean valueEquals(final AbstractImmutableNormalizedNode<?, ?> other) {
142 return children.equals(((ImmutableOrderedMapNode) other).children);
146 public MapEntryNode getChild(final int position) {
147 return Iterables.get(getValue(), position);
151 public int getSize() {
152 return children.size();
156 public Iterable<MapEntryNode> getValue() {
157 return Iterables.unmodifiableIterable(children.values());