Repository: apache/kafka
File: group-coordinator/src/main/java/org/apache/kafka/coordinator/group/assignor/RangeSet.java (Java)

Code before:
    @Override
    public void clear() {
        throw new UnsupportedOperationException();
    }

    @Override
    public String toString() {
        return "RangeSet(from=" + from + " (inclusive), to=" + to + " (exclusive))";
    }

    /**
     * Compares the specified object with this set for equality.
     * Returns {@code true} if the specified object is also a set,
     * the two sets have the same size, and every member of the specified
     * set is contained in this set.
     *
     * @param o object to be compared for equality with this set
     * @return {@code true} if the specified object is equal to this set
     */
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Set<?> otherSet)) return false;

        if (o instanceof RangeSet other) {
            if (this.size() == 0 && other.size() == 0) return true;
            return this.from == other.from && this.to == other.to;
        }

        if (otherSet.size() != this.size()) return false;

        for (int i = from; i < to; i++) {
            if (!otherSet.contains(i)) return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        // The hash code of a Set is defined as the sum of the hash codes of its elements.
        // The hash code of an integer is the integer itself.

        // The sum of the integers from 1 to n is n * (n + 1) / 2.
        // To get the sum of the integers from 1 + k to n + k, we can add n * k.
        // So our hash code comes out to n * (from + to - 1) / 2.
        long size = size();
        if (size <= 0) return 0;

        // The arithmetic has to be done using longs, since the division by 2 is equivalent to
        // shifting the 33rd bit right.
        long sum = size * (from + to - 1) / 2;

Review comment (refactor):
If there is no `to < from`, probably we can remove this condition.

Code after:
    @Override
    public void clear() {
        throw new UnsupportedOperationException();
    }

    @Override
    public String toString() {
        return "RangeSet(from=" + from + " (inclusive), to=" + to + " (exclusive))";
    }

    /**
     * Compares the specified object with this set for equality.
     * Returns {@code true} if the specified object is also a set,
     * the two sets have the same size, and every member of the specified
     * set is contained in this set.
     *
     * @param o object to be compared for equality with this set
     * @return {@code true} if the specified object is equal to this set
     */
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Set<?> otherSet)) return false;

        if (o instanceof RangeSet other) {
            if (this.size() == 0 && other.size() == 0) return true;
            return this.from == other.from && this.to == other.to;
        }

        if (otherSet.size() != this.size()) return false;

        for (int i = from; i < to; i++) {
            if (!otherSet.contains(i)) return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        // The hash code of a Set is defined as the sum of the hash codes of its elements.
        // The hash code of an integer is the integer itself.

        // The sum of the integers from 1 to n is n * (n + 1) / 2.
        // To get the sum of the integers from 1 + k to n + k, we can add n * k.
        // So our hash code comes out to n * (from + to - 1) / 2.

        // The arithmetic has to be done using longs, since the division by 2 is equivalent to
        // shifting the 33rd bit right.
        long sum = size() * ((long) from + (long) to - 1) / 2;
        return (int) sum;
    }