<commit_before>/**
 * Copyright (C) 2010-12 Brookhaven National Laboratory
 * All rights reserved. Use is subject to license terms.
 */
package org.epics.pvmanager;

import java.util.ArrayList;
import java.util.List;

/**
 * Collects value at read rate and puts them in a queue.
 * <p>
 * Always returns new value as every read clears the cache.
 * The values are returned oldest value first. When maxSize is reached,
 * the oldest values are discarded.
 *
 * @param <T> the type contained in the queue
 * @author carcassi
 */
public class QueueCollector<T> implements Collector<T, List<T>> {
    
    private final Object lock = new Object();
    private List<T> writeBuffer;
    private int maxSize;

    /**
     * New queue collector with the given max size for the queue.
     * 
     * @param maxSize maximum number of elements in the queue
     */
    public QueueCollector(int maxSize) {
        synchronized(lock) {
            this.maxSize = maxSize;
            writeBuffer = new ArrayList<>();
        }
    }

    @Override
    public void writeValue(T newValue) {
        synchronized(lock) {
            writeBuffer.add(newValue);
            if (writeBuffer.size() > maxSize) {
                writeBuffer.remove(0);
            }
        }
    }

    @Override
    public List<T> readValue() {
        synchronized(lock) {
            List<T> data = new ArrayList<>(writeBuffer);
            writeBuffer.clear();
            return data;
        }
    }

    /**
     * Changes the number of maximum values in the queue.
     * <p>
     * If new maxSize is less than the current number of element in the queue,
     * the old values are discarded.
     * 
     * @param maxSize the maximum number of elements in the queue
     */
    public void setMaxSize(int maxSize) {
        synchronized(lock) {
            this.maxSize = maxSize;
            while (writeBuffer.size() > maxSize) {
                writeBuffer.remove(0);
            }
        }
    }<commit_msg>core: reverting how queuecollector works.<commit_after>/**
 * Copyright (C) 2010-12 Brookhaven National Laboratory
 * All rights reserved. Use is subject to license terms.
 */
package org.epics.pvmanager;

import java.util.ArrayList;
import java.util.List;

/**
 * Collects value at read rate and puts them in a queue.
 * <p>
 * Always returns new value as every read clears the cache.
 * The values are returned oldest value first. When maxSize is reached,
 * the oldest values are discarded.
 *
 * @param <T> the type contained in the queue
 * @author carcassi
 */
public class QueueCollector<T> implements Collector<T, List<T>> {
    
    private final Object lock = new Object();
    private List<T> readBuffer;
    private List<T> writeBuffer;
    private int maxSize;

    /**
     * New queue collector with the given max size for the queue.
     * 
     * @param maxSize maximum number of elements in the queue
     */
    public QueueCollector(int maxSize) {
        synchronized(lock) {
            this.maxSize = maxSize;
            readBuffer = new ArrayList<>();
            writeBuffer = new ArrayList<>();
        }
    }

    @Override
    public void writeValue(T newValue) {
        synchronized(lock) {
            writeBuffer.add(newValue);
            if (writeBuffer.size() > maxSize) {
                writeBuffer.remove(0);
            }
        }
    }

    @Override
    public List<T> readValue() {
        synchronized(lock) {
            List<T> data = writeBuffer;
            writeBuffer = readBuffer;
            writeBuffer.clear();
            readBuffer = data;
        }
        return readBuffer;
    }

    /**
     * Changes the number of maximum values in the queue.
     * <p>
     * If new maxSize is less than the current number of element in the queue,
     * the old values are discarded.
     * 
     * @param maxSize the maximum number of elements in the queue
     */
    public void setMaxSize(int maxSize) {
        synchronized(lock) {
            this.maxSize = maxSize;
            while (writeBuffer.size() > maxSize) {
                writeBuffer.remove(0);
            }
        }
    }
{'fraction_non_alphanumeric': 0.08987403100775193, 'fraction_numerical': 0.003875968992248062, 'mean_word_length': 2.0813432835820898, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 30, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6439024', 'n_tokens_mistral': 1144, 'n_tokens_neox': 1121, 'n_words': 393}