Skip to content

ironsigma/converter-manager

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Conversion manager.

Your one stop bean conversion.

Simple conversion between various types, no need for casting or calling different class or methods.

:::java
YourObject yourObj = converterManager.convert(myObject, YourObject.class);
HerObject herObj = converterManager.convert(myObject, HerObject.class);
HisObject hisObj = converterManager.convert(myObject, HisObject.class);

Quick start

  1. Write converter
  2. Register converter
  3. Convert beans

1. Write a converter

A converter is just a class with one or more methods annotated with @Converter that take an object and return an object of a different type. Optionally it can take additional arguments.

:::java
public MyConverter {
  @Converter
  public YourObject convert(MyObject object) {
  	YourObject yo = new YourObject();
  	yo.setAttr(object.getAttr());
  	return yo;
  }
  
  @Converter
  public MyObject revert(YourObject object) {
  	...
  }
  
  @Converter
  public MyObject revertConditionally(YourObject object, Boolean includeSubObjects) {
  	...
  }
}

2. Register a converter

Create a Conversion Manager object to hold the converters and register the new converter.

:::java
ConverterManager converterManager = new ConverterManager();
converterManager.register(new MyConverter());

If you are using Spring you can add a converter manager bean and list each one of the converters.

:::html
<bean id="converterManager" class="com.example.app.ConverterManager">
	<property name="converters">
    <list>
    	 <bean class="com.example.app.MyConverter" />
    </list>
  </property>  
</bean>

3. Convert beans

Once the converters are registered, use the converter manager bean to perform the conversions.

:::java
YourObject yourObj = converterManager.convert(myObject, YourObject.class);

The convert method will look through the list of registered converters, if a converter with matching types is found it will perform the conversion.

The convert method will try to search for a converter of the matching object type, if nothing is found it will then try to match on the interface(s) type, and finally on the superclass type.

If no matching converter was found a ConversionFailedException is thrown.

If a converter takes additional arguments they are passed during the convert call.

:::java
converterManager.convert(myObject, TargetType.class, arg1);

To do

  • More testing

Copyright

  • Copyright 2012 Juan D Frias

License

  • Apache License Version 2.0

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages