001package votorola.g.option; // Copyright 2006, Michael Allan.  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Votorola Software"), to deal in the Votorola Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Votorola Software, and to permit persons to whom the Votorola Software is furnished to do so, subject to the following conditions: The preceding copyright notice and this permission notice shall be included in all copies or substantial portions of the Votorola Software. THE VOTOROLA SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE VOTOROLA SOFTWARE OR THE USE OR OTHER DEALINGS IN THE VOTOROLA SOFTWARE.
002
003import gnu.getopt.*;
004
005
006/** A formal command line option.
007  */
008public class Option extends LongOpt implements Comparable<Option>
009{
010
011
012    /** Constructs an Option.
013      */
014    public Option( String _name, int _has_arg ) { super( _name, _has_arg, null, 0 ); }
015
016
017
018   // ------------------------------------------------------------------------------------
019
020
021    /** Adds an actual occurence of this formal option.
022      *
023      *     @param _argumentValue the value of any argument that accompanies the
024      *       occurence.
025      */
026    public final void addOccurence( final String _argumentValue )
027    {
028        ++occurenceCount;
029        argumentValue = _argumentValue;
030        apply();
031    }
032
033
034
035    /** Applies the option.  This hook method is called once for each actual occurence of
036      * the option.  Subclasses may override it to carry out specific actions.  By default
037      * it does nothing.
038      */
039    public void apply() {}
040
041
042
043    /** The value of the option argument from the last actual occurence of this option.
044      *
045      *     @return the value from the last actual occurence, or null if there was none.
046      */
047    public String argumentValue() { return argumentValue; }
048
049
050        protected String argumentValue;
051
052
053
054    /** True iff this option has actually occured.
055      */
056    public boolean hasOccured() { return occurenceCount > 0; }
057
058
059
060    /** The number of times this option has actually occured.
061      */
062    public int occurenceCount() { return occurenceCount; }
063
064
065        private int occurenceCount;
066
067
068
069   // - C o m p a r a b l e --------------------------------------------------------------
070
071
072    /** Compares based on name.
073      *
074      *     @see #getName()
075      */
076    public int compareTo( final Option other ) { return getName().compareTo( other.getName() ); }
077
078
079
080   // - O b j e c t ----------------------------------------------------------------------
081
082
083    /** Compares based on name.
084      *
085      *     @see #getName()
086      */
087    public @Override boolean equals( final Object o )
088    {
089        if( !(o instanceof Option) ) return false; // or if null
090
091        final Option other = (Option)o;
092        return getName().equals( other.getName() );
093    }
094
095
096
097    public @Override int hashCode() { return getName().hashCode(); }
098
099
100
101    /** The name of this option.
102      *
103      *     @see #getName()
104      */
105    public final @Override String toString() { return getName(); }
106
107
108
109}