a5a59acd86d292853e5905a5f3147921b8808300
[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 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;
19
20 /**
21  * Dedicated object identifying a YANG module revision.
22  *
23  * @author Robert Varga
24  */
25 public abstract class Revision implements Comparable<Revision>, Serializable {
26     private static final long serialVersionUID = 1L;
27
28     /**
29      * Legacy implementation.
30      *
31      * @author Robert Varga
32      */
33     private static final class ForDate extends Revision {
34         private static final long serialVersionUID = 1L;
35
36         private final Date date;
37         private String str;
38
39         ForDate(final Date date) {
40             this.date = Preconditions.checkNotNull(date);
41         }
42
43         ForDate(final Date date, final String str) {
44             this.date = Preconditions.checkNotNull(date);
45             this.str = Preconditions.checkNotNull(str);
46         }
47
48         @Override
49         public Date toDate() {
50             return date;
51         }
52
53         @Override
54         public String toString() {
55             String ret = str;
56             if (ret == null) {
57                 synchronized (this) {
58                     ret = str;
59                     if (ret == null) {
60                         ret = SimpleDateFormatUtil.getRevisionFormat().format(date);
61                         str = ret;
62                     }
63                 }
64             }
65
66             return ret;
67         }
68     }
69
70     private static final class Proxy implements Externalizable {
71         private static final long serialVersionUID = 1L;
72
73         private String str;
74
75         @SuppressWarnings("checkstyle:redundantModifier")
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 new RuntimeException(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     @SuppressWarnings("checkstyle:parameterName")
134     public final int compareTo(final Revision o) {
135         return toDate().compareTo(o.toDate());
136     }
137
138     /**
139      * Convert this Revision to a Date object. The returned Date will be in UTC.
140      *
141      * @return Data representation of this Revision
142      *
143      * @deprecated Transition bridge method to ease transition from Date.
144      */
145     @Deprecated
146     @Nonnull public abstract Date toDate();
147
148     @Override
149     public abstract String toString();
150
151     final Object writeReplace() {
152         return new Proxy(toString());
153     }
154 }