001package org.unix4j.processor; 002 003import org.unix4j.command.Arguments; 004import org.unix4j.command.Command; 005import org.unix4j.context.ExecutionContext; 006 007/** 008 * Abstract base implementation for {@link LineProcessor} returned by the 009 * execute method of commands constructed with a reference to the command plus 010 * context and output passed to 011 * {@link Command#execute(ExecutionContext, LineProcessor)}. 012 */ 013abstract public class AbstractLineProcessor<A extends Arguments<A>> implements LineProcessor { 014 015 private final Command<A> command; 016 private final ExecutionContext context; 017 private final LineProcessor output; 018 private A arguments;//lazy init 019 020 /** 021 * Constructor with command creating this processor, execution context and 022 * output to write to. 023 * 024 * @param command 025 * the command whose execute method usually returns this line 026 * processor 027 * @param context 028 * the execution context passed to the command's execute method 029 * @param output 030 * the output object to write to when executing the command 031 * through this processor 032 */ 033 public AbstractLineProcessor(Command<A> command, ExecutionContext context, LineProcessor output) { 034 this.command = command; 035 this.context = context; 036 this.output = output; 037 } 038 039 /** 040 * Returns the command that was passed to the constructor of this line 041 * processor, the command whose execute method usually returns this line 042 * processor. 043 * 044 * @return the command whose execute method usually returns this line 045 * processor 046 */ 047 protected Command<A> getCommand() { 048 return command; 049 } 050 051 /** 052 * Returns the command arguments for the current variable context. This 053 * method returns and caches the result of <br> 054 * {@code getCommand().getArguments(getContext())}. 055 * 056 * @return the command arguments for the current variable context 057 */ 058 protected A getArguments() { 059 if (arguments == null) { 060 arguments = getCommand().getArguments(getContext()); 061 } 062 return arguments; 063 } 064 065 /** 066 * Returns the execution context that was passed to the constructor 067 * 068 * @return the execution context 069 */ 070 protected ExecutionContext getContext() { 071 return context; 072 } 073 074 /** 075 * Returns the output that was passed to the constructor of this line 076 * processor, the object to write to when executing the command through the 077 * this processor 078 * 079 * @return the output to write to when using this processor 080 */ 081 protected LineProcessor getOutput() { 082 return output; 083 } 084}