001package org.unix4j.unix.uniq;
002
003import java.io.File;
004import java.util.Collections;
005import java.util.List;
006
007import org.unix4j.command.AbstractCommand;
008import org.unix4j.context.ExecutionContext;
009import org.unix4j.io.FileInput;
010import org.unix4j.processor.LineProcessor;
011import org.unix4j.processor.RedirectInputLineProcessor;
012import org.unix4j.unix.Ls;
013import org.unix4j.unix.Uniq;
014import org.unix4j.util.FileUtil;
015
016/**
017 * Implementation of the {@link Uniq uniq} command.
018 */
019class UniqCommand extends AbstractCommand<UniqArguments> {
020        public UniqCommand(UniqArguments arguments) {
021                super(Ls.NAME, arguments);
022        }
023
024        @Override
025        public LineProcessor execute(final ExecutionContext context, final LineProcessor output) {
026                final UniqArguments args = getArguments(context);
027                //input from file?
028                if (args.isFileSet()) {
029                        final FileInput input = new FileInput(args.getFile());
030                        return getFileInputProcessor(Collections.singletonList(input), context, output, args);
031                } else if (args.isPathSet()) {
032                        final List<File> files = FileUtil.expandFiles(context.getCurrentDirectory(), args.getPath());
033                        final List<FileInput> inputs = FileInput.multiple(files);
034                        return getFileInputProcessor(inputs, context, output, args);
035                }
036                
037                //no, from standard input
038                return getStandardInputProcessor(context, output, args);
039        }
040        
041        private LineProcessor getStandardInputProcessor(ExecutionContext context, LineProcessor output, UniqArguments args) {
042                if (args.isGlobal()) {
043                        if (args.isUniqueOnly()) {
044                                return new GlobalProcessor.UniqueOnly(this, context, output);
045                        } else if (args.isDuplicatedOnly()) {
046                                return new GlobalProcessor.DuplicateOnly(this, context, output);
047                        } else if (args.isCount()) {
048                                return new GlobalProcessor.Count(this, context, output);
049                        }
050                        return new GlobalProcessor.Normal(this, context, output);
051                } else {
052                        if (args.isUniqueOnly()) {
053                                return new AdjacentProcessor.UniqueOnly(this, context, output);
054                        } else if (args.isDuplicatedOnly()) {
055                                return new AdjacentProcessor.DuplicateOnly(this, context, output);
056                        } else if (args.isCount()) {
057                                return new AdjacentProcessor.Count(this, context, output);
058                        }
059                        return new AdjacentProcessor.Normal(this, context, output);
060                }
061        }
062        private LineProcessor getFileInputProcessor(List<FileInput> inputs, ExecutionContext context, LineProcessor output, UniqArguments args) {
063                final LineProcessor standardInputProcessor = getStandardInputProcessor(context, output, args); 
064                return new RedirectInputLineProcessor(inputs, standardInputProcessor);
065        }
066
067}