001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    
019    package org.apache.hadoop.yarn.api.records;
020    
021    import org.apache.hadoop.classification.InterfaceAudience.Private;
022    import org.apache.hadoop.classification.InterfaceAudience.Public;
023    import org.apache.hadoop.classification.InterfaceStability.Stable;
024    import org.apache.hadoop.classification.InterfaceStability.Unstable;
025    import org.apache.hadoop.yarn.util.Records;
026    
027    /**
028     * <p><code>NodeId</code> is the unique identifier for a node.</p>
029     * 
030     * <p>It includes the <em>hostname</em> and <em>port</em> to uniquely 
031     * identify the node. Thus, it is unique across restarts of any 
032     * <code>NodeManager</code>.</p>
033     */
034    @Public
035    @Stable
036    public abstract class NodeId implements Comparable<NodeId> {
037    
038      @Private
039      @Unstable
040      public static NodeId newInstance(String host, int port) {
041        NodeId nodeId = Records.newRecord(NodeId.class);
042        nodeId.setHost(host);
043        nodeId.setPort(port);
044        nodeId.build();
045        return nodeId;
046      }
047    
048      /**
049       * Get the <em>hostname</em> of the node.
050       * @return <em>hostname</em> of the node
051       */ 
052      @Public
053      @Stable
054      public abstract String getHost();
055      
056      @Private
057      @Unstable
058      protected abstract void setHost(String host);
059    
060      /**
061       * Get the <em>port</em> for communicating with the node.
062       * @return <em>port</em> for communicating with the node
063       */
064      @Public
065      @Stable
066      public abstract int getPort();
067      
068      @Private
069      @Unstable
070      protected abstract void setPort(int port);
071    
072      @Override
073      public String toString() {
074        return this.getHost() + ":" + this.getPort();
075      }
076    
077      @Override
078      public int hashCode() {
079        final int prime = 493217;
080        int result = 8501;
081        result = prime * result + this.getHost().hashCode();
082        result = prime * result + this.getPort();
083        return result;
084      }
085    
086      @Override
087      public boolean equals(Object obj) {
088        if (this == obj)
089          return true;
090        if (obj == null)
091          return false;
092        if (getClass() != obj.getClass())
093          return false;
094        NodeId other = (NodeId) obj;
095        if (!this.getHost().equals(other.getHost()))
096          return false;
097        if (this.getPort() != other.getPort())
098          return false;
099        return true;
100      }
101    
102      @Override
103      public int compareTo(NodeId other) {
104        int hostCompare = this.getHost().compareTo(other.getHost());
105        if (hostCompare == 0) {
106          if (this.getPort() > other.getPort()) {
107            return 1;
108          } else if (this.getPort() < other.getPort()) {
109            return -1;
110          }
111          return 0;
112        }
113        return hostCompare;
114      }
115    
116      protected abstract void build();
117    }