如何自定义compareTo方法来考虑双向流

发布于 2024-09-14 10:09:51 字数 2576 浏览 13 评论 0原文

如果我需要用这个逻辑自定义我的代码

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寻梦旅人 2024-09-21 10:09:51

您可以极大地简化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());
}

如果您需要更高的性能,请考虑在构造流并将其存储在私有字段中时构造串联字符串。

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:

@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());
}

If you need more performance, consider constructing the concatenated String while you construct a flow and store it in a private field.

终难遇 2024-09-21 10:09:51

只是一个猜测,但我认为您正在寻找的是以下内容(空检查、类型检查和错误处理作为用户的练习):

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

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):

return ((this.srcAddr.equals(other.srcAddr) && this.srcPort.equals(other.srcPort) || 
        (this.srcAddr.equals(other.dstAddr) && this.srcPort.equals(other.dstPort));

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文