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

net.razorvine.pickle.UnpickleStack Maven / Gradle / Ivy

Go to download

This library allows your Java program to interface very easily with the Python world. It uses the Pyro protocol to call methods on remote objects. (See https://pyro5.readthedocs.io/). Pyrolite only implements part of the client side Pyro library, hence its name 'lite'... So if you don't need Pyro's full feature set, Pyrolite may be a good choice to connect java or .NET and python. Version 5.0 changes: support Pyro5 wire protocol. Dropped support of Pyro4 (stick to version 4.xx for that).

There is a newer version: 5.1
Show newest version
package net.razorvine.pickle;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;

/**
 * Helper type that represents the unpickler working stack.
 * 
 * @author Irmen de Jong ([email protected])
 */
public class UnpickleStack implements Serializable {
	private static final long serialVersionUID = 6148106506441423350L;
	private ArrayList stack;
	protected Object MARKER;

	public UnpickleStack() {
		stack = new ArrayList();
		MARKER = new Object(); // any new unique object
	}

	public void add(Object o) {
		this.stack.add(o);
	}

	public void add_mark() {
		this.stack.add(this.MARKER);
	}

	public Object pop() {
		int size = this.stack.size();
		Object result = this.stack.get(size - 1);
		this.stack.remove(size - 1);
		return result;
	}

	public ArrayList pop_all_since_marker() {
		ArrayList result = new ArrayList();
		Object o = pop();
		while (o != this.MARKER) {
			result.add(o);
			o = pop();
		}
		result.trimToSize();
		Collections.reverse(result);
		return result;
	}

	public Object peek() {
		return this.stack.get(this.stack.size() - 1);
	}

	public void trim() {
		this.stack.trimToSize();
	}

	public int size() {
		return this.stack.size();
	}

	public void clear() {
		this.stack.clear();
		this.stack.trimToSize();
	}
}