001package org.unix4j.unix.cat; 002 003import java.util.Arrays; 004import java.util.Collections; 005import java.util.EnumSet; 006import java.util.Iterator; 007import org.unix4j.option.Option; 008 009import org.unix4j.unix.Cat; 010 011/** 012 * Option sets for the {@link Cat cat} command with 013 * the following options: {@link #n n}, {@link #b b}, {@link #s s}. 014 * <p> 015 * Application code does normally not directly refer to this class; 016 * {@link Cat#Options} should be used instead to specify command 017 * options. See also {@link org.unix4j.unix.cat.CatOptions} for more information. 018 */ 019public enum CatOptionSet_bns implements CatOptions { 020 /** Option set with the following active options: {@link #squeezeEmptyLines s}.*/ 021 Active_s( 022 /*n:*/CatOptionSet_ns.Active_ns, /*numberLines:*/CatOptionSet_ns.Active_ns_long, /*b:*/CatOptionSet_bs.Active_bs, /*numberNonBlankLines:*/CatOptionSet_bs.Active_bs_long, /*s:*/null /*already set*/, /*squeezeEmptyLines:*/null /*already set*/, 023 true, 024 /*active:*/CatOption.squeezeEmptyLines 025 ), 026 /** Option set with the following active options: {@link #squeezeEmptyLines s}.*/ 027 Active_s_long( 028 /*n:*/CatOptionSet_ns.Active_ns, /*numberLines:*/CatOptionSet_ns.Active_ns_long, /*b:*/CatOptionSet_bs.Active_bs, /*numberNonBlankLines:*/CatOptionSet_bs.Active_bs_long, /*s:*/null /*already set*/, /*squeezeEmptyLines:*/null /*already set*/, 029 false, 030 /*active:*/CatOption.squeezeEmptyLines 031 ); 032 private CatOptionSet_bns( 033 CatOptionSet_ns n, CatOptionSet_ns numberLines, CatOptionSet_bs b, CatOptionSet_bs numberNonBlankLines, CatOptionSet_bns s, CatOptionSet_bns squeezeEmptyLines, 034 boolean useAcronym, 035 CatOption... activeOptions 036 ) { 037 this.n = notNull(n); 038 this.numberLines = notNull(numberLines); 039 this.b = notNull(b); 040 this.numberNonBlankLines = notNull(numberNonBlankLines); 041 this.s = s == null ? this : s; 042 this.squeezeEmptyLines = squeezeEmptyLines == null ? this : squeezeEmptyLines; 043 this.useAcronym = useAcronym; 044 this.options = activeOptions.length == 0 ? EnumSet.noneOf(CatOption.class) : EnumSet.copyOf(Arrays.asList(activeOptions)); 045 } 046 private final boolean useAcronym; 047 /** 048 * Option {@code "-n"}: Number the output lines, starting at 1. 049 * <p> 050 * The option {@code "-n"} is equivalent to the {@code "--}{@link #numberLines numberLines}{@code "} option. 051 * <p> 052 * Technically speaking, this field points to a set with the options of the 053 * current set plus the option {@code "-n"}. If the option {@code "-n"} 054 * is already set, the field {@code n} points to the enum constant itself 055 * as it already represents the current set of options. 056 */ 057 public final CatOptionSet_ns n; 058 /** 059 * Option {@code "--numberLines"}: Number the output lines, starting at 1. 060 * <p> 061 * The option {@code "--numberLines"} is equivalent to the {@code "-}{@link #n n}{@code "} option. 062 * <p> 063 * Technically speaking, this field points to a set with the options of the 064 * current set plus the option {@code "--numberLines"}. If the option {@code "--numberLines"} 065 * is already set, the field {@code numberLines} points to the enum constant itself 066 * as it already represents the current set of options. 067 */ 068 public final CatOptionSet_ns numberLines; 069 /** 070 * Option {@code "-b"}: Number the non-blank output lines, starting at 1. 071 * <p> 072 * The option {@code "-b"} is equivalent to the {@code "--}{@link #numberNonBlankLines numberNonBlankLines}{@code "} option. 073 * <p> 074 * Technically speaking, this field points to a set with the options of the 075 * current set plus the option {@code "-b"}. If the option {@code "-b"} 076 * is already set, the field {@code b} points to the enum constant itself 077 * as it already represents the current set of options. 078 */ 079 public final CatOptionSet_bs b; 080 /** 081 * Option {@code "--numberNonBlankLines"}: Number the non-blank output lines, starting at 1. 082 * <p> 083 * The option {@code "--numberNonBlankLines"} is equivalent to the {@code "-}{@link #b b}{@code "} option. 084 * <p> 085 * Technically speaking, this field points to a set with the options of the 086 * current set plus the option {@code "--numberNonBlankLines"}. If the option {@code "--numberNonBlankLines"} 087 * is already set, the field {@code numberNonBlankLines} points to the enum constant itself 088 * as it already represents the current set of options. 089 */ 090 public final CatOptionSet_bs numberNonBlankLines; 091 /** 092 * Option {@code "-s"}: Squeeze multiple adjacent empty lines, causing the output to be 093 single spaced. 094 * <p> 095 * The option {@code "-s"} is equivalent to the {@code "--}{@link #squeezeEmptyLines squeezeEmptyLines}{@code "} option. 096 * <p> 097 * Technically speaking, this field points to a set with the options of the 098 * current set plus the option {@code "-s"}. If the option {@code "-s"} 099 * is already set, the field {@code s} points to the enum constant itself 100 * as it already represents the current set of options. 101 */ 102 public final CatOptionSet_bns s; 103 /** 104 * Option {@code "--squeezeEmptyLines"}: Squeeze multiple adjacent empty lines, causing the output to be 105 single spaced. 106 * <p> 107 * The option {@code "--squeezeEmptyLines"} is equivalent to the {@code "-}{@link #s s}{@code "} option. 108 * <p> 109 * Technically speaking, this field points to a set with the options of the 110 * current set plus the option {@code "--squeezeEmptyLines"}. If the option {@code "--squeezeEmptyLines"} 111 * is already set, the field {@code squeezeEmptyLines} points to the enum constant itself 112 * as it already represents the current set of options. 113 */ 114 public final CatOptionSet_bns squeezeEmptyLines; 115 private final EnumSet<CatOption> options; 116 117 //inherit javadoc 118 @Override 119 public Class<CatOption> optionType() { 120 return CatOption.class; 121 } 122 //inherit javadoc 123 @Override 124 public boolean isSet(CatOption option) { 125 return options.contains(option); 126 } 127 //inherit javadoc 128 @Override 129 public int size() { 130 return options.size(); 131 } 132 /** 133 * Checks that the given {@code value} is not null and throws an exception 134 * otherwise. 135 * 136 * @param the value to check 137 * @return the given {@code value} if it is not null 138 * @throws NullPointerException if {@code value==null} 139 */ 140 private static <T> T notNull(T value) { 141 if (value != null) return value; 142 throw new NullPointerException(); 143 } 144 /** 145 * Returns the set with the active options. The returned set a new defensive 146 * copy instance created when this method is called, modifications of this 147 * set will therefore not alter {@code this} option set. 148 * 149 * @return a copy of the set with the active options. 150 */ 151 @Override 152 public EnumSet<CatOption> asSet() { 153 return EnumSet.copyOf(options); 154 } 155 /** 156 * Returns an immutable iterator with the active options of this option set. 157 * 158 * @return an immutable iterator for over the active options 159 */ 160 @Override 161 public Iterator<CatOption> iterator() { 162 return Collections.unmodifiableSet(options).iterator(); 163 } 164 /** 165 * Returns true if the {@link Option#acronym() acronym} should be used in 166 * for the specified {@code option} string representations. 167 * <p> 168 * In particular and independent from the {@code option} argument, this 169 * option set returns true if the last option added to this set was an 170 * acronym, and false if it was a long option name. 171 * <p> 172 * For instance, the set defined as 173 * <pre> 174 * CatOptionSet_bns.numberLines.b; 175 * </pre> 176 * uses acronyms, that is, this method always returns true for the above 177 * set. 178 * <p> 179 * On the other hand, long option names are used and this method always 180 * returns false for the set 181 * <pre> 182 * CatOptionSet_bns.n.numberNonBlankLines; 183 * </pre> 184 * <p> 185 * Note that a repeated option is <i>not</i> treated as the last set option. 186 * For instance, the first and last option of the following set are 187 * equivalent and acronyms are used: 188 * <pre> 189 * CatOptionSet_bns.n.b.numberLines; 190 * </pre> 191 * <p> 192 * This method always returns true for the empty set with no active options. 193 * 194 * @param option 195 * the option of interest, has no impact on the result returned 196 * by this method 197 * @return true if option acronyms should be used for string representations 198 * of any option of this option set 199 */ 200 @Override 201 public boolean useAcronymFor(CatOption option) { 202 return useAcronym; 203 } 204}