Cleanup use of Guava library
[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 static java.util.Objects.requireNonNull;
11
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 = requireNonNull(date);
42         }
43
44         ForDate(final Date date, final String str) {
45             this.date = requireNonNull(date);
46             this.str = requireNonNull(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         @SuppressWarnings("checkstyle:redundantModifier")
77         public Proxy() {
78             // For Externalizable
79         }
80
81         Proxy(final String str) {
82             this.str = requireNonNull(str);
83         }
84
85         @Override
86         public void writeExternal(final ObjectOutput out) throws IOException {
87             out.writeObject(str);
88         }
89
90         @Override
91         public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
92             str = (String) in.readObject();
93         }
94
95         private Object readResolve() {
96             try {
97                 return Revision.forString(str);
98             } catch (ParseException e) {
99                 throw new RuntimeException(e);
100             }
101         }
102     }
103
104     Revision() {
105         // Hidden from the world
106     }
107
108     /**
109      * Convert a Date into a Revision.
110      *
111      * @param date Input date
112      * @return A Revision instance.
113      *
114      * @deprecated Transition bridge method to ease transition from Date.
115      */
116     @Deprecated
117     public static Revision forDate(@Nonnull final Date date) {
118         return new ForDate(date);
119     }
120
121     /**
122      * Parse a revision string.
123      *
124      * @param str String to be parsed
125      * @return A Revision instance.
126      * @throws ParseException if the string format does not conform specification.
127      */
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);
131     }
132
133     @Override
134     @SuppressWarnings("checkstyle:parameterName")
135     public final int compareTo(final Revision o) {
136         return toDate().compareTo(o.toDate());
137     }
138
139     /**
140      * Convert this Revision to a Date object. The returned Date will be in UTC.
141      *
142      * @return Data representation of this Revision
143      *
144      * @deprecated Transition bridge method to ease transition from Date.
145      */
146     @Deprecated
147     @Nonnull public abstract Date toDate();
148
149     @Override
150     public abstract String toString();
151
152     final Object writeReplace() {
153         return new Proxy(toString());
154     }
155 }