Bug 1984, Bug 2005: Changed valueEquals to work with null values.
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / nodes / AbstractImmutableNormalizedValueAttrNode.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.nodes;
9
10 import com.google.common.base.Objects;
11 import com.google.common.base.Objects.ToStringHelper;
12 import com.google.common.collect.ImmutableMap;
13 import java.util.Map;
14 import org.opendaylight.yangtools.yang.common.QName;
15 import org.opendaylight.yangtools.yang.data.api.AttributesContainer;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
17
18 public abstract class AbstractImmutableNormalizedValueAttrNode<K extends YangInstanceIdentifier.PathArgument,V>
19         extends AbstractImmutableNormalizedValueNode<K, V>
20         implements AttributesContainer {
21
22     private final Map<QName, String> attributes;
23
24     protected AbstractImmutableNormalizedValueAttrNode(final K nodeIdentifier, final V value, final Map<QName, String> attributes) {
25         super(nodeIdentifier, value);
26         this.attributes = ImmutableMap.copyOf(attributes);
27     }
28
29     @Override
30     public final Map<QName, String> getAttributes() {
31         return attributes;
32     }
33
34     @Override
35     public final Object getAttributeValue(final QName value) {
36         return attributes.get(value);
37     }
38
39     @Override
40     protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
41         return super.addToStringAttributes(toStringHelper).add("attributes", attributes);
42     }
43
44     @Override
45     protected int valueHashCode() {
46         final int result = getValue() != null ? getValue().hashCode() : 1;
47 // FIXME: are attributes part of hashCode/equals?
48 //        for (final Entry<?, ?> a : attributes.entrySet()) {
49 //            result = 31 * result + a.hashCode();
50 //        }
51         return result;
52     }
53
54     @Override
55     protected boolean valueEquals(final AbstractImmutableNormalizedNode<?, ?> other) {
56         // We can not call directly getValue.equals because of Empty Type Definition leaves
57         // which allways have NULL value
58         if (!Objects.equal(getValue(), other.getValue())) {
59             return false;
60         }
61
62 // FIXME: are attributes part of hashCode/equals?
63 //        final Set<Entry<QName, String>> tas = getAttributes().entrySet();
64 //        final Set<Entry<QName, String>> oas = container.getAttributes().entrySet();
65 //
66 //        return tas.containsAll(oas) && oas.containsAll(tas);
67         return true;
68     }
69 }