package com.mjrichardson.teamCity.buildTriggers.MachineAdded;

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

public class Machine implements Comparable<Machine> {
    public final String id;
    public final String name;
    public final String[] environmentIds;
    public final String[] roleIds;

    public Machine(String id, String name) {
        this(id, name, new String[0], new String[0]);
    }

    public Machine(String id, String name, String[] environmentIds, String[] roleIds) {
        this.id = id;
        this.name = name;
        this.environmentIds = environmentIds;
        this.roleIds = roleIds;
    }

    @Override
    public String toString() {
        return id + ";" + name;
    }

    public static Machine Parse(Map item) {
        String id = item.get("Id").toString();
        String name = item.get("Name").toString();

        List items = (List) item.get("EnvironmentIds");
        ArrayList<String> environmentIds = new ArrayList<>();
        for (Object environmentId : items) {
            environmentIds.add((String)environmentId);
        }

        items = (List) item.get("Roles");
        ArrayList<String> roleIds = new ArrayList<>();
        for (Object roleId : items) {
            roleIds.add((String)roleId);
        }

        return new Machine(id, name, environmentIds.toArray(new String[0]), roleIds.toArray(new String[0]));
    }

    public static Machine Parse(String pair) {
        if (pair == null || pair == "") {
            return new NullMachine();
        }
        final Integer DONT_REMOVE_EMPTY_VALUES = -1;
        final String[] split = pair.split(";", DONT_REMOVE_EMPTY_VALUES);
        final String id = split[0];
        final String name = split[1];

        Machine result = new Machine(id, name);
        if (result.equals(new NullMachine()))
            return new NullMachine();
        return result;
    }

    @Override
    public int compareTo(Machine o) {
        Integer thisId = Integer.parseInt(id.split("-")[1]);
        Integer otherId = Integer.parseInt(o.id.split("-")[1]);
        return thisId.compareTo(otherId);
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null)
            return false;
        if (obj.getClass() != Machine.class && obj.getClass() != NullMachine.class)
            return false;
        return toString().equals(obj.toString());
    }
}

@@ -56,16 +56,59 @@
 Added;%0A%0A
+import org.jetbrains.annotations.NotNull;%0A%0A
 import j
@@ -123,24 +123,24 @@
 .ArrayList;%0A
-
 import java.
@@ -227,16 +227,29 @@
 hine%3E %7B%0A
+    @NotNull%0A
     publ
@@ -264,24 +264,37 @@
  String id;%0A
+    @NotNull%0A
     public f
@@ -307,24 +307,37 @@
 tring name;%0A
+    @NotNull%0A
     public f
@@ -362,24 +362,37 @@
 ronmentIds;%0A
+    @NotNull%0A
     public f
@@ -532,32 +532,41 @@
  public Machine(
+@NotNull 
 String id, Strin
@@ -551,32 +551,41 @@
 tNull String id,
+ @NotNull
  String name, St
@@ -581,16 +581,25 @@
 ng name,
+ @NotNull
  String%5B
@@ -615,16 +615,25 @@
 mentIds,
+ @NotNull
  String%5B

Mark all Machine properties as NotNull to add some null safety