View Javadoc

1   /*
2    * $Id: QueueInfo.java 7963 2007-08-21 08:53:15Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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 int hashCode()
30      {
31          return name.hashCode();
32      }
33  
34      public void putNow(Object o)
35      {
36          synchronized (list)
37          {
38              list.addLast(o);
39              list.notifyAll();
40          }
41      }
42  
43      public boolean offer(Object o, int room, long timeout) throws InterruptedException
44      {
45          if (Thread.interrupted())
46          {
47              throw new InterruptedException();
48          }
49          synchronized (list)
50          {
51              if (config.capacity > 0)
52              {
53                  if (config.capacity <= room)
54                  {
55                      throw new IllegalStateException("Can not add more objects than the capacity in one time");
56                  }
57                  long l1 = timeout > 0L ? System.currentTimeMillis() : 0L;
58                  long l2 = timeout;
59                  while (list.size() >= config.capacity - room)
60                  {
61                      if (l2 <= 0L)
62                      {
63                          return false;
64                      }
65                      list.wait(l2);
66                      l2 = timeout - (System.currentTimeMillis() - l1);
67                  }
68              }
69              if (o != null)
70              {
71                  list.addLast(o);
72              }
73              list.notifyAll();
74              return true;
75          }
76      }
77  
78      public Object poll(long timeout) throws InterruptedException
79      {
80          if (Thread.interrupted())
81          {
82              throw new InterruptedException();
83          }
84          synchronized (list)
85          {
86              long l1 = timeout > 0L ? System.currentTimeMillis() : 0L;
87              long l2 = timeout;
88              while (list.isEmpty())
89              {
90                  if (l2 <= 0L)
91                  {
92                      return null;
93                  }
94                  list.wait(l2);
95                  l2 = timeout - (System.currentTimeMillis() - l1);
96              }
97              Object o = list.removeFirst();
98              list.notifyAll();
99              return o;
100         }
101     }
102 
103     public Object 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 }