8915b08a3eb72e1825efedb8653d8b9e55053607
[yangtools.git] / yang / yang-common / src / main / java / org / opendaylight / yangtools / yang / common / Revision.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.common;
9
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;
20
21 /**
22  * Dedicated object identifying a YANG module revision.
23  *
24  * @author Robert Varga
25  */
26 public abstract class Revision implements Comparable<Revision>, Serializable {
27     private static final long serialVersionUID = 1L;
28
29     /**
30      * Legacy implementation.
31      *
32      * @author Robert Varga
33      */
34     private static final class ForDate extends Revision {
35         private static final long serialVersionUID = 1L;
36
37         private final Date date;
38         private String str;
39
40         ForDate(final Date date) {
41             this.date = Preconditions.checkNotNull(date);
42         }
43
44         ForDate(final Date date, final String str) {
45             this.date = Preconditions.checkNotNull(date);
46             this.str = Preconditions.checkNotNull(str);
47         }
48
49         @Override
50         public Date toDate() {
51             return date;
52         }
53
54         @Override
55         public String toString() {
56             String ret = str;
57             if (ret == null) {
58                 synchronized (this) {
59                     ret = str;
60                     if (ret == null) {
61                         ret = SimpleDateFormatUtil.getRevisionFormat().format(date);
62                         str = ret;
63                     }
64                 }
65             }
66
67             return ret;
68         }
69     }
70
71     private static final class Proxy implements Externalizable {
72         private static final long serialVersionUID = 1L;
73
74         private String str;
75
76         public Proxy() {
77             // For Externalizable
78         }
79
80         Proxy(final String str) {
81             this.str = Preconditions.checkNotNull(str);
82         }
83
84         @Override
85         public void writeExternal(final ObjectOutput out) throws IOException {
86             out.writeObject(str);
87         }
88
89         @Override
90         public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
91             str = (String) in.readObject();
92         }
93
94         private Object readResolve() {
95             try {
96                 return Revision.forString(str);
97             } catch (ParseException e) {
98                 throw Throwables.propagate(e);
99             }
100         }
101     }
102
103     Revision() {
104         // Hidden from the world
105     }
106
107     /**
108      * Convert a Date into a Revision.
109      *
110      * @param date Input date
111      * @return A Revision instance.
112      *
113      * @deprecated Transition bridge method to ease transition from Date.
114      */
115     @Deprecated
116     public static Revision forDate(@Nonnull final Date date) {
117         return new ForDate(date);
118     }
119
120     /**
121      * Parse a revision string.
122      *
123      * @param str String to be parsed
124      * @return A Revision instance.
125      * @throws ParseException if the string format does not conform specification.
126      */
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);
130     }
131
132     @Override
133     public final int compareTo(final Revision o) {
134         return toDate().compareTo(o.toDate());
135     }
136
137     /**
138      * Convert this Revision to a Date object. The returned Date will be in UTC.
139      *
140      * @return Data representation of this Revision
141      *
142      * @deprecated Transition bridge method to ease transition from Date.
143      */
144     @Deprecated
145     @Nonnull public abstract Date toDate();
146
147     @Override
148     public abstract String toString();
149
150     final Object writeReplace() {
151         return new Proxy(toString());
152     }
153 }