a602a821fe72d68ec8b030bb4cfeeba0d2d3ec65
[yangtools.git] / yang / yang-parser-api / src / main / java / org / opendaylight / yangtools / yang / model / parser / api / YangParserConfiguration.java
1 /*
2  * Copyright (c) 2021 PANTHEON.tech, s.r.o. 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.parser.api;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.annotations.Beta;
13 import com.google.common.base.MoreObjects;
14 import java.util.Objects;
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.opendaylight.yangtools.concepts.Immutable;
18 import org.opendaylight.yangtools.yang.model.repo.api.StatementParserMode;
19
20 /**
21  * A configuration of {@link YangParser} wiring for use with {@link YangParserFactory}.
22  */
23 @Beta
24 @NonNullByDefault
25 public final class YangParserConfiguration implements Immutable {
26     /**
27      * System-wide default configuration.
28      */
29     public static final YangParserConfiguration DEFAULT = builder().build();
30
31     private final StatementParserMode parserMode;
32     private final boolean retainDeclarationReferences;
33
34     private YangParserConfiguration(final StatementParserMode parserMode, final boolean retainDeclarationReferences) {
35         this.parserMode = requireNonNull(parserMode);
36         this.retainDeclarationReferences = retainDeclarationReferences;
37     }
38
39     public StatementParserMode parserMode() {
40         return parserMode;
41     }
42
43     public boolean retainDeclarationReferences() {
44         return retainDeclarationReferences;
45     }
46
47     @Override
48     public int hashCode() {
49         return Objects.hash(parserMode, retainDeclarationReferences);
50     }
51
52     @Override
53     public boolean equals(final @Nullable Object obj) {
54         if (this == obj) {
55             return true;
56         }
57         if (!(obj instanceof YangParserConfiguration)) {
58             return false;
59         }
60         final YangParserConfiguration other = (YangParserConfiguration) obj;
61         return parserMode == other.parserMode && retainDeclarationReferences == other.retainDeclarationReferences;
62     }
63
64     @Override
65     public String toString() {
66         return MoreObjects.toStringHelper(this)
67             .add("parserMode", parserMode)
68             .add("retainDeclarationReferences", retainDeclarationReferences)
69             .toString();
70     }
71
72     public static Builder builder() {
73         return new Builder();
74     }
75
76     public static final class Builder implements org.opendaylight.yangtools.concepts.Builder<YangParserConfiguration> {
77         private StatementParserMode parserMode = StatementParserMode.DEFAULT_MODE;
78         private boolean retainDeclarationReferences = false;
79
80         private Builder() {
81             // Hidden on purpose
82         }
83
84         @Override
85         public YangParserConfiguration build() {
86             return new YangParserConfiguration(parserMode, retainDeclarationReferences);
87         }
88
89         public Builder parserMode(final StatementParserMode newParserMode) {
90             this.parserMode = requireNonNull(newParserMode);
91             return this;
92         }
93
94         public Builder retainDeclarationReferences(final boolean newRetainDeclarationReferences) {
95             this.retainDeclarationReferences = newRetainDeclarationReferences;
96             return this;
97         }
98     }
99 }