/**
 * Copyright (c) 2020, RTE (http://www.rte-france.com)
 * 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.matpower.model;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.*;

/**
 * @author Christian Biasuzzi <christian.biasuzzi@techrain.eu>
 */
public class MatpowerModel {

    @JsonProperty("caseName")
    private String caseName;
    private double baseMva;
    private String version;

    private final List<MBus> buses = new ArrayList<>();

    private final Map<Integer, MBus> busByNum = new HashMap<>();

    private final List<MGen> generators = new ArrayList<>();

    private final Map<Integer, List<MGen>> generatorsByBusNum = new HashMap<>();

    private final List<MBranch> branches = new ArrayList<>();

    @JsonCreator
    public MatpowerModel(@JsonProperty("caseName") String caseName) {
        this.caseName = Objects.requireNonNull(caseName);
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public String getCaseName() {
        return caseName;
    }

    public double getBaseMva() {
        return baseMva;
    }

    public void setBaseMva(double baseMva) {
        this.baseMva = baseMva;
    }

    public void addBus(MBus bus) {
        Objects.requireNonNull(bus);
        buses.add(bus);
        busByNum.put(bus.getNumber(), bus);
    }

    public List<MBus> getBuses() {
        return buses;
    }

    public MBus getBusByNum(int num) {
        return busByNum.get(num);
    }

    public List<MGen> getGenerators() {
        return generators;
    }

    public void addGenerator(MGen generator) {
        Objects.requireNonNull(generator);
        generators.add(generator);
        generatorsByBusNum.computeIfAbsent(generator.getNumber(), k -> new ArrayList<>())
                .add(generator);
    }

    public List<MGen> getGeneratorsByBusNum(int busNum) {
        return generatorsByBusNum.getOrDefault(busNum, Collections.emptyList());
    }

    public List<MBranch> getBranches() {
        return branches;
    }

    public void addBranch(MBranch branch) {
        Objects.requireNonNull(branch);
        branches.add(branch);
    }
}

@@ -1711,143 +1711,560 @@
 lic 
-MBus getBusByNum(int num) %7B%0A        return busByNum.get(num);%0A    %7D%0A%0A    public List%3CMGen%3E getGenerators() %7B%0A        return generators;
+void setBuses(List%3CMBus%3E buses) %7B%0A        Objects.requireNonNull(buses);%0A        this.buses.clear();%0A        for (MBus bus : buses) %7B%0A            addBus(bus);%0A        %7D%0A    %7D%0A%0A    public MBus getBusByNum(int num) %7B%0A        return busByNum.get(num);%0A    %7D%0A%0A    public List%3CMGen%3E getGenerators() %7B%0A        return generators;%0A    %7D%0A%0A    public void setGenerators(List%3CMGen%3E generators) %7B%0A        Objects.requireNonNull(generators);%0A        this.generators.clear();%0A        for (MGen generator : generators) %7B%0A            addGenerator(generator);%0A        %7D
 %0A   
@@ -2733,32 +2733,249 @@
 ranches;%0A    %7D%0A%0A
+    public void setBranches(List%3CMBranch%3E branches) %7B%0A        Objects.requireNonNull(branches);%0A        this.branches.clear();%0A        for (MBranch branch : branches) %7B%0A            addBranch(branch);%0A        %7D%0A    %7D%0A%0A
     public void 

Fix Matpower json read (#1911)