2 * Copyright (c) 2016 Cisco Systems, Inc. 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 java.io.Externalizable;
13 import java.io.IOException;
14 import java.io.ObjectInput;
15 import java.io.ObjectOutput;
16 import java.io.Serial;
17 import java.io.Serializable;
18 import java.time.format.DateTimeFormatter;
19 import java.time.format.DateTimeParseException;
20 import java.util.Optional;
21 import java.util.regex.Pattern;
22 import org.checkerframework.checker.regex.qual.Regex;
23 import org.eclipse.jdt.annotation.NonNull;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.opendaylight.yangtools.concepts.Immutable;
28 * Dedicated object identifying a YANG module revision.
30 * <h2>API design note</h2>
31 * This class defines the contents of a revision statement, but modules do not require to have a revision (e.g. they
32 * have not started to keep track of revisions).
35 * APIs which involve this class should always transfer instances via {@code Optional<Revision>}, which is
36 * the primary bridge data type. Implementations can use nullable fields with explicit conversions to/from
37 * {@link Optional}. Both patterns can take advantage of {@link #compare(Optional, Optional)} and
38 * {@link #compare(Revision, Revision)} respectively.
40 * @author Robert Varga
42 public final class Revision implements Comparable<Revision>, Immutable, Serializable {
43 // Note: since we are using writeReplace() this version is not significant.
45 private static final long serialVersionUID = 1L;
47 private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");
50 // FIXME: we should improve this to filter incorrect dates -- see constructor.
51 private static final String STRING_FORMAT_PATTERN_STR = "\\d\\d\\d\\d\\-\\d\\d-\\d\\d";
54 * String format pattern, which can be used to match parts of a string into components.
56 public static final Pattern STRING_FORMAT_PATTERN = Pattern.compile(STRING_FORMAT_PATTERN_STR);
59 * Revision which compares as greater than any other valid revision.
61 public static final Revision MAX_VALUE = Revision.of("9999-12-31");
63 private final @NonNull String str;
65 private Revision(final @NonNull String str) {
67 * According to RFC7950 (https://tools.ietf.org/html/rfc7950#section-7.1.9):
69 * The "revision" statement specifies the editorial revision history of
70 * the module, including the initial revision. A series of "revision"
71 * statements detail the changes in the module's definition. The
72 * argument is a date string in the format "YYYY-MM-DD", [...]
74 * Hence we use JDK-provided parsing faculties to parse the date.
81 * Parse a revision string.
83 * @param str String to be parsed
84 * @return A Revision instance.
85 * @throws DateTimeParseException if the string format does not conform specification.
86 * @throws NullPointerException if the string is null
88 public static @NonNull Revision of(final @NonNull String str) {
89 return new Revision(str);
93 * Parse a (potentially null) revision string. Null strings result result in {@link Optional#empty()}.
95 * @param str String to be parsed
96 * @return An optional Revision instance.
97 * @throws DateTimeParseException if the string format does not conform specification.
99 public static @NonNull Optional<Revision> ofNullable(final @Nullable String str) {
100 return str == null ? Optional.empty() : Optional.of(new Revision(str));
104 * Compare two {@link Optional}s wrapping Revisions. Arguments and return value are consistent with
105 * {@link java.util.Comparator#compare(Object, Object)} interface contract. Missing revisions compare as lower
106 * than any other revision.
108 * @param first First optional revision
109 * @param second Second optional revision
110 * @return Positive, zero, or negative integer.
112 public static int compare(final @NonNull Optional<Revision> first, final @NonNull Optional<Revision> second) {
113 if (first.isPresent()) {
114 return second.isPresent() ? first.get().compareTo(second.get()) : 1;
116 return second.isPresent() ? -1 : 0;
120 * Compare two explicitly nullable Revisions. Unlike {@link #compareTo(Revision)}, this handles both arguments
121 * being null such that total ordering is defined.
123 * @param first First revision
124 * @param second Second revision
125 * @return Positive, zero, or negative integer.
127 public static int compare(final @Nullable Revision first, final @Nullable Revision second) {
129 return second != null ? first.compareTo(second) : 1;
131 return second != null ? -1 : 0;
135 @SuppressWarnings("checkstyle:parameterName")
136 public int compareTo(final Revision o) {
137 // Since all strings conform to the format, we can use their comparable property to do the correct thing
138 // with respect to temporal ordering.
139 return str.compareTo(o.str);
143 public int hashCode() {
144 return str.hashCode();
148 public boolean equals(final Object obj) {
149 return this == obj || obj instanceof Revision other && str.equals(other.str);
153 public String toString() {
158 Object writeReplace() {
159 return new Proxy(str);
162 private static final class Proxy implements Externalizable {
164 private static final long serialVersionUID = 1L;
168 @SuppressWarnings("checkstyle:redundantModifier")
170 // For Externalizable
173 Proxy(final String str) {
174 this.str = requireNonNull(str);
178 public void writeExternal(final ObjectOutput out) throws IOException {
179 out.writeObject(str);
183 public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
184 str = (String) in.readObject();
188 private Object readResolve() {
189 return Revision.of(requireNonNull(str));