builder-pattern-generator

Library to generate "builder pattern" source code

License

License

GroupId

GroupId

org.riversun
ArtifactId

ArtifactId

builder-pattern-generator
Last Version

Last Version

1.0.0
Release Date

Release Date

Type

Type

jar
Description

Description

builder-pattern-generator
Library to generate "builder pattern" source code
Project URL

Project URL

https://github.com/riversun/java-builder-pattern-source-generator
Source Code Management

Source Code Management

https://github.com/riversun/java-builder-pattern-source-generator

Download builder-pattern-generator

How to add to project

<!-- https://jarcasting.com/artifacts/org.riversun/builder-pattern-generator/ -->
<dependency>
    <groupId>org.riversun</groupId>
    <artifactId>builder-pattern-generator</artifactId>
    <version>1.0.0</version>
</dependency>
// https://jarcasting.com/artifacts/org.riversun/builder-pattern-generator/
implementation 'org.riversun:builder-pattern-generator:1.0.0'
// https://jarcasting.com/artifacts/org.riversun/builder-pattern-generator/
implementation ("org.riversun:builder-pattern-generator:1.0.0")
'org.riversun:builder-pattern-generator:jar:1.0.0'
<dependency org="org.riversun" name="builder-pattern-generator" rev="1.0.0">
  <artifact name="builder-pattern-generator" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.riversun', module='builder-pattern-generator', version='1.0.0')
)
libraryDependencies += "org.riversun" % "builder-pattern-generator" % "1.0.0"
[org.riversun/builder-pattern-generator "1.0.0"]

Dependencies

There are no dependencies for this project. It is a standalone project that does not depend on any other jars.

Project Modules

There are no modules declared in this project.

Overview

A library that automatically generates the source code of the "builder pattern"

It is licensed under MIT.

How to use

Dependency

Add dependency.

<dependency>
	<groupId>org.riversun</groupId>
	<artifactId>builder-pattern-generator</artifactId>
	<version>1.0.0</version>
</dependency>

Example1:Generate "Effective Java" style builder pattern source code.

package examples;

import org.riversun.dp.builder.BuilderPattern;
import org.riversun.dp.builder.Variable;

public class Example1 {

    public static void main(String[] args) {

        String sourceCode = new BuilderPattern.Builder()
                .packageName("com.example")
                .className("Person")
                // specify variable type and name
                .addVariable(new Variable("String", "name"))
                .addVariable(new Variable("String", "address"))
                // In the case of "List<>", name of singular and plural can be specified
                .addVariable(new Variable("List<String>", "hobbies", "hobby"))
                .build()
                .get();

        System.out.println(sourceCode);

    }
}

Result

You can get following source code. If you specify a variable of type "List<>", the method named "add[Variable]" is also automatically generated.

package com.example;

import java.util.ArrayList;
import java.util.List;


public class Person {

	private String name;
	private String address;
	private List<String> hobbies;

	public static class Builder {
	
		private String name;
		private String address;
		private List<String> hobbies = new ArrayList<String>();

		public Builder() {	
		}
		
		Builder(String name, String address, List<String> hobbies) {	
			this.name = name; 
			this.address = address; 
			this.hobbies = hobbies; 			
		}
		
		public Builder name(String name){
			this.name = name;
			return Builder.this;
		}

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

		public Builder hobbies(List<String> hobbies){
			this.hobbies = hobbies;
			return Builder.this;
		}

		public Builder addHobby(String hobby){
			this.hobbies.add(hobby);
			return Builder.this;
		}

		public Person build() {
		
			if (name == null || address == null || hobbies == null) {
				throw new NullPointerException();
			}
			
			return new Person(this);
		}
	}

	private Person(Builder builder) {
		this.name = builder.name; 
		this.address = builder.address; 
		this.hobbies = builder.hobbies; 	
	}

	public void doSomething() {
		// do something
	}
}

Example2:Save generated code to directory.

package examples;

import java.io.File;

import org.riversun.dp.builder.BuilderPattern;
import org.riversun.dp.builder.Variable;

public class Example2 {

    public static void main(String[] args) {

        new BuilderPattern.Builder()
                .packageName("com.example")
                .className("Person")
                // specify variable type and name
                .addVariable(new Variable("String", "name"))
                .addVariable(new Variable("String", "address"))
                // In the case of "List<>", name of singular and plural can be specified
                .addVariable(new Variable("List<String>", "hobbies", "hobby"))
                .build()
                .save(new File("c:/temp"));

    }
}

Versions

Version
1.0.0