2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 package org
.apache
.hadoop
.hbase
.util
;
21 import java
.util
.Collection
;
22 import java
.util
.Iterator
;
23 import java
.util
.concurrent
.BlockingQueue
;
24 import java
.util
.concurrent
.DelayQueue
;
25 import java
.util
.concurrent
.Delayed
;
26 import java
.util
.concurrent
.TimeUnit
;
28 import org
.apache
.yetus
.audience
.InterfaceAudience
;
31 * A blocking queue implementation for adding a constant delay. Uses a DelayQueue as a backing store
32 * @param <E> type of elements
34 @InterfaceAudience.Private
35 public class ConstantDelayQueue
<E
> implements BlockingQueue
<E
> {
37 private static final class DelayedElement
<T
> implements Delayed
{
40 public DelayedElement(T element
, long delayMs
) {
41 this.element
= element
;
42 this.end
= EnvironmentEdgeManager
.currentTime() + delayMs
;
46 public int compareTo(Delayed o
) {
47 long cmp
= getDelay(TimeUnit
.MILLISECONDS
) - o
.getDelay(TimeUnit
.MILLISECONDS
);
48 return cmp
== 0 ?
0 : ( cmp
< 0 ?
-1 : 1);
52 public long getDelay(TimeUnit unit
) {
53 return unit
.convert(end
- System
.currentTimeMillis(), TimeUnit
.MILLISECONDS
);
57 private final long delayMs
;
60 private DelayQueue
<DelayedElement
<E
>> queue
= new DelayQueue
<>();
62 public ConstantDelayQueue(TimeUnit timeUnit
, long delay
) {
63 this.delayMs
= TimeUnit
.MILLISECONDS
.convert(delay
, timeUnit
);
68 DelayedElement
<E
> el
= queue
.remove();
69 return el
== null ?
null : el
.element
;
74 DelayedElement
<E
> el
= queue
.poll();
75 return el
== null ?
null : el
.element
;
80 DelayedElement
<E
> el
= queue
.element();
81 return el
== null ?
null : el
.element
;
86 DelayedElement
<E
> el
= queue
.peek();
87 return el
== null ?
null : el
.element
;
96 public boolean isEmpty() {
97 return queue
.isEmpty();
101 public Iterator
<E
> iterator() {
102 throw new UnsupportedOperationException(); // not implemented yet
106 public Object
[] toArray() {
107 throw new UnsupportedOperationException(); // not implemented yet
111 public <T
> T
[] toArray(T
[] a
) {
112 throw new UnsupportedOperationException(); // not implemented yet
116 public boolean containsAll(Collection
<?
> c
) {
117 throw new UnsupportedOperationException(); // not implemented yet
121 public boolean addAll(Collection
<?
extends E
> c
) {
122 throw new UnsupportedOperationException(); // not implemented yet
126 public boolean removeAll(Collection
<?
> c
) {
127 throw new UnsupportedOperationException(); // not implemented yet
131 public boolean retainAll(Collection
<?
> c
) {
132 throw new UnsupportedOperationException(); // not implemented yet
136 public void clear() {
141 public boolean add(E e
) {
142 return queue
.add(new DelayedElement
<>(e
, delayMs
));
146 public boolean offer(E e
) {
147 return queue
.offer(new DelayedElement
<>(e
, delayMs
));
151 public void put(E e
) throws InterruptedException
{
152 queue
.put(new DelayedElement
<>(e
, delayMs
));
156 public boolean offer(E e
, long timeout
, TimeUnit unit
) throws InterruptedException
{
157 return queue
.offer(new DelayedElement
<>(e
, delayMs
), timeout
, unit
);
161 public E
take() throws InterruptedException
{
162 DelayedElement
<E
> el
= queue
.take();
163 return el
== null ?
null : el
.element
;
167 public E
poll(long timeout
, TimeUnit unit
) throws InterruptedException
{
168 DelayedElement
<E
> el
= queue
.poll(timeout
, unit
);
169 return el
== null ?
null : el
.element
;
173 public int remainingCapacity() {
174 return queue
.remainingCapacity();
178 public boolean remove(Object o
) {
179 throw new UnsupportedOperationException(); // not implemented yet
183 public boolean contains(Object o
) {
184 throw new UnsupportedOperationException(); // not implemented yet
188 public int drainTo(Collection
<?
super E
> c
) {
189 throw new UnsupportedOperationException(); // not implemented yet
193 public int drainTo(Collection
<?
super E
> c
, int maxElements
) {
194 throw new UnsupportedOperationException(); // not implemented yet