Java - POJO Builder


Source link: https://github.com/jenzz/Java-PojoBuilder

Java - POJO Builder

  • A Java code generator for the builder pattern using annotation processing

Usage

Just annotate your POJOs with the @Builder annotation, like so:

@Builder public class User {

String name;
int age;
Address address; 
}
@Builder public class Address {

String street;
String postcode;
String city; 
}

When you compile your project, a new Builder class will be generated for each annotated POJO, like so:

public final class UserBuilder {

 private String name;
private int age;
private Address address;
 public static UserBuilder user() {

  return new UserBuilder();

}

 public static UserBuilder user(User from) {

  UserBuilder builder = new UserBuilder();

  builder.name = from.name;
  builder.age = from.age;
  builder.address = from.address;
  return builder;

}

 public UserBuilder name(String name) {

  this.name = name;
  return this;

}

 public UserBuilder age(int age) {

  this.age = age;
  return this;

}

 public UserBuilder address(Address address) {

  this.address = address;
  return this;

}

 public UserBuilder address(AddressBuilder addressBuilder) {

  this.address = addressBuilder.build();

  return this;

}

 public User build() {

  User user = new User();

  user.name = name;
  user.age = age;
  user.address = address;
  return user;

}
 
}

Using static imports, you can then create those objects as simple as:

User user = user()
.name("Bob The Builder")
.age(25)
.address(

 address()

 .street("10 Hight Street")

 .postcode("WC2")

 .city("London"))
.build();

Fields annotated with @Ignore will be ignored.

Example

Check out the sample project for an example implementation.

The sample project is currently an Android module, but it should work with a pure Java project just as well. If somebody knows how to setup an annotation processor for a Java module using Gradle, please let me know. The apt plugin currently supports only Android modules.

Download

Grab it via Gradle:

compile 'com.jenzz.pojobuilder:api:1.0' apt 'com.jenzz.pojobuilder:processor:1.0'

You only need to include the api module (contains just the annotations) as a real dependency.

The annotation processor module can be a compile time only dependency. To do that I highly recommend using Hugo Visser's great apt plugin.

License

This project is licensed under the MIT License.

Resources

Remove boilerplate code for using SharePreference to persist app's config data.

Celebrate more with this lightweight confetti particle system 🎊 Create realistic confetti by implementing this easy to use library.

A library for draggable boardview.

Kloud is a Kotlin implementation of a new architecture idea that I named cloud, it's based on system communication without any type of instance reference or static properties, in a robust, simple and reactive way.

It is a debugging tool for measuring simple network traffic.

A simple cache for android.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes