EclipseのSpark Builder Generatorプラグインによるbuilder生成

https://twitter.com/java/status/997711950963294208

Spark Builder GeneratorというEclipseプラグインでいわゆるbuilderパターンのコードが生成できる、とのこと。とりあえず使ってみる。

## Generate regular builder

```java
import javax.annotation.Generated;

public class Person {
	private String firstName;

	private String middleName;

	private String lastName;

	private String city;

	private String jobTitle;

	@Generated("SparkTools")
	private Person(Builder builder) {
		this.firstName = builder.firstName;
		this.middleName = builder.middleName;
		this.lastName = builder.lastName;
		this.city = builder.city;
		this.jobTitle = builder.jobTitle;
	}

	/**
	 * Creates builder to build {@link Person}.
	 * @return created builder
	 */
	@Generated("SparkTools")
	public static Builder builder() {
		return new Builder();
	}

	/**
	 * Builder to build {@link Person}.
	 */
	@Generated("SparkTools")
	public static final class Builder {
		private String firstName;
		private String middleName;
		private String lastName;
		private String city;
		private String jobTitle;

		private Builder() {
		}

		public Builder withFirstName(String firstName) {
			this.firstName = firstName;
			return this;
		}

		public Builder withMiddleName(String middleName) {
			this.middleName = middleName;
			return this;
		}

		public Builder withLastName(String lastName) {
			this.lastName = lastName;
			return this;
		}

		public Builder withCity(String city) {
			this.city = city;
			return this;
		}

		public Builder withJobTitle(String jobTitle) {
			this.jobTitle = jobTitle;
			return this;
		}

		public Person build() {
			return new Person(this);
		}
	}
	
	
}
```

使うときはこんな感じ。

```java
Person person = Person.builder()
    .withCity("city")
    .withFirstName("firstName")
    .build();
```

## Generate staged builder
なんか選択画面が出てくる。

![spark001.jpg](https://qiita-image-store.s3.amazonaws.com/0/31934/e8caeda1-18da-dbd5-a913-83e39ad4ad76.jpeg)

```java
public class Person2 {
	private String firstName;

	private String middleName;

	private String lastName;

	private String city;

	private String jobTitle;

	@Generated("SparkTools")
	private Person2(Builder builder) {
		this.firstName = builder.firstName;
		this.middleName = builder.middleName;
		this.lastName = builder.lastName;
		this.city = builder.city;
		this.jobTitle = builder.jobTitle;
	}

	/**
	 * Creates builder to build {@link Person2}.
	 * @return created builder
	 */
	@Generated("SparkTools")
	public static IFirstNameStage builder() {
		return new Builder();
	}

	@Generated("SparkTools")
	public interface IFirstNameStage {
		public IMiddleNameStage withFirstName(String firstName);
	}

	@Generated("SparkTools")
	public interface IMiddleNameStage {
		public ILastNameStage withMiddleName(String middleName);
	}

	@Generated("SparkTools")
	public interface ILastNameStage {
		public ICityStage withLastName(String lastName);
	}

	@Generated("SparkTools")
	public interface ICityStage {
		public IJobTitleStage withCity(String city);
	}

	@Generated("SparkTools")
	public interface IJobTitleStage {
		public IBuildStage withJobTitle(String jobTitle);
	}

	@Generated("SparkTools")
	public interface IBuildStage {
		public Person2 build();
	}

	/**
	 * Builder to build {@link Person2}.
	 */
	@Generated("SparkTools")
	public static final class Builder
			implements IFirstNameStage, IMiddleNameStage, ILastNameStage, ICityStage, IJobTitleStage, IBuildStage {
		private String firstName;
		private String middleName;
		private String lastName;
		private String city;
		private String jobTitle;

		private Builder() {
		}

		@Override
		public IMiddleNameStage withFirstName(String firstName) {
			this.firstName = firstName;
			return this;
		}

		@Override
		public ILastNameStage withMiddleName(String middleName) {
			this.middleName = middleName;
			return this;
		}

		@Override
		public ICityStage withLastName(String lastName) {
			this.lastName = lastName;
			return this;
		}

		@Override
		public IJobTitleStage withCity(String city) {
			this.city = city;
			return this;
		}

		@Override
		public IBuildStage withJobTitle(String jobTitle) {
			this.jobTitle = jobTitle;
			return this;
		}

		@Override
		public Person2 build() {
			return new Person2(this);
		}
	}
	
	
}

```

使うときはこんな感じ。

```java
Person2 person2 = Person2.builder()
    .withFirstName("firstName")
    .withMiddleName("middleName")
    .withLastName("lastName")
    .withCity("city")
    .withJobTitle("jobTitle")
    .build();
```

## 設定
なんか色々設定できる（ぜんぜん試してない）

![spark002.jpg](https://qiita-image-store.s3.amazonaws.com/0/31934/a536932c-ef15-daf8-1a33-7d2069e2eaf1.jpeg)