What is bean-cp?

Today modern applications usually contain more than one object model of the same real-life objects. The reason behind that approach is need for model optimization for specific use or system modularization. For example, domain model could be used to organize your application logic and is mapped to view models optimized for UI layer. Another example are data transfer objects – optimized model for transferring data over network and integration between systems using technologies like WebServices.

The drawback of this approach is need for extra mapping code to map data between models. In fact those models are usually similar, so writing mapping code could be really boring. This is why mapping frameworks were invented.

Bean-cp is object to object mapping framework for Java

Library is targeted for Java version 8 to bring the power of lambda expressions to object mapping.

Release 1.0.2 is available for download and in Maven's central repository:

<dependency>
	<groupId>com.github.erchu</groupId>
	<artifactId>beancp</artifactId>
	<version>1.0.2</version>
</dependency>

Why bean-cp?

Easy to read and standardized mapping code

Lambda expressions and fluent API comes to play...

Mapper mapper = new MapperBuilder()
    .addMap(Order.class, OrderOverviewDto.class, (conf, source, destination) -> conf
        .bind(source::getDate, destination::setDate)
        .bind(source::getPaymentDueDay, destination::setPaymentDueDay)
        .bind(source::getStatus, destination::setStatus)
        .bind(source::getTotal, destination::setTotal)
        .bind(source.getCustomer()::getName, destination::setCustomerName))
    .buildMapper();

Saves your time

Conventions feature lets you define mapping quickly. Above example can be fully replaced by convention:

Mapper mapper = new MapperBuilder()
    .addMap(Order.class, OrderOverviewDto.class, (conf, source, destination) -> conf
        .useConvention(NameBasedMapConvention.get().enableFlattening()))
    .buildMapper();

Both mechanism can be mixed:

Mapper mapper = new MapperBuilder()
    .addMap(Order.class, OrderOverviewDto.class, (conf, source, destination) -> conf
        .useConvention(NameBasedMapConvention.get().enableFlattening())
        .bind(() -> source.getStatus() == OrderStatus.OVERDUE, destination::isOverdue))
    .buildMapper();

Map anything

When you face non standard situation converters comes to rescue:

Mapper mapper = new MapperBuilder()
	.addConverter(int.class, Range.class, source -> Range.of(source, Integer.MAX_VALUE))
	.buildMapper();

Extensibility

If you need something different than name-based convention you can implement your own mapping convention. See MapConvention interface. In fact NameBasedMapConvention is provided as implementation of this interface with no extra magic.

public interface MapConvention {

  /**
   * Returns list of bindings for specified source and destination classes. Must
   * be thread-safe.
   *
   * @param mappingsInfo current mapping information.
   * @param sourceClass source class.
   * @param destinationClass destination class.
   * @return found bindings.
   */
  List<Binding> getBindings(
      final MappingInfo mappingsInfo,
      final Class sourceClass,
      final Class destinationClass);
}

There is more

Bean-cp gives to you some useful utilities like common collections mapping or null substitution.

It is free for any use with source code publicly available

Bean-cp is licensed under LGPL 3.0, so you can use it for free both, for open source and commercial projects. Source code publicly available on GitHub and you are welcome to contribute to the project or create own fork.

License

Copyright (©) 2014, Rafał Chojnacki, All rights reserved.

This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 3.0 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.