All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.conqat.lib.commons.serialization.objects.SerializedArrayObject Maven / Gradle / Ivy

There is a newer version: 2024.7.2
Show newest version
/*
 * Copyright (c) CQSE GmbH
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.conqat.lib.commons.serialization.objects;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.ObjectStreamConstants;
import java.util.ArrayList;
import java.util.List;

import org.conqat.lib.commons.serialization.SerializedEntityParser;
import org.conqat.lib.commons.serialization.SerializedEntityPool;
import org.conqat.lib.commons.serialization.SerializedEntitySerializer;
import org.conqat.lib.commons.serialization.classes.SerializedArrayField;
import org.conqat.lib.commons.serialization.classes.SerializedClass;
import org.conqat.lib.commons.serialization.classes.SerializedFieldBase;
import org.conqat.lib.commons.serialization.classes.SerializedObjectField;
import org.conqat.lib.commons.serialization.classes.SerializedPrimitiveFieldBase;

/**
 * An array in a serialized stream.
 */
public class SerializedArrayObject extends SerializedObjectBase {

	/** The values in the array. */
	private final List values = new ArrayList<>();

	/** The type of the elements use for reading/writing them. */
	private final SerializedFieldBase elementType;

	/** Constructor. */
	public SerializedArrayObject(DataInputStream din, SerializedEntityPool pool, SerializedEntityParser parser,
			int classHandle) throws IOException {
		super(pool, classHandle);

		String className = pool.getEntity(classHandle, SerializedClass.class).getName();
		if (className.length() == 2 && className.charAt(0) == SerializedArrayField.TYPE_CODE) {
			elementType = SerializedPrimitiveFieldBase.fromTypeCode(className.charAt(1), null);
		} else {
			elementType = new SerializedObjectField();
		}

		int size = din.readInt();
		for (int i = 0; i < size; ++i) {
			values.add(elementType.readValue(din, parser));
		}
	}

	/** Constructor with explicit element type. */
	public SerializedArrayObject(SerializedEntityPool pool, int classHandle, SerializedFieldBase elementType) {
		super(pool, classHandle);
		this.elementType = elementType;
	}

	/** Returns this array's element type. */
	public SerializedFieldBase getElementType() {
		return elementType;
	}

	/** Returns this array's value at the given index. */
	public Object getValue(int i) {
		return values.get(i);
	}

	/** Adds a value to this array. */
	public void addValue(Object value) {
		values.add(value);
	}

	/** Returns this array's size. */
	public int size() {
		return values.size();
	}

	/** {@inheritDoc} */
	@Override
	protected byte getObjectTagConstant() {
		return ObjectStreamConstants.TC_ARRAY;
	}

	/** {@inheritDoc} */
	@Override
	protected void serializeObjectContent(DataOutputStream dos, SerializedEntitySerializer serializer)
			throws IOException {
		dos.writeInt(values.size());
		for (Object value : values) {
			elementType.writeValue(value, pool, dos, serializer);
		}
	}
}