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

org.conqat.lib.commons.serialization.SerializedEntityBase 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;

import java.io.DataOutputStream;
import java.io.IOException;

/**
 * Base class for anything that is part of a serialized stream (objects, classes, etc.).
 */
public abstract class SerializedEntityBase {

	/** The handle of this entity. */
	protected final int handle;

	/** The pool this entity is registered at. */
	protected final SerializedEntityPool pool;

	/**
	 * Constructor. This also registers the entity with the pool. The reason that this happens here is
	 * that adding to the pool also defines the handle of the entity (handle counter in the pool). Thus
	 * we must be sure that obtaining the handle happens before any other (child) entities are added.
	 */
	protected SerializedEntityBase(SerializedEntityPool pool) {
		this.handle = pool.addEntity(this);
		this.pool = pool;
	}

	/** Returns the handle for this entity. */
	public int getHandle() {
		return handle;
	}

	/** Serializes the entity on an output stream. */
	public void serialize(DataOutputStream dos, SerializedEntitySerializer serializer) throws IOException {
		if (serializer.writeReference(this)) {
			return;
		}

		serializeContent(dos, serializer);
	}

	/**
	 * Serializes the content of the entity (no reference) on an output stream.
	 */
	protected abstract void serializeContent(DataOutputStream dos, SerializedEntitySerializer serializer)
			throws IOException;
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy