Skip to content

a mapping library for java that recursively copies data from one object to another.

Notifications You must be signed in to change notification settings

tachibanana/mapfierJ

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

96 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Overview

MapfierJ !!!

A Reflection-based mappers library that maps objects to another objects. It can be very useful when developing multi-layered applications.

  • Map complex and deeply structured objects
  • Create converters for complete control over the mapping of a specific set of objects anywhere in the object graph

Usage

Suppose we have some instances of class Person that we’d like to map to instances of another class PersonDto.

 public class Person {
  @FieldName("fullname")
  String name;
  int age;
 }
 
 public class PersonDto {
  String fullname;
  int age;
 }

In order to map between these classes, we’ll need to first create a mapper.

 SimpleMapper mapper = new SimpleMapper();

By default it will only map the same field name and data type

 PersonDto person = mapper.set(new Person("Foo", 3)).mapTo(PersonDto.class);
 List<PersonDto> personList = mapper.set(new ArrayList<Animal>()).mapToList(PersonDto.class);

In order to map one or more fields to a different type. Follow the example here, or just simply use:

 PersonDto dto = mapper.set(person).mapAllTo(PersonDto.class);

Basically, to use ModelMapper first instantiate it with or without base package of your custom converters

 ModelMapper mapper = new ModelMapper();
 ModelMapper mapper = new ModelMaper("my.package");

In order to map different field between the two class:

 mapper.set(dog)
  .field("name", "title")
  .field("age", "year")

To explicitly exclude a field from mapping:

 mapper.set(dog).exclude("title")

To explicitly exclude a field (of object within a collection) from mapping:

 mapper.set(dogs).excludeAll("title")

Use a converters out of the box (or your own custom converter) where the mapper can't handle mapping an instance of a source object into a specific destination type.

 mapper.set(dog).converter("height", new IntegerStringConverter())

The code below will search the converter for the field that match the type and automatically convert it to the specific type. By default it will only look for built-in converters or the classes inside the package of ModelMapper you instantiated

 mapper.set(dog).convertFieldToType("height", String.class)

@Excluded

Exclude the field of your dto class

  @Excluded
  private List<PetDto> pets = new ArrayList<>();

@FieldName

The field will map as the value of the annotation

  @FieldName("petDto")
  private List<Pet> pets = new ArrayList<>();

@MapTo(value=[class])

Map one or more fields to a different class

  • value
  • collection
  • type
  @MapTo(PetDto.class)
  private Pet pet;

  @MapTo(value = PetDto.class, collection = true)
  private Set<Pet> pets = new HashSet<>();

@NoRepeat

The mapper will ignore the recursive instances of a class

  @NoRepeat
  public class Person { ...

In order to create your own custom converter you need to extends the TypeConverterAdapter and add the two generic type

  @TypeConverter
  public class MyConverter extends TypeConverterAdapter<Long, Dog> { ...

You have to override and write your own implementations

  @Override
  public Dog convertTo(Long id) {
     return dogRepository.findById(id);
  }

  @Override
  public Long convertFrom(Dog dog) {
     return dog.getId();
  }

  @Override
  public Object convert(Object o) {
     if(o instanceof Long)
       return convertTo(o);
     else
       return convertFrom(o);
  }

And that's all, you just need to use your custom converter:

  public class Person {
    String name;
    Long dogId;
  }
  
  public class PersonDto {
    String name;
    Dog dog;
  }
  new ModelMapper("my.package").set(person)
    .field("dogId", "dog")
    .convertFieldToType("dog", MyConverter.class)
    .getTransaction().mapTo(PersonDto.class);

Download

Download the latest jar here or via:

  • Gradle
allprojects {
  repositories {

    maven { url 'https://jitpack.io' }
  }
}
dependencies {
   compile 'com.github.erafaelmanuel:mapfierJ:v1.0-beta.5.0'
}
  • Maven
<repositories>
  <repository>
      <id>jitpack.io</id>
      <url>https://jitpack.io</url>
  </repository>
</repositories>
<dependencies>
  <dependency>
    <groupId>com.github.erafaelmanuel</groupId>
    <artifactId>mapfierJ</artifactId>
    <version>v1.0-beta.5.0</version>
  </dependency>
</dependencies>

License

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

About

a mapping library for java that recursively copies data from one object to another.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Java 100.0%