2 * Copyright (c) 2024 PANTHEON.tech, 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 java.io.Serializable;
11 import java.util.NoSuchElementException;
12 import java.util.Optional;
13 import org.eclipse.jdt.annotation.NonNullByDefault;
14 import org.eclipse.jdt.annotation.Nullable;
15 import org.opendaylight.yangtools.concepts.Immutable;
18 * A capture of an optional {@code revision-date}. This is a replacement for {@code Optional<Revision>}, with the added
19 * benefit of having a non-null string representation in {@link #unionString()}, which is also conveniently returned
20 * from {@link #toString()}.
23 * This contract is exactly the same as this fragment from {@code ietf-yang-library}:
26 * type revision-identifier;
34 public sealed interface RevisionUnion extends Comparable<RevisionUnion>, Immutable, Serializable
35 permits Revision, NotRevision {
37 * Return empty {@link RevisionUnion}.
39 * @return empty {@link RevisionUnion}
41 static NotRevision none() {
42 return NotRevision.of();
45 static RevisionUnion of(final String unionString) {
46 return unionString.isEmpty() ? none() : Revision.of(unionString);
50 * A {@code revision-date}-compliant date, or an empty string ({@code ""}).
52 * @return A revision-date or empty string
57 @SuppressWarnings("checkstyle:parameterName")
58 default int compareTo(final RevisionUnion o) {
59 // Since all strings conform to the format, we can use their comparable property to do the correct thing
60 // with respect to temporal ordering.
61 return unionString().compareTo(o.unionString());
65 * Return the {@link Revision}, if present.
67 * @return the revision, or {@code null} if not present
69 @Nullable Revision revision();
71 default Optional<Revision> findRevision() {
72 return Optional.ofNullable(revision());
75 default Revision getRevision() {
76 final var revision = revision();
77 if (revision == null) {
78 throw new NoSuchElementException();
87 boolean equals(@Nullable Object obj);
90 * Returns {@link #unionString()}.
92 * @return {@link #unionString()}