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