/*
 * 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 Thomas Pantelis
 *
 * @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 );
        }
    }
}

@@ -372,16 +372,60 @@
 rrent;%0A%0A
+import com.google.common.annotations.Beta;%0A%0A
 import j
@@ -577,59 +577,15 @@
 omic
-Long;%0Aimport java.util.concurrent.atomic.AtomicLong
+Integer
 Fiel
@@ -828,16 +828,251 @@
 ue%3CE%3E %7B%0A
+    @SuppressWarnings(%22rawtypes%22)%0A    private static final AtomicIntegerFieldUpdater%3CTrackingLinkedBlockingQueue%3E LARGEST_QUEUE_SIZE_UPDATER = AtomicIntegerFieldUpdater.newUpdater(TrackingLinkedBlockingQueue.class, %22largestQueueSize%22);
 %0A    pri
@@ -1276,20 +1276,19 @@
 olatile 
-long
+int
  largest
@@ -1307,233 +1307,8 @@
 0;%0A%0A
-    @SuppressWarnings(%22rawtypes%22)%0A    private static AtomicLongFieldUpdater%3CTrackingLinkedBlockingQueue%3E LARGEST_QUEUE_SIZE_UPDATER = AtomicLongFieldUpdater.newUpdater(TrackingLinkedBlockingQueue.class, %22largestQueueSize%22);%0A%0A
     
@@ -1557,16 +1557,22 @@
 ngQueue(
+ final
  Collect
@@ -1731,16 +1731,22 @@
 ngQueue(
+ final
  int cap
@@ -1836,25 +1836,116 @@
 size.%0A     *
-/
+%0A     * FIXME: the this return will be changed to int in a future release.%0A     */%0A    @Beta
 %0A    public 
@@ -1970,16 +1970,17 @@
 ueSize()
+ 
 %7B%0A      
@@ -2057,12 +2057,24 @@
 er( 
-E e,
+final E e, final
  lon
@@ -2083,16 +2083,22 @@
 timeout,
+ final
  TimeUni
@@ -2326,16 +2326,22 @@
 n offer(
+ final
  E e ) %7B
@@ -2511,16 +2511,22 @@
 oid put(
+ final
  E e ) t
@@ -2656,16 +2656,22 @@
 ean add(
+ final
  E e ) %7B
@@ -2816,16 +2816,22 @@
  addAll(
+ final
  Collect
@@ -3032,20 +3032,25 @@
         
-long
+final int
  size = 
@@ -3057,24 +3057,25 @@
 size();%0A
+%0A
         
 long lar
@@ -3066,20 +3066,53 @@
         
-long
+int largest;%0A        do %7B%0A           
  largest
@@ -3140,20 +3140,25 @@
         
-if( 
+%7D while (
 size %3E l
@@ -3164,32 +3164,20 @@
 largest 
-) %7B%0A            
+&& !
 LARGEST_
@@ -3232,22 +3232,12 @@
 size
- 
+)
 );%0A
-        %7D%0A
     

fix thread safety issues