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

org.unix4j.processor.MultipleInputLineProcessor Maven / Gradle / Ivy

There is a newer version: 0.6
Show newest version
package org.unix4j.processor;

import java.util.List;

import org.unix4j.command.ExitValueException;
import org.unix4j.io.Input;
import org.unix4j.line.Line;

/**
 * A line processor for multiple inputs processing the same operation for each
 * input object individually. An operation here is another {@link LineProcessor}
 * reading the lines passed to it from the standard input.
 * 

* The {@link #processLine(Line)} method does nothing and returns false * indicating that the (standard) input is not read by this processor. The * {@link #finish()} method reads the lines from the {@link Input} object passed * to the constructor and passes them as input to the delegate processor * performing the real work. */ public class MultipleInputLineProcessor implements LineProcessor { private final List inputs; private final InputProcessor processor; private final LineProcessor output; /** * Constructor with input objects (usually file operands of the command) and * the input processor of the command that reads from the standard input. * * @param inputs * the input devices, usually file operands of the command * @param processor * the operation applied to every input in the given * {@code inputs} list */ public MultipleInputLineProcessor(List inputs, InputProcessor processor, LineProcessor output) { this.inputs = inputs; this.processor = processor; this.output = output; } @Override public boolean processLine(Line line) { return false;// we want no input, we have it already } /** * Performs the following operations to process all {@code Input} objects * that have been passed to the constructor: *

    *
  1. Calls {@link #beginMultiple(List, LineProcessor) beginMultiple(..)}
  2. *
  3. Iterates over all input objects in sequence
  4. *
  5. Calls {@link InputProcessor#begin(Input, LineProcessor)}
  6. *
  7. Calls {@link InputProcessor#processLine(Input, Line, LineProcessor)} * for every line in the current input
  8. *
  9. Calls {@link InputProcessor#finish(Input, LineProcessor)}
  10. *
  11. Calls {@link #finishMultiple(List, LineProcessor) finishMultiple(..)} *
  12. *
*/ @Override public void finish() { beginMultiple(inputs, output); for (int i = 0; i < inputs.size(); i++) { final Input input = inputs.get(i); try { processor.begin(input, output); for (final Line line : input) { if (!processor.processLine(input, line, output)) { break;// wants no more lines } } processor.finish(input, output); } catch (ExitValueException e) { e.setInput(input); throw e; } } finishMultiple(inputs, output); } /** * Called once at the beginning before iterating over the {@link Input} * objects in the given {@code inputs} list. *

* The DEFAULT implementation performs no operation. * * @param inputs * the input object being iterated next * @param output * the output to write to */ protected void beginMultiple(List inputs, LineProcessor output) { // default: no op } /** * Called once at the end after iterating over the {@link Input} objects in * the given {@code inputs} list. *

* The DEFAULT implementation calls {@link LineProcessor#finish() * output.finish()}. * * @param inputs * the input object being iterated next * @param output * the output to write to */ protected void finishMultiple(List inputs, LineProcessor output) { output.finish(); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy