io.logspace.agent.shaded.tape.TaskQueue Maven / Gradle / Ivy
// Copyright 2012 Square, Inc.
package io.logspace.agent.shaded.tape;
/**
* Persistent task queue. Not safe for concurrent use.
*
* @param The type of tasks in the queue.
*/
public class TaskQueue implements ObjectQueue {
private final TaskInjector taskInjector;
private final ObjectQueue delegate;
public TaskQueue(ObjectQueue delegate) {
this(delegate, null);
}
public TaskQueue(ObjectQueue delegate, TaskInjector taskInjector) {
this.delegate = delegate;
this.taskInjector = taskInjector;
}
/**
* {@inheritDoc}
*
* Overridden to inject members into Tasks.
*/
@Override public T peek() {
T task = delegate.peek();
if (task != null && taskInjector != null) {
taskInjector.injectMembers(task);
}
return task;
}
@Override public int size() {
return delegate.size();
}
@Override public void add(T entry) {
delegate.add(entry);
}
@Override public void remove() {
delegate.remove();
}
@Override public void setListener(final Listener listener) {
if (listener != null) {
// Intercept event delivery to pass the correct TaskQueue instance to listener.
delegate.setListener(new Listener() {
@Override
public void onAdd(ObjectQueue queue, T entry) {
listener.onAdd(TaskQueue.this, entry);
}
@Override
public void onRemove(ObjectQueue queue) {
listener.onRemove(TaskQueue.this);
}
});
} else {
delegate.setListener(null);
}
}
}