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
019package org.apache.hadoop.yarn.api.records;
020
021import org.apache.hadoop.classification.InterfaceAudience.Private;
022import org.apache.hadoop.classification.InterfaceAudience.Public;
023import org.apache.hadoop.classification.InterfaceStability.Stable;
024import org.apache.hadoop.classification.InterfaceStability.Unstable;
025
026/**
027 * <p><code>NodeId</code> is the unique identifier for a node.</p>
028 * 
029 * <p>It includes the <em>hostname</em> and <em>port</em> to uniquely 
030 * identify the node. Thus, it is unique across restarts of any 
031 * <code>NodeManager</code>.</p>
032 */
033@Public
034@Stable
035public abstract class NodeId implements Comparable<NodeId> {
036
037  /**
038   * Get the <em>hostname</em> of the node.
039   * @return <em>hostname</em> of the node
040   */ 
041  @Public
042  @Stable
043  public abstract String getHost();
044  
045  @Private
046  @Unstable
047  public abstract void setHost(String host);
048
049  /**
050   * Get the <em>port</em> for communicating with the node.
051   * @return <em>port</em> for communicating with the node
052   */
053  @Public
054  @Stable
055  public abstract int getPort();
056  
057  @Private
058  @Unstable
059  public abstract void setPort(int port);
060
061  @Override
062  public String toString() {
063    return this.getHost() + ":" + this.getPort();
064  }
065
066  @Override
067  public int hashCode() {
068    final int prime = 31;
069    int result = 1;
070    result = prime * result + this.getHost().hashCode();
071    result = prime * result + this.getPort();
072    return result;
073  }
074
075  @Override
076  public boolean equals(Object obj) {
077    if (this == obj)
078      return true;
079    if (obj == null)
080      return false;
081    if (getClass() != obj.getClass())
082      return false;
083    NodeId other = (NodeId) obj;
084    if (!this.getHost().equals(other.getHost()))
085      return false;
086    if (this.getPort() != other.getPort())
087      return false;
088    return true;
089  }
090
091  @Override
092  public int compareTo(NodeId other) {
093    int hostCompare = this.getHost().compareTo(other.getHost());
094    if (hostCompare == 0) {
095      if (this.getPort() > other.getPort()) {
096        return 1;
097      } else if (this.getPort() < other.getPort()) {
098        return -1;
099      }
100      return 0;
101    }
102    return hostCompare;
103  }
104
105}