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.objects;
018
019import java.io.DataOutputStream;
020import java.io.IOException;
021import java.util.ArrayList;
022import java.util.Collections;
023import java.util.List;
024
025import org.conqat.lib.commons.serialization.SerializedEntityBase;
026import org.conqat.lib.commons.serialization.SerializedEntityPool;
027import org.conqat.lib.commons.serialization.SerializedEntitySerializer;
028import org.conqat.lib.commons.serialization.classes.SerializedClass;
029import org.conqat.lib.commons.serialization.classes.SerializedClassBase;
030
031/**
032 * Base class for all serialized objects.
033 */
034public abstract class SerializedObjectBase extends SerializedEntityBase {
035
036        /** The handle of the class this object instantiates. */
037        protected final int classHandle;
038
039        /** Constructor. */
040        protected SerializedObjectBase(SerializedEntityPool pool, int classHandle) {
041                super(pool);
042                this.classHandle = classHandle;
043        }
044
045        /**
046         * Returns the class hierarchy from most super class (nearest to
047         * {@link Object}) to sub class including only plain classes (no proxies).
048         */
049        public List<SerializedClass> getPlainClassHierarchy() throws IOException {
050                int currentClassHandle = this.classHandle;
051                List<SerializedClass> hierarchy = new ArrayList<>();
052                while (currentClassHandle != SerializedEntityPool.NULL_HANDLE) {
053                        SerializedClassBase classEntity = pool.getEntity(currentClassHandle, SerializedClassBase.class);
054                        if (classEntity instanceof SerializedClass) {
055                                // Omits other serialized class implementations like proxies
056                                hierarchy.add((SerializedClass) classEntity);
057                        }
058                        currentClassHandle = classEntity.getSuperClassHandle();
059                }
060
061                Collections.reverse(hierarchy);
062                return hierarchy;
063        }
064
065        /** Returns the handle of the class for this object. */
066        public int getClassHandle() {
067                return classHandle;
068        }
069
070        /** {@inheritDoc} */
071        @Override
072        protected void serializeContent(DataOutputStream dos, SerializedEntitySerializer serializer) throws IOException {
073                dos.writeByte(getObjectTagConstant());
074                pool.getEntity(classHandle, SerializedClassBase.class).serialize(dos, serializer);
075
076                serializer.registerHandle(this);
077
078                serializeObjectContent(dos, serializer);
079        }
080
081        /** Returns the tag code for the object type. */
082        protected abstract byte getObjectTagConstant();
083
084        /** Serializes the object's content. */
085        protected abstract void serializeObjectContent(DataOutputStream dos, SerializedEntitySerializer serializer)
086                        throws IOException;
087}