001/*
002 * Copyright (C) 2012 eXo Platform SAS.
003 *
004 * This is free software; you can redistribute it and/or modify it
005 * under the terms of the GNU Lesser General Public License as
006 * published by the Free Software Foundation; either version 2.1 of
007 * the License, or (at your option) any later version.
008 *
009 * This software is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * You should have received a copy of the GNU Lesser General Public
015 * License along with this software; if not, write to the Free
016 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
017 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
018 */
019
020package org.crsh.cli.impl.invocation;
021
022import org.crsh.cli.impl.SyntaxException;
023import org.crsh.cli.impl.LiteralValue;
024import org.crsh.cli.descriptor.ParameterDescriptor;
025
026import java.util.ArrayList;
027import java.util.List;
028
029public class ParameterMatch<P extends ParameterDescriptor> {
030
031  /** . */
032  private final P parameter;
033
034  /** . */
035  private final List<LiteralValue> values;
036
037  /** . */
038  private List<String> strings;
039
040  public ParameterMatch(P parameter, List<LiteralValue> values) {
041    this.parameter = parameter;
042    this.values = values;
043    this.strings = null;
044  }
045
046  public P getParameter() {
047    return parameter;
048  }
049
050  public List<LiteralValue> getValues() {
051    return values;
052  }
053
054  public List<String> getStrings() {
055    if (strings == null) {
056      List<String> strings = new ArrayList<String>(values.size());
057      for (LiteralValue value : values) {
058        strings.add(parameter.isUnquote() ? value.getValue() : value.getRawValue());
059      }
060      this.strings = strings;
061    }
062    return strings;
063  }
064
065  /**
066   * Compute the value from the parameter metadata and the values list.
067   *
068   * @return the invocation value
069   * @throws org.crsh.cli.impl.SyntaxException anything that would prevent the value from being computed
070   */
071  public Object computeValue() throws SyntaxException {
072    List<String> strings = getStrings();
073    return parameter.parse(strings);
074  }
075}