001/*
002 * (C) Copyright 2010 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Robert Browning - initial implementation
018 *     Nuxeo - code review and integration
019 */
020package org.nuxeo.ecm.directory.ldap.dns;
021
022/**
023 * Encapsulates a hostname and port.
024 */
025public class DNSServiceEntry implements Comparable<DNSServiceEntry> {
026
027    private final String host;
028
029    private final int port;
030
031    private final int priority;
032
033    private final int weight;
034
035    public DNSServiceEntry(String host, int port, int priority, int weight) {
036        this.host = host;
037        this.port = port;
038        this.priority = priority;
039        this.weight = weight;
040    }
041
042    /**
043     * Get the priority of this DNS entry, descending priority 0(highest)..Integer.MAX_VALUE(lowest)
044     */
045    public int getPriority() {
046        return priority;
047    }
048
049    /**
050     * Get the weight of this DNS entry to compare entries with equal priority, ascending weight
051     * 0(lowest)..Integer.MAX_VALUE(highest)
052     */
053    public int getWeight() {
054        return weight;
055    }
056
057    /**
058     * Returns the hostname.
059     */
060    public String getHost() {
061        return host;
062    }
063
064    /**
065     * Returns the port.
066     */
067    public int getPort() {
068        return port;
069    }
070
071    /**
072     * Return the hostname in the form (hostname:port)
073     */
074    @Override
075    public String toString() {
076        return host + ":" + port;
077    }
078
079    /**
080     * @see java.lang.Object#equals(java.lang.Object)
081     */
082    @Override
083    public boolean equals(Object o) {
084        if (this == o) {
085            return true;
086        }
087        if (!(o instanceof DNSServiceEntry)) {
088            return false;
089        }
090
091        final DNSServiceEntry address = (DNSServiceEntry) o;
092
093        if (!host.equals(address.host)) {
094            return false;
095        }
096        return port == address.port;
097    }
098
099    /**
100     * @see java.lang.Object#hashCode()
101     */
102    @Override
103    public int hashCode() {
104        return toString().hashCode();
105    }
106
107    /**
108     * @see java.lang.Comparable#compareTo(java.lang.Object)
109     */
110    @Override
111    public int compareTo(DNSServiceEntry o) {
112        if (o.priority == priority) {
113            return o.weight - weight;
114        }
115        return priority - o.priority;
116    }
117}