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

protoj.lang.CommandStore Maven / Gradle / Ivy

The newest version!
/**
 * Copyright 2009 Ashley Williams
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you
 * may not use this file except in compliance with the License. You may
 * obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */
package protoj.lang;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;

/**
 * A repository of the application commands. A command can only be created by
 * calling the {@link #addCommand(String, String, Runnable)}, which
 * automatically ensures that command is added to this store.
 * 
 * @author Ashley Williams
 * 
 */
public final class CommandStore {
	/**
	 * See {@link #getCommands()}.
	 */
	private ArrayList commands = new ArrayList();

	/**
	 * See {@link #CommandStore(CoreProject)}.
	 */
	private final StandardProject parent;

	/**
	 * Accepts the parent of this instance.
	 * 
	 * @param parent
	 */
	public CommandStore(final StandardProject parent) {
		this.parent = parent;
	}

	/**
	 * The parent core for this store.
	 * 
	 * @return
	 */
	public StandardProject getParent() {
		return parent;
	}

	/**
	 * Adds a new command with the specified command body and that can execute
	 * in a new vm. See
	 * {@link Command#Command(CommandStore, String, String, Runnable)} for a
	 * description of the parameters.
	 * 
	 * @param name
	 * @param maxMemory
	 * @param body
	 * @return
	 */
	public Command addCommand(String name, String maxMemory, Runnable body) {
		Command command = new Command(this, name, maxMemory, body);
		commands.add(command);
		return command;
	}

	/**
	 * Read-only access to the available commands.
	 * 
	 * @return
	 */
	public Collection getCommands() {
		return Collections.unmodifiableCollection(commands);
	}

	/**
	 * Looks up the command with the given name. If no match is found then null
	 * is returned.
	 * 
	 * @param name
	 * @return
	 */
	public Command getCommand(String name) {
		Command targetCommand = null;
		Collection values = commands;
		for (Command command : values) {
			if (command.containsAlias(name)) {
				targetCommand = command;
				break;
			}
		}
		return targetCommand;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy