// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.node.admin.configserver.noderepository;

import com.yahoo.config.provision.NodeType;

import java.util.Collections;
import java.util.Optional;
import java.util.Set;

/**
 * @author freva
 */
public class AddNode {

    public final String hostname;
    public final Optional<String> parentHostname;
    public final String nodeFlavor;
    public final NodeType nodeType;
    public final Set<String> ipAddresses;
    public final Set<String> additionalIpAddresses;

    /**
     * Constructor for a host node (has no parent)
     */
    public AddNode(String hostname, String nodeFlavor, NodeType nodeType, Set<String> ipAddresses, Set<String> additionalIpAddresses) {
        this(hostname, Optional.empty(), nodeFlavor, nodeType, ipAddresses, additionalIpAddresses);
    }

    /**
     * Constructor for a child node (Must set parentHostname, no additionalIpAddresses)
     */
    public AddNode(String hostname, String parentHostname, String nodeFlavor, NodeType nodeType, Set<String> ipAddresses) {
        this(hostname, Optional.of(parentHostname), nodeFlavor, nodeType, ipAddresses, Collections.emptySet());
    }

    public AddNode(String hostname, Optional<String> parentHostname, String nodeFlavor, NodeType nodeType, Set<String> ipAddresses, Set<String> additionalIpAddresses) {
        this.hostname = hostname;
        this.parentHostname = parentHostname;
        this.nodeFlavor = nodeFlavor;
        this.nodeType = nodeType;
        this.ipAddresses = ipAddresses;
        this.additionalIpAddresses = additionalIpAddresses;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        AddNode addNode = (AddNode) o;

        if (!hostname.equals(addNode.hostname)) return false;
        if (!parentHostname.equals(addNode.parentHostname)) return false;
        if (!nodeFlavor.equals(addNode.nodeFlavor)) return false;
        if (nodeType != addNode.nodeType) return false;
        if (!ipAddresses.equals(addNode.ipAddresses)) return false;
        return additionalIpAddresses.equals(addNode.additionalIpAddresses);
    }

    @Override
    public int hashCode() {
        int result = hostname.hashCode();
        result = 31 * result + parentHostname.hashCode();
        result = 31 * result + nodeFlavor.hashCode();
        result = 31 * result + nodeType.hashCode();
        result = 31 * result + ipAddresses.hashCode();
        result = 31 * result + additionalIpAddresses.hashCode();
        return result;
    }

    @Override
    public String toString() {
        return "AddNode{" +
                "hostname='" + hostname + '\'' +
                ", parentHostname=" + parentHostname +
                ", nodeFlavor='" + nodeFlavor + '\'' +
                ", nodeType=" + nodeType +
                ", ipAddresses=" + ipAddresses +
                ", additionalIpAddresses=" + additionalIpAddresses +
                '}';
    }
}

@@ -259,16 +259,42 @@
 ctions;%0A
+import java.util.Objects;%0A
 import j
@@ -1875,17 +1875,16 @@
  false;%0A
-%0A
         
@@ -1914,25 +1914,24 @@
 ode) o;%0A
-%0A
         
 if (!hos
@@ -1926,75 +1926,92 @@
     
-if (!hostname.equals(addNode.hostname)) return false;%0A        if (!
+return Objects.equals(hostname, addNode.hostname) &&%0A                Objects.equals(
 pare
@@ -2020,24 +2020,18 @@
 Hostname
-.equals(
+, 
 addNode.
@@ -2049,37 +2049,43 @@
 ame)
-) return false;%0A        if (!
+ &&%0A                Objects.equals(
 node
@@ -2090,24 +2090,18 @@
 deFlavor
-.equals(
+, 
 addNode.
@@ -2115,32 +2115,22 @@
 vor)
-) return false;%0A
+ &&%0A  
         
 if (
@@ -2125,20 +2125,22 @@
         
-if (
+      
 nodeType
@@ -2140,17 +2140,17 @@
 odeType 
-!
+=
 = addNod
@@ -2163,37 +2163,43 @@
 Type
-) return false;%0A        if (!
+ &&%0A                Objects.equals(
 ipAd
@@ -2201,32 +2201,26 @@
 (ipAddresses
-.equals(
+, 
 addNode.ipAd
@@ -2231,39 +2231,43 @@
 ses)
-) return false;%0A        return 
+ &&%0A                Objects.equals(
 addi
@@ -2283,24 +2283,18 @@
 ddresses
-.equals(
+, 
 addNode.
@@ -2378,347 +2378,111 @@
     
-int result = hostname.hashCode();%0A        result = 31 * result + parentHostname.hashCode();%0A        result = 31 * result + nodeFlavor.hashCode();%0A        result = 31 * result + nodeType.hashCode();%0A        result = 31 * result + ipAddresses.hashCode();%0A        result = 31 * result + additionalIpAddresses.hashCode();%0A        return result
+return Objects.hash(hostname, parentHostname, nodeFlavor, nodeType, ipAddresses, additionalIpAddresses)
 ;%0A  

Fix hashCode