tomp2p / TomP2P

A P2P-based high performance key-value pair storage library
http://tomp2p.net
Apache License 2.0
438 stars 122 forks source link

Boolean logic and method name mismatch. #121

Closed sandymac closed 8 years ago

sandymac commented 8 years ago

I think the logic in NATTypeDetection.twoOutOfThreeSame is likely backwards. The method name and code logic don't match.

The method's current form, below, has or (||) boolean operations meaning if any one pair matches, the method returns true.

    private static boolean twoOutOfThreeSame(int i1, int i2, int i3, int k1, int k2, int k3) {
        if (i1 == k1 || i2 == k2) {
            return true;
        }
        if (i1 == k1 || i3 == k3) {
            return true;
        }
        if (i2 == k2 || i3 == k3) {
            return true;
        }
        return false;
    }

Replacing the ors (||) with ands (&&) will have the behavior described by the method name. Alternatively the version below has the same behavior with one line.

    private static boolean twoOutOfThreeSame(int i1, int i2, int i3, int k1, int k2, int k3) {
        return (i1 == k1 && i2 == k2) || (i1 == k1 && i3 == k3) || (i2 == k2 && i3 == k3);
    }
tbocek commented 8 years ago

You are right, this logic is flawed, thanks for the info!