BUG-648: Fixup hashCode/equals
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / builder / impl / ImmutableLeafSetNodeBuilder.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.Collections;
11 import java.util.List;
12 import java.util.Map;
13
14 import org.opendaylight.yangtools.concepts.Immutable;
15 import org.opendaylight.yangtools.yang.common.QName;
16 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
17 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifier;
18 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
19 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
20 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
21 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.ListNodeBuilder;
22 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeContainerBuilder;
23 import org.opendaylight.yangtools.yang.data.impl.schema.nodes.AbstractImmutableNormalizedNode;
24
25 import com.google.common.base.Optional;
26 import com.google.common.collect.ImmutableList;
27 import com.google.common.collect.Iterables;
28 import com.google.common.collect.Maps;
29
30 public class ImmutableLeafSetNodeBuilder<T> implements ListNodeBuilder<T, LeafSetEntryNode<T>> {
31
32     protected Map<InstanceIdentifier.NodeWithValue, LeafSetEntryNode<T>> value = Maps.newLinkedHashMap();
33     protected InstanceIdentifier.NodeIdentifier nodeIdentifier;
34
35     public static <T> ListNodeBuilder<T, LeafSetEntryNode<T>> create() {
36         return new ImmutableLeafSetNodeBuilder<>();
37     }
38
39     @Override
40     public ListNodeBuilder<T, LeafSetEntryNode<T>> withChild(final LeafSetEntryNode<T> child) {
41         this.value.put(child.getIdentifier(), child);
42         return this;
43     }
44
45     @Override
46     public LeafSetNode<T> build() {
47         return new ImmutableLeafSetNode<>(nodeIdentifier, value);
48     }
49
50     @Override
51     public ListNodeBuilder<T, LeafSetEntryNode<T>> withNodeIdentifier(
52             final InstanceIdentifier.NodeIdentifier nodeIdentifier) {
53         this.nodeIdentifier = nodeIdentifier;
54         return this;
55     }
56
57     @Override
58     public ListNodeBuilder<T, LeafSetEntryNode<T>> withValue(final List<LeafSetEntryNode<T>> value) {
59         for (final LeafSetEntryNode<T> leafSetEntry : value) {
60             withChild(leafSetEntry);
61         }
62
63         return this;
64     }
65
66
67     @Override
68     public ListNodeBuilder<T, LeafSetEntryNode<T>> withChildValue(final T value, final Map<QName, String> attributes) {
69         return withChild(new ImmutableLeafSetEntryNodeBuilder.ImmutableLeafSetEntryNode<>(
70                 new InstanceIdentifier.NodeWithValue(nodeIdentifier.getNodeType(), value), value, attributes));
71
72     }
73
74     @Override
75     public ListNodeBuilder<T, LeafSetEntryNode<T>> withChildValue(final T value) {
76         return withChildValue(value, Collections.<QName,String>emptyMap());
77     }
78
79
80     private final static class ImmutableLeafSetNode<T> extends
81             AbstractImmutableNormalizedNode<InstanceIdentifier.NodeIdentifier, Iterable<LeafSetEntryNode<T>>> implements
82             Immutable, LeafSetNode<T> {
83
84         private final Map<InstanceIdentifier.NodeWithValue, LeafSetEntryNode<T>> mappedChildren;
85
86         ImmutableLeafSetNode(final InstanceIdentifier.NodeIdentifier nodeIdentifier,
87                 final Map<InstanceIdentifier.NodeWithValue, LeafSetEntryNode<T>> children) {
88             super(nodeIdentifier, ImmutableList.copyOf(children.values()));
89             this.mappedChildren = children;
90         }
91
92         @Override
93         public Optional<LeafSetEntryNode<T>> getChild(final InstanceIdentifier.NodeWithValue child) {
94             return Optional.fromNullable(mappedChildren.get(child));
95         }
96
97         @Override
98         protected int valueHashCode() {
99             return mappedChildren.hashCode();
100         }
101
102         @Override
103         protected boolean valueEquals(final AbstractImmutableNormalizedNode<?, ?> other) {
104             return mappedChildren.equals(((ImmutableLeafSetNode<?>) other).mappedChildren);
105         }
106     }
107
108     @Override
109     public NormalizedNodeContainerBuilder<NodeIdentifier, PathArgument, LeafSetEntryNode<T>, LeafSetNode<T>> addChild(
110             final LeafSetEntryNode<T> child) {
111         return withChild(child);
112     }
113
114 }