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

org.swisspush.gateleen.player.log.BufferingRequestLog Maven / Gradle / Ivy

package org.swisspush.gateleen.player.log;

import org.swisspush.gateleen.player.exchange.Exchange;

import java.util.Iterator;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;

/**
 * A request log implemented with an in-memory buffer. Thread safe.
 *
 * @author https://github.com/lbovet [Laurent Bovet]
 */
public class BufferingRequestLog extends RequestLog {

    Queue buffer = new ConcurrentLinkedQueue<>();

    public void add(Exchange exchange) {
        buffer.add(exchange);
    }

    /**
     * Move all exchange from this request log to another one.
     * 
     * @param target target
     */
    public void dump(BufferingRequestLog target) {
        while (!buffer.isEmpty()) {
            target.add(buffer.poll());
        }
    }

    public void clear() {
        buffer.clear();
    }

    @Override
    public Iterator iterator() {
        return buffer.iterator();
    }

    @Override
    public String toString() {
        StringBuffer result = new StringBuffer();
        for (Exchange exchange : this) {
            result.append(exchange);
            result.append("\n");
        }
        return result.toString();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy