blob: 8187369ca3c596c1549999e29bb0092bd062813c [file] [log] [blame]
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.kafka.common;
/**
* Information about a Kafka node
*/
public class Node {
private static final Node NO_NODE = new Node(-1, "", -1);
private final int id;
private final String idString;
private final String host;
private final int port;
private final String rack;
public Node(int id, String host, int port) {
this(id, host, port, null);
}
public Node(int id, String host, int port, String rack) {
super();
this.id = id;
this.idString = Integer.toString(id);
this.host = host;
this.port = port;
this.rack = rack;
}
public static Node noNode() {
return NO_NODE;
}
/**
* Check whether this node is empty, which may be the case if noNode() is used as a placeholder
* in a response payload with an error.
* @return true if it is, false otherwise
*/
public boolean isEmpty() {
return host == null || host.isEmpty() || port < 0;
}
/**
* The node id of this node
*/
public int id() {
return id;
}
/**
* String representation of the node id.
* Typically the integer id is used to serialize over the wire, the string representation is used as an identifier with NetworkClient code
*/
public String idString() {
return idString;
}
/**
* The host name for this node
*/
public String host() {
return host;
}
/**
* The port for this node
*/
public int port() {
return port;
}
/**
* True if this node has a defined rack
*/
public boolean hasRack() {
return rack != null;
}
/**
* The rack for this node
*/
public String rack() {
return rack;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((host == null) ? 0 : host.hashCode());
result = prime * result + id;
result = prime * result + port;
result = prime * result + ((rack == null) ? 0 : rack.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Node other = (Node) obj;
if (host == null) {
if (other.host != null)
return false;
} else if (!host.equals(other.host))
return false;
if (id != other.id)
return false;
if (port != other.port)
return false;
if (rack == null) {
if (other.rack != null)
return false;
} else if (!rack.equals(other.rack))
return false;
return true;
}
@Override
public String toString() {
return host + ":" + port + " (id: " + idString + " rack: " + rack + ")";
}
}