001/*
002 * Copyright 2003-2005 The Apache Software Foundation
003 * Copyright 2005 Stephen McConnell
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package net.dpml.cli.commandline;
018
019import java.util.Collections;
020import java.util.Iterator;
021import java.util.List;
022
023import net.dpml.cli.CommandLine;
024import net.dpml.cli.Option;
025import net.dpml.cli.resource.ResourceConstants;
026import net.dpml.cli.resource.ResourceHelper;
027
028/**
029 * Instances of CommandLine represent a command line that has been processed
030 * according to the definition supplied to the parser.
031 *
032 * @author <a href="@PUBLISHER-URL@">@PUBLISHER-NAME@</a>
033 * @version @PROJECT-VERSION@
034 */
035public abstract class CommandLineImpl implements CommandLine
036{
037    /**
038     * Detects the presence of an option with the specified trigger in this 
039     * CommandLine.
040     * 
041     * @param trigger the trigger to search for
042     * @return true iff an option with this trigger is present
043     */
044    public final boolean hasOption( final String trigger )
045    {
046        return hasOption( getOption( trigger ) );
047    }
048
049    /**
050     * Retrieves the Argument values associated with the specified Option
051     * 
052     * @param trigger a trigger used to lookup the Option
053     * @return a list of values or an empty List if none are found
054     */
055    public final List getValues( final String trigger )
056    {
057        return getValues( getOption( trigger ), Collections.EMPTY_LIST );
058    }
059
060    /**
061     * Retrieves the Argument values associated with the specified Option
062     * 
063     * @param trigger a trigger used to lookup the Option
064     * @param defaultValues the result to return if no values are found
065     * @return a list of values or defaultValues if none are found
066     */
067    public final List getValues(
068      final String trigger, final List defaultValues )
069    {
070        return getValues( getOption( trigger ), defaultValues );
071    }
072
073    /**
074     * Retrieves the Argument values associated with the specified Option
075     * 
076     * @param option the Option associated with the values
077     * @return a list of values or an empty List if none are found
078     */
079    public final List getValues( final Option option )
080    {
081        return getValues( option, Collections.EMPTY_LIST );
082    }
083
084    /**
085     * Retrieves the single Argument value associated with the specified Option
086     * 
087     * @param trigger a trigger used to lookup the Option
088     * @return the matching value or null if none exists
089     * @throws IllegalStateException if more than one values are found
090     */
091    public final Object getValue( final String trigger ) throws IllegalStateException
092    {
093        return getValue( getOption( trigger ), null );
094    }
095
096    /**
097     * Retrieves the single Argument value associated with the specified Option
098     * 
099     * @param trigger a trigger used to lookup the Option
100     * @param defaultValue the result to use if no values are found
101     * @return the matching value or defaultValue if none exists
102     * @throws IllegalStateException if more than one values are found
103     */
104    public final Object getValue(
105      final String trigger, final Object defaultValue ) throws IllegalStateException
106    {
107        return getValue( getOption( trigger ), defaultValue );
108    }
109
110    /**
111     * Retrieves the single Argument value associated with the specified Option
112     * 
113     * @param option the Option associated with the value
114     * @return the matching value or null if none exists
115     * @throws IllegalStateException if more than one values are found
116     */
117    public final Object getValue( final Option option ) throws IllegalStateException
118    {
119        return getValue( option, null );
120    }
121
122    /**
123     * Retrieves the single Argument value associated with the specified Option
124     * 
125     * @param option the Option associated with the value
126     * @param defaultValue the result to use if no values are found
127     * @return the matching value or defaultValue if none exists
128     * @throws IllegalStateException if more than one value is found
129     */
130    public final Object getValue( final Option option, final Object defaultValue )
131      throws IllegalStateException
132    {
133        final List values;
134        if( defaultValue == null )
135        {
136            values = getValues( option );
137        }
138        else
139        {
140            values = getValues( option, Collections.singletonList( defaultValue ) );
141        }
142        if( values.size() > 1 )
143        {
144            throw new IllegalStateException(
145              ResourceHelper.getResourceHelper().getMessage(
146                ResourceConstants.ARGUMENT_TOO_MANY_VALUES ) );
147        }
148        if( values.isEmpty() )
149        {
150            return defaultValue;
151        }
152        return values.get( 0 );
153    }
154
155    /**
156     * Retrieves the Boolean value associated with the specified Switch
157     * 
158     * @param trigger a trigger used to lookup the Option
159     * @return the Boolean associated with trigger or null if none exists
160     */
161    public final Boolean getSwitch( final String trigger )
162    {
163        return getSwitch( getOption( trigger ), null );
164    }
165
166    /**
167     * Retrieves the Boolean value associated with the specified Switch
168     * 
169     * @param trigger a trigger used to lookup the Option
170     * @param defaultValue the Boolean to use if none match
171     * @return the Boolean associated with trigger or defaultValue if none exists
172     */
173    public final Boolean getSwitch(
174      final String trigger, final Boolean defaultValue )
175    {
176        return getSwitch( getOption( trigger ), defaultValue );
177    }
178
179    /**
180     * Retrieves the Boolean value associated with the specified Switch
181     * 
182     * @param option the Option associated with the value
183     * @return the Boolean associated with option or null if none exists
184     */
185    public final Boolean getSwitch( final Option option )
186    {
187        return getSwitch( option, null );
188    }
189
190    /**
191     * Retrieves the value associated with the specified property 
192     * 
193     * @param property the property name to lookup
194     * @return the value of the property or null
195     */
196    public final String getProperty( final String property )
197    {
198        return getProperty( property, null );
199    }
200
201    /**
202     * Retrieves the number of times the specified Option appeared in this 
203     * CommandLine
204     * 
205     * @param trigger a trigger used to lookup the Option
206     * @return the number of occurrences of the option
207     */
208    public final int getOptionCount( final String trigger )
209    {
210        return getOptionCount( getOption( trigger ) );
211    }
212
213    /**
214     * Retrieves the number of times the specified Option appeared in this 
215     * CommandLine
216     * 
217     * @param option the Option associated to check
218     * @return the number of occurrences of the option
219     */
220    public final int getOptionCount( final Option option )
221    {
222        if( option == null )
223        {
224            return 0;
225        }
226        int count = 0;
227        for( Iterator i = getOptions().iterator(); i.hasNext();) 
228        {
229            if( option.equals( i.next() ) )
230            {
231                ++count;
232            }
233        }
234        return count;
235    }
236}