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.DataInput;
11 import java.io.DataOutput;
12 import java.io.IOException;
13 import java.io.Serializable;
14 import java.util.NoSuchElementException;
15 import java.util.Optional;
16 import org.eclipse.jdt.annotation.NonNullByDefault;
17 import org.eclipse.jdt.annotation.Nullable;
18 import org.opendaylight.yangtools.concepts.Immutable;
19 import org.opendaylight.yangtools.concepts.WritableObject;
22 * A capture of an optional {@code revision-date}. This is a replacement for {@code Optional<Revision>}, with the added
23 * benefit of having a non-null string representation in {@link #unionString()}, which is also conveniently returned
24 * from {@link #toString()}.
27 * This contract is exactly the same as this fragment from {@code ietf-yang-library}:
30 * type revision-identifier;
38 public sealed interface RevisionUnion extends Comparable<RevisionUnion>, Immutable, Serializable, WritableObject
39 permits Revision, NotRevision {
41 * Return empty {@link RevisionUnion}.
43 * @return empty {@link RevisionUnion}
45 static NotRevision none() {
46 return NotRevision.of();
49 static RevisionUnion of(final String unionString) {
50 return unionString.isEmpty() ? none() : Revision.of(unionString);
53 static RevisionUnion of(final @Nullable Revision revision) {
54 return revision != null ? revision : none();
58 * A {@code revision-date}-compliant date, or an empty string ({@code ""}).
60 * @return A revision-date or empty string
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();
84 @SuppressWarnings("checkstyle:parameterName")
85 default int compareTo(final RevisionUnion o) {
86 // Since all strings conform to the format, we can use their comparable property to do the correct thing
87 // with respect to temporal ordering.
88 return unionString().compareTo(o.unionString());
92 default void writeTo(final DataOutput out) throws IOException {
93 out.writeUTF(unionString());
96 static RevisionUnion readFrom(final DataInput in) throws IOException {
97 final var unionString = in.readUTF();
98 return unionString.isEmpty() ? none() : Revision.ofRead(unionString);
105 boolean equals(@Nullable Object obj);
108 * Returns {@link #unionString()}.
110 * @return {@link #unionString()}