001: /*
002: * CommandParseData.java
003: *
004: * Copyright (C) 2007 Ferran Busquets
005: *
006: * This program is free software: you can redistribute it and/or modify
007: * it under the terms of the GNU General Public License as published by
008: * the Free Software Foundation, either version 3 of the License, or
009: * any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program. If not, see <http://www.gnu.org/licenses/>.
018: *
019: */
020: package org.naturalcli;
021:
022: /**
023: * Encapsulate the data that restuls of a command parse.
024: * It means the parameters values and the list of tokens found.
025: *
026: * @author Ferran Busquets
027: */
028: public class ParseResult {
029:
030: /** Parameters values **/
031: private Object[] parameterValues;
032:
033: /** Tokens given **/
034: private boolean[] tokensGiven;
035:
036: /**
037: * Constructor
038: *
039: * @param paramValues the ordered parameter values array. For optional
040: * parameters not provided will be <code>null</code>
041: * @param tokensGiven the ordered array with all the tokens saying if
042: * each token is given or not. For non-optional tokens
043: * the value will be always <code>true</code>.
044: */
045: public ParseResult(Object[] parameterValues, boolean[] tokensGiven) {
046: if (parameterValues == null)
047: throw new IllegalArgumentException(
048: "Parameter values array cannot be null.");
049: if (tokensGiven == null)
050: throw new IllegalArgumentException(
051: "Tokens found array cannot be null.");
052: this .parameterValues = parameterValues;
053: this .tokensGiven = tokensGiven;
054: }
055:
056: /**
057: * Get the parameter value in the given index.
058: *
059: * @param parameterIndex the parameter index. This index is relative
060: * only for the parameters.
061: * @return the parameter value.
062: */
063: public Object getParameterValue(int parameterIndex) {
064: return this .parameterValues[parameterIndex];
065: }
066:
067: /**
068: * Get a copy of the parameter values
069: *
070: * @return object array with the prameter values
071: */
072: public Object[] getParameterValues() {
073: return this .parameterValues.clone();
074: }
075:
076: /**
077: * Get the number of all possible parameters
078: *
079: * @return number of parameters
080: */
081: public int getParameterCount() {
082: return this .parameterValues.length;
083: }
084:
085: /**
086: * Get if the token is given or not.
087: *
088: * @param tokenIndex the token index.
089: *
090: * @return <code>true</code> if the token is given or <code>false</code>
091: * if the token is not given (only for optional parameters).
092: */
093: public boolean isTokenGiven(int tokenIndex) {
094: return this .tokensGiven[tokenIndex];
095: }
096:
097: /**
098: * Get a copy of the tokens given.
099: *
100: * @return boolean array with the tokens given.
101: */
102: public boolean[] getTokensGiven() {
103: return this.tokensGiven.clone();
104: }
105:
106: }
|