BUG-648: Fixup hashCode/equals
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / builder / impl / ImmutableMapNodeBuilder.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 java.util.List;
11 import java.util.Map;
12
13 import org.opendaylight.yangtools.concepts.Immutable;
14 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
15 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
16 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
17 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder;
18 import org.opendaylight.yangtools.yang.data.impl.schema.nodes.AbstractImmutableNormalizedNode;
19
20 import com.google.common.base.Optional;
21 import com.google.common.collect.ImmutableList;
22 import com.google.common.collect.Iterables;
23 import com.google.common.collect.Maps;
24
25 public class ImmutableMapNodeBuilder
26         implements CollectionNodeBuilder<MapEntryNode, MapNode> {
27
28     protected Map<InstanceIdentifier.NodeIdentifierWithPredicates, MapEntryNode> value = Maps.newLinkedHashMap();
29     protected InstanceIdentifier.NodeIdentifier nodeIdentifier;
30
31     public static CollectionNodeBuilder<MapEntryNode, MapNode> create() {
32         return new ImmutableMapNodeBuilder();
33     }
34
35     @Override
36     public CollectionNodeBuilder<MapEntryNode, MapNode> withChild(final MapEntryNode child) {
37         this.value.put(child.getIdentifier(), child);
38         return this;
39     }
40
41     @Override
42     public CollectionNodeBuilder<MapEntryNode, MapNode> withValue(final List<MapEntryNode> value) {
43         // TODO replace or putAll ?
44         for (final MapEntryNode mapEntryNode : value) {
45             withChild(mapEntryNode);
46         }
47
48         return this;
49     }
50
51     @Override
52     public CollectionNodeBuilder<MapEntryNode, MapNode> withNodeIdentifier(final InstanceIdentifier.NodeIdentifier nodeIdentifier) {
53         this.nodeIdentifier = nodeIdentifier;
54         return this;
55     }
56
57     @Override
58     public MapNode build() {
59         return new ImmutableMapNode(nodeIdentifier, value);
60     }
61
62     @Override
63     public CollectionNodeBuilder<MapEntryNode, MapNode> addChild(
64             final MapEntryNode child) {
65         return withChild(child);
66     }
67
68     static final class ImmutableMapNode extends AbstractImmutableNormalizedNode<InstanceIdentifier.NodeIdentifier, Iterable<MapEntryNode>> implements Immutable,MapNode {
69
70         private final Map<InstanceIdentifier.NodeIdentifierWithPredicates, MapEntryNode> mappedChildren;
71
72         ImmutableMapNode(final InstanceIdentifier.NodeIdentifier nodeIdentifier,
73                          final Map<InstanceIdentifier.NodeIdentifierWithPredicates, MapEntryNode> children) {
74             super(nodeIdentifier, ImmutableList.copyOf(children.values()));
75             this.mappedChildren = children;
76         }
77
78         @Override
79         public Optional<MapEntryNode> getChild(final InstanceIdentifier.NodeIdentifierWithPredicates child) {
80             return Optional.fromNullable(mappedChildren.get(child));
81         }
82
83         @Override
84         protected int valueHashCode() {
85             return mappedChildren.hashCode();
86         }
87
88         @Override
89         protected boolean valueEquals(final AbstractImmutableNormalizedNode<?, ?> other) {
90             return mappedChildren.equals(((ImmutableMapNode) other).mappedChildren);
91         }
92     }
93 }