/**
 * Copyright (c) 2016, All partners of the iTesla project (http://www.itesla-project.eu/consortium)
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */
package com.powsybl.iidm.network.impl;

import com.powsybl.iidm.network.impl.util.Ref;

/**
 *
 * @author Geoffroy Jamgotchian <geoffroy.jamgotchian at rte-france.com>
 */
class TerminalBuilder {

    private final Ref<? extends MultiStateObject> network;

    private final Validable validable;

    private Integer node;

    private String bus;

    private String connectableBus;

    TerminalBuilder(Ref<? extends MultiStateObject> network, Validable validable) {
        this.network = network;
        this.validable = validable;
    }

    TerminalBuilder setBus(String bus) {
        this.bus = bus;
        return this;
    }

    TerminalBuilder setConnectableBus(String connectableBus) {
        this.connectableBus = connectableBus;
        return this;
    }

    TerminalBuilder setNode(Integer node) {
        this.node = node;
        return this;
    }

    TerminalExt build() {
        String connectionBus = getConnectionBus();
        if (node != null && connectionBus != null) {
            throw new ValidationException(validable,
                    "connection node and connection bus are exclusives");
        }

        return node != null ? new NodeTerminal(network, node)
                            : new BusTerminal(network, connectionBus, bus != null);
    }

    private String getConnectionBus() {
        if (bus != null) {
            if ((connectableBus != null) && (!bus.equals(connectableBus))) {
                throw new ValidationException(validable, "connection bus is different to connectable bus");
            }

            return bus;
        } else {
            return connectableBus;
        }
    }
}

@@ -1455,66 +1455,157 @@
     
-return 
+if (
 node 
-!
+=
 = null
- ? new NodeTerminal(network, node)%0A    
+) %7B%0A            if (connectionBus == null) %7B%0A                throw new ValidationException(validable, %22connectable bus is not set%22);%0A
     
@@ -1604,32 +1604,35 @@
 %22);%0A            
+%7D%0A%0A
             : ne
@@ -1627,17 +1627,22 @@
         
-:
+return
  new Bus
@@ -1688,16 +1688,95 @@
  null);%0A
+        %7D else %7B%0A            return new NodeTerminal(network, node);%0A        %7D%0A
     %7D%0A%0A 

Fix error message if bus or connectableBus are not specified