001/*-------------------------------------------------------------------------+
002|                                                                          |
003| Copyright 2005-2011 the ConQAT Project                                   |
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+-------------------------------------------------------------------------*/
017package org.conqat.lib.commons.serialization.classes;
018
019import java.io.DataInputStream;
020import java.io.DataOutputStream;
021import java.io.IOException;
022
023import org.conqat.lib.commons.serialization.SerializedEntityParser;
024import org.conqat.lib.commons.serialization.SerializedEntityPool;
025import org.conqat.lib.commons.serialization.SerializedEntitySerializer;
026
027/**
028 * A field of a {@link SerializedClass}.
029 */
030public abstract class SerializedFieldBase {
031
032        /** The name of the field. */
033        private String name;
034
035        /** Constructor. */
036        protected SerializedFieldBase(String name) {
037                this.name = name;
038        }
039
040        /** Returns the name. */
041        public String getName() {
042                return name;
043        }
044
045        /** Sets the name. */
046        public void setName(String name) {
047                this.name = name;
048        }
049
050        /** Reads the value for a field from the given stream. */
051        public abstract Object readValue(DataInputStream din, SerializedEntityParser parser) throws IOException;
052
053        /**
054         * Writes the given field value to the output stream using the correct
055         * format for this field.
056         */
057        public abstract void writeValue(Object value, SerializedEntityPool pool, DataOutputStream dos,
058                        SerializedEntitySerializer serializer) throws IOException;
059
060        /**
061         * Serializes this field.
062         * 
063         * @param serializer
064         *            the serializer is used by sub classes.
065         */
066        public void serialize(DataOutputStream dos, SerializedEntitySerializer serializer) throws IOException {
067                dos.writeByte(getTypeCode());
068                dos.writeUTF(name);
069        }
070
071        /** Returns the type code for the king of field. */
072        protected abstract char getTypeCode();
073}