view src/nabble/naml/namespaces/StringList.java @ 0:7ecd1a4ef557

add content
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 21 Mar 2019 19:15:52 -0600
parents
children
line wrap: on
line source

package nabble.naml.namespaces;

import nabble.naml.compiler.Command;
import nabble.naml.compiler.CommandSpec;
import nabble.naml.compiler.IPrintWriter;
import nabble.naml.compiler.Interpreter;
import nabble.naml.compiler.Namespace;

import java.util.Collections;
import java.util.List;


@Namespace (
	name = "string_list",
	global = true
)
public class StringList extends ListSequence<String> {

	public StringList(List<String> strings) {
		super(strings);
	}

	@Command public void current_string(IPrintWriter out,Interpreter interp) {
		current_element(out,interp);
	}

	@Command public void string_count(IPrintWriter out,Interpreter interp) {
		element_count(out,interp);
	}

	public static final CommandSpec add = CommandSpec.NO_OUTPUT()
		.dotParameter("s")
		.build()
	;

	@Command public void add(IPrintWriter out,Interpreter interp) {
		String s = interp.getArgString("s");
		if( s != null && !elements.contains(s))
			elements.add(s);
	}

	public static final CommandSpec remove = add;

	@Command public void remove(IPrintWriter out,Interpreter interp) {
		String s = interp.getArgString("s");
		if( s != null )
			elements.remove(s);
	}

	public static final CommandSpec sort = CommandSpec.NO_OUTPUT;

	@Command public void sort(IPrintWriter out,Interpreter interp) {
		Collections.sort(elements);
	}

	public static final CommandSpec contains = new CommandSpec.Builder()
		.dotParameter("s")
		.build()
	;

	@Command public void contains(IPrintWriter out,Interpreter interp) {
		String s = interp.getArgString("s");
		out.print( elements.contains(s) );
	}

	@Command public void next_string(IPrintWriter out,Interpreter interp) {
		next_element(out,interp);
	}

	public static final CommandSpec has_more_strings = has_more_elements;

	@Command public final void has_more_strings(IPrintWriter out,Interpreter interp) {
		has_more_elements(out,interp);
	}

	public static final CommandSpec at_index = new CommandSpec.Builder()
		.dotParameter("index")
		.build()
	;

	@Command public void at_index(IPrintWriter out,Interpreter interp) {
		int i = interp.getArgAsInt("index");
		out.print(elements.get(i));
	}
}