View Javadoc

1   /*
2    * $Id: QueueInfo.java 19191 2010-08-25 21:05:23Z tcarlson $
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  
11  package org.mule.util.queue;
12  
13  import java.util.LinkedList;
14  
15  /**
16   * Stores information about a Queue
17   */
18  public class QueueInfo
19  {
20      protected LinkedList list;
21      protected String name;
22      protected QueueConfiguration config;
23  
24      public boolean equals(Object obj)
25      {
26          return (obj instanceof QueueInfo && name.equals(((QueueInfo) obj).name));
27      }
28  
29      public String getName()
30      {
31          return name;
32      }
33      
34      public int hashCode()
35      {
36          return name.hashCode();
37      }
38  
39      public void putNow(Object o)
40      {
41          synchronized (list)
42          {
43              list.addLast(o);
44              list.notifyAll();
45          }
46      }
47  
48      public boolean offer(Object o, int room, long timeout) throws InterruptedException
49      {
50          if (Thread.interrupted())
51          {
52              throw new InterruptedException();
53          }
54          synchronized (list)
55          {
56              if (config.capacity > 0)
57              {
58                  if (config.capacity <= room)
59                  {
60                      throw new IllegalStateException("Can not add more objects than the capacity in one time");
61                  }
62                  long l1 = timeout > 0L ? System.currentTimeMillis() : 0L;
63                  long l2 = timeout;
64                  while (list.size() >= config.capacity - room)
65                  {
66                      if (l2 <= 0L)
67                      {
68                          return false;
69                      }
70                      list.wait(l2);
71                      l2 = timeout - (System.currentTimeMillis() - l1);
72                  }
73              }
74              if (o != null)
75              {
76                  list.addLast(o);
77              }
78              list.notifyAll();
79              return true;
80          }
81      }
82  
83      public Object poll(long timeout) throws InterruptedException
84      {
85          if (Thread.interrupted())
86          {
87              throw new InterruptedException();
88          }
89          synchronized (list)
90          {
91              long l1 = timeout > 0L ? System.currentTimeMillis() : 0L;
92              long l2 = timeout;
93              while (list.isEmpty())
94              {
95                  if (l2 <= 0L)
96                  {
97                      return null;
98                  }
99                  list.wait(l2);
100                 l2 = timeout - (System.currentTimeMillis() - l1);
101             }
102             Object o = list.removeFirst();
103             list.notifyAll();
104             return o;
105         }
106     }
107 
108     public Object peek() throws InterruptedException
109     {
110         if (Thread.interrupted())
111         {
112             throw new InterruptedException();
113         }
114         synchronized (list)
115         {
116             if (list.isEmpty())
117             {
118                 return null;
119             }
120             else
121             {
122                 return list.getFirst();
123             }
124         }
125     }
126 
127     public void untake(Object item) throws InterruptedException
128     {
129         if (Thread.interrupted())
130         {
131             throw new InterruptedException();
132         }
133         synchronized (list)
134         {
135             list.addFirst(item);
136         }
137     }
138 
139 }