Serial.java
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.moea;
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.2
31  @since 5.2
32  */
33 final class Serial implements Externalizable {
34 
35     private static final long serialVersionUID = 1;
36 
37     static final byte SIMPLE_INT_VEC = 1;
38     static final byte SIMPLE_LONG_VEC = 2;
39     static final byte SIMPLE_DOUBLE_VEC = 3;
40 
41     /**
42      * The type being serialized.
43      */
44     private byte _type;
45 
46     /**
47      * The object being serialized.
48      */
49     private Object _object;
50 
51     /**
52      * Constructor for deserialization.
53      */
54     public Serial() {
55     }
56 
57     /**
58      * Creates an instance for serialization.
59      *
60      @param type  the type
61      @param object  the object
62      */
63     Serial(final byte type, final Object object) {
64         _type = type;
65         _object = object;
66     }
67 
68     @Override
69     public void writeExternal(final ObjectOutput outthrows IOException {
70         out.writeByte(_type);
71         switch (_type) {
72             case SIMPLE_INT_VEC: ((SimpleIntVec)_object).write(out)break;
73             case SIMPLE_LONG_VEC: ((SimpleLongVec)_object).write(out)break;
74             case SIMPLE_DOUBLE_VEC: ((SimpleDoubleVec)_object).write(out)break;
75             default:
76                 throw new StreamCorruptedException("Unknown serialized type.");
77         }
78     }
79 
80     @Override
81     public void readExternal(final ObjectInput in)
82         throws IOException
83     {
84         _type = in.readByte();
85         switch (_type) {
86             case SIMPLE_INT_VEC: _object = SimpleIntVec.read(in)break;
87             case SIMPLE_LONG_VEC: _object = SimpleLongVec.read(in)break;
88             case SIMPLE_DOUBLE_VEC: _object = SimpleDoubleVec.read(in)break;
89             default:
90                 throw new StreamCorruptedException("Unknown serialized type.");
91         }
92     }
93 
94     private Object readResolve() {
95         return _object;
96     }
97 
98 }