Skip to content

Martini Data Model Object Converters

Object converters in Martini are designed to simplify data type conversions during service execution. These converters automatically handle the transformation of various object types, enhancing the platform's ease of use.

Automatic Type Conversion

In Martini, when a service requires a data type to be transformed, such as converting a String to a numeric type (e.g., Integer, BigDecimal, or Float), the system automatically performs this conversion. This is achieved through a series of built-in converters, each tailored to specific type transformations.

Built-in Converters

Martini includes a range of built-in converters to facilitate smooth data manipulation across different data types and formats. These converters are an integral part of the platform, ensuring efficient and error-free data conversions in various scenarios.

Details of these built-in converters, including their supported types and use cases, will be provided in the following sections.

AssignableFromConverter

The AssignableFromConverter in Martini plays a fundamental role in simplifying data type handling by leveraging Java's inheritance principles. It checks if the source object's class aligns with the target class, including cases where the source is a subclass or implements an interface of the target.

Functionality

This converter operates on a straightforward principle: it determines if the source object is either the same as, a subclass of, or an implementer of the interface represented by the target class. If this condition is met, the conversion process recognizes that the source object is already compatible with the target type, and thus, no additional conversion is necessary.

Practical Usage

In practice, the AssignableFromConverter mirrors this Java code snippet:

1
2
if (targetClass.isAssignableFrom(sourceValue.getClass()))
   return sourceValue;

This means that if sourceValue is an instance of targetClass or its subclasses, sourceValue is returned directly without any conversion. This converter is particularly useful in scenarios where polymorphism is involved, ensuring that object hierarchies are respected and leveraged effectively in Martini services.

NumberConverter

The NumberConverter in Martini is a versatile tool designed for converting various data types into numeric formats, handling both primitive data types and object representations of numbers.

Conversion Scope

This converter specializes in transforming CharSequence objects, primitives (such as int, short, float, double, long, byte, char), and Number objects into other Number types, char, or Character objects. The versatility of the NumberConverter is key to handling a wide range of numeric conversions required in data processing and manipulation.

Character Conversion

A unique aspect of the NumberConverter is its approach to converting values into char or Character objects. It does this by calling toString() on the source object and then using the first character of the resulting string. In scenarios where toString() yields a null or an empty string, the converter defaults to returning 0.

Practical Implications

This functionality is particularly important when dealing with numeric data that needs to be represented in different formats or when numeric values are derived from non-numeric sources. The NumberConverter ensures that these transformations are handled efficiently and accurately, contributing to the robustness of data handling in Martini.

IterableConverter

The IterableConverter in Martini is adept at transforming a variety of collection-based data types, ensuring seamless interaction and compatibility among different iterable and collection structures.

Conversion Functionality

This converter is specialized in handling conversions from Iterable, Iterator, and Map objects to Iterable, Iterator, Set, or SortedSet objects. Its ability to interchangeably convert between these collection types is essential for data processing tasks that require flexibility in the structure and handling of collections.

Special Handling for Non-Array Sources

When the source object is not an array but requires conversion into an iterable form, the IterableConverter uniquely handles this by encapsulating the source object’s value into a single-entry target object. This ensures that the integrity of the source data is maintained while adapting it to a different collection format.

Practical Uses in Martini

The IterableConverter is particularly useful in scenarios where data needs to be restructured for compatibility or specific processing requirements. It plays a vital role in the manipulation and conversion of collection-based data within Martini services, facilitating efficient data handling and transformation.

ArrayConverter

The ArrayConverter in Martini plays a pivotal role in handling conversions between different array and collection types, as well as managing the transformation of array elements to various formats.

Utilization of Built-in Converters

This converter leverages the capabilities of other built-in converters to perform conversions. It is adept at transforming Java object arrays and collections into other array and collection formats. This includes using appropriate converters for individual elements within an array, ensuring that each element is converted to the target type effectively.

Conversion to and from Arrays

A key feature of the ArrayConverter is its versatility in dealing with both array-to-array and array-to-non-array conversions. It can handle complex scenarios where elements within an array need to be transformed into different types or structures.

Byte Array Conversion

Additionally, the ArrayConverter is equipped with the functionality to convert byte arrays into String format. This capability is particularly useful in data processing tasks where raw data, often in byte array format, needs to be interpreted as human-readable text.

Practical Applications

In Martini services, the ArrayConverter is essential for managing data that comes in array or collection formats, especially when these need to be transformed for compatibility with other data types or for specific processing requirements. Its ability to handle a diverse range of conversions makes it a versatile tool in the Martini toolkit.

ObjectMapConverter

The ObjectMapConverter in Martini is a critical tool for converting complex object models to Java Map objects and vice versa. This converter facilitates the integration and manipulation of structured data within Martini services.

Conversion from Object Models to Maps

This converter excels at transforming object models into Map objects. It enables the representation of structured model data in a map format, which is useful in situations where data needs to be accessed or manipulated as key-value pairs.

Conversion from Maps to Object Models

In addition to converting object models to maps, the converter also handles the transformation of Map objects into structured object models. This feature is essential when integrating map-based data into structured models, ensuring a seamless data flow and compatibility within Martini's framework.

Practical Application

The ObjectMapConverter is pivotal in Martini for data transformation and integration. It provides the necessary mechanics for bridging structured object models with the flexibility of map structures, thus enhancing data handling and efficiency in various service scenarios.

Certainly, here's a concise and informative description for the CharSequenceToClassConverter subsection:

CharSequenceToClassConverter

The CharSequenceToClassConverter in Martini is specifically designed to handle conversions from CharSequence instances to Class objects. This converter is particularly useful in dynamic object instantiation based on class names represented as string-like sequences.

Conversion Mechanism

When the source data is a CharSequence and the target is a Class object, this converter activates. Its primary function is to use the value of the CharSequence as a class name and attempt to instantiate an instance of this class.

Practical Usage

This process involves dynamically identifying the class specified by the CharSequence value and creating a new instance of it. It's an essential feature for scenarios in Martini where class names are provided as strings (such as in configurations or dynamic data processing) and need to be converted into actual class instances for further operation.

Significance in Martini

The CharSequenceToClassConverter offers a flexible approach to object creation, allowing Martini services to handle class instantiation dynamically based on string-based inputs. This capability is crucial for scenarios requiring runtime decisions and object generation based on textual class references.

CharSequenceToClosureConverter

The CharSequenceToClosureConverter in Martini is adept at transforming CharSequence instances into Closure objects, specifically focusing on the compilation of Groovy closures.

Conversion Process

When the source material is a CharSequence, such as a string or a sequence of characters, and the target is a Closure object, this converter comes into play. Its main function is to interpret and compile the CharSequence into a Groovy closure, effectively turning a string representation of code into an executable closure.

Practical Application

This capability is particularly valuable in Martini for scenarios requiring dynamic scripting and code execution. For instance, when a script or a piece of code is provided as a text input, the CharSequenceToClosureConverter enables the conversion of this text into a functional Groovy closure.

Significance in Dynamic Scripting

This converter is a critical tool for Martini services that leverage dynamic scripting capabilities. It provides a flexible and efficient way to handle and execute code represented as character sequences, broadening the scope of dynamic operations and customizations possible within Martini.

CharSequenceToEnumConverter

The CharSequenceToEnumConverter is a specialized converter in Martini that handles the conversion of CharSequence instances to corresponding enum values.

Conversion Functionality

This converter comes into action when the source data is a CharSequence — such as a string or a sequence of characters — and the target is an enumeration (enum) value. Its primary role is to interpret the CharSequence and match it to the appropriate enum value.

Practical Implementation

The process involves analyzing the character sequence and finding an enum constant that matches the sequence. This is particularly useful in scenarios where text inputs need to be translated into predefined enum values for processing or decision-making within Martini services.

Significance in Data Processing

The CharSequenceToEnumConverter is essential for Martini services that require dynamic interpretation of text inputs into structured enum formats. It facilitates flexibility in handling user inputs or configuration parameters that are represented as text but need to be converted to more rigid, predefined enum types for further processing.

CharSequenceToRunnableConverter

The CharSequenceToRunnableConverter in Martini is designed to convert CharSequence instances into Java Runnable objects, facilitating the creation of executable tasks from character sequences.

Conversion Mechanism

When the source data is a CharSequence, such as a string or a sequence of characters, and the target is a Runnable object, this converter initiates its process. Its task is to compile the CharSequence into a Java Runnable. This involves interpreting the character sequence as Java code and transforming it into a Runnable instance.

Practical Usage

This converter is particularly valuable in scenarios where dynamic code execution is required. It allows for text inputs, like script snippets or code blocks, to be directly converted into Runnable objects that can be executed within the Martini framework.

Application in Dynamic Operations

The CharSequenceToRunnableConverter is crucial for Martini services that utilize dynamic scripting or require on-the-fly creation of executable tasks. This feature enhances the flexibility and adaptability of Martini, allowing for dynamic task generation and execution based on textual inputs.

DateCalendarConverter

The DateCalendarConverter in Martini is a versatile tool for handling conversions involving Java date and time objects. It is designed to work with a range of date-related types and convert them into various date and time formats.

Supported Source Types

This converter accepts various source types, including:

  • java.lang.CharSequence
  • java.util.Calendar
  • java.sql.Date
  • java.util.Date
  • java.lang.Number

Target Conversion Types

It aims to convert these sources into several date and time-related types such as:

  • java.util.Calendar
  • java.sql.Date
  • java.util.Date
  • java.sql.Time
  • java.sql.Timestamp
  • java.lang.Long

Conversion Methodology

The DateCalendarConverter typically first converts the source to a java.util.Date object and then transforms it into the desired target format. The conversion process for CharSequence and Number sources includes:

  1. Parsing CharSequence Formats: Recognizes standard date-time formats like ISO and XML-formatted dates and times.
  2. Handling now Keyword: Interprets now or now() as the current time.
  3. Number Conversion: Transforms numeric values (or numeric strings) into dates by treating them as timestamps (milliseconds since epoch).
  4. XML Date Formats: Handles various XML formatted dates and times.

Custom Date Formats

Martini allows for the addition of custom date format patterns to enhance the flexibility of the DateCalendarConverter. This feature ensures that the converter can recognize and handle a wide range of date formats used in different contexts within Martini services.

ObjectToBooleanConverter

The ObjectToBooleanConverter in Martini is specifically designed to convert various objects into boolean values, applying intuitive rules based on the source object's type.

Conversion from Number

When the source object is a Number, the converter evaluates its value to determine the boolean outcome. If the numerical value is greater than 0, the converter returns true. This approach aligns with common programming paradigms where non-zero values are often equated with a "true" condition.

Conversion from CharSequence

If the source object is a CharSequence, such as a string, the converter uses the toString() method of the source and checks if the resulting string, irrespective of case, equals true. This case-insensitive comparison ensures that various textual representations of truth, like "TRUE", "True", or "true", are consistently interpreted as a boolean true.

Practical Implications

The ObjectToBooleanConverter is essential in Martini for scenarios where objects of various types need to be evaluated in a boolean context. It facilitates straightforward and logical conversions, particularly useful in conditional processing and decision-making within Martini services.

ObjectToConstructorConverter

The ObjectToConstructorConverter in Martini is designed to facilitate the instantiation of target objects by utilizing constructors that match the type of the source object.

Matching Process

This converter operates by examining the target class for a constructor that has a single argument, where the parameter type matches the type of the source object. If such a constructor is found, it is used to create an instance of the target object.

Constructor Utilization

The key functionality of this converter is its ability to identify and use constructors that are compatible with the source object's type. This is particularly useful when dealing with classes that provide specific constructors for different data types, allowing for dynamic and type-specific object instantiation.

Practical Application

In Martini services, the ObjectToConstructorConverter plays a crucial role in object creation, especially when objects need to be instantiated with specific characteristics or properties derived from the source data. This converter ensures a streamlined and efficient object creation process, adapting to the source object's type.

CharSequenceToFileConverter

The CharSequenceToFileConverter in Martini is designed to transform CharSequence instances, such as strings, into File objects. This conversion is straightforward yet powerful, enabling the creation of file references from textual data.

Conversion Process

The primary function of this converter is to use the value of a CharSequence as the path or filename for a File object. When a CharSequence is provided, the converter interprets its value as the file path and instantiates a new File object pointing to that location.

Practical Usage

This converter is particularly useful in scenarios where file paths or names are dynamically provided as strings within Martini services. It allows for the seamless and efficient creation of file references, facilitating file operations like reading, writing, and manipulation based on textual inputs.

Significance in File Handling

In Martini, the CharSequenceToFileConverter plays a crucial role in bridging textual data with file system operations. It enhances the platform's capability to handle file-related tasks dynamically, driven by character sequences representing file paths or names.

InputStreamConverter

The InputStreamConverter in Martini is designed to handle the conversion of various input-based I/O objects and byte arrays into either InputStream or Reader objects. This converter is essential for streamlining data flow in I/O operations within Martini services.

Supported Source Types

This converter can handle a wide range of source types, including:

  • byte[]
  • java.sql.Blob
  • java.sql.Clob
  • java.io.File
  • java.io.InputStream
  • java.io.Reader

Conversion Targets

The converter focuses on transforming these source types into either:

  • java.io.InputStream
  • java.io.Reader

Conversion Functionality

The primary role of the InputStreamConverter is to ensure seamless conversion between different types of input streams and readers. This includes converting byte arrays and various SQL and I/O objects into formats that are suitable for reading and processing data streams in Martini.

Practical Implications

In practical terms, the InputStreamConverter is pivotal for handling file and stream-based data within Martini services. Whether dealing with file uploads, database BLOBs and CLOBs, or other stream-based data, this converter provides the necessary functionality to manipulate and process such data efficiently.

OutputStreamConverter

The OutputStreamConverter in Martini is adept at converting various types of output-based I/O objects into other, similar types. This converter is crucial for managing data output processes within Martini services, ensuring compatibility and flexibility in output operations.

Supported Source Types

This converter is capable of handling a range of source types, including:

  • java.io.OutputStream
  • java.io.Writer
  • java.io.PrintStream
  • java.io.File

Conversion Targets

The target formats for this converter include:

  • java.io.OutputStream
  • java.io.Writer
  • java.io.PrintStream

Converter Functionality

The primary function of the OutputStreamConverter is to facilitate the transformation of various output stream and writer types into other formats suitable for outputting data. This includes converting file objects and different types of streams and writers into a specific output stream or writer format.

Practical Application

In practical terms, the OutputStreamConverter is essential for handling and manipulating data output in various forms within Martini services. Whether it's writing to files, output streams, or print streams, this converter ensures that data is efficiently and effectively outputted in the desired format.

StreamToContentConverter

The StreamToContentConverter in Martini is a specialized tool for reading and converting input stream data into more accessible content formats. This converter is essential for efficiently processing stream-based data within Martini services.

Converter Functionality

Unlike the InputStreamConverter, the StreamToContentConverter focuses on reading all available data from an input stream and converting it into either:

  • CharSequence
  • StringBuffer objects
  • Byte arrays

Data Processing and Conversion

This converter ensures that data from streams is fully read and transformed into a format that is easily manipulatable and usable within Martini's context. It is particularly useful when dealing with data streams that need to be converted into string-based representations or raw byte arrays for further processing or analysis.

Practical Application

In practice, the StreamToContentConverter plays a vital role in scenarios where data from streams, such as file inputs or network streams, needs to be comprehensively captured and converted into a more tangible or actionable format, like a String or a byte array.

ContentToStreamConverter

The ContentToStreamConverter in Martini is designed to facilitate the conversion of various data-containing objects into commonly used Java objects for data reading. This converter is crucial for handling diverse data sources within Martini services.

Supported Source Types

This converter is capable of handling a wide array of source types, including:

  • byte[]
  • java.lang.CharSequence
  • java.sql.Blob
  • java.sql.Clob
  • java.io.File
  • java.io.InputStream
  • java.io.Reader
  • org.springframework.web.multipart.MultipartFile (commonly used in Martini for HTTP file uploads)

Conversion Targets

The target formats for conversion include:

  • java.io.InputStream
  • java.io.Reader
  • java.sql.Blob
  • java.sql.Clob

Conversion Functionality

The primary role of the ContentToStreamConverter is to read data from various source objects and transform them into formats suitable for data reading and processing. This includes converting diverse source types into input streams, readers, and SQL Blob and Clob types.

Practical Application

In practical terms, the ContentToStreamConverter is essential for processing and managing data from various sources, such as files, streams, and web uploads. It ensures that data is efficiently converted into a format that is readable and processable within the Martini framework, catering to a wide range of data handling requirements.

ObjectToCharSequenceConverter

The ObjectToCharSequenceConverter in Martini serves as a fundamental tool for converting various object types into character sequences, ensuring data is in a readable and processable format.

Conversion Scope

This converter is adept at transforming the following source types:

  • byte[]
  • char[]
  • java.lang.StringBuffer
  • Any generic java.lang.Object

Target Formats

The conversion targets for this converter include:

  • java.lang.StringBuffer
  • java.lang.CharSequence

Converter Functionality

The primary function of the ObjectToCharSequenceConverter is to take various object types and convert them into string-like formats. Whether dealing with byte arrays, character arrays, string buffers, or any generic object, this converter ensures a smooth transition to a CharSequence or StringBuffer.

Practical Implications

In Martini, this converter is particularly useful in scenarios requiring the textual representation of data for further processing, display, or storage. It plays a crucial role in ensuring data compatibility across different stages of processing and in various services within the Martini environment.

Adding Custom Converters

Martini allows for the addition of custom converters, enhancing the capability to handle diverse data conversions seamlessly within your applications.

Registering Custom Converters

To register a custom converter, use the ObjectConverterManager#registerConverter(ObjectConverter, int) method. The first argument is the custom converter you want to register, and the second argument determines its position in the list of converters. This method is crucial for integrating custom conversion logic into Martini.

The ObjectConverter Interface

Custom converters need to implement the ObjectConverter interface, which includes two essential methods:

  • supports(Class from, Class to): Checks if the converter can handle conversion between specified classes.
  • convertTo(Object from, Class to): Performs the conversion. It should throw a ClassCastException if unable to convert.

Practical Usage

Integrating custom converters allows for the direct transformation of specific object types, including custom Java objects and Martini's internal data structures. This is particularly useful for leveraging existing Java code or custom data types within Martini's framework.

Example Implementation

Below is a sample implementation of a custom converter that facilitates conversions between a custom Java object and Martini's internal data models:

1
2
3
4
5
6
7
8
9
public class CustomObjectConverter implements ObjectConverter {
    // Implementation of the converter methods
    // ...
}

// Registering the converter
public static void register() {
    ObjectConverterManager.registerConverter(new CustomObjectConverter());
}

Startup Service Integration

For effective use, it's recommended to register your custom converter as part of the startup services in your Martini package. This ensures that the converter is readily available and operational as the application starts.