Coder Social home page Coder Social logo

beanutils's Introduction

BeanUtils

Build Status Maven Central

A wrapper for cglib BeanCopier providing conversion between JavaBeans.

Introduction

Provides simple and convenient APIs to copy properties/do conversion between JavaBeans. For performance, cglib is chosen as the underlying library. Currently, there are two tool classes for conversion, BeanUtils and BeanConverter. BeanUtils provides a straight way to use but it requires that types of properties to map MUST be the same, while BeanConverter provides a more flexible way to use.

Usage

Maven Dependency

Add the following dependency to your project's pom.xml.

<dependency>
    <groupId>com.github.mottox</groupId>
    <artifactId>bean-utils</artifactId>
    <version>1.1.0</version>
</dependency>

API

BeanUtils provides the following methods:

  • copyProperties(Object source, Object target)
    Copy the property values of the given source bean into the target bean.
  • <T> T convert(Object source, Class<T> clazz)
    Convert the given source bean to a target bean of specified type.

BeanConverter provides the same methods above. Below is the list of differences between BeanUtils and BeanConverter.

  1. The above two methods are static in BeanUtils, but non static in BeanConverter.
  2. BeanConverter provides relatively flexible conversion strategy, so you can create your custom mapping method for a BeanConverter instance.

Examples

It's quite easy and convenient to use BeanUtils for JavaBean conversion.

BeanUtils

public class SourceBean {
    private String name;

    private Integer age;

    private Gender gender;

    private Double height;

    private BigDecimal wealth;

    SourceBean(String name, Integer age, Gender gender, Double height, BigDecimal wealth) {
        this.name = name;
        this.age = age;
        this.gender = gender;
        this.height = height;
        this.wealth = wealth;
    }

    // Getters and setters omitted
}

public class TargetBean {
    private String name;

    private Integer age;

    private Gender gender;

    private Double height;

    private BigDecimal wealth;

    TargetBean() {
    }

    TargetBean(String name, Integer age, Gender gender, Double height, BigDecimal wealth) {
        this.name = name;
        this.age = age;
        this.gender = gender;
        this.height = height;
        this.wealth = wealth;
    }

    // Getters and setters omitted
}
SourceBean source = new SourceBean("Peter", 34, Gender.MALE, 1.85, BigDecimal.valueOf(123456789.87654321));

TargetBean target = BeanUtils.convert(source, TargetBean.class);
Assert.assertEquals(source.name, target.name);
Assert.assertEquals(source.age, target.age);
Assert.assertEquals(source.gender, target.gender);
Assert.assertEquals(source.height, target.height);
Assert.assertEquals(source.wealth, target.wealth);

BeanConverter

public class SourceBean {
    private String name;

    private Integer age;

    private Gender gender;

    private Double height;

    private BigDecimal wealth;

    public SourceBean(String name, Integer age, Gender gender, Double height, BigDecimal wealth) {
        this.name = name;
        this.age = age;
        this.gender = gender;
        this.height = height;
        this.wealth = wealth;
    }

    // Getters and setters omitted
}

public class TargetBean {
    private String name;

    private int age;

    private int gender;

    private BigDecimal height;

    private String wealth;

    public TargetBean() {
    }

    public TargetBean(String name, int age, int gender, BigDecimal height, String wealth) {
        this.name = name;
        this.age = age;
        this.gender = gender;
        this.height = height;
        this.wealth = wealth;
    }

    // Getters and setters omitted
}
BeanConverter converter = BeanConverterBuilder.create()
        .registerConverter((TypeConverter<Gender, Integer>) Gender::getValue)
        .registerConverter((TypeConverter<Double, BigDecimal>) BigDecimal::valueOf)
        .registerConverter((TypeConverter<BigDecimal, String>) BigDecimal::toPlainString)
        .build();

TargetBean target = converter.convert(source, TargetBean.class);

Assert.assertEquals(source.name, target.name);
Assert.assertEquals((int) source.age, target.age);
Assert.assertEquals(source.gender.value, target.gender);
Assert.assertTrue(BigDecimal.valueOf(source.height).compareTo(target.height) == 0);
Assert.assertTrue(source.wealth.compareTo(new BigDecimal(target.wealth)) == 0);

Note that cglib BeanCopier will copy null properties.

License

MIT License

beanutils's People

Contributors

mottox avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.