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.Serial;
17 import java.io.Serializable;
18 import java.util.Objects;
19 import java.util.Optional;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.opendaylight.yangtools.concepts.Either;
23 import org.opendaylight.yangtools.concepts.Immutable;
26 * A violation of a {@link CanonicalValue} validation. Contains details as mandated by RFC7950 Section 8.3.1.
28 * @author Robert Varga
32 public abstract class CanonicalValueViolation implements Immutable, Serializable {
33 public static class Regular extends CanonicalValueViolation {
35 private static final long serialVersionUID = 1L;
37 private final @Nullable String appTag;
38 private final @Nullable String message;
40 Regular(final @Nullable String appTag, final @Nullable String message) {
42 this.message = message;
46 @Nullable String appTag() {
51 @Nullable String message() {
56 ToStringHelper addToString(final ToStringHelper helper) {
57 return helper.omitNullValues().add("app-tag", appTag).add("message", message);
61 @SuppressFBWarnings("NM_CLASS_NOT_EXCEPTION")
62 public static class WithException extends CanonicalValueViolation {
64 private static final long serialVersionUID = 1L;
66 private final Exception cause;
68 WithException(final Exception cause) {
69 this.cause = requireNonNull(cause);
73 @Nullable String appTag() {
78 @Nullable String message() {
79 return cause.getMessage();
82 public final Exception getCause() {
87 ToStringHelper addToString(final ToStringHelper helper) {
88 return helper.add("cause", cause);
92 private static final CanonicalValueViolation EMPTY = new Regular(null, null);
93 private static final Either<?, CanonicalValueViolation> EMPTY_VARIANT = Either.ofSecond(EMPTY);
95 private static final long serialVersionUID = 1L;
97 public static CanonicalValueViolation empty() {
101 public static CanonicalValueViolation of(final Exception cause) {
102 return new WithException(cause);
105 public static CanonicalValueViolation of(final @Nullable String appTag, final @Nullable String message) {
106 return appTag == null && message == null ? EMPTY : new Regular(appTag, message);
109 @SuppressWarnings("unchecked")
110 public static <T> Either<T, CanonicalValueViolation> emptyVariant() {
111 return (Either<T, CanonicalValueViolation>) EMPTY_VARIANT;
114 public static <T> Either<T, CanonicalValueViolation> variantOf(final Exception cause) {
115 return Either.ofSecond(CanonicalValueViolation.of(cause));
118 public static <T> Either<T, CanonicalValueViolation> variantOf(final String message) {
119 return variantOf(null, message);
122 public static <T> Either<T, CanonicalValueViolation> variantOf(final @Nullable String appTag,
123 final String message) {
124 return Either.ofSecond(CanonicalValueViolation.of(appTag, message));
127 public final Optional<String> getAppTag() {
128 return nullableString(appTag());
131 public final Optional<String> getMessage() {
132 return nullableString(message());
135 abstract @Nullable String appTag();
137 abstract @Nullable String message();
140 public final int hashCode() {
141 return Objects.hash(appTag(), message());
145 public final boolean equals(final @Nullable Object obj) {
146 return this == obj || obj instanceof CanonicalValueViolation other
147 && Objects.equals(appTag(), other.appTag()) && Objects.equals(message(), other.message());
151 public final String toString() {
152 return addToString(MoreObjects.toStringHelper(this)).toString();
155 abstract ToStringHelper addToString(ToStringHelper helper);
157 private static Optional<String> nullableString(@Nullable final String str) {
158 return str != null ? Optional.of(str) : Optional.empty();