2 * Copyright (c) 2018 Pantheon Technologies, s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.common;
10 import static java.util.Objects.requireNonNull;
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.Immutable;
22 import org.opendaylight.yangtools.concepts.Variant;
25 * A violation of a {@link CanonicalValue} validation. Contains details as mandated by RFC7950 Section 8.3.1.
27 * @author Robert Varga
31 public abstract class CanonicalValueViolation implements Immutable, Serializable {
32 public static class Regular extends CanonicalValueViolation {
33 private static final long serialVersionUID = 1L;
35 private final @Nullable String appTag;
36 private final @Nullable String message;
38 Regular(final @Nullable String appTag, final @Nullable String message) {
40 this.message = message;
44 @Nullable String appTag() {
49 @Nullable String message() {
54 ToStringHelper addToString(final ToStringHelper helper) {
55 return helper.omitNullValues().add("app-tag", appTag).add("message", message);
59 @SuppressFBWarnings("NM_CLASS_NOT_EXCEPTION")
60 public static class WithException extends CanonicalValueViolation {
61 private static final long serialVersionUID = 1L;
63 private final Exception cause;
65 WithException(final Exception cause) {
66 this.cause = requireNonNull(cause);
70 @SuppressFBWarnings("NP_NONNULL_RETURN_VIOLATION")
71 @Nullable String appTag() {
76 @Nullable String message() {
77 return cause.getMessage();
80 public final Exception getCause() {
85 ToStringHelper addToString(final ToStringHelper helper) {
86 return helper.add("cause", cause);
90 private static final CanonicalValueViolation EMPTY = new Regular(null, null);
91 private static final Variant<?, CanonicalValueViolation> EMPTY_VARIANT = Variant.ofSecond(EMPTY);
92 private static final long serialVersionUID = 1L;
94 public static CanonicalValueViolation empty() {
98 public static CanonicalValueViolation of(final Exception cause) {
99 return new WithException(cause);
102 public static CanonicalValueViolation of(final @Nullable String appTag, final @Nullable String message) {
103 return appTag == null && message == null ? EMPTY : new Regular(appTag, message);
106 @SuppressWarnings("unchecked")
107 public static <T> Variant<T, CanonicalValueViolation> emptyVariant() {
108 return (Variant<T, CanonicalValueViolation>) EMPTY_VARIANT;
111 public static <T> Variant<T, CanonicalValueViolation> variantOf(final Exception cause) {
112 return Variant.ofSecond(CanonicalValueViolation.of(cause));
115 public static <T> Variant<T, CanonicalValueViolation> variantOf(final String message) {
116 return variantOf(null, message);
119 public static <T> Variant<T, CanonicalValueViolation> variantOf(final @Nullable String appTag,
120 final String message) {
121 return Variant.ofSecond(CanonicalValueViolation.of(appTag, message));
124 public final Optional<String> getAppTag() {
125 return nullableString(appTag());
128 public final Optional<String> getMessage() {
129 return nullableString(appTag());
132 abstract @Nullable String appTag();
134 abstract @Nullable String message();
137 public final int hashCode() {
138 return Objects.hash(appTag(), message());
142 public final boolean equals(final @Nullable Object obj) {
146 if (!(obj instanceof CanonicalValueViolation)) {
149 final CanonicalValueViolation other = (CanonicalValueViolation) obj;
150 return Objects.equals(appTag(), other.appTag()) && Objects.equals(message(), other.message());
154 public final String toString() {
155 return addToString(MoreObjects.toStringHelper(this)).toString();
158 abstract ToStringHelper addToString(ToStringHelper helper);
160 private static Optional<String> nullableString(@Nullable final String str) {
161 return str != null ? Optional.of(str) : Optional.empty();