001package org.unix4j.unix.cut; 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.Cut; 013import org.unix4j.unix.cut.CutOption; 014 015/** 016 * Interface implemented by all option sets for the {@link Cut cut} command. 017 * It is recommended to use {@link Cut#Options} to specify a valid 018 * combination of options. 019 * <p> 020 * The options for the cut command are: 021 * <p> 022 * <table> 023 * <tr valign="top"><td width="10px"></td><td nowrap="nowrap">{@code -c}</td><td> </td><td nowrap="nowrap">{@code --chars}</td><td> </td><td>The list specifies character positions.</td></tr> 024 * <tr valign="top"><td width="10px"></td><td nowrap="nowrap">{@code -f}</td><td> </td><td nowrap="nowrap">{@code --fields}</td><td> </td><td>The list specifies fields, separated in the input by the field 025 delimiter character (see the -d option.) Output fields are 026 separated by a single occurrence of the field delimiter character.</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 CutOptions extends OptionSet<CutOption> { 033 /** 034 * Constant for an empty option set. 035 */ 036 CutOptions EMPTY = new CutOptions() { 037 @Override 038 public Class<CutOption> optionType() { 039 return CutOption.class; 040 } 041 @Override 042 public boolean isSet(CutOption 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<CutOption> 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<CutOption> 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(CutOption option) { 086 return true; 087 } 088 }; 089 /** 090 * Default implementation for a modifiable option set. 091 */ 092 class Default extends DefaultOptionSet<CutOption> implements CutOptions { 093 /** 094 * Default constructor for an empty option set with no active options. 095 */ 096 public Default() { 097 super(CutOption.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(CutOption 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(CutOption... 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<CutOption> optionSet) { 121 this(); 122 setAll(optionSet); 123 } 124 } 125 126 /** 127 * Value converter for {@link CutOptions} based on an {@link OptionSetConverter}. 128 */ 129 ValueConverter<CutOptions> CONVERTER = new ValueConverter<CutOptions>() { 130 private final OptionSetConverter<CutOption> converter = new OptionSetConverter<CutOption>(CutOption.class); 131 @Override 132 public CutOptions convert(Object value) { 133 final OptionSet<CutOption> set = converter.convert(value); 134 return set == null ? null : new Default(set); 135 } 136 }; 137}