public static int sanitizedSize(int capacity, long size) {
        // Concurrent updates to cIndex and pIndex may lag behind other progress enablers (e.g. FastFlow), so we need
        // to check bounds [0,capacity]
        if (size < 0)
        {
            return 0;
        }
        if (capacity != MessagePassingQueue.UNBOUNDED_CAPACITY && size > capacity)
        {
            return capacity;
        }
        // Integer overflow is possible for the unbounded indexed queues.
        if (size > Integer.MAX_VALUE)
        {
            return Integer.MAX_VALUE;
        }
        return (int) size;
    }
A note to maintainers on index assumptions: in a single threaded world it would seem intuitive to assume:
<pre>
<code>producerIndex >= consumerIndex</code>
</pre>
As an invariant, but in a concurrent, long running settings all of the following need to be considered:
<ul>
    <li> <code>consumerIndex > producerIndex</code> : due to counter overflow (unlikey with longs, but easy to reason)
    <li> <code>consumerIndex > producerIndex</code> : due to consumer FastFlow like implementation discovering the
    element before the counter is updated.
    <li> <code>producerIndex - consumerIndex < 0</code> : due to above.
    <li> <code>producerIndex - consumerIndex > Integer.MAX_VALUE</code> : as linked buffers allow constructing queues
    with more than <code>Integer.MAX_VALUE</code> elements.

</ul>
jctools-core/src/main/java/org/jctools/queues/IndexedQueueSizeUtil.java