TextBuffer.java

jmud-0.11/src/TextBuffer.java · 1.5 KB

From the archive jmud-0_11.tgz

Download the archive   « Back to the archive contents

import java.util.StringTokenizer;
import java.util.Enumeration;

import util.list.Enumerable;
import util.list.Vector;
import misc.Separators;

class TextBuffer implements Enumerable {

    private Vector buf  = new Vector();

    void insertAtBegin(String str) {
	if (buf.isEmpty())
	    buf.insert(str);
	else
	    buf.insertAt(0, str);
    }

    void insertAtEnd(String str) {
	buf.insert(str);
    }

    void insertAt(int ind, String str) throws ArrayIndexOutOfBoundsException {
	buf.insertAt(ind, str);
    }

    // null if empty
    String removeFromBegin() {
	return buf.isEmpty() ? null : (String) buf.removeFrom(0);
    }

    // null if empty
    String removeFromEnd() {
	return buf.isEmpty() ? null : (String) buf.remove();
    }

    String removeFrom(int ind) throws ArrayIndexOutOfBoundsException {
	return (String) buf.removeFrom(ind);
    }

    void clear() {
	buf.removeAll();
    }

    public Enumeration elements() {
	return buf.elements();
    }

    int getSize() {
	return buf.getSize();
    }

    public String toString() {
	String txt = "";
	for (Enumeration enum = buf.elements(); enum.hasMoreElements(); ) {
	    String str = (String) enum.nextElement();
	    txt += ((txt.length() == 0) ? str : (Separators.NL + str));
	}
	return txt;
    }

    void buildFrom(String txt) {
	StringTokenizer toker = new StringTokenizer(txt, Separators.NL);
	while (toker.hasMoreElements())
	    {
		String line = (String) toker.nextToken();
		insertAtEnd(line);
	    }
    }
}

Site Stats

Snippets: 1791
Codebases: 571