Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 56a0aca60c2b772ed261d3996ae8ced4386ebeb6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*******************************************************************************
* Copyright (c) 2018 protos software gmbh (http://www.protos.de).
* All rights reserved.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* CONTRIBUTORS:
*           Jan Belle (initial contribution)
*
 *******************************************************************************/

package org.eclipse.etrice.generator.base.args;

import java.util.Arrays;
import java.util.HashMap;
import java.util.StringJoiner;

/**
 * Encapsulates a set of arguments.
 */
public class Arguments {
	
	private Options options;
	private HashMap<String, Object> option2Arg;
	
	/**
	 * Creates a new set of arguments initialized to their default values.
	 * 
	 * @param options the options to create the arguments from
	 */
	public Arguments(Options options) {
		this.options = options;
		option2Arg = new HashMap<>();
		
		for(Option<?> option: options) {
			option2Arg.put(option.getName(), option.getDefaultValue());
		}
	}
	
	/**
	 * Queries an argument by its name.
	 * 
	 * @param name the name of the option
	 * @return the argument
	 */
	public Object get(String name) throws IllegalArgumentException {
		Object value = option2Arg.get(name);
		if(value == null) {
			throw new IllegalArgumentException("option " + name + " not recognized");
		}
		return value;
	}
	
	/**
	 * Queries an argument.
	 * 
	 * @param option the option of the argument
	 * @return the argument
	 */
	public <T> T get(Option<T> option) throws IllegalArgumentException {
		return option.getType().cast(get(option.getName()));
	}
	
	/**
	 * Sets an argument to the passed value.
	 * 
	 * @param name the name of the option
	 * @param value the value to be set
	 */
	public void set(String name, Object value) throws IllegalArgumentException {
		Option<?> opt = options.get(name);
		if(!opt.getType().isInstance(value)) {
			throw new IllegalArgumentException("value " + value.toString() + " is not assignable to Option " + opt.toString());
		}
		option2Arg.put(name, value);
	}
	
	/**
	 * Sets an argument to the passed value.
	 * 
	 * @param option the option
	 * @param value the value to be set
	 */
	public <T> void set(Option<T> option, T value) throws IllegalArgumentException {
		set(option.getName(), value);
	}
	
	/**
	 * Returns the options for the arguments.
	 * 
	 * @return options the options
	 */
	public Options getOptions() {
		return options;
	}
	
	@Override
	public String toString() {
		StringJoiner joiner = new StringJoiner(", ");
		for(Option<?> opt: options) {
			joiner.add(argToString(opt));
		}
		return joiner.toString();
	}
	
	private String argToString(Option<?> opt) {
		String result = opt.getName() + "=";
		Object obj = option2Arg.get(opt.getName());
		if(opt.getType().isArray()) {
			Object[] objArr = (Object[]) obj;
			result += Arrays.toString(objArr);
		}
		else {
			result += obj.toString();
		}
		return result;
	}
}

Back to the top