001 /*
002 * Copyright (C) 2010 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
020 package org.crsh.cmdline.matcher;
021
022 import org.crsh.cmdline.binding.TypeBinding;
023 import org.crsh.cmdline.ParameterDescriptor;
024
025 import java.util.ArrayList;
026 import java.util.List;
027
028 /**
029 * @author <a href="mailto:julien.viet@exoplatform.com">Julien Viet</a>
030 * @version $Revision$
031 */
032 public class ParameterMatch<P extends ParameterDescriptor<B>, B extends TypeBinding> {
033
034 /** . */
035 private final P parameter;
036
037 /** . */
038 private final List<LiteralValue> values;
039
040 /** . */
041 private List<String> strings;
042
043 public ParameterMatch(P parameter, List<LiteralValue> values) {
044 this.parameter = parameter;
045 this.values = values;
046 this.strings = null;
047 }
048
049 public P getParameter() {
050 return parameter;
051 }
052
053 public List<LiteralValue> getValues() {
054 return values;
055 }
056
057 public List<String> getStrings() {
058 if (strings == null) {
059 List<String> strings = new ArrayList<String>(values.size());
060 for (LiteralValue value : values) {
061 strings.add(parameter.isUnquote() ? value.getValue() : value.getRawValue());
062 }
063 this.strings = strings;
064 }
065 return strings;
066 }
067
068 /**
069 * Compute the value from the parameter metadata and the values list.
070 *
071 * @return the invocation value
072 * @throws CmdSyntaxException anything that would prevent the value from being computed
073 */
074 public Object computeValue() throws CmdSyntaxException {
075 List<String> strings = getStrings();
076 return parameter.parse(strings);
077 }
078 }