From 90d94a479e5af0d884c63643f1266dc0a6596318 Mon Sep 17 00:00:00 2001 From: Manuel Bentele Date: Thu, 15 Apr 2021 14:01:47 +0200 Subject: Add generic transformation library to transform configs --- .../transformation/Transformation.java | 65 +++++++++ .../transformation/TransformationException.java | 25 ++++ .../transformation/TransformationFunction.java | 39 ++++++ .../transformation/TransformationGeneric.java | 25 ++++ .../transformation/TransformationManager.java | 146 +++++++++++++++++++++ .../transformation/TransformationSpecific.java | 43 ++++++ 6 files changed, 343 insertions(+) create mode 100644 src/main/java/org/openslx/virtualization/configuration/transformation/Transformation.java create mode 100644 src/main/java/org/openslx/virtualization/configuration/transformation/TransformationException.java create mode 100644 src/main/java/org/openslx/virtualization/configuration/transformation/TransformationFunction.java create mode 100644 src/main/java/org/openslx/virtualization/configuration/transformation/TransformationGeneric.java create mode 100644 src/main/java/org/openslx/virtualization/configuration/transformation/TransformationManager.java create mode 100644 src/main/java/org/openslx/virtualization/configuration/transformation/TransformationSpecific.java diff --git a/src/main/java/org/openslx/virtualization/configuration/transformation/Transformation.java b/src/main/java/org/openslx/virtualization/configuration/transformation/Transformation.java new file mode 100644 index 0000000..af0e181 --- /dev/null +++ b/src/main/java/org/openslx/virtualization/configuration/transformation/Transformation.java @@ -0,0 +1,65 @@ +package org.openslx.virtualization.configuration.transformation; + +/** + * Represents a transformation that transforms (alters) a given configuration with specified input + * arguments. + * + * @author Manuel Bentele + * @version 1.0 + * + * @param type of the configuration which will be transformed. + * @param type of input arguments for the transformation. + */ +public abstract class Transformation implements TransformationFunction +{ + /** + * Name of the transformation. + */ + private final String name; + + /** + * State of the transformation. + */ + private boolean enabled; + + /** + * Creates a transformation. + * + * @param name comprehensible name for the transformation. + */ + public Transformation( String name ) + { + this.name = name; + this.setEnabled( true ); + } + + /** + * Returns the name of the transformation. + * + * @return name of the transformation. + */ + public String getName() + { + return this.name; + } + + /** + * Returns the state of the transformation. + * + * @return state of the transformation. + */ + public boolean isEnabled() + { + return this.enabled; + } + + /** + * Sets the state for the transformation. + * + * @param enabled state for the transformation. + */ + public void setEnabled( boolean enabled ) + { + this.enabled = enabled; + } +} diff --git a/src/main/java/org/openslx/virtualization/configuration/transformation/TransformationException.java b/src/main/java/org/openslx/virtualization/configuration/transformation/TransformationException.java new file mode 100644 index 0000000..72f320d --- /dev/null +++ b/src/main/java/org/openslx/virtualization/configuration/transformation/TransformationException.java @@ -0,0 +1,25 @@ +package org.openslx.virtualization.configuration.transformation; + +/** + * An exception of a transformation error. + * + * @author Manuel Bentele + * @version 1.0 + */ +public class TransformationException extends Exception +{ + /** + * Version for serialization. + */ + private static final long serialVersionUID = 7293420658901349154L; + + /** + * Creates a transformation exception including an error message. + * + * @param errorMsg message to describe the exception. + */ + public TransformationException( String errorMsg ) + { + super( errorMsg ); + } +} diff --git a/src/main/java/org/openslx/virtualization/configuration/transformation/TransformationFunction.java b/src/main/java/org/openslx/virtualization/configuration/transformation/TransformationFunction.java new file mode 100644 index 0000000..b5be7a0 --- /dev/null +++ b/src/main/java/org/openslx/virtualization/configuration/transformation/TransformationFunction.java @@ -0,0 +1,39 @@ +package org.openslx.virtualization.configuration.transformation; + +/** + * Represents a transformation operation that transforms (alters) a given configuration with + * specified input arguments and returns no result. + * + * @author Manuel Bentele + * @version 1.0 + * + * @param type of the configuration which will be transformed. + * @param type of input arguments for the transformation. + */ +@FunctionalInterface +public interface TransformationFunction +{ + /** + * Transforms a given configuration with the specified input arguments. + * + * @param config configuration which will be transformed. + * @param args input arguments for the transformation. + * + * @throws TransformationException transformation of the configuration failed. + */ + public void transform( T config, R args ) throws TransformationException; + + /** + * Applies the transformation function {@link #transform(Object, Object)} to the given + * configuration and specified input arguments. + * + * @param config configuration which will be transformed. + * @param args input arguments for the transformation. + * + * @throws TransformationException transformation of the configuration failed. + */ + public default void apply( T config, R args ) throws TransformationException + { + this.transform( config, args ); + } +} diff --git a/src/main/java/org/openslx/virtualization/configuration/transformation/TransformationGeneric.java b/src/main/java/org/openslx/virtualization/configuration/transformation/TransformationGeneric.java new file mode 100644 index 0000000..4ad36c7 --- /dev/null +++ b/src/main/java/org/openslx/virtualization/configuration/transformation/TransformationGeneric.java @@ -0,0 +1,25 @@ +package org.openslx.virtualization.configuration.transformation; + +/** + * Represents a generic transformation that transforms (alters) a given configuration with specified + * input arguments. The generic transformation does not depend on any external states of a + * virtualizer. + * + * @author Manuel Bentele + * @version 1.0 + * + * @param type of the configuration which will be transformed. + * @param type of input arguments for the transformation. + */ +public abstract class TransformationGeneric extends Transformation +{ + /** + * Create a generic transformation. + * + * @param name comprehensible name for the transformation. + */ + public TransformationGeneric( String name ) + { + super( name ); + } +} diff --git a/src/main/java/org/openslx/virtualization/configuration/transformation/TransformationManager.java b/src/main/java/org/openslx/virtualization/configuration/transformation/TransformationManager.java new file mode 100644 index 0000000..ce0ad96 --- /dev/null +++ b/src/main/java/org/openslx/virtualization/configuration/transformation/TransformationManager.java @@ -0,0 +1,146 @@ +package org.openslx.virtualization.configuration.transformation; + +import java.util.ArrayList; + +/** + * A transformation manager is a class to manage several transformations and their application. + * + * Transformations can be registered at the transformation manager. The transformation manager has + * the ability to apply all registered transformations on a given configuration and specified input + * arguments. + * + * @author Manuel Bentele + * @version 1.0 + * + * @param type of the configuration which will be transformed by all transformations. + * @param type of input arguments for all transformations. + */ +public class TransformationManager +{ + /** + * List of registered transformations. + */ + private ArrayList> transformations; + + /** + * Reference to the configuration that will be transformed (altered). + */ + private T config; + + /** + * Reference to the input arguments for all registered transformations. + */ + private R args; + + /** + * Create a transformation manager. + * + * @param config configuration which will be transformed. + * @param args input arguments for all registered transformations. + */ + public TransformationManager( T config, R args ) + { + this.transformations = new ArrayList>(); + this.config = config; + this.args = args; + } + + /** + * Registers and enables a transformation. + * + * @param transformation existing transformation that will be registered and enabled. + */ + public void register( Transformation transformation ) + { + this.register( transformation, true ); + } + + /** + * Registers a transformation and sets its state. + * + * @param transformation existing transformation that will be registered. + * @param enabled state for the existing transformation that will be set. + */ + public void register( Transformation transformation, boolean enabled ) + { + transformation.setEnabled( enabled ); + this.transformations.add( transformation ); + } + + /** + * Registers a transformation function as a new transformation and enables the registered + * transformation. + * + * @param name comprehensible name for the transformation. + * @param function transformation operation for the transformation. + */ + public void register( String name, TransformationFunction function ) + { + this.register( name, function, true ); + } + + /** + * Registers a transformation function as a new transformation and sets the state of the + * registered transformation. + * + * @param name comprehensible name for the transformation. + * @param function transformation operation for the transformation. + * @param enabled state for the transformation. + */ + public void register( String name, TransformationFunction function, boolean enabled ) + { + this.register( new Transformation( name ) { + @Override + public void transform( T document, R args ) throws TransformationException + { + function.apply( document, args ); + } + }, enabled ); + } + + /** + * Applies all registered transformations, whose state is set to enabled, to the + * referenced configuration and input arguments. + * + * @throws TransformationException transformation of the configuration failed. + */ + public void transform() throws TransformationException + { + for ( Transformation transformation : this.transformations ) { + try { + transformation.apply( this.config, this.args ); + } catch ( TransformationException e ) { + final String errorMsg = new String( + "Error in configuration filter '" + transformation.getName() + "':" + e.getLocalizedMessage() ); + throw new TransformationException( errorMsg ); + } + } + } + + /** + * Returns a human readable summary of all registered transformations. + * + * @return human readable summary of all registered transformations. + */ + private String showTransformations() + { + String transformationSummary = new String(); + final int maxFilterNumCharacters = ( this.transformations.size() + 1 ) / 10; + + for ( int i = 0; i < this.transformations.size(); i++ ) { + final Transformation transformation = this.transformations.get( i ); + final String paddedNumber = String.format( "%-" + maxFilterNumCharacters + "s", i + 1 ); + final String transformationState = transformation.isEnabled() ? "[ active ]" : "[inactive]"; + transformationSummary += paddedNumber + ": " + transformationState + " "; + transformationSummary += transformation.getName() + System.lineSeparator(); + } + + return transformationSummary; + } + + @Override + public String toString() + { + return this.showTransformations(); + } +} diff --git a/src/main/java/org/openslx/virtualization/configuration/transformation/TransformationSpecific.java b/src/main/java/org/openslx/virtualization/configuration/transformation/TransformationSpecific.java new file mode 100644 index 0000000..5b13f45 --- /dev/null +++ b/src/main/java/org/openslx/virtualization/configuration/transformation/TransformationSpecific.java @@ -0,0 +1,43 @@ +package org.openslx.virtualization.configuration.transformation; + +/** + * Represents a specific transformation that transforms (alters) a given configuration with + * specified input arguments. The specific transformation depends on external states of a + * specified virtualizer. + * + * @author Manuel Bentele + * @version 1.0 + * + * @param type of the configuration which will be transformed. + * @param type of input arguments for the transformation. + * @param type of the external input source. + */ +public abstract class TransformationSpecific extends Transformation +{ + /** + * Reference to virtualizer to query external states. + */ + private final H virtualizer; + + /** + * Create a specific transformation. + * + * @param name comprehensible name for the transformation. + */ + public TransformationSpecific( String name, H virtualizer ) + { + super( name ); + + this.virtualizer = virtualizer; + } + + /** + * Returns the referenced virtualizer of the transformation. + * + * @return referenced virtualizer of the transformation. + */ + public H getVirtualizer() + { + return this.virtualizer; + } +} -- cgit v1.2.3-55-g7522