summaryrefslogtreecommitdiff
path: root/src/main/java/bjc/everge/ControlledString.java
blob: f27b3d25adc8d965b4d82f9d0010b8728b61c597 (plain)
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
package bjc.everge;

import java.util.Arrays;

/* @FixMe Ben Culkin Oct. 31, 2020 - :LeadingControl
 * 
 * At the moment, this only parses a single control that is at the start of the
 * string. Should this be improved?
 */
/**
 * Represents a string with a set of control flags attached to it.
 *
 * @author Ben Culkin
 */
public class ControlledString {
	/**
	 * Represents a single control (a key-values pair)
	 *
	 * @author Ben Culkin
	 */
	public static class Control {
		/**
		 * The name of the control.
		 */
		public String name;

		/**
		 * The arguments to the control.
		 */
		public String[] args;

		/**
		 * Create a new blank control.
		 */
		public Control() {

		}

		/**
		 * Create a new argless control.
		 *
		 * @param nam
		 *            The name of the control.
		 */
		public Control(String nam) {
			this();
			
			name = nam;
		}

		/**
		 * Create a new control.
		 *
		 * @param nam
		 *            The name of the control.
		 * @param ars
		 *            The arguments of the control.
		 */
		public Control(String nam, String... ars) {
			this(nam);
			
			args = ars;
		}

		/**
		 * Get the count of arguments this control has.
		 *
		 * @return The number of arguments to this control.
		 */
		public int count() {
			if (args == null) return 0;
			
			return args.length;
		}

		/**
		 * Get an argument from the control.
		 *
		 * @param i
		 *          The index of the argument to get.
		 * @return The argument at that position.
		 */
		public String get(int i) {
			// @Cleanup: I'm pretty sure Java will auto-throw these, so we should
			// remove this stuff. --bculkin, Oct. 31, 2020
			if (i < 0) {
				String msg = String.format(
						"Control argument index must be greater than 0 (was %d)", i);

				throw new IndexOutOfBoundsException(msg);
			}

			if (i > args.length) {
				String msg = String.format(
						"Control argument index must be less than %d (was %d)",
						args.length, i);

				throw new IndexOutOfBoundsException(msg);
			}

			return args[i];
		}

		@Override
		public String toString() {
			StringBuilder sb = new StringBuilder();
			sb.append(name);

			if (args != null && args.length > 0) {
				sb.append("/");

				for (String arg : args) {
					sb.append(arg);
					sb.append(";");
				}
			}

			return sb.toString();
		}

		@Override
		public int hashCode() {
			final int prime = 31;
			int result = 1;
			result = prime * result + Arrays.hashCode(args);
			result = prime * result + ((name == null) ? 0 : name.hashCode());
			return result;
		}

		@Override
		public boolean equals(Object obj) {
			if (this == obj) {
				return true;
			}
			if (obj == null) {
				return false;
			}
			if (getClass() != obj.getClass()) {
				return false;
			}

			Control other = (Control) obj;

			if (name == null) {
				if (other.name != null) {
					return false;
				}
			} else if (!name.equals(other.name)) {
				return false;
			}

			boolean isArged = args != null && args.length > 0;
			boolean oIsArged = other.args != null && other.args.length > 0;

			if (isArged && !oIsArged) {
				return false;
			}
			if (!isArged && oIsArged) {
				return false;
			}

			if (isArged && oIsArged) {
				return Arrays.equals(args, other.args);
			}

			return true;
		}

		/**
		 * Convenient static constructor for static imports.
		 *
		 * @param nam
		 *            The name of the control.
		 * @param ars
		 *            The arguments to the control.
		 * @return A control with the right parameters.
		 */
		public static Control C(String nam, String... ars) {
			return new Control(nam, ars);
		}
	}

	/**
	 * Parameter class for defining how to parse a ControlledString.
	 *
	 * @author Ben Culkin
	 */
	public static class ControlledStringParseOptions {
		/**
		 * The indicator for separating controls from the regular string.
		 */
		public String controlIndicator;

		/**
		 * The indicator for separating individual controls.
		 */
		public String controlSeparator;

		/**
		 * The indicator for separating arguments to a control.
		 */
		public String controlArgumentSeparator;

		/**
		 * The indicator for escaping any of the indicators (including itself)
		 */
		public String controlEscape;

		/**
		 * Create a new set of parse strings.
		 *
		 * @param controlIndicator
		 *                The control indicator.
		 * @param controlSeparator
		 *                The control separator.
		 * @param controlArgumentSeparator
		 *                The argument separator.
		 * @param controlEscape
		 *                The control escape.
		 */
		public ControlledStringParseOptions(String controlIndicator,
				String controlSeparator, String controlArgumentSeparator,
				String controlEscape) {
			this.controlIndicator = controlIndicator;
			this.controlSeparator = controlSeparator;
			this.controlArgumentSeparator = controlArgumentSeparator;
			this.controlEscape = controlEscape;
		}

		/**
		 * Convenient static constructor.
		 *
		 * @param controlIndicator
		 *                The control indicator.
		 * @param controlSeparator
		 *                The control separator.
		 * @param controlArgumentSeparator
		 *                The argument separator.
		 * @param controlEscape
		 *                The control escape.
		 * @return A new set of control strings.
		 */
		public static ControlledStringParseOptions CSPS(String controlIndicator,
				String controlSeparator, String controlArgumentSeparator,
				String controlEscape) {
			return new ControlledStringParseOptions(controlIndicator, controlSeparator, controlArgumentSeparator, controlEscape);
		}
	}

	/**
	 * The string the controls apply to.
	 */
	public String body;

	/**
	 * The controls that apply to the string.
	 */
	// @NOTE Why is this an array? Would it make more sense for it to be a List
	// of some sort? --bculkin, Oct 31, 2020
	public Control[] controls;

	/**
	 * Create a new blank controlled string.
	 */
	public ControlledString() {
		controls = new Control[0];
	}

	/**
	 * Create a new controlled string without any controls.
	 *
	 * @param strung
	 *               The string to use.
	 */
	public ControlledString(String strung) {
		this();
		
		body = strung;
	}

	/**
	 * Create a new controlled string.
	 *
	 * @param strung
	 *                 The string to use.
	 * @param controls
	 *                 The controls that apply to the string.
	 */
	public ControlledString(String strung, Control... controls) {
		this(strung);

		this.controls = controls;
	}

	/**
	 * Check if the string has controls.
	 *
	 * @return Whether or not the string has controls.
	 */
	public boolean hasControls() {
		return controls.length > 0;
	}

	/**
	 * Get the count of controls.
	 *
	 * @return The number of controls for this string.
	 */
	public int count() {
		return controls.length;
	}

	/**
	 * Parse a controlled string from a regular string.
	 *
	 * The controls must be parsed from the beginning of the string.
	 *
	 * @param lne
	 *                The string to parse from.
	 * @param strangs
	 *                The object to read the strings from
	 * @return A parsed control string.
	 */
	public static ControlledString parse(String lne, ControlledStringParseOptions strangs) {
		if (!lne.startsWith(strangs.controlIndicator)) return new ControlledString(lne);

		// Split off initial control
		String[] controlIntervals = StringUtils.escapeSplit(strangs.controlEscape, strangs.controlIndicator, lne);

		if (controlIntervals.length < 2) {
			String msg = "Did not find control terminator (%s) where it should be";
			msg = String.format(msg, strangs.controlIndicator);

			throw new IllegalArgumentException(msg);
		}

		ControlledString controlString = new ControlledString(controlIntervals[0]);
		/* :LeadingControl
		 * ... Is this even correct? It would seem that we are discarding any
		 * text that came before the control.
		 * 
		 * Ideally, what we would want to do is concatenate any non-control text,
		 * and then process each control interval by itself.
		 */
		if (controlIntervals.length > 2) controlString.body = controlIntervals[2];

		// Split the individual controls from the string
		String[] unparsedControls = StringUtils.escapeSplit(strangs.controlEscape,
				strangs.controlSeparator, controlIntervals[1]);
		controlString.controls = new Control[unparsedControls.length];

		for (int i = 0; i < unparsedControls.length; i++) {
			String controlText = unparsedControls[i];

			// Get the control arguments
			String[] controlArguments
					= StringUtils.escapeSplit(strangs.controlEscape, strangs.controlArgumentSeparator, controlText);

			Control control = new Control(controlArguments[0]);

			if (control.name.length() > 1) {
				// Only single-character controls can be lower-case
				control.name = control.name.toUpperCase();
			}

			if (controlArguments.length > 1) {
				control.args = new String[controlArguments.length - 1];
				for (int j = 1; j < controlArguments.length; j++) {
					control.args[j - 1] = controlArguments[j];
				}
			}

			controlString.controls[i] = control;
		}

		return controlString;
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();

		sb.append("//");

		for (Control control : controls) sb.append(control);

		sb.append("//");
		sb.append(body);

		return sb.toString();
	}
}