如何自定义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<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;
}
}
if I need to customize my code with this logic
if this.srcAddr=other.srcAddr or
this.src.Addr = other.sdstAddr
this.srcPort=other.srcPort
this.srcPort=other.dstPort
because I am going to consider bi-directional flow, a packet from source to destination and a packet from destination to source belong to a flow.
How should I change my code?
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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以极大地简化compareTo 方法。您只是比较字符串,如果您将所有字符串连接到一个比较,您将得到相同的结果。以下示例添加了 toString() 实现作为奖励:
如果您需要更高的性能,请考虑在构造流并将其存储在私有字段中时构造串联字符串。
You can simplify the compareTo Method dramatically. You're just comparing Strings and you'll have the same result if you just concatenate all String and to one single compare. The following example adds a toString() implementation as a bonus:
If you need more performance, consider constructing the concatenated String while you construct a flow and store it in a private field.
只是一个猜测,但我认为您正在寻找的是以下内容(空检查、类型检查和错误处理作为用户的练习):
请注意,这是基于以下假设:连接从机器 1 端口 b 到机器 2 端口 a 与从机器 1 端口 a 到机器 2 端口 b 的连接不同。
Just a guess, but I think what you're looking for is something along the lines of the following (null checking, type checking and error handling left as an exercise to the user):
Note that this is based on the assumption that a connection from machine 1 port b to machine 2 port a is not the same as a connection from machine 1 port a to machine 2 port b.