1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.application.arguments;
14 import java.util.Arrays;
15 import java.util.ListIterator;
17 import java.util.TreeMap;
19 import org.simantics.application.arguments.IArgument;
20 import org.simantics.application.arguments.IArgumentFactory;
22 public class Arguments implements IArguments {
24 private Map<String, IArgument<?>> args = new TreeMap<String, IArgument<?>>();
27 * @see org.simantics.application.arguments.IArguments#put(org.simantics.application.arguments.IArgument)
29 public void put(IArgument<?> arg) {
30 args.put(arg.getArgument(), arg);
34 * @see org.simantics.application.arguments.IArguments#putAll(org.simantics.application.arguments.IArguments)
37 public void putAll(IArguments args) {
38 for (IArgument<?> arg : args.toArray()) {
43 @SuppressWarnings("unchecked")
44 public <T> IArgument<T> remove(IArgumentFactory<T> key) {
45 return (IArgument<T>) args.remove(key.getArgument());
49 * @see org.simantics.application.arguments.IArguments#contains(org.simantics.application.arguments.IArgumentFactory)
51 public <T> boolean contains(IArgumentFactory<T> key) {
52 return args.containsKey(key.getArgument());
56 * @see org.simantics.application.arguments.IArguments#get(org.simantics.application.arguments.IArgumentFactory)
58 @SuppressWarnings("unchecked")
59 public <T> T get(IArgumentFactory<T> key) {
60 IArgument<T> a = (IArgument<T>) args.get(key.getArgument());
61 return a == null ? null : a.getValue();
64 @SuppressWarnings("unchecked")
66 public <T> T get(IArgumentFactory<T> key, T defaultValue) {
67 IArgument<T> a = (IArgument<T>) args.get(key.getArgument());
68 return a == null ? defaultValue : a.getValue();
72 * @see org.simantics.application.arguments.IArguments#toArray()
74 public IArgument<?>[] toArray() {
75 int size = args.size();
76 return args.values().toArray(new IArgument<?>[size]);
81 * @param parsedArguments
84 public static IArguments parse(String[] args, IArgumentFactory<?>... parsedArguments) {
85 ListIterator<String> iter = Arrays.asList(args).listIterator();
86 IArguments result = new Arguments();
87 while (iter.hasNext()) {
88 String arg = iter.next();
90 IArgument<?> ret = null;
91 for (IArgumentFactory<?> factory : parsedArguments) {
93 if ((ret = factory.parse(arg, iter)) != null)
95 } catch (IllegalArgumentException e) {
96 // Argument matched, but the argument value was invalid.
97 // TODO: react to this.