001package org.unix4j.unix.head; 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.Head; 013import org.unix4j.unix.head.HeadOption; 014 015/** 016 * Interface implemented by all option sets for the {@link Head head} command. 017 * It is recommended to use {@link Head#Options} to specify a valid 018 * combination of options. 019 * <p> 020 * The options for the head 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 {@code count} argument is in units of characters instead of 024 lines. Starts from 1 and includes line ending characters.</td></tr> 025 * <tr valign="top"><td width="10px"></td><td nowrap="nowrap">{@code -q}</td><td> </td><td nowrap="nowrap">{@code --suppressHeaders}</td><td> </td><td>Suppresses printing of headers when multiple files are being 026 examined.</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 HeadOptions extends OptionSet<HeadOption> { 033 /** 034 * Constant for an empty option set. 035 */ 036 HeadOptions EMPTY = new HeadOptions() { 037 @Override 038 public Class<HeadOption> optionType() { 039 return HeadOption.class; 040 } 041 @Override 042 public boolean isSet(HeadOption 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<HeadOption> 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<HeadOption> 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(HeadOption option) { 086 return true; 087 } 088 }; 089 /** 090 * Default implementation for a modifiable option set. 091 */ 092 class Default extends DefaultOptionSet<HeadOption> implements HeadOptions { 093 /** 094 * Default constructor for an empty option set with no active options. 095 */ 096 public Default() { 097 super(HeadOption.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(HeadOption 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(HeadOption... 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<HeadOption> optionSet) { 121 this(); 122 setAll(optionSet); 123 } 124 } 125 126 /** 127 * Value converter for {@link HeadOptions} based on an {@link OptionSetConverter}. 128 */ 129 ValueConverter<HeadOptions> CONVERTER = new ValueConverter<HeadOptions>() { 130 private final OptionSetConverter<HeadOption> converter = new OptionSetConverter<HeadOption>(HeadOption.class); 131 @Override 132 public HeadOptions convert(Object value) { 133 final OptionSet<HeadOption> set = converter.convert(value); 134 return set == null ? null : new Default(set); 135 } 136 }; 137}