<commit_before>/**
 * 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 com.bmd.jrt.runner;

import com.bmd.jrt.common.RoutineInterruptedException;
import com.bmd.jrt.time.TimeDuration;

import java.util.concurrent.TimeUnit;

import static com.bmd.jrt.time.TimeDuration.ZERO;
import static com.bmd.jrt.time.TimeDuration.fromUnit;
import static com.bmd.jrt.time.TimeDuration.nanos;

/**
 * Class maintaining a queue of invocations which is local to the calling thread.<br/>
 * The implementation ensures that recursive invocations are broken into a consuming loop running
 * in the same thread.
 * <p/>
 * Created by davide on 9/18/14.
 */
class LocalQueue {

    private static final int INITIAL_CAPACITY = 10;

    private static final ThreadLocal<LocalQueue> sQueue = new ThreadLocal<LocalQueue>() {

        @Override
        protected LocalQueue initialValue() {

            return new LocalQueue();
        }
    };

    private TimeDuration[] mDelays;

    private int mFirst;

    private long[] mInvocationTimeNs;

    private Invocation[] mInvocations;

    private boolean mIsRunning;

    private int mLast;

    private InvocationType[] mTypes;

    /**
     * Default constructor.
     */
    private LocalQueue() {

        mInvocationTimeNs = new long[INITIAL_CAPACITY];
        mInvocations = new Invocation[INITIAL_CAPACITY];
        mDelays = new TimeDuration[INITIAL_CAPACITY];
        mTypes = new InvocationType[INITIAL_CAPACITY];
    }

    /**
     * Runs the specified invocation.
     *
     * @param invocation the invocation.
     * @param delay      the execution delay.
     * @param timeUnit   the delay time unit.
     */
    public static void run(final Invocation invocation, final long delay, final TimeUnit timeUnit) {

        sQueue.get().addRun(invocation, delay, timeUnit);
    }

    /**
     * Runs the specified abort invocation.
     *
     * @param invocation the invocation.
     */
    public static void runAbort(final Invocation invocation) {

        sQueue.get().addAbort(invocation);
    }

    private static <T> void resizeArray(final T[] src, final T[] dst, final int first) {

        final int remainder = src.length - first;

        System.arraycopy(src, 0, dst, 0, first);
        System.arraycopy(src, first, dst, dst.length - remainder, remainder);
    }

    private static void resizeArray(final long[] src, final long[] dst, final int first) {

        final int remainder = src.length - first;

        System.arraycopy(src, 0, dst, 0, first);
        System.arraycopy(src, first, dst, dst.length - remainder, remainder);
    }

    private void add(final Invocation invocation, TimeDuration delay, InvocationType type) {

        if (mInvocations.length == 0) {

            mInvocationTimeNs = new long[1];
            mInvocations = new Invocation[1];
            mDelays = new TimeDuration[1];
            mTypes = new InvocationType[1];
        }

        final int i = mLast;

        mInvocationTimeNs[i] = System.nanoTime();
        mInvocations[i] = invocation;
        mDelays[i] = delay;
        mTypes[i] = type;

        final int newLast;

        if ((i >= (mInvocations.length - 1)) || (i == Integer.MAX_VALUE)) {

            newLast = 0;

        } else {

            newLast = i + 1;
        }

        if (mFirst == newLast) {

            ensureCapacity(mInvocations.length + 1);
        }

        mLast = newLast;
    }

    private void addAbort(final Invocation invocation) {
<commit_msg> Unit tests + Cosmetics<commit_after>/**
 * 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 com.bmd.jrt.runner;

import com.bmd.jrt.common.RoutineInterruptedException;
import com.bmd.jrt.time.TimeDuration;

import java.util.concurrent.TimeUnit;

import static com.bmd.jrt.time.TimeDuration.ZERO;
import static com.bmd.jrt.time.TimeDuration.fromUnit;
import static com.bmd.jrt.time.TimeDuration.nanos;

/**
 * Class maintaining a queue of invocations which is local to the calling thread.<br/>
 * The implementation ensures that recursive invocations are broken into a consuming loop running
 * in the same thread.
 * <p/>
 * Created by davide on 9/18/14.
 */
class LocalQueue {

    private static final int INITIAL_CAPACITY = 10;

    private static final ThreadLocal<LocalQueue> sQueue = new ThreadLocal<LocalQueue>() {

        @Override
        protected LocalQueue initialValue() {

            return new LocalQueue();
        }
    };

    private TimeDuration[] mDelays;

    private int mFirst;

    private long[] mInvocationTimeNs;

    private Invocation[] mInvocations;

    private boolean mIsRunning;

    private int mLast;

    private InvocationType[] mTypes;

    /**
     * Default constructor.
     */
    private LocalQueue() {

        mInvocationTimeNs = new long[INITIAL_CAPACITY];
        mInvocations = new Invocation[INITIAL_CAPACITY];
        mDelays = new TimeDuration[INITIAL_CAPACITY];
        mTypes = new InvocationType[INITIAL_CAPACITY];
    }

    /**
     * Runs the specified invocation.
     *
     * @param invocation the invocation.
     * @param delay      the execution delay.
     * @param timeUnit   the delay time unit.
     */
    public static void run(final Invocation invocation, final long delay, final TimeUnit timeUnit) {

        sQueue.get().addRun(invocation, delay, timeUnit);
    }

    /**
     * Runs the specified abort invocation.
     *
     * @param invocation the invocation.
     */
    public static void runAbort(final Invocation invocation) {

        sQueue.get().addAbort(invocation);
    }

    private static <T> void resizeArray(final T[] src, final T[] dst, final int first) {

        final int remainder = src.length - first;

        System.arraycopy(src, 0, dst, 0, first);
        System.arraycopy(src, first, dst, dst.length - remainder, remainder);
    }

    private static void resizeArray(final long[] src, final long[] dst, final int first) {

        final int remainder = src.length - first;

        System.arraycopy(src, 0, dst, 0, first);
        System.arraycopy(src, first, dst, dst.length - remainder, remainder);
    }

    private void add(final Invocation invocation, TimeDuration delay, InvocationType type) {

        final int i = mLast;

        mInvocationTimeNs[i] = System.nanoTime();
        mInvocations[i] = invocation;
        mDelays[i] = delay;
        mTypes[i] = type;

        final int newLast;

        if ((i >= (mInvocations.length - 1)) || (i == Integer.MAX_VALUE)) {

            newLast = 0;

        } else {

            newLast = i + 1;
        }

        if (mFirst == newLast) {

            ensureCapacity(mInvocations.length + 1);
        }

        mLast = newLast;
    }

    private void addAbort(final Invocation invocation) {

{'fraction_non_alphanumeric': 0.09407216494845361, 'fraction_numerical': 0.005541237113402062, 'mean_word_length': 2.674715909090909, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5523806', 'n_tokens_mistral': 2300, 'n_tokens_neox': 2155, 'n_words': 710}