commands.AbstractCommand.java Source code

Java tutorial

Introduction

Here is the source code for commands.AbstractCommand.java

Source

/**
 * This file is a component of jiminix
 *
 * Copyright (c) 2013 Jason Campos <jcampos8782@gmail.com>
 * All Rights Reserved.
 *
 * This software is licensed under The MIT License (MIT)
 * http://opensource.org/licenses/MIT
 */
package commands;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.commons.cli.Options;

import com.google.inject.Inject;

import exceptions.CommandException;
import exceptions.UsageException;

/**
 * An abstract Command implementation which provides base implementations for several 
 * of the Command interface methods. 
 * 
 * @author Jason Campos <jcampos8782@gmail.com>
 */
public abstract class AbstractCommand implements Command {

    protected PrintStream out = System.out;
    protected File outputFile;
    protected boolean append;
    protected List<String> args;

    @Inject
    public AbstractCommand() {
        this.args = new ArrayList<>();
    }

    @Override
    public void usage() throws UsageException {
        throw new UsageException("Usage unspecified.");
    }

    @Override
    public Options getOptions() {
        return null;
    }

    @Override
    public void addArgument(String arg) {
        this.args.add(arg);
    }

    @Override
    public void addArguments(Collection<String> args) {
        this.args.addAll(args);
    }

    @Override
    public void outputToFile(File outputFile, boolean append) {
        try {
            this.out = new PrintStream(new FileOutputStream(outputFile, append));
            this.outputFile = outputFile;
            this.append = append;
        } catch (FileNotFoundException e) {
            throw new CommandException("Could not find output file");
        }
    }
}