001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.cluster;
016    
017    import com.liferay.portal.kernel.util.StringBundler;
018    import com.liferay.portal.kernel.util.Validator;
019    
020    import java.io.Serializable;
021    
022    import java.net.InetAddress;
023    
024    /**
025     * @author Tina Tian
026     */
027    public class ClusterNode implements Comparable<ClusterNode>, Serializable {
028    
029            public ClusterNode(String clusterNodeId, InetAddress inetAddress) {
030                    if (clusterNodeId == null) {
031                            throw new IllegalArgumentException("Cluster node ID is null");
032                    }
033    
034                    if (inetAddress == null) {
035                            throw new IllegalArgumentException("Inet address is null");
036                    }
037    
038                    _clusterNodeId = clusterNodeId;
039                    _inetAddress = inetAddress;
040            }
041    
042            @Override
043            public int compareTo(ClusterNode clusterNode) {
044                    InetAddress inetAddress = clusterNode._inetAddress;
045    
046                    String ipAddress = inetAddress.getHostAddress();
047    
048                    String curIpAddress = _inetAddress.getHostAddress();
049    
050                    int value = curIpAddress.compareTo(ipAddress);
051    
052                    if (value == 0) {
053                            if (_port > clusterNode._port) {
054                                    value = 1;
055                            }
056                            else if (_port < clusterNode._port) {
057                                    value = -1;
058                            }
059                    }
060    
061                    return value;
062            }
063    
064            @Override
065            public boolean equals(Object obj) {
066                    if (this == obj) {
067                            return true;
068                    }
069    
070                    if (!(obj instanceof ClusterNode)) {
071                            return false;
072                    }
073    
074                    ClusterNode clusterNode = (ClusterNode)obj;
075    
076                    if (Validator.equals(_clusterNodeId, clusterNode._clusterNodeId)) {
077                            return true;
078                    }
079    
080                    return false;
081            }
082    
083            public String getClusterNodeId() {
084                    return _clusterNodeId;
085            }
086    
087            public InetAddress getInetAddress() {
088                    return _inetAddress;
089            }
090    
091            public int getPort() {
092                    return _port;
093            }
094    
095            @Override
096            public int hashCode() {
097                    return _clusterNodeId.hashCode();
098            }
099    
100            public void setPort(int port) {
101                    _port = port;
102            }
103    
104            @Override
105            public String toString() {
106                    StringBundler sb = new StringBundler(7);
107    
108                    sb.append("{clusterNodeId=");
109                    sb.append(_clusterNodeId);
110                    sb.append(", inetAddress=");
111                    sb.append(_inetAddress);
112                    sb.append(", port=");
113                    sb.append(_port);
114                    sb.append("}");
115    
116                    return sb.toString();
117            }
118    
119            private String _clusterNodeId;
120            private InetAddress _inetAddress;
121            private int _port;
122    
123    }