001package org.unix4j.unix.ls;
002
003import java.util.Collections;
004import java.util.EnumSet;
005import java.util.Iterator;
006
007import org.unix4j.option.Option;
008import org.unix4j.unix.Ls;
009
010/**
011 * Options for the {@link Ls ls} command.
012 * <p>
013 * For most applications, it may be more convenient to use {@link Ls#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 -a}</td><td>&nbsp;&nbsp;</td><td nowrap="nowrap">{@code --allFiles}</td><td>&nbsp;</td><td>Lists all files in the given directory, including hidden files
018                        (those whose names start with \".\" in Unix). By default, these 
019                        files are excluded from the list.</td></tr>
020 * <tr valign="top"><td width="10px"></td><td nowrap="nowrap">{@code -h}</td><td>&nbsp;&nbsp;</td><td nowrap="nowrap">{@code --humanReadable}</td><td>&nbsp;</td><td>Print sizes in human readable format. (e.g., 1K, 234M, 2G, etc.)</td></tr>
021 * <tr valign="top"><td width="10px"></td><td nowrap="nowrap">{@code -l}</td><td>&nbsp;&nbsp;</td><td nowrap="nowrap">{@code --longFormat}</td><td>&nbsp;</td><td>Long format, displaying file types, permissions, number of hard 
022                        links, owner, group, size, date, and filename.</td></tr>
023 * <tr valign="top"><td width="10px"></td><td nowrap="nowrap">{@code -R}</td><td>&nbsp;&nbsp;</td><td nowrap="nowrap">{@code --recurseSubdirs}</td><td>&nbsp;</td><td>Recursively lists subdirectories encountered.</td></tr>
024 * <tr valign="top"><td width="10px"></td><td nowrap="nowrap">{@code -r}</td><td>&nbsp;&nbsp;</td><td nowrap="nowrap">{@code --reverseOrder}</td><td>&nbsp;</td><td>Reverses the order of the sort to get reverse collating sequence or 
025                        oldest first.</td></tr>
026 * <tr valign="top"><td width="10px"></td><td nowrap="nowrap">{@code -t}</td><td>&nbsp;&nbsp;</td><td nowrap="nowrap">{@code --timeSorted}</td><td>&nbsp;</td><td>Sorts with the primary key being time modified (most recently 
027                        modified first) and the secondary key being filename in the 
028                        collating sequence.</td></tr>
029 * </table>
030 */
031public enum LsOption implements Option, LsOptions {
032        /**
033         * Option <b>{@code --allFiles}</b>, <b>{@code -a}</b>: 
034         * Lists all files in the given directory, including hidden files
035                        (those whose names start with \".\" in Unix). By default, these 
036                        files are excluded from the list.
037         */
038        allFiles('a'),
039        /**
040         * Option <b>{@code --humanReadable}</b>, <b>{@code -h}</b>: 
041         * Print sizes in human readable format. (e.g., 1K, 234M, 2G, etc.)
042         */
043        humanReadable('h'),
044        /**
045         * Option <b>{@code --longFormat}</b>, <b>{@code -l}</b>: 
046         * Long format, displaying file types, permissions, number of hard 
047                        links, owner, group, size, date, and filename.
048         */
049        longFormat('l'),
050        /**
051         * Option <b>{@code --recurseSubdirs}</b>, <b>{@code -R}</b>: 
052         * Recursively lists subdirectories encountered.
053         */
054        recurseSubdirs('R'),
055        /**
056         * Option <b>{@code --reverseOrder}</b>, <b>{@code -r}</b>: 
057         * Reverses the order of the sort to get reverse collating sequence or 
058                        oldest first.
059         */
060        reverseOrder('r'),
061        /**
062         * Option <b>{@code --timeSorted}</b>, <b>{@code -t}</b>: 
063         * Sorts with the primary key being time modified (most recently 
064                        modified first) and the secondary key being filename in the 
065                        collating sequence.
066         */
067        timeSorted('t');
068        
069        private final char acronym;
070        private LsOption(char acronym) {
071                this.acronym = acronym;
072        }
073        @Override
074        public Class<LsOption> optionType() {
075                return LsOption.class;
076        }
077        /**
078         * Returns the option with the given {@code acronym}, or {@code null} if no
079         * such option is found.
080         * 
081         * @param acronym the option {@link #acronym() acronym}
082         * @return      the option with the given {@code acronym} or {@code null} if it
083         *                      is not found
084         */
085        public static LsOption findByAcronym(char acronym) {
086                for (final LsOption opt : values()) {
087                        if (opt.acronym() == acronym) return opt;
088                }
089                return null;
090        }
091        @Override
092        public char acronym() {
093                return acronym;
094        }
095        @Override
096        public boolean isSet(LsOption option) {
097                return equals(option);
098        }
099        /**
100         * Returns a new set with {@code this} active option.
101         * 
102         * @return a new set containing this option
103         */
104        @Override
105        public EnumSet<LsOption> asSet() {
106                return EnumSet.of(this);
107        }
108        
109        /**
110         * Returns an immutable iterator returning o single element: {@code this} 
111         * option.
112         * 
113         * @return an immutable iterator with {@code this} active option.
114         */
115        @Override
116        public Iterator<LsOption> iterator() {
117                return Collections.singleton(this).iterator();
118        }
119        
120        /**
121         * Returns 1 as this is a set with a single element: {@code this} option
122         * 
123         * @return one
124         */
125        @Override
126        public int size() {
127                return 1;
128        }
129
130        /**
131         * Returns true if the {@link Option#acronym() acronym} should be used for
132         * the specified {@code option} in string representations. 
133         * <p>
134         * This method returns always true for all options.
135         *  
136         * @param option
137         *            the option of interest
138         * @return always true indicating that option acronyms should be used in
139         *                      string representations for all options
140         */
141        @Override
142        public boolean useAcronymFor(LsOption option) {
143                return true;
144        }
145}