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