001: /*
002: Copyright (c) 2003, Dennis M. Sosnoski
003: All rights reserved.
004:
005: Redistribution and use in source and binary forms, with or without modification,
006: are permitted provided that the following conditions are met:
007:
008: * Redistributions of source code must retain the above copyright notice, this
009: list of conditions and the following disclaimer.
010: * Redistributions in binary form must reproduce the above copyright notice,
011: this list of conditions and the following disclaimer in the documentation
012: and/or other materials provided with the distribution.
013: * Neither the name of JargP nor the names of its contributors may be used
014: to endorse or promote products derived from this software without specific
015: prior written permission.
016:
017: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
018: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
019: WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
020: DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
021: ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
022: (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
023: LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
024: ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
026: SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027: */
028:
029: package org.jargp;
030:
031: /**
032: * Command line parameter collection definition. Each collection consists of
033: * some number of parameter definitions. Multiple collections may be linked
034: * to function as a single collection.
035: *
036: * @author Dennis M. Sosnoski
037: * @version 1.0
038: */
039:
040: public class ParameterSet {
041: /** Arguments known by this handler. */
042: private final ParameterDef[] m_knownArguments;
043:
044: /** Next parameter set for control flags not included in this one. */
045: private final ParameterSet m_nextSet;
046:
047: /**
048: * Constructor
049: *
050: * @param defs parameter definitions for this handler
051: * @param next parameter set used for parameters not defined in this set
052: */
053:
054: public ParameterSet(ParameterDef[] defs, ParameterSet next) {
055: m_knownArguments = defs;
056: m_nextSet = next;
057: }
058:
059: /**
060: * Find the parameter definition for a particular control flag. If the
061: * control flag is not defined in the set this will pass the call on
062: * to the next set until we reach the end of the chain.
063: *
064: * @param flag control flag for parameter
065: * @param parameter definition, or <code>null</code> if not defined
066: */
067:
068: /*package*/ParameterDef findDef(char flag) {
069: for (int i = 0; i < m_knownArguments.length; i++) {
070: if (flag == m_knownArguments[i].getFlag()) {
071: return m_knownArguments[i];
072: }
073: }
074: if (m_nextSet == null) {
075: return null;
076: } else {
077: return m_nextSet.findDef(flag);
078: }
079: }
080:
081: /**
082: * Get the parameter definition at a particular position in the list. If
083: * the index value supplied is not defined in the set this will pass the
084: * call on to the next set until we reach the end of the chain. The caller
085: * can index through all defined values by starting at zero and
086: * incrementing until a <code>null</code> is returned.
087: *
088: * @param index position for parameter definition to be returned
089: * @param parameter definition, or <code>null</code> if not defined
090: */
091:
092: /*package*/ParameterDef indexDef(int index) {
093: if (index < m_knownArguments.length) {
094: return m_knownArguments[index];
095: } else if (m_nextSet == null) {
096: return null;
097: } else {
098: return m_nextSet.indexDef(index);
099: }
100: }
101: }
|