<commit_before>/*
 * jlib - Open Source Java Library
 *
 *     www.jlib.org
 *
 *
 *     Copyright 2005-2015 <NAME>
 *
 *     Licensed under the Apache License, Version 2.0 (the "License");
 *     you may not use this file except in compliance with the License.
 *     You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *     Unless required by applicable law or agreed to in writing, software
 *     distributed under the License is distributed on an "AS IS" BASIS,
 *     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *     See the License for the specific language governing permissions and
 *     limitations under the License.
 */

package org.jlib.container.capacity;

import org.jlib.basefunctions.ApplicationObject;
import org.jlib.container.storage.ContentIndexRegistry;
import org.jlib.container.storage.InvalidIndexException;
import org.jlib.container.storage.LinearIndexStorage;
import org.jlib.container.storage.LinearIndexStorageException;
import org.jlib.container.storage.IndexRangeOperationDescriptor;

import static org.jlib.core.message.MessageUtility.mfmessage;

public abstract class AbstractCapacityStrategy<Item>
extends ApplicationObject {

    private final LinearIndexStorage<Item> storage;

    private final ContentIndexRegistry contentIndexRegistry;

    protected AbstractCapacityStrategy(final LinearIndexStorage<Item> storage,
                                       final ContentIndexRegistry contentIndexRegistry) {
        this.storage = storage;
        this.contentIndexRegistry = contentIndexRegistry;
    }

    protected LinearIndexStorage<Item> getStorage() {
        return storage;
    }

    protected ContentIndexRegistry getContentIndexRegistry() {
        return contentIndexRegistry;
    }

    /**
     * Returns the tail capacity, that is, the number of storable {@link Item}s behind the last {@link Item}.
     *
     * @return integer specifying the tail capacity
     */
    protected int getTailCapacity() {
        return storage.getCapacity() - contentIndexRegistry.getLastItemIndex();
    }

    protected IndexRangeOperationDescriptor getDescriptorCopyAllItemsToIndex(final int targetIndex) {
        return new IndexRangeOperationDescriptor(contentIndexRegistry.getFirstItemIndex(),
                                                 contentIndexRegistry.getLastItemIndex(), targetIndex);
    }

    /**
     * Ensures that the specified capacity is valid.
     *
     * @param capacity
     *        integer specifying the capacity
     *
     * @throws LinearIndexStorageException
     *         if {@code capacity < 0}
     */
    public void ensurePartialCapacityValid(final int capacity) {
        if (capacity < 0)
            throw new InvalidPartialCapacityException(storage, capacity);
    }

    protected void ensureIndexValid(final int index) {
        if (index < contentIndexRegistry.getFirstItemIndex())
            throw new InvalidIndexException(storage, mfmessage("index = {0} > {1} = " + "firstItemIndex", index,
                                                               contentIndexRegistry.getFirstItemIndex()));<commit_msg>AbstractCapacityStrategy: String formatted<commit_after>/*
 * jlib - Open Source Java Library
 *
 *     www.jlib.org
 *
 *
 *     Copyright 2005-2015 <NAME>
 *
 *     Licensed under the Apache License, Version 2.0 (the "License");
 *     you may not use this file except in compliance with the License.
 *     You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *     Unless required by applicable law or agreed to in writing, software
 *     distributed under the License is distributed on an "AS IS" BASIS,
 *     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *     See the License for the specific language governing permissions and
 *     limitations under the License.
 */

package org.jlib.container.capacity;

import org.jlib.container.storage.ContentIndexRegistry;
import org.jlib.container.storage.IndexRangeOperationDescriptor;
import org.jlib.container.storage.InvalidIndexException;
import org.jlib.container.storage.LinearIndexStorage;
import org.jlib.container.storage.LinearIndexStorageException;

import org.jlib.basefunctions.ApplicationObject;
import static org.jlib.core.message.MessageUtility.mfmessage;

public abstract class AbstractCapacityStrategy<Item>
extends ApplicationObject {

    private final LinearIndexStorage<Item> storage;

    private final ContentIndexRegistry contentIndexRegistry;

    protected AbstractCapacityStrategy(final LinearIndexStorage<Item> storage,
                                       final ContentIndexRegistry contentIndexRegistry) {
        this.storage = storage;
        this.contentIndexRegistry = contentIndexRegistry;
    }

    protected LinearIndexStorage<Item> getStorage() {
        return storage;
    }

    protected ContentIndexRegistry getContentIndexRegistry() {
        return contentIndexRegistry;
    }

    /**
     * Returns the tail capacity, that is, the number of storable {@link Item}s behind the last {@link Item}.
     *
     * @return integer specifying the tail capacity
     */
    protected int getTailCapacity() {
        return storage.getCapacity() - contentIndexRegistry.getLastItemIndex();
    }

    protected IndexRangeOperationDescriptor getDescriptorCopyAllItemsToIndex(final int targetIndex) {
        return new IndexRangeOperationDescriptor(contentIndexRegistry.getFirstItemIndex(),
                                                 contentIndexRegistry.getLastItemIndex(), targetIndex);
    }

    /**
     * Ensures that the specified capacity is valid.
     *
     * @param capacity
     *        integer specifying the capacity
     *
     * @throws LinearIndexStorageException
     *         if {@code capacity < 0}
     */
    public void ensurePartialCapacityValid(final int capacity) {
        if (capacity < 0)
            throw new InvalidPartialCapacityException(storage, capacity);
    }

    protected void ensureIndexValid(final int index) {
        if (index < contentIndexRegistry.getFirstItemIndex())
            throw new InvalidIndexException(storage, mfmessage("index = {0} > {1} = firstItemIndex", index,
                                                               contentIndexRegistry.getFirstItemIndex()));
{'fraction_non_alphanumeric': 0.07259073842302878, 'fraction_numerical': 0.0050062578222778474, 'mean_word_length': 2.9390018484288354, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5047886', 'n_tokens_mistral': 1538, 'n_tokens_neox': 1490, 'n_words': 455}