3514e312934a3fc95bb3e3addfea3ba9a0a4baf1
[yangtools.git] / parser / yang-parser-api / src / main / java / org / opendaylight / yangtools / yang / 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.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.concepts.Mutable;
19
20 /**
21  * A configuration of {@link YangParser} wiring for use with {@link YangParserFactory}.
22  */
23 @NonNullByDefault
24 public final class YangParserConfiguration implements Immutable {
25     /**
26      * System-wide default configuration.
27      */
28     public static final YangParserConfiguration DEFAULT = builder().build();
29
30     private final ImportResolutionMode importResolutionMode;
31     private final boolean retainDeclarationReferences;
32
33     private YangParserConfiguration(final ImportResolutionMode importResolutionMode,
34             final boolean retainDeclarationReferences) {
35         this.importResolutionMode = requireNonNull(importResolutionMode);
36         this.retainDeclarationReferences = retainDeclarationReferences;
37     }
38
39     @Beta
40     public ImportResolutionMode importResolutionMode() {
41         return importResolutionMode;
42     }
43
44     public boolean retainDeclarationReferences() {
45         return retainDeclarationReferences;
46     }
47
48     @Override
49     public int hashCode() {
50         return Objects.hash(importResolutionMode, retainDeclarationReferences);
51     }
52
53     @Override
54     public boolean equals(final @Nullable Object obj) {
55         if (this == obj) {
56             return true;
57         }
58         if (!(obj instanceof YangParserConfiguration)) {
59             return false;
60         }
61         final YangParserConfiguration other = (YangParserConfiguration) obj;
62         return importResolutionMode == other.importResolutionMode
63             && retainDeclarationReferences == other.retainDeclarationReferences;
64     }
65
66     @Override
67     public String toString() {
68         return MoreObjects.toStringHelper(this)
69             .add("importResolution", importResolutionMode)
70             .add("declarationReferences", retainDeclarationReferences)
71             .toString();
72     }
73
74     public static Builder builder() {
75         return new Builder();
76     }
77
78     public static final class Builder implements Mutable {
79         private ImportResolutionMode importResolutionMode = ImportResolutionMode.DEFAULT;
80         private boolean retainDeclarationReferences = false;
81
82         private Builder() {
83             // Hidden on purpose
84         }
85
86         /**
87          * Return a {@link YangParserConfiguration} initialized with contents of this builder.
88          *
89          * @return A YangParserConfiguration
90          */
91         public YangParserConfiguration build() {
92             return new YangParserConfiguration(importResolutionMode, retainDeclarationReferences);
93         }
94
95         @Beta
96         public Builder importResolutionMode(final ImportResolutionMode newImportResolutionMode) {
97             importResolutionMode = requireNonNull(newImportResolutionMode);
98             return this;
99         }
100
101         public Builder retainDeclarationReferences(final boolean newRetainDeclarationReferences) {
102             retainDeclarationReferences = newRetainDeclarationReferences;
103             return this;
104         }
105     }
106 }