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

com.alibaba.hologres.client.impl.binlog.ArrayBuffer Maven / Gradle / Ivy

There is a newer version: 2.5.5
Show newest version
/*
 * Copyright (c) 2022. Alibaba Group Holding Limited
 */

package com.alibaba.hologres.client.impl.binlog;

import java.lang.reflect.Array;
import java.util.Arrays;

/**
 * Array辅助类.
 *
 * @param 
 */
public class ArrayBuffer {
	private T[] buffer;
	private int position;
	private int size;
	private boolean readable = false;
	private final Class arrayClass;

	public ArrayBuffer(int initSize, Class arrayClass) {
		this.arrayClass = arrayClass;
		this.buffer = (T[]) Array.newInstance(arrayClass.getComponentType(), initSize);
		this.position = 0;
		this.size = initSize;
	}

	public void setPosition(int position) {
		this.position = position;
	}

	public int getPosition() {
		return position;
	}

	public int getSize() {
		return size;
	}

	public int remain() {
		return size - position;
	}

	public T last() {
		if (remain() > 0) {
			return buffer[size - 1];
		}
		return null;
	}

	public T pop() {
		if (remain() > 0) {
			T ret = buffer[position];
			buffer[position] = null;
			position++;
			return ret;
		}
		return null;
	}

	public T peek() {
		if (remain() > 0) {
			return buffer[position];
		}
		return null;
	}

	public void add(T r) {
		if (remain() == 0) {
			buffer = Arrays.copyOf(buffer, Math.max(buffer.length + 1, buffer.length * 3 / 2), arrayClass);
			size = buffer.length;
		}
		buffer[position++] = r;
	}

	public void beginRead() {
		size = position;
		position = 0;
		readable = true;
	}

	public void beginWrite() {
		position = 0;
		size = buffer.length;
		readable = false;
	}

	public boolean isReadable() {
		return readable;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy