,如果我需要自定義代碼才能使用這個邏輯如何自定義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;
}
}
請修正代碼的格式,關閉compareTo方法的}沒有縮進。 – 2010-08-13 18:47:30
只爲了解它?你想兩個不同的'Flaws'實例將'srcPort'和'dstPort'切換(分別)被認爲是相同的? – whiskeysierra 2010-08-13 20:18:48
是的,也是港口。 – 2010-08-13 23:35:04