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

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

import java.io.IOException;
import java.util.List;
import java.util.function.Function;

import org.conqat.lib.commons.assertion.CCSMAssert;
import org.conqat.lib.commons.serialization.SerializedEntityPool;
import org.conqat.lib.commons.serialization.classes.SerializedBooleanField;
import org.conqat.lib.commons.serialization.classes.SerializedClass;
import org.conqat.lib.commons.serialization.classes.SerializedFieldBase;
import org.conqat.lib.commons.serialization.classes.SerializedIntField;
import org.conqat.lib.commons.serialization.classes.SerializedLongField;
import org.conqat.lib.commons.serialization.classes.SerializedObjectField;
import org.conqat.lib.commons.serialization.objects.SerializedObject;
import org.conqat.lib.commons.serialization.objects.SerializedStringObject;

/**
 * Utility class to consistently migrate field names of a {@link SerializedClass} with their
 * corresponding field values in all {@link SerializedObject} of the {@link SerializedClass}.
 */
public class SerializedClassFieldMigrator {

	private final SerializedEntityPool entityPool;

	public SerializedClassFieldMigrator(SerializedEntityPool entityPool) {
		this.entityPool = entityPool;
	}

	/**
	 * Renames the field of the {@link SerializedClass} and adjusts the corresponding field values in
	 * all {@link SerializedObject}s of the {@link SerializedClass}.
	 */
	public void renameField(String serializedClassName, String oldFieldName, String newFieldName) {
		SerializedClass serializedClass = entityPool.findClass(serializedClassName);

		if (serializedClass == null) {
			return;
		}

		serializedClass.renameField(oldFieldName, newFieldName);
	}

	/**
	 * Updates the {@link String} value of a field with type {@link String} according to the field
	 * updater function.
	 */
	public void updateStringFieldValue(String serializedClassName, String fieldName,
			Function fieldValueUpdater) throws IOException {
		SerializedClass serializedClass = entityPool.findClass(serializedClassName);

		if (serializedClass == null) {
			return;
		}

		for (SerializedObject serializedObject : SerializedEntityUtils.findInstancesOf(serializedClass, entityPool)) {
			int stringObjectHandle = (int) serializedObject.getFieldValue(fieldName);
			String oldValue = entityPool.getEntity(stringObjectHandle, SerializedStringObject.class).getValue();
			String newValue = fieldValueUpdater.apply(oldValue);

			serializedObject.setFieldValue(fieldName, new SerializedStringObject(newValue, entityPool).getHandle());
		}
	}

	/**
	 * Adds a new {@link Boolean} field with a default value set in all {@link SerializedObject}s of the
	 * {@link SerializedClass}. The field can't exist yet.
	 */
	public void addBooleanField(String serializedClassName, String fieldName, boolean defaultValue) throws IOException {
		addField(serializedClassName, fieldName, new SerializedBooleanField(fieldName), defaultValue);
	}

	/**
	 * Adds a new long integer field with a default value set in all {@link SerializedObject}s of the
	 * {@link SerializedClass}. The field can't exist yet.
	 */
	public void addLongField(String serializedClassName, String fieldName, long defaultValue) throws IOException {
		addField(serializedClassName, fieldName, new SerializedLongField(fieldName), defaultValue);
	}

	/**
	 * Adds a new integer field with a default value set in all {@link SerializedObject}s of the
	 * {@link SerializedClass}. The field can't exist yet.
	 */
	public void addIntField(String serializedClassName, String fieldName, int defaultValue) throws IOException {
		addField(serializedClassName, fieldName, new SerializedIntField(fieldName), defaultValue);
	}

	/**
	 * Adds a new {@link String} field with a default value set in all {@link SerializedObject}s of the
	 * {@link SerializedClass}. The field can't exist yet.
	 */
	public void addStringField(String serializedClassName, String fieldName, String defaultValue) throws IOException {
		addField(serializedClassName, fieldName,
				new SerializedObjectField(fieldName, SerializedObjectField.STRING_TYPE),
				new SerializedStringObject(defaultValue, entityPool).getHandle());
	}

	/**
	 * Adds a new {@link List} field with a default value set in all {@link SerializedObject}s of the
	 * {@link SerializedClass}. The field can't exist yet.
	 */
	public void addListField(String serializedClassName, String fieldName, int defaultValueHandle) throws IOException {
		addField(serializedClassName, fieldName, new SerializedObjectField(fieldName, SerializedObjectField.LIST_TYPE),
				defaultValueHandle);
	}

	private void addField(String serializedClassName, String fieldName, SerializedFieldBase serializedField,
			Object defaultValue) throws IOException {
		SerializedClass serializedClass = entityPool.findClass(serializedClassName);

		if (serializedClass == null) {
			return;
		}

		CCSMAssert.isTrue(serializedClass.getField(fieldName) == null,
				() -> "Can't add the already existing field " + fieldName + " to class " + serializedClassName);
		serializedClass.addField(serializedField);

		for (SerializedObject entity : SerializedEntityUtils.findInstancesOf(serializedClass, entityPool)) {
			entity.setFieldValue(fieldName, defaultValue);
		}
	}

	/**
	 * Removes the field from the given class and the corresponding values from all object instances.
	 */
	public void removeField(String className, String fieldName) throws IOException {
		SerializedClass serializedClass = entityPool.findClass(className);
		if (serializedClass == null) {
			return;
		}
		List instances = SerializedEntityUtils.findInstancesOf(serializedClass, entityPool);
		for (SerializedObject instance : instances) {
			instance.removeFieldValue(fieldName);
		}
		serializedClass.removeField(fieldName);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy