001package org.unix4j.unix.wc;
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.Wc;
013import org.unix4j.unix.wc.WcOption;
014
015/**
016 * Interface implemented by all option sets for the {@link Wc wc} command.
017 * It is recommended to use {@link Wc#Options} to specify a valid 
018 * combination of options.
019 * <p>
020 * The options for the wc command are: 
021 * <p>
022 * <table>
023 * <tr valign="top"><td width="10px"></td><td nowrap="nowrap">{@code -l}</td><td>&nbsp;&nbsp;</td><td nowrap="nowrap">{@code --lines}</td><td>&nbsp;</td><td>Executes a count of lines and writes this count to the output.</td></tr>
024 * <tr valign="top"><td width="10px"></td><td nowrap="nowrap">{@code -w}</td><td>&nbsp;&nbsp;</td><td nowrap="nowrap">{@code --words}</td><td>&nbsp;</td><td>Executes a count of words and writes this count to the output. A
025                        word is a non-zero-length string of characters delimited by white
026                        space as defined by {@link Character#isWhitespace(char)}.</td></tr>
027 * <tr valign="top"><td width="10px"></td><td nowrap="nowrap">{@code -m}</td><td>&nbsp;&nbsp;</td><td nowrap="nowrap">{@code --chars}</td><td>&nbsp;</td><td>Executes a count of chars and writes this count to the output.</td></tr>
028 * </table>
029 * <p>
030 * This interface serves as an alias for the extended interface to simplify the
031 * command signature methods by avoiding generic parameters.
032 */
033public interface WcOptions extends OptionSet<WcOption> {
034        /**
035         * Constant for an empty option set.
036         */
037        WcOptions EMPTY = new WcOptions() {
038                @Override
039                public Class<WcOption> optionType() {
040                        return WcOption.class;
041                }
042                @Override
043                public boolean isSet(WcOption option) {
044                        return false;
045                }
046                /**
047                 * Returns 0 as this is a set with no active options.
048                 * 
049                 * @return zero
050                 */
051                @Override
052                public int size() {
053                        return 0;
054                }
055                /**
056                 * Returns an immutable empty set.
057                 * 
058                 * @return an immutable empty set.
059                 */
060                @Override
061                public java.util.Set<WcOption> asSet() {
062                        return Collections.emptySet();
063                }
064                
065                /**
066                 * Returns an iterator returning no elements. 
067                 * 
068                 * @return an immutable iterator with no elements.
069                 */
070                @Override
071                public Iterator<WcOption> iterator() {
072                        return asSet().iterator();
073                }
074                
075                /**
076                 * Returns true if the {@link Option#acronym() acronym} should be used
077                 * for the specified {@code option} in string representations. 
078                 * <p>
079                 * This method returns always true;
080                 *  
081                 * @param option
082                 *            the option of interest
083                 * @return always true
084                 */
085                @Override
086                public boolean useAcronymFor(WcOption option) {
087                        return true;
088                }
089        };
090        /**
091         * Default implementation for a modifiable option set.
092         */
093        class Default extends DefaultOptionSet<WcOption> implements WcOptions {
094                /**
095                 * Default constructor for an empty option set with no active options.
096                 */
097                public Default() {
098                        super(WcOption.class);
099                }
100                /**
101                 * Constructor for an option set with a single active option.
102                 * @param option the option to be set
103                 */
104                public Default(WcOption option) {
105                        super(option);
106                }
107                /**
108                 * Constructor for an option set with the given active options.
109                 * @param options the options to be set
110                 */
111                public Default(WcOption... options) {
112                        this();
113                        setAll(options);
114                }
115                /**
116                 * Constructor for an option set initialized with the options given by
117                 * another option set.
118                 * 
119                 * @param optionSet set with the options to be active
120                 */
121                public Default(OptionSet<WcOption> optionSet) {
122                        this();
123                        setAll(optionSet);
124                }
125        }
126        
127        /**
128         * Value converter for {@link WcOptions} based on an {@link OptionSetConverter}. 
129         */
130        ValueConverter<WcOptions> CONVERTER = new ValueConverter<WcOptions>() {
131                private final OptionSetConverter<WcOption> converter = new OptionSetConverter<WcOption>(WcOption.class);
132                @Override
133                public WcOptions convert(Object value) {
134                        final OptionSet<WcOption> set = converter.convert(value);
135                        return set == null ? null : new Default(set);
136                }
137        };
138}