<commit_before>/*
 * Copyright (c) 2014 Brocade Communications Systems, Inc. and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */

package org.opendaylight.yangtools.util.concurrent;

import java.util.Collection;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicLongFieldUpdater;

/**
 * A {@link LinkedBlockingQueue} that tracks the largest queue size for debugging.
 *
 * @author <NAME>
 *
 * @param <E> the element t.ype
 */
public class TrackingLinkedBlockingQueue<E> extends LinkedBlockingQueue<E> {

    private static final long serialVersionUID = 1L;

    /**
     * Holds largestQueueSize, this long field should be only accessed
     * using {@value #LARGEST_QUEUE_SIZE_UPDATER}
     */
    private volatile long largestQueueSize = 0;

    @SuppressWarnings("rawtypes")
    private static AtomicLongFieldUpdater<TrackingLinkedBlockingQueue> LARGEST_QUEUE_SIZE_UPDATER = AtomicLongFieldUpdater.newUpdater(TrackingLinkedBlockingQueue.class, "largestQueueSize");

    /**
     * @see LinkedBlockingQueue#LinkedBlockingQueue
     */
    public TrackingLinkedBlockingQueue() {
        super();
    }

    /**
     * @see LinkedBlockingQueue#LinkedBlockingQueue(Collection)
     */
    public TrackingLinkedBlockingQueue( Collection<? extends E> c ) {
        super(c);
    }

    /**
     * @see LinkedBlockingQueue#LinkedBlockingQueue(int)
     */
    public TrackingLinkedBlockingQueue( int capacity ) {
        super(capacity);
    }

    /**
     * Returns the largest queue size.
     */
    public long getLargestQueueSize(){
        return largestQueueSize;
    }

    @Override
    public boolean offer( E e, long timeout, TimeUnit unit ) throws InterruptedException {
        if( super.offer( e, timeout, unit ) ) {
            updateLargestQueueSize();
            return true;
        }

        return false;
    }

    @Override
    public boolean offer( E e ) {
        if( super.offer( e ) ) {
            updateLargestQueueSize();
            return true;
        }

        return false;
    }

    @Override
    public void put( E e ) throws InterruptedException {
        super.put( e );
        updateLargestQueueSize();
    }

    @Override
    public boolean add( E e ) {
        boolean result = super.add( e );
        updateLargestQueueSize();
        return result;
    }

    @Override
    public boolean addAll( Collection<? extends E> c ) {
        try {
            return super.addAll( c );
        } finally {
            updateLargestQueueSize();
        }
    }

    private void updateLargestQueueSize() {
        long size = size();
        long largest = largestQueueSize;
        if( size > largest ) {
            LARGEST_QUEUE_SIZE_UPDATER.compareAndSet(this, largest, size );
        }
    }
}<commit_msg>BUG-1783: fix thread safety issues<commit_after>/*
 * Copyright (c) 2014 Brocade Communications Systems, Inc. and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */

package org.opendaylight.yangtools.util.concurrent;

import com.google.common.annotations.Beta;

import java.util.Collection;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;

/**
 * A {@link LinkedBlockingQueue} that tracks the largest queue size for debugging.
 *
 * @author <NAME>
 *
 * @param <E> the element t.ype
 */
public class TrackingLinkedBlockingQueue<E> extends LinkedBlockingQueue<E> {
    @SuppressWarnings("rawtypes")
    private static final AtomicIntegerFieldUpdater<TrackingLinkedBlockingQueue> LARGEST_QUEUE_SIZE_UPDATER = AtomicIntegerFieldUpdater.newUpdater(TrackingLinkedBlockingQueue.class, "largestQueueSize");
    private static final long serialVersionUID = 1L;

    /**
     * Holds largestQueueSize, this long field should be only accessed
     * using {@value #LARGEST_QUEUE_SIZE_UPDATER}
     */
    private volatile int largestQueueSize = 0;

    /**
     * @see LinkedBlockingQueue#LinkedBlockingQueue
     */
    public TrackingLinkedBlockingQueue() {
        super();
    }

    /**
     * @see LinkedBlockingQueue#LinkedBlockingQueue(Collection)
     */
    public TrackingLinkedBlockingQueue( final Collection<? extends E> c ) {
        super(c);
    }

    /**
     * @see LinkedBlockingQueue#LinkedBlockingQueue(int)
     */
    public TrackingLinkedBlockingQueue( final int capacity ) {
        super(capacity);
    }

    /**
     * Returns the largest queue size.
     *
     * FIXME: the this return will be changed to int in a future release.
     */
    @Beta
    public long getLargestQueueSize() {
        return largestQueueSize;
    }

    @Override
    public boolean offer( final E e, final long timeout, final TimeUnit unit ) throws InterruptedException {
        if( super.offer( e, timeout, unit ) ) {
            updateLargestQueueSize();
            return true;
        }

        return false;
    }

    @Override
    public boolean offer( final E e ) {
        if( super.offer( e ) ) {
            updateLargestQueueSize();
            return true;
        }

        return false;
    }

    @Override
    public void put( final E e ) throws InterruptedException {
        super.put( e );
        updateLargestQueueSize();
    }

    @Override
    public boolean add( final E e ) {
        boolean result = super.add( e );
        updateLargestQueueSize();
        return result;
    }

    @Override
    public boolean addAll( final Collection<? extends E> c ) {
        try {
            return super.addAll( c );
        } finally {
            updateLargestQueueSize();
        }
    }

    private void updateLargestQueueSize() {
        final int size = size();

        int largest;
        do {
            largest = largestQueueSize;
        } while (size > largest && !LARGEST_QUEUE_SIZE_UPDATER.compareAndSet(this, largest, size));
    }
}
{'fraction_non_alphanumeric': 0.09023854362837413, 'fraction_numerical': 0.003766478342749529, 'mean_word_length': 2.658438576349024, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 19, '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': '5941433', 'n_tokens_mistral': 1834, 'n_tokens_neox': 1782, 'n_words': 481}