<commit_before>package org.jlib.container.sequence;

import org.jlib.core.traverser.TwoWayTraverser;

/**
 * Sequence containing exactly one Item.
 * 
 * @param <Item>
 *        type of the item held in the {@link Sequence}
 * 
 * @author <NAME>
 */
class SingletonSequence<Item>
extends AbstractNonEmptySequence<Item> {

    /** sole item of this {@link SingletonSequence} */
    private final Item item;

    /** current delegate {@link SequenceTraverser} */
    private SequenceTraverser<Item> currentDelegateTraverser;

    /** delegate {@link SequenceTraverser} at the {@link SingletonSequence} head */
    private final SequenceTraverser<Item> headTraverser = new AbstractSequenceTraverser<>(this) {

        @Override
        public boolean isPreviousItemAccessible() {
            return false;
        }

        @Override
        public Item getPreviousItem()
        throws NoPreviousSequenceItemException {
            throw new NoPreviousSequenceItemException(SingletonSequence.this);
        }

        @Override
        public boolean isNextItemAccessible() {
            return true;
        }

        @Override
        public Item getNextItem() {
            currentDelegateTraverser = tailTraverser;

            return item;
        }
    };

    /**
     * delegate {@link TwoWayTraverser} at the {@link SingletonSequence}
     * tailTraverser
     */
    private final SequenceTraverser<Item> tailTraverser = new AbstractSequenceTraverser<>(this) {

        @Override
        public boolean isPreviousItemAccessible() {
            return true;
        }

        @Override
        public Item getPreviousItem()
        throws NoPreviousSequenceItemException {
            currentDelegateTraverser = headTraverser;

            return item;
        }

        @Override
        public boolean isNextItemAccessible() {
            return false;
        }

        @Override
        public Item getNextItem() {
            throw new NoPreviousSequenceItemException(SingletonSequence.this);
        }
    };

    /**
     * Creates a new {@link SingletonSequence} with the specified Item.
     * 
     * @param item
     *        sole Item in this {@link SingletonSequence}
     */
    public SingletonSequence(final Item item) {
        this.item = item;
    }

    @Override
    public SequenceTraverser<Item> createTraverser() {
        return new AbstractSequenceTraverser<>(this) {

            @Override
            public boolean isPreviousItemAccessible() {
                return currentDelegateTraverser.isPreviousItemAccessible();
            }

            @Override
            public Item getPreviousItem()
            throws NoPreviousSequenceItemException {
                return currentDelegateTraverser.getPreviousItem();
            }

            @Override
            public boolean isNextItemAccessible() {
                return currentDelegateTraverser.isNextItemAccessible();
            }

            @Override
            public Item getNextItem()
            throws NoNextSequenceItemException {
                return currentDelegateTraverser.getNextItem();
            }

        };
    }

    @Override
    public int getItemsCount() {
        return 1;
    }

    @Override
    public SequenceTraverser<Item> createTraverser() {
        return createTraverser();
    }
}<commit_msg>SingletonSequence: optimized<commit_after>package org.jlib.container.sequence;

/**
 * Sequence containing exactly one Item.
 * 
 * @param <Item>
 *        type of the item held in the {@link Sequence}
 * 
 * @author <NAME>
 */
class SingletonSequence<Item>
extends AbstractNonEmptySequence<Item> {

    /** sole item of this {@link SingletonSequence} */
    private final Item item;

    /** current {@link SequenceTraverser} state */
    private SequenceTraverser<Item> currentTraverserState;

    /** {@link SequenceTraverser} state at the {@link SingletonSequence} head */
    private final SequenceTraverser<Item> headTraverserState =
        new AbstractSequenceTraverser<Item, SingletonSequence<Item>>(this) {

            @Override
            public boolean isPreviousItemAccessible() {
                return false;
            }

            @Override
            public Item getPreviousItem()
            throws NoPreviousSequenceItemException {
                throw new NoPreviousSequenceItemException(getSequence());
            }

            @Override
            public boolean isNextItemAccessible() {
                return true;
            }

            @Override
            public Item getNextItem() {
                currentTraverserState = tailTraverserState;

                return item;
            }
        };

    /** {@link SequenceTraverser} state at the {@link SingletonSequence} tail */
    private final SequenceTraverser<Item> tailTraverserState =
        new AbstractSequenceTraverser<Item, SingletonSequence<Item>>(this) {

            @Override
            public boolean isPreviousItemAccessible() {
                return true;
            }

            @Override
            public Item getPreviousItem()
            throws NoPreviousSequenceItemException {
                currentTraverserState = headTraverserState;

                return item;
            }

            @Override
            public boolean isNextItemAccessible() {
                return false;
            }

            @Override
            public Item getNextItem() {
                throw new NoPreviousSequenceItemException(getSequence());
            }
        };

    /**
     * Creates a new {@link SingletonSequence} with the specified Item.
     * 
     * @param item
     *        sole Item in this {@link SingletonSequence}
     */
    public SingletonSequence(final Item item) {
        this.item = item;
    }

    @Override
    public SequenceTraverser<Item> createTraverser() {
        return new AbstractSequenceTraverser<Item, SingletonSequence<Item>>(this) {

            @Override
            public boolean isPreviousItemAccessible() {
                return currentTraverserState.isPreviousItemAccessible();
            }

            @Override
            public Item getPreviousItem()
            throws NoPreviousSequenceItemException {
                return currentTraverserState.getPreviousItem();
            }

            @Override
            public boolean isNextItemAccessible() {
                return currentTraverserState.isNextItemAccessible();
            }

            @Override
            public Item getNextItem()
            throws NoNextSequenceItemException {
                return currentTraverserState.getNextItem();
            }

        };
    }

    @Override
    public int getItemsCount() {
        return 1;
    }
}
{'fraction_non_alphanumeric': 0.07388137356919876, 'fraction_numerical': 0.0002973093503790694, 'mean_word_length': 1.9743589743589745, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '262017', 'n_tokens_mistral': 1675, 'n_tokens_neox': 1584, 'n_words': 394}