001 /*
002 * Java Genetic Algorithm Library (jenetics-5.1.0).
003 * Copyright (c) 2007-2019 Franz Wilhelmstötter
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 *
017 * Author:
018 * Franz Wilhelmstötter (franz.wilhelmstoetter@gmail.com)
019 */
020 package io.jenetics.ext.rewriting;
021
022 import java.io.Externalizable;
023 import java.io.IOException;
024 import java.io.ObjectInput;
025 import java.io.ObjectOutput;
026 import java.io.StreamCorruptedException;
027
028 import io.jenetics.ext.rewriting.TreePattern.Val;
029 import io.jenetics.ext.rewriting.TreePattern.Var;
030
031 /**
032 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
033 * @version 5.0
034 * @since 5.0
035 */
036 final class Serial implements Externalizable {
037
038 private static final long serialVersionUID = 1;
039
040 static final byte TREE_PATTERN = 1;
041 static final byte TREE_PATTERN_VAR = 2;
042 static final byte TREE_PATTERN_VAL = 3;
043 static final byte TREE_REWRITE_RULE = 4;
044 static final byte TRS_KEY = 5;
045
046 /**
047 * The type being serialized.
048 */
049 private byte _type;
050
051 /**
052 * The object being serialized.
053 */
054 private Object _object;
055
056 /**
057 * Constructor for deserialization.
058 */
059 public Serial() {
060 }
061
062 /**
063 * Creates an instance for serialization.
064 *
065 * @param type the type
066 * @param object the object
067 */
068 Serial(final byte type, final Object object) {
069 _type = type;
070 _object = object;
071 }
072
073 @Override
074 public void writeExternal(final ObjectOutput out) throws IOException {
075 out.writeByte(_type);
076 switch (_type) {
077 case TREE_PATTERN: ((TreePattern)_object).write(out); break;
078 case TREE_PATTERN_VAR: ((Var)_object).write(out); break;
079 case TREE_PATTERN_VAL: ((Val)_object).write(out); break;
080 case TREE_REWRITE_RULE: ((TreeRewriteRule)_object).write(out); break;
081 case TRS_KEY: ((TRS)_object).write(out); break;
082 default:
083 throw new StreamCorruptedException("Unknown serialized type.");
084 }
085 }
086
087 @Override
088 public void readExternal(final ObjectInput in)
089 throws IOException, ClassNotFoundException
090 {
091 _type = in.readByte();
092 switch (_type) {
093 case TREE_PATTERN: _object = TreePattern.read(in); break;
094 case TREE_PATTERN_VAR: _object = Var.read(in); break;
095 case TREE_PATTERN_VAL: _object = Val.read(in); break;
096 case TREE_REWRITE_RULE: _object = TreeRewriteRule.read(in); break;
097 case TRS_KEY: _object = TRS.read(in); break;
098 default:
099 throw new StreamCorruptedException("Unknown serialized type.");
100 }
101 }
102
103 private Object readResolve() {
104 return _object;
105 }
106
107 }
|