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.SerializationConsistencyException;
024import org.conqat.lib.commons.serialization.SerializedEntityParser;
025import org.conqat.lib.commons.serialization.SerializedEntityPool;
026import org.conqat.lib.commons.serialization.SerializedEntitySerializer;
027import org.conqat.lib.commons.serialization.objects.SerializedObjectBase;
028
029/**
030 * Base class for field of complex type.
031 */
032public abstract class SerializedComplexFieldBase extends SerializedFieldBase {
033
034        /** The detailed type contains the fully qualified type name. */
035        private final String detailedType;
036
037        /**
038         * Constructor.
039         * 
040         * @param parser
041         *            the parser used for reading the {@link #detailedType}. If this
042         *            is null (which can be used to construct artificial fields not
043         *            resulting from a serialized stream), the {@link #detailedType}
044         *            will also be null.
045         */
046        protected SerializedComplexFieldBase(String fieldName, SerializedEntityParser parser) throws IOException {
047                super(fieldName);
048                if (parser == null) {
049                        this.detailedType = null;
050                } else {
051                        this.detailedType = parser.parseStringObject().getValue();
052                }
053        }
054
055        /** Constructor with explicit type construction. */
056        protected SerializedComplexFieldBase(String fieldName, String detailedType) {
057                super(fieldName);
058                this.detailedType = detailedType;
059        }
060
061        /** Returns the detailed type (may be null). */
062        public String getDetailedType() {
063                return detailedType;
064        }
065
066        /** {@inheritDoc} */
067        @Override
068        public void serialize(DataOutputStream dos, SerializedEntitySerializer serializer) throws IOException {
069                super.serialize(dos, serializer);
070                serializer.serializeStringObject(detailedType);
071        }
072
073        /** {@inheritDoc} */
074        @Override
075        public Object readValue(DataInputStream din, SerializedEntityParser parser) throws IOException {
076                return parser.parseContent();
077        }
078
079        /** {@inheritDoc} */
080        @Override
081        public void writeValue(Object value, SerializedEntityPool pool, DataOutputStream dos,
082                        SerializedEntitySerializer serializer) throws IOException {
083                if (!(value instanceof Integer)) {
084                        throw new SerializationConsistencyException("Expected a handle as value for field " + getName());
085                }
086
087                int handle = (Integer) value;
088                SerializedEntitySerializer.serializeObject(handle, SerializedObjectBase.class, pool, dos, serializer);
089        }
090}