08246d9a0960122a6cefd938f07d4a585e310903
[yangtools.git] / yang / yang-model-util / src / main / java / org / opendaylight / yangtools / yang / model / util / type / BitImpl.java
1 /*
2  * Copyright (c) 2016 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.model.util.type;
9
10 import com.google.common.base.Preconditions;
11 import java.util.List;
12 import java.util.Objects;
13 import javax.annotation.Nonnull;
14 import org.opendaylight.yangtools.concepts.Immutable;
15 import org.opendaylight.yangtools.yang.common.QName;
16 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
17 import org.opendaylight.yangtools.yang.model.api.Status;
18 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
19 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition.Bit;
20
21 final class BitImpl implements Bit, Immutable {
22     private final List<UnknownSchemaNode> unknownNodes;
23     private final SchemaPath schemaPath;
24     private final String description;
25     private final String reference;
26     private final Status status;
27     private final long position;
28
29     BitImpl(final SchemaPath schemaPath, final long position, final String description,
30             final String reference, final Status status, final List<UnknownSchemaNode> unknownNodes) {
31         this.schemaPath = Preconditions.checkNotNull(schemaPath, "Schema Path should not be null");
32
33         Preconditions.checkArgument(position >= 0L && position <= 4294967295L, "Invalid position %s", position);
34         this.position = position;
35         this.description = description;
36         this.reference = reference;
37         this.status = Preconditions.checkNotNull(status);
38         this.unknownNodes = Preconditions.checkNotNull(unknownNodes);
39     }
40
41     @Nonnull
42     @Override
43     public QName getQName() {
44         return schemaPath.getLastComponent();
45     }
46
47     @Nonnull
48     @Override
49     public SchemaPath getPath() {
50         return schemaPath;
51     }
52
53     @Override
54     public String getDescription() {
55         return description;
56     }
57
58     @Override
59     public String getReference() {
60         return reference;
61     }
62
63     @Nonnull
64     @Override
65     public Status getStatus() {
66         return status;
67     }
68
69     @Nonnull
70     @Override
71     public List<UnknownSchemaNode> getUnknownSchemaNodes() {
72         return unknownNodes;
73     }
74
75     @Override
76     public long getPosition() {
77         return position;
78     }
79
80     @Nonnull
81     @Override
82     public String getName() {
83         return getQName().getLocalName();
84     }
85
86     @Override
87     public int hashCode() {
88         final int prime = 31;
89         int result = 1;
90         result = prime * result + getQName().hashCode();
91         result = prime * result + schemaPath.hashCode();
92         result = prime * result + Long.hashCode(position);
93         result = prime * result + unknownNodes.hashCode();
94         return result;
95     }
96
97     @Override
98     public boolean equals(final Object obj) {
99         if (this == obj) {
100             return true;
101         }
102         if (obj == null) {
103             return false;
104         }
105         if (getClass() != obj.getClass()) {
106             return false;
107         }
108         final Bit other = (Bit) obj;
109         return Objects.equals(schemaPath, other.getPath());
110     }
111
112     @Override
113     public String toString() {
114         return Bit.class.getSimpleName() + "[name=" + getQName().getLocalName() + ", position=" + position + "]";
115     }
116
117 }