implementing compareTo method for several fields

后端 未结 4 1826
自闭症患者
自闭症患者 2021-02-11 10:51

I want to compare two object based on 5-tuple which are: srcAddr, dstAddr, srcPort, dstPort, protocol

here is what i have:

public class Flows implements          


        
4条回答
  •  [愿得一人]
    2021-02-11 11:44

    Try:

    @Override
    public int compareTo(final Flows that) {
        return ComparisonChain.start().
            compare(this.srcAddr, that.srcAddr).
            compare(this.dstAddr, that.dstAddr).
            compare(this.srcPort, that.srcPort).
            compare(this.dstPort, that.dstPort).
            compare(this.protocol, that.protocol).
            result();
    }
    

    Requires Guava

提交回复
热议问题