17
17
*/
18
18
package me .brandonli .mcav .media .player .vm ;
19
19
20
- import java .util .*;
21
- import org .checkerframework .checker .nullness .qual .KeyFor ;
20
+ import java .util .ArrayList ;
21
+ import java .util .Collections ;
22
+ import java .util .List ;
22
23
23
24
/**
24
25
* A builder class for creating QEMU virtual machine configurations.
27
28
*/
28
29
public class VMConfiguration {
29
30
30
- private final Map <String , String > options ;
31
+ private final List <String > arguments ;
31
32
32
33
private VMConfiguration () {
33
- this .options = new HashMap <>();
34
+ this .arguments = new ArrayList <>();
34
35
}
35
36
36
37
/**
@@ -49,8 +50,7 @@ public static VMConfiguration builder() {
49
50
* @return this builder instance
50
51
*/
51
52
public VMConfiguration memory (final int memoryMB ) {
52
- this .options .put ("m" , memoryMB + "M" );
53
- return this ;
53
+ return this .option ("m" , memoryMB + "M" );
54
54
}
55
55
56
56
/**
@@ -61,8 +61,7 @@ public VMConfiguration memory(final int memoryMB) {
61
61
* @return this builder instance
62
62
*/
63
63
public VMConfiguration memory (final int amount , final String unit ) {
64
- this .options .put ("m" , amount + unit );
65
- return this ;
64
+ return this .option ("m" , amount + unit );
66
65
}
67
66
68
67
/**
@@ -72,8 +71,7 @@ public VMConfiguration memory(final int amount, final String unit) {
72
71
* @return this builder instance
73
72
*/
74
73
public VMConfiguration cores (final int cores ) {
75
- this .options .put ("smp" , String .valueOf (cores ));
76
- return this ;
74
+ return this .option ("smp" , String .valueOf (cores ));
77
75
}
78
76
79
77
/**
@@ -83,8 +81,7 @@ public VMConfiguration cores(final int cores) {
83
81
* @return this builder instance
84
82
*/
85
83
public VMConfiguration cdrom (final String isoPath ) {
86
- this .options .put ("cdrom" , isoPath );
87
- return this ;
84
+ return this .option ("cdrom" , isoPath );
88
85
}
89
86
90
87
/**
@@ -94,8 +91,7 @@ public VMConfiguration cdrom(final String isoPath) {
94
91
* @return this builder instance
95
92
*/
96
93
public VMConfiguration hda (final String hdaPath ) {
97
- this .options .put ("hda" , hdaPath );
98
- return this ;
94
+ return this .option ("hda" , hdaPath );
99
95
}
100
96
101
97
/**
@@ -105,8 +101,7 @@ public VMConfiguration hda(final String hdaPath) {
105
101
* @return this builder instance
106
102
*/
107
103
public VMConfiguration hdb (final String hdbPath ) {
108
- this .options .put ("hdb" , hdbPath );
109
- return this ;
104
+ return this .option ("hdb" , hdbPath );
110
105
}
111
106
112
107
/**
@@ -116,8 +111,7 @@ public VMConfiguration hdb(final String hdbPath) {
116
111
* @return this builder instance
117
112
*/
118
113
public VMConfiguration boot (final String bootOrder ) {
119
- this .options .put ("boot" , bootOrder );
120
- return this ;
114
+ return this .option ("boot" , bootOrder );
121
115
}
122
116
123
117
/**
@@ -127,8 +121,7 @@ public VMConfiguration boot(final String bootOrder) {
127
121
* @return this builder instance
128
122
*/
129
123
public VMConfiguration network (final String netConfig ) {
130
- this .options .put ("net" , netConfig );
131
- return this ;
124
+ return this .option ("net" , netConfig );
132
125
}
133
126
134
127
/**
@@ -138,8 +131,7 @@ public VMConfiguration network(final String netConfig) {
138
131
* @return this builder instance
139
132
*/
140
133
public VMConfiguration cpu (final String model ) {
141
- this .options .put ("cpu" , model );
142
- return this ;
134
+ return this .option ("cpu" , model );
143
135
}
144
136
145
137
/**
@@ -149,8 +141,7 @@ public VMConfiguration cpu(final String model) {
149
141
* @return this builder instance
150
142
*/
151
143
public VMConfiguration machine (final String machineType ) {
152
- this .options .put ("machine" , machineType );
153
- return this ;
144
+ return this .option ("machine" , machineType );
154
145
}
155
146
156
147
/**
@@ -161,10 +152,10 @@ public VMConfiguration machine(final String machineType) {
161
152
*/
162
153
public VMConfiguration kvm (final boolean enable ) {
163
154
if (enable ) {
164
- this .options .put ("enable-kvm" , "" );
165
- } else {
166
- this .options .remove ("enable-kvm" );
155
+ return this .flag ("enable-kvm" );
167
156
}
157
+ // Remove the flag if it exists
158
+ this .arguments .removeIf (arg -> arg .equals ("-enable-kvm" ));
168
159
return this ;
169
160
}
170
161
@@ -175,8 +166,7 @@ public VMConfiguration kvm(final boolean enable) {
175
166
* @return this builder instance
176
167
*/
177
168
public VMConfiguration display (final String displayType ) {
178
- this .options .put ("display" , displayType );
179
- return this ;
169
+ return this .option ("display" , displayType );
180
170
}
181
171
182
172
/**
@@ -187,10 +177,10 @@ public VMConfiguration display(final String displayType) {
187
177
*/
188
178
public VMConfiguration graphics (final boolean enable ) {
189
179
if (!enable ) {
190
- this .options .put ("nographic" , "" );
191
- } else {
192
- this .options .remove ("nographic" );
180
+ return this .flag ("nographic" );
193
181
}
182
+ // Remove the flag if it exists
183
+ this .arguments .removeIf (arg -> arg .equals ("-nographic" ));
194
184
return this ;
195
185
}
196
186
@@ -201,8 +191,7 @@ public VMConfiguration graphics(final boolean enable) {
201
191
* @return this builder instance
202
192
*/
203
193
public VMConfiguration audio (final String driver ) {
204
- this .options .put ("audio" , driver );
205
- return this ;
194
+ return this .option ("audio" , driver );
206
195
}
207
196
208
197
/**
@@ -222,8 +211,7 @@ public VMConfiguration diskSize(final int size) {
222
211
* @return this builder instance
223
212
*/
224
213
public VMConfiguration diskSize (final String size ) {
225
- this .options .put ("drive" , "file=fat:rw:" + size );
226
- return this ;
214
+ return this .option ("drive" , "file=fat:rw:" + size );
227
215
}
228
216
229
217
/**
@@ -234,7 +222,13 @@ public VMConfiguration diskSize(final String size) {
234
222
* @return this builder instance
235
223
*/
236
224
public VMConfiguration option (final String key , final String value ) {
237
- this .options .put (key , value );
225
+ this .arguments .removeIf (arg -> arg .equals ("-" + key ));
226
+ final int valueIndex = this .arguments .indexOf ("-" + key ) + 1 ;
227
+ if (valueIndex > 0 && valueIndex < this .arguments .size ()) {
228
+ this .arguments .remove (valueIndex );
229
+ }
230
+ this .arguments .add ("-" + key );
231
+ this .arguments .add (value );
238
232
return this ;
239
233
}
240
234
@@ -245,17 +239,19 @@ public VMConfiguration option(final String key, final String value) {
245
239
* @return this builder instance
246
240
*/
247
241
public VMConfiguration flag (final String flag ) {
248
- this .options .put (flag , "" );
242
+ if (!this .arguments .contains ("-" + flag )) {
243
+ this .arguments .add ("-" + flag );
244
+ }
249
245
return this ;
250
246
}
251
247
252
248
/**
253
- * Returns an unmodifiable view of the configuration options .
249
+ * Returns an unmodifiable view of the configuration arguments .
254
250
*
255
- * @return the configured options
251
+ * @return the configured arguments list
256
252
*/
257
- public Map <String , String > getOptions () {
258
- return Collections .unmodifiableMap (this .options );
253
+ public List <String > getArguments () {
254
+ return Collections .unmodifiableList (this .arguments );
259
255
}
260
256
261
257
/**
@@ -264,16 +260,6 @@ public Map<String, String> getOptions() {
264
260
* @return a string array of QEMU arguments
265
261
*/
266
262
public String [] buildArgs () {
267
- final List <String > args = new ArrayList <>();
268
- final Set <Map .Entry <@ KeyFor ("this.options" ) String , String >> entries = this .options .entrySet ();
269
- for (final Map .Entry <String , String > entry : entries ) {
270
- final String key = entry .getKey ();
271
- final String value = entry .getValue ();
272
- args .add ("-" + key );
273
- if (!value .isEmpty ()) {
274
- args .add (value );
275
- }
276
- }
277
- return args .toArray (new String [0 ]);
263
+ return this .arguments .toArray (new String [0 ]);
278
264
}
279
265
}
0 commit comments