Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ExecutionOptions |
|
| 2.5833333333333335;2.583 |
1 | package org.kuali.rice.krms.api.engine; | |
2 | ||
3 | import java.util.Collections; | |
4 | import java.util.HashMap; | |
5 | import java.util.Map; | |
6 | ||
7 | import org.apache.commons.lang.StringUtils; | |
8 | ||
9 | /** | |
10 | * The ExecutionOptions contain a set of options that can be passed to the KRMS | |
11 | * engine to control certain aspects related to it's execution. It supports | |
12 | * two different types of options: | |
13 | * | |
14 | * <ol> | |
15 | * <li>flags - a map of pre-defined boolean {@link ExecutionFlag} instances which can be either true or false</li> | |
16 | * <li>options - a general-purpose map of Strings which can be used to define optinos that have non-boolen values</li> | |
17 | * </ol> | |
18 | * | |
19 | * <p>The options map can be used to pass user-defined or provider-specific options. The ExecutionOptions are made | |
20 | * available as part of the {@link ExecutionEnvironment} during engine execution. | |
21 | * | |
22 | * <p>Instances of ExecutionOptions are not safe for use by multiple threads. | |
23 | * | |
24 | * @author Kuali Rice Team (rice.collab@kuali.org) | |
25 | * | |
26 | */ | |
27 | public final class ExecutionOptions { | |
28 | ||
29 | private final Map<ExecutionFlag, Boolean> flags; | |
30 | private final Map<String, String> options; | |
31 | ||
32 | /** | |
33 | * Construct an empty set of execution options. | |
34 | */ | |
35 | 40 | public ExecutionOptions() { |
36 | 40 | flags = new HashMap<ExecutionFlag, Boolean>(); |
37 | 40 | options = new HashMap<String, String>(); |
38 | 40 | } |
39 | ||
40 | /** | |
41 | * Constructs a new set of execution options, initialized with all options | |
42 | * and flags copied from the given set of execution options. | |
43 | * | |
44 | * @param executionOptions the execution options from which to copy into the newly created instance. | |
45 | * If the given execution options are null, then this constructor is equivalent to {@link ExecutionOptions#ExecutionOptions()} | |
46 | */ | |
47 | public ExecutionOptions(ExecutionOptions executionOptions) { | |
48 | 1 | this(); |
49 | 1 | if (executionOptions != null) { |
50 | 1 | this.flags.putAll(executionOptions.getFlags()); |
51 | 1 | this.options.putAll(executionOptions.getOptions()); |
52 | } | |
53 | 1 | } |
54 | ||
55 | /** | |
56 | * Sets the value for the given flag to the given boolean value. | |
57 | * | |
58 | * @param flag the flag to set | |
59 | * @param value the flag value to set | |
60 | * @return a reference to this object | |
61 | * | |
62 | * @throws IllegalArgumentException if the given flag is null | |
63 | */ | |
64 | public ExecutionOptions setFlag(ExecutionFlag flag, boolean value) { | |
65 | 15 | if (flag == null) { |
66 | 1 | throw new IllegalArgumentException("flag was null"); |
67 | } | |
68 | 14 | flags.put(flag, value); |
69 | 14 | return this; |
70 | } | |
71 | ||
72 | /** | |
73 | * Sets the value for the given option name to the given value. | |
74 | * | |
75 | * @param optionName the name of the option to set | |
76 | * @param value the value of the option to set | |
77 | * @return a reference to this object | |
78 | * | |
79 | * @throws IllegalArgumentException if the given optionName is blank | |
80 | */ | |
81 | public ExecutionOptions setOption(String optionName, String value) { | |
82 | 14 | if (StringUtils.isBlank(optionName)) { |
83 | 1 | throw new IllegalArgumentException("optionName was blank"); |
84 | } | |
85 | 13 | options.put(optionName, value); |
86 | 13 | return this; |
87 | } | |
88 | ||
89 | /** | |
90 | * Removes the specified flag (if it has been set) from the set of execution options. | |
91 | * If the flag is not currently set, this method will do nothing. | |
92 | * | |
93 | * @param flag the flag to remove | |
94 | * @return a reference to this object | |
95 | * | |
96 | * @throws IllegalArgumentException if the given flag is null | |
97 | */ | |
98 | public ExecutionOptions removeFlag(ExecutionFlag flag) { | |
99 | 5 | if (flag == null) { |
100 | 1 | throw new IllegalArgumentException("flag was null"); |
101 | } | |
102 | 4 | flags.remove(flag); |
103 | 4 | return this; |
104 | } | |
105 | ||
106 | /** | |
107 | * Removes the option with the specified name (if it has been set) from the set | |
108 | * of execution options. If the option is not currently set, this method will | |
109 | * do nothing. | |
110 | * | |
111 | * @param optionName the name of the option to remove | |
112 | * @return a reference to this object | |
113 | * | |
114 | * @throws IllegalArgumentException if the given optionName is blank | |
115 | */ | |
116 | public ExecutionOptions removeOption(String optionName) { | |
117 | 7 | if (StringUtils.isBlank(optionName)) { |
118 | 3 | throw new IllegalArgumentException("optionName was blank"); |
119 | } | |
120 | 4 | options.remove(optionName); |
121 | 4 | return this; |
122 | } | |
123 | ||
124 | /** | |
125 | * Returns the value the given flag. If the specified flag has not been set | |
126 | * on this object, then {@link ExecutionFlag#getDefaultValue()} will be returned. | |
127 | * | |
128 | * @param flag the flag to check | |
129 | * @return the value of the flag, or the flag's default value if the flag value | |
130 | * is not currently set on this object | |
131 | * | |
132 | * @throws IllegalArgumentException if the given flag is null | |
133 | */ | |
134 | public boolean getFlag(ExecutionFlag flag) { | |
135 | 9 | if (flag == null) { |
136 | 1 | throw new IllegalArgumentException("flag is null"); |
137 | } | |
138 | 8 | if (isFlagSet(flag)) { |
139 | 6 | return flags.get(flag).booleanValue(); |
140 | } | |
141 | 2 | return flag.getDefaultValue(); |
142 | } | |
143 | ||
144 | /** | |
145 | * Returns the value for the option with the given name, or null | |
146 | * if the option is not set. | |
147 | * | |
148 | * @param optionName the name of the option for which to retrieve the value | |
149 | * @return the value of the option, or null if the option is not set | |
150 | * | |
151 | * @throws IllegalArgumentException if the given optionName is blank | |
152 | */ | |
153 | public String getOption(String optionName) { | |
154 | 11 | if (StringUtils.isBlank(optionName)) { |
155 | 3 | throw new IllegalArgumentException("optionName is blank"); |
156 | } | |
157 | 8 | return options.get(optionName); |
158 | } | |
159 | ||
160 | /** | |
161 | * Checks whether or not the given flag is set. | |
162 | * | |
163 | * @param flag the flag to check | |
164 | * @return true if the flag is set, false if not | |
165 | * | |
166 | * @throws IllegalArgumentException if the given flag is null | |
167 | */ | |
168 | public boolean isFlagSet(ExecutionFlag flag) { | |
169 | 18 | if (flag == null) { |
170 | 1 | throw new IllegalArgumentException("flag is null"); |
171 | } | |
172 | 17 | return flags.containsKey(flag); |
173 | } | |
174 | ||
175 | /** | |
176 | * Checks whether or not the option with the given name has been set. | |
177 | * | |
178 | * @param optionName the name of the option to check | |
179 | * @return true if the option is set, false if not | |
180 | * | |
181 | * @throws IllegalArgumentException if the given optionName is blank | |
182 | */ | |
183 | public boolean isOptionSet(String optionName) { | |
184 | 14 | if (StringUtils.isBlank(optionName)) { |
185 | 3 | throw new IllegalArgumentException("optionName is blank"); |
186 | } | |
187 | 11 | return options.containsKey(optionName); |
188 | } | |
189 | ||
190 | /** | |
191 | * Returns an immutable map of the flags that have been set on this object. | |
192 | * | |
193 | * @return the flags that have been set, this map be empty but will never be null | |
194 | */ | |
195 | public Map<ExecutionFlag, Boolean> getFlags() { | |
196 | 11 | return Collections.unmodifiableMap(new HashMap<ExecutionFlag, Boolean>(flags)); |
197 | } | |
198 | ||
199 | /** | |
200 | * Returns an immutable map of the options that have been set on this object. | |
201 | * | |
202 | * @return the options that have been set, this map be empty but will never be null | |
203 | */ | |
204 | public Map<String, String> getOptions() { | |
205 | 13 | return Collections.unmodifiableMap(new HashMap<String, String>(options)); |
206 | } | |
207 | ||
208 | } |