001package org.unix4j.unix.uniq;
002
003import java.util.Collections;
004import java.util.Iterator;
005
006import org.unix4j.convert.OptionSetConverters.OptionSetConverter;
007import org.unix4j.convert.ValueConverter;
008import org.unix4j.option.DefaultOptionSet;
009import org.unix4j.option.Option;
010import org.unix4j.option.OptionSet;
011
012import org.unix4j.unix.Uniq;
013import org.unix4j.unix.uniq.UniqOption;
014
015/**
016 * Interface implemented by all option sets for the {@link Uniq uniq} command.
017 * It is recommended to use {@link Uniq#Options} to specify a valid 
018 * combination of options.
019 * <p>
020 * The options for the uniq command are: 
021 * <p>
022 * <table>
023 * <tr valign="top"><td width="10px"></td><td nowrap="nowrap">{@code -c}</td><td>&nbsp;&nbsp;</td><td nowrap="nowrap">{@code --count}</td><td>&nbsp;</td><td>Precedes each output line with a count of the number of times the
024                        line occurred in the input.</td></tr>
025 * <tr valign="top"><td width="10px"></td><td nowrap="nowrap">{@code -d}</td><td>&nbsp;&nbsp;</td><td nowrap="nowrap">{@code --duplicatedOnly}</td><td>&nbsp;</td><td>Suppresses the writing of lines that are not repeated in the input.</td></tr>
026 * <tr valign="top"><td width="10px"></td><td nowrap="nowrap">{@code -u}</td><td>&nbsp;&nbsp;</td><td nowrap="nowrap">{@code --uniqueOnly}</td><td>&nbsp;</td><td>Suppresses the writing of lines that are repeated in the input.</td></tr>
027 * <tr valign="top"><td width="10px"></td><td nowrap="nowrap">{@code -g}</td><td>&nbsp;&nbsp;</td><td nowrap="nowrap">{@code --global}</td><td>&nbsp;</td><td>Suppresses repeated lines globally, that is, if lines are 
028                        non-adjacent. This option guarantees unique output lines even if the
029                        input lines are not sorted.</td></tr>
030 * </table>
031 * <p>
032 * This interface serves as an alias for the extended interface to simplify the
033 * command signature methods by avoiding generic parameters.
034 */
035public interface UniqOptions extends OptionSet<UniqOption> {
036        /**
037         * Constant for an empty option set.
038         */
039        UniqOptions EMPTY = new UniqOptions() {
040                @Override
041                public Class<UniqOption> optionType() {
042                        return UniqOption.class;
043                }
044                @Override
045                public boolean isSet(UniqOption option) {
046                        return false;
047                }
048                /**
049                 * Returns 0 as this is a set with no active options.
050                 * 
051                 * @return zero
052                 */
053                @Override
054                public int size() {
055                        return 0;
056                }
057                /**
058                 * Returns an immutable empty set.
059                 * 
060                 * @return an immutable empty set.
061                 */
062                @Override
063                public java.util.Set<UniqOption> asSet() {
064                        return Collections.emptySet();
065                }
066                
067                /**
068                 * Returns an iterator returning no elements. 
069                 * 
070                 * @return an immutable iterator with no elements.
071                 */
072                @Override
073                public Iterator<UniqOption> iterator() {
074                        return asSet().iterator();
075                }
076                
077                /**
078                 * Returns true if the {@link Option#acronym() acronym} should be used
079                 * for the specified {@code option} in string representations. 
080                 * <p>
081                 * This method returns always true;
082                 *  
083                 * @param option
084                 *            the option of interest
085                 * @return always true
086                 */
087                @Override
088                public boolean useAcronymFor(UniqOption option) {
089                        return true;
090                }
091        };
092        /**
093         * Default implementation for a modifiable option set.
094         */
095        class Default extends DefaultOptionSet<UniqOption> implements UniqOptions {
096                /**
097                 * Default constructor for an empty option set with no active options.
098                 */
099                public Default() {
100                        super(UniqOption.class);
101                }
102                /**
103                 * Constructor for an option set with a single active option.
104                 * @param option the option to be set
105                 */
106                public Default(UniqOption option) {
107                        super(option);
108                }
109                /**
110                 * Constructor for an option set with the given active options.
111                 * @param options the options to be set
112                 */
113                public Default(UniqOption... options) {
114                        this();
115                        setAll(options);
116                }
117                /**
118                 * Constructor for an option set initialized with the options given by
119                 * another option set.
120                 * 
121                 * @param optionSet set with the options to be active
122                 */
123                public Default(OptionSet<UniqOption> optionSet) {
124                        this();
125                        setAll(optionSet);
126                }
127        }
128        
129        /**
130         * Value converter for {@link UniqOptions} based on an {@link OptionSetConverter}. 
131         */
132        ValueConverter<UniqOptions> CONVERTER = new ValueConverter<UniqOptions>() {
133                private final OptionSetConverter<UniqOption> converter = new OptionSetConverter<UniqOption>(UniqOption.class);
134                @Override
135                public UniqOptions convert(Object value) {
136                        final OptionSet<UniqOption> set = converter.convert(value);
137                        return set == null ? null : new Default(set);
138                }
139        };
140}