2010-08-13 24 views
0

,如果我需要自定义代码才能使用这个逻辑如何自定义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; 
    } 

} 
+0

请修正代码的格式,关闭compareTo方法的}没有缩进。 – 2010-08-13 18:47:30

+0

只为了解它?你想两个不同的'Flaws'实例将'srcPort'和'dstPort'切换(分别)被认为是相同的? – whiskeysierra 2010-08-13 20:18:48

+0

是的,也是港口。 – 2010-08-13 23:35:04

回答

0

只是一个猜测,但我认为你要寻找的是沿着以下线的东西(空检查,类型检查和错误处理作为练习留给用户):

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的连接不相同。

+0

恰恰是我想的那个。 – 2010-08-13 19:52:50

+1

只需返回您在'if'中使用的表达式,而不是具有这些多余的返回语句。 – whiskeysierra 2010-08-13 20:16:01

+0

更新了,对不起,当我最初写这篇文章的时候,我已经睡着了。 – 2010-08-16 15:37:53

0

您可以大大简化compareTo方法。你只是比较字符串,如果你只是连接所有的字符串和一个单一的比较,你会得到相同的结果。下面的示例添加一个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()); 
} 

如果您需要更多的性能,可以考虑构建连接字符串,而你构建一个流并将其存储在私有字段。

+0

对不起,我不完全明白。它是如何工作的?如果我给它跟着两个流srcAddr = x,dstAddr = y,srcPort = 12345,dstPort = 80,pro = tcp AND srcAddr = y,dstAddr = x,srcPort = 80,dstPort = 12345,pro = tcp 假设他们是一样的? – 2010-08-13 19:28:03

+0

不,它不会。 – whiskeysierra 2010-08-13 20:20:20

+0

@红狮 - 对不起,我的文本不是你的'反之亦然'问题的答案,它只是简化你当前的代码。但是你的最后一个例子澄清了你想要的东西 - 你想要一个考虑两个流程(从你的例子)是平等的测试。 – 2010-08-13 20:30:45