class DefaultPartitionAttributesBuilder implements PartitionAttributesBuilder {

    @Override
    public <T> PartitionAttributesBuilder add(Key<T> key, T value) {
        if (keyValueArray.length == nextIndex) {
            keyValueArray = copyOf(keyValueArray, (keyValueArray.length << 1) + 2);
        }

        // left child index = n * 2 + 2
        // right child index = n * 2 + 4
        // parent index = ((n - 2) / 2) & ~0x1
        // Bubble up to preserve max heap constraint
        int currentIndex = nextIndex;
        while (currentIndex > 0) {
            final int parentKeyIndex = ((currentIndex - 2) >>> 1) & PARENT_MASK;
            final Key parentKey = (Key) keyValueArray[parentKeyIndex];

            final int cmp = parentKey.compareTo(key);
            if (cmp > 0) {
                break;
            } else if (cmp == 0) {
                // We maintain a max heap during insertion, note that we do a best effort check for duplicates here
                // but it is not reliable because of the heap data structure.
                throw new DuplicateAttributeException(key, "duplicate key [" + key + "] with values: [" + value + ", " +
                        keyValueArray[parentKeyIndex + 1] + "]");
            }

            // Bubble down the parent.
            keyValueArray[currentIndex] = parentKey;
            keyValueArray[currentIndex + 1] = keyValueArray[parentKeyIndex + 1];

            currentIndex = parentKeyIndex;
        }

        keyValueArray[currentIndex] = key;
        keyValueArray[currentIndex + 1] = value;
        nextIndex += 2;
        return this;
    }

    public  DefaultPartitionAttributesBuilder(int initialSize);

}