All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.logspace.agent.shaded.tape.InMemoryObjectQueue Maven / Gradle / Ivy

There is a newer version: 0.3.2
Show newest version
// Copyright 2012 Square, Inc.
package io.logspace.agent.shaded.tape;

import java.util.LinkedList;
import java.util.Queue;

/**
 * A queue for objects that are not serious enough to be written to disk.  Objects in this queue
 * are kept in memory and will not be serialized.
 *
 * @param  The type of elements in the queue.
 */
public class InMemoryObjectQueue implements ObjectQueue {
  private final Queue tasks;
  private Listener listener;

  @SuppressWarnings("unchecked")
  public InMemoryObjectQueue() {
    tasks = (Queue) new LinkedList();
  }

  @Override public void add(T entry) {
    tasks.add(entry);
    if (listener != null) listener.onAdd(this, entry);
  }

  @Override public T peek() {
    return tasks.peek();
  }

  @Override public int size() {
    return tasks.size();
  }

  @Override public void remove() {
    tasks.remove();
    if (listener != null) listener.onRemove(this);
  }

  @Override public void setListener(Listener listener) {
    if (listener != null) {
      for (T task : tasks) {
        listener.onAdd(this, task);
      }
    }
    this.listener = listener;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy