Skip to content

JAVA-1404: Fix min token handling in TokenRange.contains#813

Merged
olim7t merged 1 commit into
3.0.xfrom
java1404
Feb 23, 2017
Merged

JAVA-1404: Fix min token handling in TokenRange.contains#813
olim7t merged 1 commit into
3.0.xfrom
java1404

Conversation

@olim7t

@olim7t olim7t commented Feb 21, 2017

Copy link
Copy Markdown
Contributor

Quick fix for a bug discovered by @adutra in #809.
Targeting to 3.0.x, we'll need to merge downstream if we include it in 3.2.0.

@olim7t olim7t added this to the 3.2.0 milestone Feb 21, 2017
return isWrappedAround()
? isAfterStart || isBeforeEnd
: isAfterStart && isBeforeEnd;
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not the most concise but I think clarity is more important.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May I propose another way to do it:

@VisibleForTesting
boolean contains(Token token, boolean isStart) {
    if (isEmpty()) {
        return false;
    }
    if (isFull()) {
        return true;
    }
    Token minToken = factory.minToken();
    if (token.equals(minToken)) {
        return isStart
                ? start.equals(minToken)
                : end.equals(minToken);
    }
    return isWrappedAround()
            ? isAfterStart(token, isStart) || isBeforeEnd(token, isStart)
            : isAfterStart(token, isStart) && isBeforeEnd(token, isStart);
}

private boolean isFull() {
    Token minToken = factory.minToken();
    return start.equals(minToken) && end.equals(minToken);
}

private boolean isAfterStart(Token token, boolean isStart) {
    Token minToken = factory.minToken();
    int c = start.equals(minToken) ? Integer.MIN_VALUE : start.compareTo(token);
    return isStart ? c <= 0 : c < 0;
}

private boolean isBeforeEnd(Token token, boolean isStart) {
    Token minToken = factory.minToken();
    int c = end.equals(minToken) ? Integer.MAX_VALUE : end.compareTo(token);
    return isStart ? c > 0 : c >= 0;
}

I find it slightly easier to follow.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (token.equals(minToken)) {
        return isStart
                ? start.equals(minToken)

Not necessarily, for example this fails with the test "]2,1] contains the start of ]min,5]".

I need to merge this and merge to the downstream branches, so I'm going to proceed with the current version.

return isWrappedAround()
? isAfterStart || isBeforeEnd
: isAfterStart && isBeforeEnd;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May I propose another way to do it:

@VisibleForTesting
boolean contains(Token token, boolean isStart) {
    if (isEmpty()) {
        return false;
    }
    if (isFull()) {
        return true;
    }
    Token minToken = factory.minToken();
    if (token.equals(minToken)) {
        return isStart
                ? start.equals(minToken)
                : end.equals(minToken);
    }
    return isWrappedAround()
            ? isAfterStart(token, isStart) || isBeforeEnd(token, isStart)
            : isAfterStart(token, isStart) && isBeforeEnd(token, isStart);
}

private boolean isFull() {
    Token minToken = factory.minToken();
    return start.equals(minToken) && end.equals(minToken);
}

private boolean isAfterStart(Token token, boolean isStart) {
    Token minToken = factory.minToken();
    int c = start.equals(minToken) ? Integer.MIN_VALUE : start.compareTo(token);
    return isStart ? c <= 0 : c < 0;
}

private boolean isBeforeEnd(Token token, boolean isStart) {
    Token minToken = factory.minToken();
    int c = end.equals(minToken) ? Integer.MAX_VALUE : end.compareTo(token);
    return isStart ? c > 0 : c >= 0;
}

I find it slightly easier to follow.

@olim7t olim7t merged commit 697d903 into 3.0.x Feb 23, 2017
@olim7t olim7t deleted the java1404 branch February 23, 2017 18:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants