Java 如何定制并列方法兼顾双向流
如果我需要使用此逻辑自定义代码Java 如何定制并列方法兼顾双向流,java,packet,packet-capture,packet-sniffers,compareto,Java,Packet,Packet Capture,Packet Sniffers,Compareto,如果我需要使用此逻辑自定义代码 if this.srcAddr=other.srcAddr or this.src.Addr = other.sdstAddr this.srcPort=other.srcPort this.srcPort=other.dstPort 因为我要考虑双向流,从源到目的地的分组和从目的地到目的地的分组属于流。 我应该如何更改代码 package myclassifier; public class Flows implements Comparable<Flo
if this.srcAddr=other.srcAddr or
this.src.Addr = other.sdstAddr
this.srcPort=other.srcPort
this.srcPort=other.dstPort
因为我要考虑双向流,从源到目的地的分组和从目的地到目的地的分组属于流。 我应该如何更改代码
package myclassifier;
public class Flows implements Comparable<Flows> {
String srcAddr, dstAddr, srcPort, dstPort, protocol;
public Flows(String sIP, String dIP){
this.srcAddr = sIP;
this.dstAddr = dIP;
}
public int compareTo(Flows other) {
int res = (this.srcAddr.compareTo(other.srcAddr));
if (res != 0) {
return res;
}
res = this.dstAddr.compareTo(other.dstAddr);
if (res != 0) {
return res;
}
res = this.srcPort.compareTo(other.srcPort);
if (res != 0) {
return res;
}
res = this.dstPort.compareTo(other.dstPort);
if (res != 0) {
return res;
}
return this.protocol.compareTo(other.protocol);
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((dstAddr == null) ? 0 : dstAddr.hashCode());
result = prime * result + ((dstPort == null) ? 0 : dstPort.hashCode());
result = prime * result + ((srcAddr == null) ? 0 : srcAddr.hashCode());
result = prime * result + ((srcPort == null) ? 0 : srcPort.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;
Flows other = (Flows) obj;
if (dstAddr == null) {
if (other.dstAddr != null)
return false;
} else if (!dstAddr.equals(other.dstAddr))
return false;
if (dstPort == null) {
if (other.dstPort != null)
return false;
} else if (!dstPort.equals(other.dstPort))
return false;
if (srcAddr == null) {
if (other.srcAddr != null)
return false;
} else if (!srcAddr.equals(other.srcAddr))
return false;
if (srcPort == null) {
if (other.srcPort != null)
return false;
} else if (!srcPort.equals(other.srcPort))
return false;
return true;
}
}
只是一个猜测,但我认为您所寻找的是以下空检查、类型检查和错误处理的内容,留给用户作为练习:
return ((this.srcAddr.equals(other.srcAddr) && this.srcPort.equals(other.srcPort) ||
(this.srcAddr.equals(other.dstAddr) && this.srcPort.equals(other.dstPort));
请注意,这是基于这样的假设,即从机器1端口b到机器2端口a的连接与从机器1端口a到机器2端口b的连接不同。您可以大大简化比较方法。您只是在比较字符串,如果您只是将所有字符串和字符串连接到一个比较,您将得到相同的结果。以下示例添加了toString实现作为奖励:
@Override
public String toString() {
return String.format("[%s, %s, %s, %s, %s]", srcAddr, dstAddr, srcPort, dstPort, protocol);
}
public int compareTo(Flows other) {
if (other == null)
return 0; // the necessary null check was missing in your code
return toString().compareTo(other.toString());
}
如果您需要更多的性能,请考虑在构建流程并将其存储在私有字段中时构建级联字符串。< /P>请修复代码的格式,关闭CopReStuto方法的}不缩进。只是为了使它正确?你想让srcPort和dstPort交换的两个不同的缺陷实例分别被认为是相同的吗?对不起,我不完全明白。它是如何工作的?如果我给它以下两个流srcadr=x,dstAddr=y,srcPort=12345,dstPort=80,pro=tcp和srcadr=y,dstAddr=x,srcPort=80,dstPort=12345,pro=tcp,它是否假设它们是相同的?@Red Lion-很抱歉,我的文本不是对你的“反之亦然”问题的回答,它只是简化了你当前的代码。但您的上一个示例阐明了您想要的内容-您想要一个测试,该测试将来自您示例的两个流视为相等。这正是我所想到的。只需返回您在if中使用的表达式,而不是这些多余的返回语句。更新,抱歉,我最初编写该语句时处于半睡眠状态。