Skip to content

h908714124/crate

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

crate

Crate generates a variant of the builder pattern, where each constructor parameter is represented by a separate object. Each of these "step" objects is fully immutable and represents a partial invocation of the constructor.

Basic example

Put the @Crate annotation on a class. The class must have a non-private constructor.

@Crate
class Animal {

  final String name;
  final boolean good;

  Animal(String name, boolean good) {
    this.name = name;
    this.good = good;
  }
}

A class Animal_Crate will be generated in the same package. You can use the static Animal_Crate.buider() method to obtain an instance.

If you'd also like a "normal" builder for updating, you may want to combine this with readable.

The following example shows how to add the usual builder and toBuilder methods:

Example: combining crate and readable

@Readable
@Crate
final class Animal {

  final String name;
  final int numberOfLegs;

  Animal(String name, int numberOfLegs) {
    this.name = name;
    this.numberOfLegs = numberOfLegs;
  }

  static Animal_Crate builder() {
    return Animal_Crate.builder();
  }

  Animal_Builder toBuilder() {
    return Animal_Builder.builder(this);
  }
}

If you'd rather have Animal implemented by auto-value, you need to use the @AutoCrate annotation instead.

Example: auto-crate

@AutoCrate
@AutoValue
abstract class Animal {

  abstract String name();
  abstract int numberOfLegs();

  static Animal_Crate builder() {
    return Animal_Crate.builder();
  }
}

Maven Dependency

Maven Central

<dependency>
  <groupId>com.github.h908714124</groupId>
  <artifactId>crate</artifactId>
  <version>1.1</version>
  <scope>provided</scope>
</dependency>