| career | drupal | java | mac | mysql | perl | scala | uml | unix  

Groovy example source code file (CommandRegistry.groovy)

This example Groovy source code file (CommandRegistry.groovy) is included in the "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Groovy tags/keywords

command, command, commandregistry, duplicate, duplicate, iterator, list, list, logger, registered, set, string, string, treeset

The Groovy CommandRegistry.groovy source code

 * Copyright 2003-2007 the original author or authors.
 * 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
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * See the License for the specific language governing permissions and
 * limitations under the License.



 * A registry of shell {@link Command} instances which may be executed.
 * @version $Id: CommandRegistry.groovy 7581 2007-08-23 01:47:47Z user57 $
 * @author <a href="">Jason Dillon
class CommandRegistry
    protected final Logger log = Logger.create(CommandRegistry.class)
    // TODO: Hook up support so one can for (command in registry) { }
    /** A list of all of the registered commands. */
    final List commands = []

    /** A set of all of the command names and shortcuts to ensure they are unique. */
    private final Set names = new TreeSet()
    Command register(final Command command) {
        assert command

        // Make sure that the command name and shortcut are unique
        assert !names.contains( : "Duplicate comamnd name: $"
        names <<
        assert !names.contains(command.shortcut) : "Duplicate command shortcut: $command.shortcut"
        names << command.shortcut

        // Hold on to the command in order
        commands << command
        // Hookup context for alias commands
        command.registry = this

        // Add any standard aliases for the command if any
        command.aliases?.each { this << it }
        if (log.debugEnabled) {
            log.debug("Registered command: $")
        return command

    def leftShift(final Command command) {
        return register(command)
    Command find(final String name) {
        assert name
        for (c in commands) {
            if (name in [, c.shortcut ]) {
                return c
        return null
    void remove(final Command command) {
        assert command
        if (log.debugEnabled) {
            log.debug("Removed command: $")
    List commands() {
        return commands
    def getProperty(final String name) {
        return find(name)
    Iterator iterator() {
        return commands().iterator()

Other Groovy examples (source code examples)

Here is a short list of links related to this Groovy CommandRegistry.groovy source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller


new blog posts


Copyright 1998-2021 Alvin Alexander,
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.