001    /**
002     * Copyright 2007 Mike Kroutikov.
003     *
004     * This program is free software; you can redistribute it and/or modify
005     *   it under the terms of the Lesser GNU General Public License as 
006     *   published by the Free Software Foundation; either version 3 of
007     *   the License, or (at your option) any later version.
008     *
009     *   This program 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
012     *   Lesser GNU General Public License for more details.
013     *
014     *   You should have received a copy of the Lesser GNU General Public License
015     *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
016     *   
017     *   Derived from code developed by Opentick Corporation, http://www.opentick.com.
018     */
019    
020    package org.otfeed.event;
021    
022    import static org.otfeed.event.IdentityUtil.equalsTo;
023    import static org.otfeed.event.IdentityUtil.safeCompare;
024    import static org.otfeed.event.IdentityUtil.safeHashCode;
025    
026    import java.io.Serializable;
027    
028    /**
029     * Encapsulates hostname and port pair.
030     */
031    public final class OTHost implements Comparable<OTHost>, Serializable {
032    
033            private static final long serialVersionUID = -5913623386353359260L;
034    
035            private String host;
036            private int port;
037    
038            /** 
039             * Default constructor
040             */
041            public OTHost() { }
042    
043            /**
044             * Constructor with parameter initialization
045             *
046             * @param host server host
047             * @param port server port
048             */
049            public OTHost(String host, int port) {
050                    this.host = host;
051                    this.port = port;
052            }
053            
054            /**
055             * Constructor that parses string in the form "host:port"
056             * 
057             * @param address
058             */
059            public OTHost(String address) {
060                    int index = address.indexOf(':');
061                    if(index < 0) {
062                            throw new IllegalArgumentException("address string parsing error: colon not find");
063                    }
064                    this.host = address.substring(0, index);
065                    this.port = Integer.parseInt(address.substring(index + 1));
066            }
067    
068            /**
069             * Sets host name.
070             *
071             * @param host host name.
072             */
073            public void setHost(String host) {
074                    this.host = host;
075            }
076    
077            /**
078             * Returns host value.
079             *
080             * @return host value.
081             */
082            public String getHost() {
083                    return host;
084            }
085    
086            /**
087             * Sets port value;
088             *
089             * @param port port value.
090             */
091            public void setPort(int port) {
092                    this.port = port;
093            }
094    
095            /**
096             * Returns port value.
097             *
098             * @return port value.
099             */
100            public int getPort() {
101                    return port;
102            }
103    
104            @Override
105            public String toString() {
106                    return host + ":" + port;
107            }
108    
109        @Override
110            public int hashCode() {
111            return safeHashCode(host) 
112                    + 3 * safeHashCode(port); 
113        }
114            
115        @Override
116            public boolean equals(Object o) {
117            return equalsTo(this, o);
118        }
119        
120            public int compareTo(OTHost other) {
121                    int rc;
122    
123                    if((rc = safeCompare(host, other.host)) != 0) {
124                            return rc;
125                    }
126                    
127                    if((rc = safeCompare(port, other.port)) != 0) {
128                            return rc;
129                    }
130                    
131                    return 0;
132            }
133    }