StepBuilder Generator

Compatible with: IntelliJ IDEA Android Studio
11.04.2016
1 173

This plugin generates a Builder class following the Step Builder pattern so that you can build instances of your class more easily in a guided manner.

Here is an example:
    

   public class Server{

    final static int DEFAULT_PORT = 8080;

    private String protocol;
    private String url;
    private String ipAddress;
    private int port;
    private String description;
    private long uptime;

    private Server(Builder builder) {
        protocol = builder.protocol;
        url = builder.url;
        ipAddress = builder.ipAddress;
        port = builder.port;
        description = builder.description;
        uptime = builder.uptime;
    }

    public static IProtocol builder() {
        return new Builder();
    }


    public interface IBuild {
        Server build();
    }

    public interface IUptime {
        IBuild withUptime(long val);
    }

    public interface IDescription {
        IUptime withDescription(String val);
    }

    public interface IPort {
        IDescription withPort(int val);
    }

    public interface IIpAddress {
        IPort withIpAddress(String val);
    }

    public interface IUrl {
        IIpAddress withUrl(String val);
    }

    public interface IProtocol {
        IUrl withProtocol(String val);
    }

    public static final class Builder implements IUptime, IDescription, IPort, IIpAddress, IUrl, IProtocol, IBuild {
        private long uptime;
        private String description;
        private int port;
        private String ipAddress;
        private String url;
        private String protocol;

        private Builder() {
        }

        @Override
        public IBuild withUptime(long val) {
            uptime = val;
            return this;
        }

        @Override
        public IUptime withDescription(String val) {
            description = val;
            return this;
        }

        @Override
        public IDescription withPort(int val) {
            port = val;
            return this;
        }

        @Override
        public IPort withIpAddress(String val) {
            ipAddress = val;
            return this;
        }

        @Override
        public IIpAddress withUrl(String val) {
            url = val;
            return this;
        }

        @Override
        public IUrl withProtocol(String val) {
            protocol = val;
            return this;
        }

        public Server build() {
            return new Server(this);
        }
    }
}
    
    

Download plugin

Recent change notes

1.0.0 First release 1.0.1 Generated interfaces aren't public 1.0.2 Added option so that you can choose whether to generate interfaces as public or not

General usage instructions

Use Shift+Ctrl+S or Alt+Insert and select Step Builder. Choose the mandatory fields (the fields that must be set for an object of this class) and press OK.