/*
 * Copyright 2016 Red Hat Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package enmasse.discovery;

import java.util.Map;

/**
 * Represents a host with a name and a set of ports.
 */
public class Host {
    private final String hostname;
    private final Map<String, Integer> portMap;

    public Host(String hostname, Map<String, Integer> portMap) {
        this.hostname = hostname;
        this.portMap = portMap;
    }

    public String getHostname() {
        return hostname;
    }

    public Endpoint amqpEndpoint() {
        return new Endpoint(hostname, portMap.get("amqp"));
    }

    public Endpoint coreEndpoint() {
        return new Endpoint(hostname, portMap.get("core"));
    }

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

        Host host = (Host) o;

        if (!hostname.equals(host.hostname)) return false;
        return (portMap.containsKey("amqp") == host.portMap.containsKey("amqp")) && portMap.get("amqp").equals(host.portMap.get("amqp"));

    }

    @Override
    public int hashCode() {
        int result = hostname.hashCode();
        if (portMap.containsKey("amqp")) {
            result = 31 * result + portMap.get("amqp");
        }
        return result;
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("host=").append(hostname).append(", ");
        builder.append("ports=").append(portMap.toString());
        return builder.toString();
    }
}

@@ -1054,36 +1054,35 @@
 %0A        return 
-new 
+get
 Endpoint(hostnam
@@ -1078,38 +1078,175 @@
 int(
-hostname, portMap.get(%22amq
+%22amqp%22);%0A    %7D%0A%0A    public Endpoint coreEndpoint() %7B%0A        return getEndpoint(%22core%22);%0A    %7D%0A%0A    public Endpoint httpEndpoint() %7B%0A        return getEndpoint(%22htt
 p%22)
-)
 ;%0A  
@@ -1262,36 +1262,35 @@
 public Endpoint 
-core
+get
 Endpoint() %7B%0A   
@@ -1274,32 +1274,47 @@
 int getEndpoint(
+String portName
 ) %7B%0A        retu
@@ -1351,22 +1351,24 @@
 Map.get(
-%22core%22
+portName
 ));%0A    

Allow requesting any port by name