Giter Site home page Giter Site logo

beanmapper's Introduction

BeanMapper

a tiny speedy BeanUtils.copyProperties replacement.

中文说明

Why

Beacuse copyProperties in BeanUtils (Spring or Apache Commons) is achieved by java reflection feature, it has serious performance issue when it is called a lot.

BeanMapper using javassist library to achieve transform code on the fly, by directly generate bytecode, it can provide performance near pure getter - setter code after first "compiled".

Limitation

  • Class must named as lower camel case JavaBean setter and getter.
  • Class must be public.

Import

Prebuilt package is provide by CI, it will automatically release it to Maven Central, import this library from Maven Central is enough.

Usage

BeanMapper library procide three BeanMapper to you.

StaticBeanMapper

StaticBeanMapper will automatically transform data by calling it using source bean class and target bean class instance.

StaticBeanMapper.copy(source, target);

StaticBeanMapper using HashMap and class full name to maintain a list, call the right transform method automatically. This will cause some performance issue, but it performace is much higher than using reflect.

BeanMapper

BeanMapper will create and maintain transform class. You need an instance of this class to transform code. One BeanMapper is related to one Source - Target Bean class pair.

BeanMapper has constructor to generate transform class.

BeanMapper mapper = new BeanMapper(Source.class, Target.class);
mapper.copy(source, target)

BeanMapperInPlace

BeanMapperInPlace is a magic usage to add fields and method on the fly. You only need source class and the type which you want to add.

BeanMapperInPlace vMapper = new BeanMapperInPlace(Source.class, Stream.of(new BeanMapperInPlace.TypePair("three", double.class)).collect(Collectors.toList()));
List<BeanMapperInPlace.DataPair> dataPairs = new ArrayList<>();
dataPairs.add(new BeanMapperInPlace.DataPair("three", 1.2));
Source target = vMapper.copy(base, dataPairs);

Then, you will get a return object cast to your original class type. It will contain these new field and data.

Performance

In 10000000 times copy test.

BeanUtils.copyProperties took 9809ms

StaticBeanMapper.copy took 100ms (First Compile) + 3633ms (10000000 times copy test)

BeanMapper.copy took 98ms (First Compile) + 1641ms (10000000 times copy test)

manually calling getter and setter took 1479ms

beanmapper's People

Contributors

cubesky avatar

Watchers

 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.