-
Notifications
You must be signed in to change notification settings - Fork 150
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
202 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package io.jenetics.engine; | ||
|
||
import java.io.Externalizable; | ||
import java.io.IOException; | ||
import java.io.ObjectInput; | ||
import java.io.ObjectOutput; | ||
import java.io.StreamCorruptedException; | ||
|
||
/** | ||
* @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a> | ||
* @version !__version__! | ||
* @since !__version__! | ||
*/ | ||
final class Serial implements Externalizable { | ||
|
||
private static final long serialVersionUID = 1; | ||
|
||
static final byte EVOLUTION_PARAMS = 1; | ||
|
||
/** | ||
* The type being serialized. | ||
*/ | ||
private byte _type; | ||
|
||
/** | ||
* The object being serialized. | ||
*/ | ||
private Object _object; | ||
|
||
/** | ||
* Constructor for deserialization. | ||
*/ | ||
public Serial() { | ||
} | ||
|
||
/** | ||
* Creates an instance for serialization. | ||
* | ||
* @param type the type | ||
* @param object the object | ||
*/ | ||
Serial(final byte type, final Object object) { | ||
_type = type; | ||
_object = object; | ||
} | ||
|
||
@Override | ||
public void writeExternal(final ObjectOutput out) throws IOException { | ||
out.writeByte(_type); | ||
switch (_type) { | ||
case EVOLUTION_PARAMS: ((EvolutionParams)_object).write(out); break; | ||
default: | ||
throw new StreamCorruptedException("Unknown serialized type."); | ||
} | ||
} | ||
|
||
@Override | ||
public void readExternal(final ObjectInput in) | ||
throws IOException, ClassNotFoundException | ||
{ | ||
_type = in.readByte(); | ||
switch (_type) { | ||
case EVOLUTION_PARAMS: _object = EvolutionParams.read(in); break; | ||
default: | ||
throw new StreamCorruptedException("Unknown serialized type."); | ||
} | ||
} | ||
|
||
private Object readResolve() { | ||
return _object; | ||
} | ||
|
||
} |