View Javadoc

1   /*
2    * $Id: DefaultQueueInfoDelegate.java 22147 2011-06-08 11:07:55Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  package org.mule.util.queue;
11  
12  import java.io.Serializable;
13  import java.util.LinkedList;
14  
15  /**
16   * The default QueueInfoDelegate. This uses a LinkedList to store the members of the queue.
17   */
18  public class DefaultQueueInfoDelegate implements QueueInfoDelegate
19  {
20      protected final int capacity;
21      protected final LinkedList<Serializable> list;
22  
23      public DefaultQueueInfoDelegate(int capacity)
24      {
25          this.capacity = capacity;
26          list = new LinkedList<Serializable>();
27      }
28  
29      @Override
30      public void putNow(Serializable o)
31      {
32          synchronized (list)
33          {
34              list.addLast(o);
35              list.notifyAll();
36          }
37      }
38  
39      @Override
40      public boolean offer(Serializable o, int room, long timeout) throws InterruptedException
41      {
42          if (Thread.interrupted())
43          {
44              throw new InterruptedException();
45          }
46          synchronized (list)
47          {
48              if (capacity > 0)
49              {
50                  if (capacity <= room)
51                  {
52                      throw new IllegalStateException("Can not add more objects than the capacity in one time");
53                  }
54                  long l1 = timeout > 0L ? System.currentTimeMillis() : 0L;
55                  long l2 = timeout;
56                  while (list.size() >= capacity - room)
57                  {
58                      if (l2 <= 0L)
59                      {
60                          return false;
61                      }
62                      list.wait(l2);
63                      l2 = timeout - (System.currentTimeMillis() - l1);
64                  }
65              }
66              if (o != null)
67              {
68                  list.addLast(o);
69              }
70              list.notifyAll();
71              return true;
72          }
73      }
74  
75      @Override
76      public Serializable poll(long timeout) throws InterruptedException
77      {
78          if (Thread.interrupted())
79          {
80              throw new InterruptedException();
81          }
82          synchronized (list)
83          {
84              long l1 = timeout > 0L ? System.currentTimeMillis() : 0L;
85              long l2 = timeout;
86              while (list.isEmpty())
87              {
88                  if (l2 <= 0L)
89                  {
90                      return null;
91                  }
92                  list.wait(l2);
93                  l2 = timeout - (System.currentTimeMillis() - l1);
94              }
95  
96              Serializable o = list.removeFirst();
97              list.notifyAll();
98              return o;
99          }
100     }
101 
102     @Override
103     public Serializable peek() throws InterruptedException
104     {
105         if (Thread.interrupted())
106         {
107             throw new InterruptedException();
108         }
109         synchronized (list)
110         {
111             if (list.isEmpty())
112             {
113                 return null;
114             }
115             else
116             {
117                 return list.getFirst();
118             }
119         }
120     }
121 
122     @Override
123     public void untake(Serializable item) throws InterruptedException
124     {
125         if (Thread.interrupted())
126         {
127             throw new InterruptedException();
128         }
129         synchronized (list)
130         {
131             list.addFirst(item);
132         }
133     }
134 
135     @Override
136     public int getSize()
137     {
138         return list.size();
139     }
140 }