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

com.github.akurilov.commons.io.collection.CircularArrayOutput Maven / Gradle / Ivy

There is a newer version: 2.3.6
Show newest version
package com.github.akurilov.commons.io.collection;

import java.io.IOException;
import java.util.List;

/**
 An array output which may use fixed space for unlimited (circular) storing.
 */
public class CircularArrayOutput
extends ArrayOutput {

	public CircularArrayOutput(final T[] items) {
		super(items);
	}

	/**
	 @param item the item to put
	 @throws IOException if the destination collection fails to add the item
	 */
	@Override
	public boolean put(final T item)
	throws IOException {
		if(i >= items.length) {
			i = 0;
		}
		return super.put(item);
	}

	/**
	 Bulk circular put method
	 @param buffer the list of the items to put in a batch mode
	 @throws IOException if the destination collection fails to add the items
	 @return the size of the buffer to put
	 */
	@Override
	public int put(final List buffer, final int from, final int to)
	throws IOException {
		//
		int n = to - from;
		if(buffer.size() > n) {
			return put(buffer.subList(from, to), 0, n);
		}
		//
		n = buffer.size();
		if(n < items.length) {
			// buffer may be placed entirely into the capacitor
			final int limit = items.length - i; // how many free space is in the capacitor;
			if(n > limit) {
				// should remove some items from the beginning of the capacitor in order to place
				// the buffer entirely
				i = n - limit;
			}
			for(int j = from; j < to; j ++) {
				items[i ++] = buffer.get(j);
			}
		} else {
			// only a tail part of the buffer may be placed into the capacitor
			i = 0; // discard all the items in the capacitor
			for(int j = n - items.length; j < n; j ++) {
				items[i ++] = buffer.get(j);
			}
		}
		return n;
	}

	/**
	 @return the corresponding input
	 @throws IOException doesn't throw
	 */
	@Override
	public CircularArrayInput getInput()
	throws IOException {
		return new CircularArrayInput<>(items);
	}

	@Override
	public final String toString() {
		return "circularArrayOutput<" + items.hashCode() + ">";
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy