40de49f021b7e01feac6ed5d554135ce86fdf81d
[yangtools.git] / yang / yang-parser-spi / src / main / java / org / opendaylight / yangtools / yang / parser / spi / source / ImplicitSubstatement.java
1 /*
2  * Copyright (c) 2017 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.parser.spi.source;
9
10 import com.google.common.annotations.Beta;
11 import com.google.common.base.Preconditions;
12 import org.opendaylight.yangtools.yang.model.api.meta.StatementSource;
13
14 /**
15  * An implicit sub-statement, which is implied to be always present in its parent, even if it does not appear
16  * in model source.
17  *
18  * @author Robert Varga
19  */
20 @Beta
21 public final class ImplicitSubstatement implements StatementSourceReference {
22
23     private final StatementSourceReference parentRef;
24
25     private ImplicitSubstatement(final StatementSourceReference parentRef) {
26         this.parentRef = Preconditions.checkNotNull(parentRef);
27     }
28
29     /**
30      * Create a new {@link ImplicitSubstatement}.
31      *
32      * @param parentRef Parent source reference
33      * @return A new reference
34      * @throws NullPointerException if parentRef is null
35      */
36     public static ImplicitSubstatement of(final StatementSourceReference parentRef) {
37         return new ImplicitSubstatement(parentRef);
38     }
39
40     @Override
41     public StatementSource getStatementSource() {
42         return StatementSource.CONTEXT;
43     }
44
45     @Override
46     public int hashCode() {
47         return parentRef.hashCode();
48     }
49
50     @Override
51     public boolean equals(final Object obj) {
52         return obj instanceof ImplicitSubstatement && parentRef.equals(((ImplicitSubstatement) obj).parentRef);
53     }
54
55     @Override
56     public String toString() {
57         return parentRef.toString();
58     }
59 }