001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.camel.impl.cloud;
018
019import java.util.Collections;
020import java.util.HashMap;
021import java.util.Map;
022
023import org.apache.camel.cloud.ServiceDefinition;
024import org.apache.camel.cloud.ServiceHealth;
025
026public class DefaultServiceDefinition implements ServiceDefinition {
027    private static final ServiceHealth DEFAULT_SERVICE_HEALTH = new DefaultServiceHealth();
028
029    private final String name;
030    private final String host;
031    private final int port;
032    private final Map<String, String> meta;
033    private final ServiceHealth health;
034
035    public DefaultServiceDefinition(String name, String host, int port) {
036        this.name = name;
037        this.host = host;
038        this.port = port;
039        this.meta = Collections.emptyMap();
040        this.health = DEFAULT_SERVICE_HEALTH;
041    }
042
043    public DefaultServiceDefinition(String name, String host, int port, ServiceHealth health) {
044        this.name = name;
045        this.host = host;
046        this.port = port;
047        this.meta = Collections.emptyMap();
048        this.health = health;
049    }
050
051    public DefaultServiceDefinition(String name, String host, int port, Map<String, String> meta) {
052        this.name = name;
053        this.host = host;
054        this.port = port;
055        this.meta = meta != null ? Collections.unmodifiableMap(new HashMap<>(meta)) : Collections.emptyMap();
056        this.health = DEFAULT_SERVICE_HEALTH;
057    }
058
059    public DefaultServiceDefinition(String name, String host, int port, Map<String, String> meta, ServiceHealth health) {
060        this.name = name;
061        this.host = host;
062        this.port = port;
063        this.meta = meta != null ? Collections.unmodifiableMap(new HashMap<>(meta)) : Collections.emptyMap();
064        this.health = health;
065    }
066
067    @Override
068    public String getName() {
069        return name;
070    }
071
072    @Override
073    public String getHost() {
074        return host;
075    }
076
077    @Override
078    public int getPort() {
079        return port;
080    }
081
082    @Override
083    public ServiceHealth getHealth() {
084        return health;
085    }
086
087    @Override
088    public Map<String, String> getMetadata() {
089        return this.meta;
090    }
091
092    @Override
093    public String toString() {
094        return "DefaultServiceCallService[" + name + "@" + host + ":" + port + "]";
095    }
096
097    @Override
098    public boolean equals(Object o) {
099        if (this == o) {
100            return true;
101        }
102        if (o == null || getClass() != o.getClass()) {
103            return false;
104        }
105
106        DefaultServiceDefinition that = (DefaultServiceDefinition) o;
107
108        if (port != that.port) {
109            return false;
110        }
111        if (name != null ? !name.equals(that.name) : that.name != null) {
112            return false;
113        }
114        return host != null ? host.equals(that.host) : that.host == null;
115    }
116
117    @Override
118    public int hashCode() {
119        int result = name != null ? name.hashCode() : 0;
120        result = 31 * result + (host != null ? host.hashCode() : 0);
121        result = 31 * result + port;
122        return result;
123    }
124}