com.neko233.toolchain.metrics.host.HostInfo Maven / Gradle / Ivy
package com.neko233.toolchain.metrics.host;
import com.neko233.toolchain.common.base.StringUtils233;
import com.neko233.toolchain.common.network.NetUtils233;
import java.io.Serializable;
import java.net.InetAddress;
/**
* 当前主机的信息。
*/
public class HostInfo implements Serializable {
private static final long serialVersionUID = 1L;
private final String HOST_NAME;
private final String HOST_ADDRESS;
public HostInfo() {
final InetAddress localhost = NetUtils233.getLocalhost();
if (null != localhost) {
HOST_NAME = localhost.getHostName();
HOST_ADDRESS = localhost.getHostAddress();
} else {
HOST_NAME = null;
HOST_ADDRESS = null;
}
}
/**
* 取得当前主机的名称。
*
*
* 例如:"webserver1"
*
*
* @return 主机名
*/
public final String getName() {
return HOST_NAME;
}
/**
* 取得当前主机的地址。
*
*
* 例如:"192.168.0.1"
*
*
* @return 主机地址
*/
public final String getAddress() {
return HOST_ADDRESS;
}
/**
* 将当前主机的信息转换成字符串。
*
* @return 主机信息的字符串表示
*/
@Override
public final String toString() {
StringBuilder builder = new StringBuilder();
StringUtils233.appendByPrintStyle(builder, "Host Name: ", getName());
StringUtils233.appendByPrintStyle(builder, "Host Address: ", getAddress());
return builder.toString();
}
}