001package org.unix4j.command; 002 003import org.unix4j.context.ExecutionContext; 004 005 006/** 007 * Abstract base class suitable for most command implementations. Name and 008 * arguments are passed to the constructor. 009 * 010 * @param <A> 011 * the type parameter defining the arguments and options of the 012 * command 013 */ 014abstract public class AbstractCommand<A extends Arguments<A>> implements Command<A> { 015 016 private final String name; 017 private final A arguments; 018 019 /** 020 * Constructor with command name, type and arguments. 021 * 022 * @param name 023 * the name of the command, usually a lower case string such as 024 * "ls" or "grep" 025 * @param arguments 026 * the command specific arguments for the new command instance 027 */ 028 public AbstractCommand(String name, A arguments) { 029 this.name = name; 030 this.arguments = arguments; 031 } 032 033 @Override 034 public String getName() { 035 return name; 036 } 037 038 @Override 039 public A getArguments(ExecutionContext context) { 040 return context == null ? arguments : arguments.getForContext(context); 041 } 042 043 @Override 044 public Command<?> join(Command<?> next) { 045 return JoinedCommand.join(this, next); 046 } 047 048 @Override 049 public String toString() { 050 final String args = getArguments(null).toString(); 051 return args.isEmpty() ? getName() : getName() + " " + args; 052 } 053}