001package org.unix4j.unix.uniq; 002 003import java.util.Collections; 004import java.util.EnumSet; 005import java.util.Iterator; 006 007import org.unix4j.option.Option; 008import org.unix4j.unix.Uniq; 009 010/** 011 * Options for the {@link Uniq uniq} command. 012 * <p> 013 * For most applications, it may be more convenient to use {@link Uniq#Options} 014 * instead of the option constants defined here. 015 * <p> 016 * <table> 017 * <tr valign="top"><td width="10px"></td><td nowrap="nowrap">{@code -c}</td><td> </td><td nowrap="nowrap">{@code --count}</td><td> </td><td>Precedes each output line with a count of the number of times the 018 line occurred in the input.</td></tr> 019 * <tr valign="top"><td width="10px"></td><td nowrap="nowrap">{@code -d}</td><td> </td><td nowrap="nowrap">{@code --duplicatedOnly}</td><td> </td><td>Suppresses the writing of lines that are not repeated in the input.</td></tr> 020 * <tr valign="top"><td width="10px"></td><td nowrap="nowrap">{@code -u}</td><td> </td><td nowrap="nowrap">{@code --uniqueOnly}</td><td> </td><td>Suppresses the writing of lines that are repeated in the input.</td></tr> 021 * <tr valign="top"><td width="10px"></td><td nowrap="nowrap">{@code -g}</td><td> </td><td nowrap="nowrap">{@code --global}</td><td> </td><td>Suppresses repeated lines globally, that is, if lines are 022 non-adjacent. This option guarantees unique output lines even if the 023 input lines are not sorted.</td></tr> 024 * </table> 025 */ 026public enum UniqOption implements Option, UniqOptions { 027 /** 028 * Option <b>{@code --count}</b>, <b>{@code -c}</b>: 029 * Precedes each output line with a count of the number of times the 030 line occurred in the input. 031 */ 032 count('c'), 033 /** 034 * Option <b>{@code --duplicatedOnly}</b>, <b>{@code -d}</b>: 035 * Suppresses the writing of lines that are not repeated in the input. 036 */ 037 duplicatedOnly('d'), 038 /** 039 * Option <b>{@code --uniqueOnly}</b>, <b>{@code -u}</b>: 040 * Suppresses the writing of lines that are repeated in the input. 041 */ 042 uniqueOnly('u'), 043 /** 044 * Option <b>{@code --global}</b>, <b>{@code -g}</b>: 045 * Suppresses repeated lines globally, that is, if lines are 046 non-adjacent. This option guarantees unique output lines even if the 047 input lines are not sorted. 048 */ 049 global('g'); 050 051 private final char acronym; 052 private UniqOption(char acronym) { 053 this.acronym = acronym; 054 } 055 @Override 056 public Class<UniqOption> optionType() { 057 return UniqOption.class; 058 } 059 /** 060 * Returns the option with the given {@code acronym}, or {@code null} if no 061 * such option is found. 062 * 063 * @param acronym the option {@link #acronym() acronym} 064 * @return the option with the given {@code acronym} or {@code null} if it 065 * is not found 066 */ 067 public static UniqOption findByAcronym(char acronym) { 068 for (final UniqOption opt : values()) { 069 if (opt.acronym() == acronym) return opt; 070 } 071 return null; 072 } 073 @Override 074 public char acronym() { 075 return acronym; 076 } 077 @Override 078 public boolean isSet(UniqOption option) { 079 return equals(option); 080 } 081 /** 082 * Returns a new set with {@code this} active option. 083 * 084 * @return a new set containing this option 085 */ 086 @Override 087 public EnumSet<UniqOption> asSet() { 088 return EnumSet.of(this); 089 } 090 091 /** 092 * Returns an immutable iterator returning o single element: {@code this} 093 * option. 094 * 095 * @return an immutable iterator with {@code this} active option. 096 */ 097 @Override 098 public Iterator<UniqOption> iterator() { 099 return Collections.singleton(this).iterator(); 100 } 101 102 /** 103 * Returns 1 as this is a set with a single element: {@code this} option 104 * 105 * @return one 106 */ 107 @Override 108 public int size() { 109 return 1; 110 } 111 112 /** 113 * Returns true if the {@link Option#acronym() acronym} should be used for 114 * the specified {@code option} in string representations. 115 * <p> 116 * This method returns always true for all options. 117 * 118 * @param option 119 * the option of interest 120 * @return always true indicating that option acronyms should be used in 121 * string representations for all options 122 */ 123 @Override 124 public boolean useAcronymFor(UniqOption option) { 125 return true; 126 } 127}