Anshul Gautam
Utopian Nerd

Utopian Nerd

BoB the Builder of Beans Plugin for IntelliJ

Photo by Markus Winkler on Unsplash

BoB the Builder of Beans Plugin for IntelliJ

Anshul Gautam's photo
Anshul Gautam
·May 15, 2022·

6 min read

Subscribe to my newsletter and never miss my upcoming articles

Table of contents

  • The Builder Pattern
  • BoB the Builder of Beans
  • Installing this plugin
  • Using this plugin
  • Conclusion

When you are implementing Builder Pattern in your code, I am sure you must have realized it's repetitive at places, and involves lot of typing. The boiler plate code will keep on increasing if you have more number of properties in your class.

BoB the Builder of Beans is one such plugin present for IntelliJ using which you can create your bean builder just with a shortcut.

The Builder Pattern

Builder Pattern is a creational design pattern that helps you create complex objects through an alternative way, with lots of possible configuration options.

This pattern is really helpful when you want to get rid of long telescoping constructors in your code for object creation at different places.

Builder pattern supports chaining of initialization of object's properties.

eg:

myBuilder->setValueA(1)->setValueB(2)->build()

BoB the Builder of Beans

BoB the Builder of Beans is a plugin that allows you to generate Java boilerplate code effortlessly! BoB allows you to specify just fields of a Java class and then generate a patterned class with standard getters, hashcode and equals, and of course a builder.

image.png

It is perfect for generated JSON serialization classes or data transfer objects.

But wait why another plugin, when you already have shortcuts provided inside IntelliJ to create Getters/Setters, Constructors, just with a bit more effort isn't it an easy go to create Builder for the bean also? Well, it's not.

With BoB not only you can create Builders, but can also tune in even more granular properties so that creating a similar type of builders across the entire project is achieved. With bob.propeties you are basically overriding the defaults of the plugin and tuning in your own custom settings. You can the place this file in the project root folder, or may be distribute it to you fellow developers so that they can also make use of it and at the end you will get uniform builders across the entire project.

Installing this plugin

You can install this plugin from the IntelliJ IDEA marketplace, or directly from the official link here.

For installing it via Market place, in IntelliJ IDEA go to File -> Settings -> Plugins -> Marketplace.

Search for 'BoB the builder of beans' in the dialog, and click on install.

image.png

Using this plugin

Go to the class where you would want to implement the builder. In the example below, I have few properties defined in my class.

image.png

We just need to right click on this class -> Generate -> BoB the Builder of Beans

image.png

And there you go. All the key components of your Builder has been created, including getters and setters as well.

image.png

This plugin also creates the toString(), hashCode and equals() method for you.

The complete generated class using this plugin is shown below:

public class Student {

  private String fullName;
  private String fatherName;
  private String motherName;
  private Date dateOfBirth;
  private String homeAddress;
  private String branch;
  private long rollNumber;
  private boolean isHostler;
  private boolean isSpeciallyAbled;

  // region Constructor, getters, setters, equals, hashCode, toString, builder -- generated by BoB the Builder of Beans
  // The code below has been generated by BoB the Builder of Beans based on the class' fields.
  // Everything after this comment will be regenerated if you invoke BoB again.
  // If you don't know who BoB is, you can find him here: https://bitbucket.org/atlassianlabs/bob-the-builder-of-beans

  protected Student(String fullName, String fatherName, String motherName, Date dateOfBirth,
      String homeAddress, String branch, long rollNumber, boolean isHostler,
      boolean isSpeciallyAbled) {
    this.fullName = Objects.requireNonNull(fullName);
    this.fatherName = Objects.requireNonNull(fatherName);
    this.motherName = Objects.requireNonNull(motherName);
    this.dateOfBirth = Objects.requireNonNull(dateOfBirth);
    this.homeAddress = Objects.requireNonNull(homeAddress);
    this.branch = Objects.requireNonNull(branch);
    this.rollNumber = rollNumber;
    this.isHostler = isHostler;
    this.isSpeciallyAbled = isSpeciallyAbled;
  }

  // region Getters and setters -- generated by BoB the Builder of Beans
  public String getFullName() {
    return fullName;
  }

  public void setFullName(String fullName) {
    this.fullName = Objects.requireNonNull(fullName);
  }

  public String getFatherName() {
    return fatherName;
  }

  public void setFatherName(String fatherName) {
    this.fatherName = Objects.requireNonNull(fatherName);
  }

  public String getMotherName() {
    return motherName;
  }

  public void setMotherName(String motherName) {
    this.motherName = Objects.requireNonNull(motherName);
  }

  public Date getDateOfBirth() {
    return dateOfBirth;
  }

  public void setDateOfBirth(Date dateOfBirth) {
    this.dateOfBirth = Objects.requireNonNull(dateOfBirth);
  }

  public String getHomeAddress() {
    return homeAddress;
  }

  public void setHomeAddress(String homeAddress) {
    this.homeAddress = Objects.requireNonNull(homeAddress);
  }

  public String getBranch() {
    return branch;
  }

  public void setBranch(String branch) {
    this.branch = Objects.requireNonNull(branch);
  }

  public long getRollNumber() {
    return rollNumber;
  }

  public void setRollNumber(long rollNumber) {
    this.rollNumber = rollNumber;
  }

  public boolean getIsHostler() {
    return isHostler;
  }

  public void setIsHostler(boolean isHostler) {
    this.isHostler = isHostler;
  }

  public boolean getIsSpeciallyAbled() {
    return isSpeciallyAbled;
  }

  public void setIsSpeciallyAbled(boolean isSpeciallyAbled) {
    this.isSpeciallyAbled = isSpeciallyAbled;
  }// endregion Getters and setters

  // region hashCode() and equals() -- generated by BoB the Builder of Beans
  @Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }
    if (o == null || getClass() != o.getClass()) {
      return false;
    }

    Student that = (Student) o;

    return Objects.equals(this.getFullName(), that.getFullName()) && Objects.equals(
        this.getFatherName(), that.getFatherName()) && Objects.equals(this.getMotherName(),
        that.getMotherName()) && Objects.equals(this.getDateOfBirth(), that.getDateOfBirth())
        && Objects.equals(this.getHomeAddress(), that.getHomeAddress()) && Objects.equals(
        this.getBranch(), that.getBranch()) && Objects.equals(this.getRollNumber(),
        that.getRollNumber()) && Objects.equals(this.getIsHostler(), that.getIsHostler())
        && Objects.equals(this.getIsSpeciallyAbled(), that.getIsSpeciallyAbled());
  }

  @Override
  public int hashCode() {
    return Objects.hash(getFullName(), getFatherName(), getMotherName(), getDateOfBirth(),
        getHomeAddress(), getBranch(), getRollNumber(), getIsHostler(), getIsSpeciallyAbled());
  }// endregion hashCode() and equals()

  // region toString() -- generated by BoB the Builder of Beans
  @Override
  public String toString() {
    return MoreObjects.toStringHelper(this)
        .add("fullName", getFullName())
        .add("fatherName", getFatherName())
        .add("motherName", getMotherName())
        .add("dateOfBirth", getDateOfBirth())
        .add("homeAddress", getHomeAddress())
        .add("branch", getBranch())
        .add("rollNumber", getRollNumber())
        .add("isHostler", getIsHostler())
        .add("isSpeciallyAbled", getIsSpeciallyAbled())
        .toString();
  }// endregion toString()

  // region Builder -- generated by BoB the Builder of Beans
  public static Student.Builder builder() {
    return new Student.Builder();
  }

  public static Student.Builder builder(Student data) {
    return new Student.Builder(data);
  }

  public static final class Builder {

    private String fullName;
    private String fatherName;
    private String motherName;
    private Date dateOfBirth;
    private String homeAddress;
    private String branch;
    private long rollNumber;
    private boolean isHostler;
    private boolean isSpeciallyAbled;

    private Builder() {
    }

    private Builder(Student initialData) {
      this.fullName = initialData.fullName;
      this.fatherName = initialData.fatherName;
      this.motherName = initialData.motherName;
      this.dateOfBirth = initialData.dateOfBirth;
      this.homeAddress = initialData.homeAddress;
      this.branch = initialData.branch;
      this.rollNumber = initialData.rollNumber;
      this.isHostler = initialData.isHostler;
      this.isSpeciallyAbled = initialData.isSpeciallyAbled;
    }

    public Builder setFullName(String fullName) {
      this.fullName = fullName;
      return this;
    }

    public Builder setFatherName(String fatherName) {
      this.fatherName = fatherName;
      return this;
    }

    public Builder setMotherName(String motherName) {
      this.motherName = motherName;
      return this;
    }

    public Builder setDateOfBirth(Date dateOfBirth) {
      this.dateOfBirth = dateOfBirth;
      return this;
    }

    public Builder setHomeAddress(String homeAddress) {
      this.homeAddress = homeAddress;
      return this;
    }

    public Builder setBranch(String branch) {
      this.branch = branch;
      return this;
    }

    public Builder setRollNumber(long rollNumber) {
      this.rollNumber = rollNumber;
      return this;
    }

    public Builder setIsHostler(boolean isHostler) {
      this.isHostler = isHostler;
      return this;
    }

    public Builder setIsSpeciallyAbled(boolean isSpeciallyAbled) {
      this.isSpeciallyAbled = isSpeciallyAbled;
      return this;
    }

    public Student build() {
      return new Student(fullName, fatherName, motherName, dateOfBirth, homeAddress, branch,
          rollNumber, isHostler, isSpeciallyAbled);
    }
  }// endregion Builder
  // endregion Constructor, getters, setters, equals, hashCode, toString, builder
}

Conclusion

This plugin is very useful and a must have for Java developers. I hope this post gave you complete idea around this plugin, and how you can install this and use.

If you have never used this plugin before, do give it a try. I am sure you will love this too like I do.

Did you find this article valuable?

Support Anshul Gautam by becoming a sponsor. Any amount is appreciated!

See recent sponsors Learn more about Hashnode Sponsors
 
Share this