Fix yang-data-impl code smells
[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.MoreObjects.ToStringHelper;
11 import com.google.common.collect.ImmutableMap;
12 import java.util.Map;
13 import java.util.Objects;
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.PathArgument;
17
18 public abstract class AbstractImmutableNormalizedValueAttrNode<K extends 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,
25             final Map<QName, String> attributes) {
26         super(nodeIdentifier, value);
27         this.attributes = ImmutableMap.copyOf(attributes);
28     }
29
30     @Override
31     public final Map<QName, String> getAttributes() {
32         return attributes;
33     }
34
35     @Override
36     public final Object getAttributeValue(final QName value) {
37         return attributes.get(value);
38     }
39
40     @Override
41     protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
42         return super.addToStringAttributes(toStringHelper).add("attributes", attributes);
43     }
44
45     @Override
46     protected int valueHashCode() {
47         final V local = value();
48         final int result = local != null ? local.hashCode() : 1;
49         // FIXME: are attributes part of hashCode/equals?
50         return result;
51     }
52
53     @Override
54     protected boolean valueEquals(final AbstractImmutableNormalizedNode<?, ?> other) {
55         // We can not call directly getValue.equals because of Empty Type
56         // RequireInstanceStatementSupport leaves which always have NULL value
57
58         // FIXME: are attributes part of hashCode/equals?
59         return Objects.deepEquals(value(), other.getValue());
60     }
61 }