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