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 com.google.common.base.Throwables;
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 = Preconditions.checkNotNull(date);
44 ForDate(final Date date, final String str) {
45 this.date = Preconditions.checkNotNull(date);
46 this.str = Preconditions.checkNotNull(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;
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 Throwables.propagate(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 public final int compareTo(final Revision o) {
134 return toDate().compareTo(o.toDate());
138 * Convert this Revision to a Date object. The returned Date will be in UTC.
140 * @return Data representation of this Revision
142 * @deprecated Transition bridge method to ease transition from Date.
145 @Nonnull public abstract Date toDate();
148 public abstract String toString();
150 final Object writeReplace() {
151 return new Proxy(toString());