package model;

import java.io.*;

public class TitanDSM {
    private int sizeOfMatrix;
    private boolean[][] dataMatrix;
    private String[] nameOfClass;

    public TitanDSM(int sizeOfMatrix) {
        this.sizeOfMatrix = sizeOfMatrix;
        dataMatrix = new boolean[sizeOfMatrix][sizeOfMatrix];
        nameOfClass = new String[sizeOfMatrix];
        initNameOfClass();
    }

    public TitanDSM(File file) throws IOException {
        loadFromFile(file);
    }

    private void initNameOfClass() {
        for(int i = 0; i < sizeOfMatrix; i++) {
            nameOfClass[i] = "entity_" + i;
        }
    }

    public int getSize() {
        return sizeOfMatrix;
    }

    public void addEntity() {
        sizeOfMatrix++;
        nameOfClass[sizeOfMatrix] = "entity_" + sizeOfMatrix;
    }

    public boolean[][] getDataMatrix() {
        return dataMatrix;
    }

    public void setData(boolean data, int row, int col) {
        dataMatrix[row][col] = data;
    }

    public String[] getNames() {
        return nameOfClass;
    }

    public void setName(String newName, int location) {
        nameOfClass[location] = newName;
    }

    public void loadFromFile(File dsm) throws IOException {
        BufferedReader fileReader = new BufferedReader(new FileReader(dsm));
        String read;
        String[] temp;

        read = fileReader.readLine();
        sizeOfMatrix = Integer.parseInt(read);

        dataMatrix = new boolean[sizeOfMatrix][sizeOfMatrix];
        nameOfClass = new String[sizeOfMatrix];

        for (int i = 0; i < sizeOfMatrix; i++) {
            read = fileReader.readLine();
            temp = read.split(" ");
            for (int j = 0; j < sizeOfMatrix; j++) {
                dataMatrix[i][j] = Integer.parseInt(temp[j]) == 1;
            }
        }

        for (int i = 0; i < sizeOfMatrix; i++) {
            read = fileReader.readLine();
            nameOfClass[i] = read;
        }

        fileReader.close();
    }

    public void saveToFile(String fileName) throws IOException{
        BufferedWriter out = new BufferedWriter(new FileWriter(fileName));

        out.write(String.valueOf(sizeOfMatrix));
        out.newLine();

        for(int i = 0; i < sizeOfMatrix; i++) {
            for(int j = 0; j < sizeOfMatrix; j++) {
                out.write(dataMatrix[i][j] + " ");
            }
            out.newLine();
        }

        for(int i = 0; i < sizeOfMatrix; i++) {
            out.write(nameOfClass[i]);
            out.newLine();
        }

        out.close();
    }

}
@@ -26,16 +26,44 @@
 va.io.*;
+%0Aimport java.util.ArrayList;
 %0A%0Apublic
@@ -158,24 +158,33 @@
 private 
+ArrayList%3C
 String
-%5B%5D
+%3E
  nameOfC
@@ -360,36 +360,29 @@
 s = new 
-String%5BsizeOfMatrix%5D
+ArrayList%3C%3E()
 ;%0A      
@@ -604,22 +604,21 @@
 eOfClass
-%5Bi%5D = 
+.add(
 %22entity_
@@ -622,16 +622,17 @@
 ty_%22 + i
+)
 ;%0A      
@@ -782,25 +782,13 @@
 lass
-%5BsizeOfMatrix%5D = 
+.add(
 %22ent
@@ -799,32 +799,33 @@
 %22 + sizeOfMatrix
+)
 ;%0A    %7D%0A%0A    pub
@@ -1005,24 +1005,33 @@
  public 
+ArrayList%3C
 String
-%5B%5D
+%3E
  getName
@@ -1150,17 +1150,21 @@
 lass
-%5B
+.set(
 location
 %5D = 
@@ -1163,19 +1163,17 @@
 tion
-%5D = 
+,
 newName
+)
 ;%0A  
@@ -1510,56 +1510,8 @@
 ix%5D;
-%0A        nameOfClass = new String%5BsizeOfMatrix%5D;
 %0A%0A  
@@ -1898,18 +1898,18 @@
 lass
-%5Bi%5D = 
+.add(
 read
+)
 ;%0A  
@@ -2449,19 +2449,23 @@
 eOfClass
-%5Bi%5D
+.get(i)
 );%0A     

modify nameOfClass type to Arraylist