01 /*
02 * Java Genetic Algorithm Library (jenetics-5.2.0).
03 * Copyright (c) 2007-2020 Franz Wilhelmstötter
04 *
05 * Licensed under the Apache License, Version 2.0 (the "License");
06 * you may not use this file except in compliance with the License.
07 * You may obtain a copy of the License at
08 *
09 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 * Author:
18 * Franz Wilhelmstötter (franz.wilhelmstoetter@gmail.com)
19 */
20 package io.jenetics.ext;
21
22 import java.io.Externalizable;
23 import java.io.IOException;
24 import java.io.ObjectInput;
25 import java.io.ObjectOutput;
26 import java.io.StreamCorruptedException;
27
28 /**
29 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
30 * @version 5.0
31 * @since 5.0
32 */
33 final class Serial implements Externalizable {
34
35 private static final long serialVersionUID = 1;
36
37 static final byte BIG_INTEGER_CHROMOSOME = 1;
38
39 /**
40 * The type being serialized.
41 */
42 private byte _type;
43
44 /**
45 * The object being serialized.
46 */
47 private Object _object;
48
49 /**
50 * Constructor for deserialization.
51 */
52 public Serial() {
53 }
54
55 /**
56 * Creates an instance for serialization.
57 *
58 * @param type the type
59 * @param object the object
60 */
61 Serial(final byte type, final Object object) {
62 _type = type;
63 _object = object;
64 }
65
66 @Override
67 public void writeExternal(final ObjectOutput out) throws IOException {
68 out.writeByte(_type);
69 switch (_type) {
70 case BIG_INTEGER_CHROMOSOME: ((BigIntegerChromosome)_object).write(out); break;
71 default:
72 throw new StreamCorruptedException("Unknown serialized type.");
73 }
74 }
75
76 @Override
77 public void readExternal(final ObjectInput in) throws IOException {
78 _type = in.readByte();
79 switch (_type) {
80 case BIG_INTEGER_CHROMOSOME: _object = BigIntegerChromosome.read(in); break;
81 default:
82 throw new StreamCorruptedException("Unknown serialized type.");
83 }
84 }
85
86 private Object readResolve() {
87 return _object;
88 }
89
90 }
|