Fix CanonicalValueViolation.getMessage()
[yangtools.git] / common / yang-common / src / main / java / org / opendaylight / yangtools / yang / common / CanonicalValueViolation.java
1 /*
2  * Copyright (c) 2018 Pantheon Technologies, 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.common;
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 com.google.common.base.MoreObjects.ToStringHelper;
15 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
16 import java.io.Serializable;
17 import java.util.Objects;
18 import java.util.Optional;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.opendaylight.yangtools.concepts.Either;
22 import org.opendaylight.yangtools.concepts.Immutable;
23
24 /**
25  * A violation of a {@link CanonicalValue} validation. Contains details as mandated by RFC7950 Section 8.3.1.
26  *
27  * @author Robert Varga
28  */
29 @Beta
30 @NonNullByDefault
31 public abstract class CanonicalValueViolation implements Immutable, Serializable {
32     public static class Regular extends CanonicalValueViolation {
33         private static final long serialVersionUID = 1L;
34
35         private final @Nullable String appTag;
36         private final @Nullable String message;
37
38         Regular(final @Nullable String appTag, final @Nullable String message) {
39             this.appTag = appTag;
40             this.message = message;
41         }
42
43         @Override
44         @Nullable String appTag() {
45             return appTag;
46         }
47
48         @Override
49         @Nullable String message() {
50             return message;
51         }
52
53         @Override
54         ToStringHelper addToString(final ToStringHelper helper) {
55             return helper.omitNullValues().add("app-tag", appTag).add("message", message);
56         }
57     }
58
59     @SuppressFBWarnings("NM_CLASS_NOT_EXCEPTION")
60     public static class WithException extends CanonicalValueViolation {
61         private static final long serialVersionUID = 1L;
62
63         private final Exception cause;
64
65         WithException(final Exception cause) {
66             this.cause = requireNonNull(cause);
67         }
68
69         @Override
70         @SuppressFBWarnings("NP_NONNULL_RETURN_VIOLATION")
71         @Nullable String appTag() {
72             return null;
73         }
74
75         @Override
76         @Nullable String message() {
77             return cause.getMessage();
78         }
79
80         public final Exception getCause() {
81             return cause;
82         }
83
84         @Override
85         ToStringHelper addToString(final ToStringHelper helper) {
86             return helper.add("cause", cause);
87         }
88     }
89
90     private static final CanonicalValueViolation EMPTY = new Regular(null, null);
91     private static final Either<?, CanonicalValueViolation> EMPTY_VARIANT = Either.ofSecond(EMPTY);
92     private static final long serialVersionUID = 1L;
93
94     public static CanonicalValueViolation empty() {
95         return EMPTY;
96     }
97
98     public static CanonicalValueViolation of(final Exception cause) {
99         return new WithException(cause);
100     }
101
102     public static CanonicalValueViolation of(final @Nullable String appTag, final @Nullable String message) {
103         return appTag == null && message == null ? EMPTY : new Regular(appTag, message);
104     }
105
106     @SuppressWarnings("unchecked")
107     public static <T> Either<T, CanonicalValueViolation> emptyVariant() {
108         return (Either<T, CanonicalValueViolation>) EMPTY_VARIANT;
109     }
110
111     public static <T> Either<T, CanonicalValueViolation> variantOf(final Exception cause) {
112         return Either.ofSecond(CanonicalValueViolation.of(cause));
113     }
114
115     public static <T> Either<T, CanonicalValueViolation> variantOf(final String message) {
116         return variantOf(null, message);
117     }
118
119     public static <T> Either<T, CanonicalValueViolation> variantOf(final @Nullable String appTag,
120             final String message) {
121         return Either.ofSecond(CanonicalValueViolation.of(appTag, message));
122     }
123
124     public final Optional<String> getAppTag() {
125         return nullableString(appTag());
126     }
127
128     public final Optional<String> getMessage() {
129         return nullableString(message());
130     }
131
132     abstract @Nullable String appTag();
133
134     abstract @Nullable String message();
135
136     @Override
137     public final int hashCode() {
138         return Objects.hash(appTag(), message());
139     }
140
141     @Override
142     public final boolean equals(final @Nullable Object obj) {
143         if (this == obj) {
144             return true;
145         }
146         if (!(obj instanceof CanonicalValueViolation)) {
147             return false;
148         }
149         final CanonicalValueViolation other = (CanonicalValueViolation) obj;
150         return Objects.equals(appTag(), other.appTag()) && Objects.equals(message(), other.message());
151     }
152
153     @Override
154     public final String toString() {
155         return addToString(MoreObjects.toStringHelper(this)).toString();
156     }
157
158     abstract ToStringHelper addToString(ToStringHelper helper);
159
160     private static Optional<String> nullableString(@Nullable final String str) {
161         return str != null ? Optional.of(str) : Optional.empty();
162     }
163 }