ConcurrentLinkedQueueAtomicSize.java
01 /*
02  *
03  * Licensed to the Apache Software Foundation (ASF) under one
04  * or more contributor license agreements.  See the NOTICE file
05  * distributed with this work for additional information
06  * regarding copyright ownership.  The ASF licenses this file
07  * to you under the Apache License, Version 2.0 (the
08  * "License"); you may not use this file except in compliance
09  * with the License.  You may obtain a copy of the License at
10  
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  */
21 package org.apache.qpid.util;
22 
23 import java.util.concurrent.ConcurrentLinkedQueue;
24 import java.util.concurrent.atomic.AtomicInteger;
25 
26 public class ConcurrentLinkedQueueAtomicSize<E> extends ConcurrentLinkedQueue<E>
27 {
28     AtomicInteger _size = new AtomicInteger(0);
29 
30     public int size()
31     {
32         return _size.get();
33     }
34 
35     public boolean offer(E o)
36     {
37 
38         if (super.offer(o))
39         {
40             _size.incrementAndGet();
41             return true;
42         }
43 
44         return false;
45     }
46 
47     public E poll()
48     {
49         E e = super.poll();
50 
51         if (e != null)
52         {
53             _size.decrementAndGet();
54         }
55 
56         return e;
57     }
58 
59     @Override
60     public boolean remove(Object o)
61     {
62         if (super.remove(o))
63         {
64             _size.decrementAndGet();
65             return true;
66         }
67 
68         return false;
69     }
70 }