001/*
002 * (C) Copyright 2010 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Robert Browning - initial implementation
016 *     Nuxeo - code review and integration
017 */
018package org.nuxeo.ecm.directory.ldap.dns;
019
020/**
021 * Encapsulates a hostname and port.
022 */
023public class DNSServiceEntry implements Comparable<DNSServiceEntry> {
024
025    private final String host;
026
027    private final int port;
028
029    private final int priority;
030
031    private final int weight;
032
033    public DNSServiceEntry(String host, int port, int priority, int weight) {
034        this.host = host;
035        this.port = port;
036        this.priority = priority;
037        this.weight = weight;
038    }
039
040    /**
041     * Get the priority of this DNS entry, descending priority 0(highest)..Integer.MAX_VALUE(lowest)
042     */
043    public int getPriority() {
044        return priority;
045    }
046
047    /**
048     * Get the weight of this DNS entry to compare entries with equal priority, ascending weight
049     * 0(lowest)..Integer.MAX_VALUE(highest)
050     */
051    public int getWeight() {
052        return weight;
053    }
054
055    /**
056     * Returns the hostname.
057     */
058    public String getHost() {
059        return host;
060    }
061
062    /**
063     * Returns the port.
064     */
065    public int getPort() {
066        return port;
067    }
068
069    /**
070     * Return the hostname in the form (hostname:port)
071     */
072    @Override
073    public String toString() {
074        return host + ":" + port;
075    }
076
077    /**
078     * @see java.lang.Object#equals(java.lang.Object)
079     */
080    @Override
081    public boolean equals(Object o) {
082        if (this == o) {
083            return true;
084        }
085        if (!(o instanceof DNSServiceEntry)) {
086            return false;
087        }
088
089        final DNSServiceEntry address = (DNSServiceEntry) o;
090
091        if (!host.equals(address.host)) {
092            return false;
093        }
094        return port == address.port;
095    }
096
097    /**
098     * @see java.lang.Object#hashCode()
099     */
100    @Override
101    public int hashCode() {
102        return toString().hashCode();
103    }
104
105    /**
106     * @see java.lang.Comparable#compareTo(java.lang.Object)
107     */
108    public int compareTo(DNSServiceEntry o) {
109        if (o.priority == priority) {
110            return o.weight - weight;
111        }
112        return priority - o.priority;
113    }
114}