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 package com.cedarsoft.serialization;
33
34 import com.cedarsoft.version.Version;
35 import com.cedarsoft.version.VersionMismatchException;
36 import com.cedarsoft.version.VersionRange;
37
38 import javax.annotation.Nonnull;
39 import java.io.ByteArrayOutputStream;
40 import java.io.IOException;
41
42
43
44
45
46
47
48
49
50 public abstract class AbstractSerializer<T, S, D, E extends Throwable> implements PluggableSerializer<T, S, D, E> {
51 @Nonnull
52 private final VersionRange formatVersionRange;
53
54 @Nonnull
55 protected final DelegatesMappings<S, D, E> delegatesMappings;
56
57
58
59
60
61
62 protected AbstractSerializer( @Nonnull VersionRange formatVersionRange ) {
63 this.formatVersionRange = formatVersionRange;
64 this.delegatesMappings = new DelegatesMappings<S, D, E>( formatVersionRange );
65 }
66
67 @Override
68 @Nonnull
69 public Version getFormatVersion() {
70 return formatVersionRange.getMax();
71 }
72
73
74
75
76
77
78 protected void verifyVersionReadable( @Nonnull Version formatVersion ) {
79 if ( !isVersionReadable( formatVersion ) ) {
80 throw new VersionMismatchException( getFormatVersionRange(), formatVersion );
81 }
82 }
83
84 public boolean isVersionReadable( @Nonnull Version formatVersion ) {
85 return getFormatVersionRange().contains( formatVersion );
86 }
87
88
89
90
91
92
93 protected void verifyVersionWritable( @Nonnull Version formatVersion ) {
94 if ( !isVersionWritable( formatVersion ) ) {
95 throw new VersionMismatchException( getFormatVersion(), formatVersion );
96 }
97 }
98
99 public boolean isVersionWritable( @Nonnull Version formatVersion ) {
100 return getFormatVersion().equals( formatVersion );
101 }
102
103 @Override
104 @Nonnull
105 public VersionRange getFormatVersionRange() {
106 return formatVersionRange;
107 }
108
109
110
111
112
113
114
115
116
117 @Nonnull
118 public byte[] serializeToByteArray( @Nonnull T object ) throws IOException {
119 ByteArrayOutputStream out = new ByteArrayOutputStream();
120 serialize( object, out );
121 return out.toByteArray();
122 }
123
124 @Nonnull
125 public DelegatesMappings<S, D, E> getDelegatesMappings() {
126 return delegatesMappings;
127 }
128
129
130
131 @Nonnull
132 public <T> DelegatesMappings<S, D, E>.FluentFactory<T> add( @Nonnull PluggableSerializer<? super T, S, D, E> pluggableSerializer ) {
133 return delegatesMappings.add( pluggableSerializer );
134 }
135
136 public <T> void serialize( @Nonnull T object, @Nonnull Class<T> type, @Nonnull S deserializeTo, @Nonnull Version formatVersion ) throws E, IOException {
137 delegatesMappings.serialize( object, type, deserializeTo, formatVersion );
138 }
139
140 @Nonnull
141 public <T> PluggableSerializer<? super T, S, D, E> getSerializer( @Nonnull Class<T> type ) {
142 return delegatesMappings.getSerializer( type );
143 }
144
145 @Nonnull
146 public <T> T deserialize( @Nonnull Class<T> type, @Nonnull Version formatVersion, @Nonnull D deserializeFrom ) throws E, IOException {
147 return delegatesMappings.deserialize( type, formatVersion, deserializeFrom );
148 }
149
150
151
152
153
154
155
156 protected static void verifyDelegatingSerializerVersion( @Nonnull Serializer<?> delegate, @Nonnull Version expectedFormatVersion ) {
157 Version actualVersion = delegate.getFormatVersion();
158 if ( !actualVersion.equals( expectedFormatVersion ) ) {
159 throw new IllegalArgumentException( "Invalid versions. Expected <" + expectedFormatVersion + "> but was <" + actualVersion + ">" );
160 }
161 }
162 }