org.bidib.wizard.api.model.common.NetBidibServiceInfo Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of bidibwizard-api Show documentation
Show all versions of bidibwizard-api Show documentation
jBiDiB BiDiB Wizard API POM
package org.bidib.wizard.api.model.common;
import java.net.InetAddress;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
public class NetBidibServiceInfo {
public static final String NONE = "";
public static final String PROPERTY_UID = "uid";
public static final String PROPERTY_USERNAME = "user";
public static final String PROPERTY_PROD = "prod";
private final NetBidibServiceInfo.DiscoveryAction action;
private final String qualifiedName;
private final InetAddress[] addresses;
private final int port;
private final Map props;
public NetBidibServiceInfo(NetBidibServiceInfo.DiscoveryAction action, final String qualifiedName,
InetAddress[] addresses, int port, final Map props) {
this.action = action;
this.qualifiedName = qualifiedName;
this.addresses = addresses;
this.port = port;
this.props = props;
}
public NetBidibServiceInfo(final String qualifiedName) {
this(NetBidibServiceInfo.DiscoveryAction.REMOVED, qualifiedName, null, 0, null);
}
public NetBidibServiceInfo.DiscoveryAction getAction() {
return action;
}
public String getQualifiedName() {
return this.qualifiedName;
}
public InetAddress[] getAddresses() {
return this.addresses;
}
public int getPort() {
return this.port;
}
public Map getProps() {
return this.props;
}
@Override
public boolean equals(Object o) {
if (o != null && o instanceof NetBidibServiceInfo) {
if (qualifiedName != null) {
return qualifiedName.equals(((NetBidibServiceInfo) o).qualifiedName);
}
return NONE.equals(((NetBidibServiceInfo) o).qualifiedName);
}
return false;
}
@Override
public int hashCode() {
if (qualifiedName != null) {
return qualifiedName.hashCode();
}
return NONE.hashCode();
}
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
}
public static String format(final NetBidibServiceInfo si) {
if (si.qualifiedName != null) {
StringBuilder sb = new StringBuilder();
// sb.append(si.qualifiedName);
if (si.addresses != null && si.addresses.length > 0) {
// get the username
String userName = si.getUserName();
if (userName != null) {
sb.append(userName).append(" - ");
}
for (InetAddress addr : si.addresses) {
sb.append(addr.getHostAddress());
break; // only add first address
}
sb.append(":").append(si.port);
return sb.toString();
}
}
return NONE;
}
public String getUserName() {
return props.getOrDefault(PROPERTY_USERNAME, null);
}
/**
* This action signals if the USB port was inserted or removed.
*
*/
public enum DiscoveryAction {
RESOLVED("resolved"), REMOVED("removed");
private final String value;
private final static Map CONSTANTS = new HashMap<>();
static {
for (NetBidibServiceInfo.DiscoveryAction c : values()) {
CONSTANTS.put(c.value, c);
}
}
private DiscoveryAction(String value) {
this.value = value;
}
@Override
public String toString() {
return this.value;
}
@JsonValue
public String value() {
return this.value;
}
@JsonCreator
public static NetBidibServiceInfo.DiscoveryAction fromValue(String value) {
NetBidibServiceInfo.DiscoveryAction constant = CONSTANTS.get(value);
if (constant == null) {
throw new IllegalArgumentException(value);
}
else {
return constant;
}
}
}
}