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 com.google.common.base.Preconditions;
11 import java.io.Externalizable;
12 import java.io.IOException;
13 import java.io.ObjectInput;
14 import java.io.ObjectOutput;
15 import java.io.Serializable;
16 import java.text.ParseException;
17 import java.util.Date;
18 import javax.annotation.Nonnull;
21 * Dedicated object identifying a YANG module revision.
23 * @author Robert Varga
25 public abstract class Revision implements Comparable<Revision>, Serializable {
26 private static final long serialVersionUID = 1L;
29 * Legacy implementation.
31 * @author Robert Varga
33 private static final class ForDate extends Revision {
34 private static final long serialVersionUID = 1L;
36 private final Date date;
39 ForDate(final Date date) {
40 this.date = Preconditions.checkNotNull(date);
43 ForDate(final Date date, final String str) {
44 this.date = Preconditions.checkNotNull(date);
45 this.str = Preconditions.checkNotNull(str);
49 public Date toDate() {
54 public String toString() {
60 ret = SimpleDateFormatUtil.getRevisionFormat().format(date);
70 private static final class Proxy implements Externalizable {
71 private static final long serialVersionUID = 1L;
75 @SuppressWarnings("checkstyle:redundantModifier")
80 Proxy(final String str) {
81 this.str = Preconditions.checkNotNull(str);
85 public void writeExternal(final ObjectOutput out) throws IOException {
90 public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
91 str = (String) in.readObject();
94 private Object readResolve() {
96 return Revision.forString(str);
97 } catch (ParseException e) {
98 throw new RuntimeException(e);
104 // Hidden from the world
108 * Convert a Date into a Revision.
110 * @param date Input date
111 * @return A Revision instance.
113 * @deprecated Transition bridge method to ease transition from Date.
116 public static Revision forDate(@Nonnull final Date date) {
117 return new ForDate(date);
121 * Parse a revision string.
123 * @param str String to be parsed
124 * @return A Revision instance.
125 * @throws ParseException if the string format does not conform specification.
127 public static Revision forString(@Nonnull final String str) throws ParseException {
128 final Date date = SimpleDateFormatUtil.getRevisionFormat().parse(str);
129 return new ForDate(date, str);
133 @SuppressWarnings("checkstyle:parameterName")
134 public final int compareTo(final Revision o) {
135 return toDate().compareTo(o.toDate());
139 * Convert this Revision to a Date object. The returned Date will be in UTC.
141 * @return Data representation of this Revision
143 * @deprecated Transition bridge method to ease transition from Date.
146 @Nonnull public abstract Date toDate();
149 public abstract String toString();
151 final Object writeReplace() {
152 return new Proxy(toString());