forked from rcseacord/JavaSCR
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPeriod.java
More file actions
93 lines (79 loc) · 2.86 KB
/
Copy pathPeriod.java
File metadata and controls
93 lines (79 loc) · 2.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package ser07j;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InvalidObjectException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Date;
public final class Period implements Serializable {
private static final long serialVersionUID = 1639369965306260328L;
private final Date start;
private final Date end;
/**
* @param start
* the beginning of the period
* @param end
* the end of the period; must not precede start
* @throws IllegalArgumentException
* if start is after end
* @throws NullPointerException
* if start or end is null
*/
public Period(Date start, Date end) {
this.start = new Date(start.getTime());
this.end = new Date(end.getTime());
if (this.start.compareTo(this.end) > 0)
throw new IllegalArgumentException(start + " after " + end);
}
public Date start() {
return new Date(this.start.getTime());
}
public Date end() {
return new Date(this.end.getTime());
}
@Override
public String toString() {
return this.start + " - " + this.end;
}
// Serialization proxy for Period class
private static class SerializationProxy implements Serializable {
private static final long serialVersionUID = 234098243823485285L;
private final Date start;
private final Date end;
SerializationProxy(Period p) {
// copy argument data without consistency checking or defensive copying
this.start = p.start;
this.end = p.end;
}
// readResolve method for Period.SerializationProxy
private Object readResolve() {
return new Period(this.start, this.end); // Uses public constructor
}
}
// writeReplace method for the serialization proxy pattern
// Method can be copied verbatim into any class with a serialization proxy.
private Object writeReplace() {
return new SerializationProxy(this);
}
// readObject method for the serialization proxy pattern
private void readObject(@SuppressWarnings("unused") ObjectInputStream stream) throws InvalidObjectException {
throw new InvalidObjectException("Proxy required");
}
// The rest of this is to serialize/deserialize
private static Object deserialize(byte[] bytes) throws Exception {
return new ObjectInputStream(new ByteArrayInputStream(bytes)).readObject();
}
private static byte[] serialize(Object o) throws IOException {
try (ByteArrayOutputStream ba = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(ba)) {
oos.writeObject(o);
return ba.toByteArray();
}
}
public static void main(String[] args) throws Exception {
Period period = (Period) deserialize(serialize(new Period(new Date(), new Date())));
System.out.println(period.toString());
}
}