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

io.github.jdcmp.codegen.customization.AvailableSerializationMode Maven / Gradle / Ivy

There is a newer version: 0.3
Show newest version
package io.github.jdcmp.codegen.customization;


import io.github.jdcmp.api.serialization.SerializationDisabledException;

/**
 * Configuration constants and documentation describing serialization compatibility for the generated classes.
 */
public enum AvailableSerializationMode {

	/**
	 * Generates serialization methods that aim to be fully compatible with Java's serialization mechanism and transparent to users.
	 * This increases the size of the generated bytecode.
	 */
	COMPATIBLE(true, false) {
		@Override
		public  T map(SerializationModeMapper mapper) {
			return mapper.onCompatible();
		}
	},
	/**
	 * Omits serialization methods. The resulting comparator does not support serialization, but does not prevent it either. This may lead
	 * to confusing behavior, with working serialization and deserialization inside the same JVM instance, but failing deserialization on
	 * another JVM instance (of the same type, just another process). Since no methods are inserted, the size of the generated bytecode
	 * is unaffected by this mode.
	 */
	INCOMPATIBLE(false, false) {
		@Override
		public  T map(SerializationModeMapper mapper) {
			return mapper.onIncompatible();
		}
	},
	/**
	 * Generates serialization methods that actively prevent serialization and deserialization. This increases the
	 * size of the generated bytecode.
	 */
	HOSTILE(false, true) {
		@Override
		public  T map(SerializationModeMapper mapper) {
			return mapper.onHostile();
		}
	};

	private final boolean supportsSerialization;

	private final boolean preventsSerialization;

	AvailableSerializationMode(boolean supportsSerialization, boolean preventsSerialization) {
		if (supportsSerialization && preventsSerialization) {
			throw new IllegalArgumentException("Conflicting settings");
		}

		this.supportsSerialization = supportsSerialization;
		this.preventsSerialization = preventsSerialization;
	}

	/**
	 * Maps the enum constant using the given mapper. See {@link SerializationModeMapper}.
	 *
	 * @param mapper The mapper
	 * @return A user-supplied return value
	 * @param  Type of the return value
	 */
	abstract public  T map(SerializationModeMapper mapper);

	/**
	 * Indicates whether serialization is supported.
	 *
	 * @return true if serialization is supported; false otherwise
	 */
	public boolean supportsSerialization() {
		return supportsSerialization;
	}

	/**
	 * Indicates whether serialization is prevented.
	 *
	 * @return true if serialization is prevented; false otherwise
	 */
	public boolean preventsSerialization() {
		return preventsSerialization;
	}

	/**
	 * Helper method for writing safeguards. Throws in case serialization is disabled.
	 *
	 * @throws SerializationDisabledException If serialization is prevented
	 */
	public void throwIfPrevented() throws SerializationDisabledException {
		if (preventsSerialization()) {
			throw new SerializationDisabledException();
		}
	}

	/**
	 * 

Maps an initialization mode to a user-supplied value. For every enum constant, there is one handler method present in * this mapper.

* *

If any enum constants are added in the future, using this mapper will force implementors to handle the new * "on"-method. This reduces the risk of missing cases in if or switch statements.

* * @param Type of the return value */ public interface SerializationModeMapper { /** * Invoked when {@link AvailableSerializationMode#COMPATIBLE} is used. * * @return User-supplied return value */ T onCompatible(); /** * Invoked when {@link AvailableSerializationMode#INCOMPATIBLE} is used. * * @return User-supplied return value */ T onIncompatible(); /** * Invoked when {@link AvailableSerializationMode#HOSTILE} is used. * * @return User-supplied return value */ T onHostile(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy