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.Serializable;
17 import java.text.ParseException;
18 import java.util.Date;
19 import javax.annotation.Nonnull;
22 * Dedicated object identifying a YANG module revision.
24 * @author Robert Varga
26 public abstract class Revision implements Comparable<Revision>, Serializable {
27 private static final long serialVersionUID = 1L;
30 * Legacy implementation.
32 * @author Robert Varga
34 private static final class ForDate extends Revision {
35 private static final long serialVersionUID = 1L;
37 private final Date date;
40 ForDate(final Date date) {
41 this.date = requireNonNull(date);
44 ForDate(final Date date, final String str) {
45 this.date = requireNonNull(date);
46 this.str = requireNonNull(str);
50 public Date toDate() {
55 public String toString() {
61 ret = SimpleDateFormatUtil.getRevisionFormat().format(date);
71 private static final class Proxy implements Externalizable {
72 private static final long serialVersionUID = 1L;
76 @SuppressWarnings("checkstyle:redundantModifier")
81 Proxy(final String str) {
82 this.str = requireNonNull(str);
86 public void writeExternal(final ObjectOutput out) throws IOException {
91 public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
92 str = (String) in.readObject();
95 private Object readResolve() {
97 return Revision.forString(str);
98 } catch (ParseException e) {
99 throw new RuntimeException(e);
105 // Hidden from the world
109 * Convert a Date into a Revision.
111 * @param date Input date
112 * @return A Revision instance.
114 * @deprecated Transition bridge method to ease transition from Date.
117 public static Revision forDate(@Nonnull final Date date) {
118 return new ForDate(date);
122 * Parse a revision string.
124 * @param str String to be parsed
125 * @return A Revision instance.
126 * @throws ParseException if the string format does not conform specification.
128 public static Revision forString(@Nonnull final String str) throws ParseException {
129 final Date date = SimpleDateFormatUtil.getRevisionFormat().parse(str);
130 return new ForDate(date, str);
134 @SuppressWarnings("checkstyle:parameterName")
135 public final int compareTo(final Revision o) {
136 return toDate().compareTo(o.toDate());
140 * Convert this Revision to a Date object. The returned Date will be in UTC.
142 * @return Data representation of this Revision
144 * @deprecated Transition bridge method to ease transition from Date.
147 @Nonnull public abstract Date toDate();
150 public abstract String toString();
152 final Object writeReplace() {
153 return new Proxy(toString());