View Javadoc
1   package io.jawk.util;
2   
3   /*-
4    * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
5    * Jawk
6    * ჻჻჻჻჻჻
7    * Copyright (C) 2006 - 2026 MetricsHub
8    * ჻჻჻჻჻჻
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation, either version 3 of the
12   * License, or (at your option) any later version.
13   *
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Lesser Public License for more details.
18   *
19   * You should have received a copy of the GNU General Lesser Public
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
22   * ╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱
23   */
24  
25  import java.util.HashMap;
26  import java.util.Locale;
27  import java.util.Map;
28  import java.util.Objects;
29  import java.util.concurrent.atomic.AtomicLong;
30  import io.jawk.Awk;
31  
32  /**
33   * Reusable behavioral configuration for the Jawk engine.
34   * <p>
35   * Instances hold settings that control how the AWK interpreter behaves
36   * (field separator, locale, sorted keys, initial variables, and default
37   * record separator) but do <em>not</em> carry per-execution state such as
38   * input sources, filename arguments, or output destinations. This
39   * separation allows a single {@code AwkSettings} object to be shared
40   * across many invocations of {@link io.jawk.Awk#eval},
41   * {@link io.jawk.Awk#script}, or {@link io.jawk.Awk#createAvm()}.
42   * </p>
43   * <p>
44   * Output is configured on the execution builder returned by
45   * {@link io.jawk.Awk#script(String)} or {@link io.jawk.Awk#script(io.jawk.AwkProgram)}
46   * and is therefore always per-execution.
47   * </p>
48   *
49   * @author Danny Daglas
50   */
51  public class AwkSettings {
52  
53  	/**
54  	 * Shared immutable settings instance representing the default configuration.
55  	 */
56  	public static final AwkSettings DEFAULT_SETTINGS = new ImmutableAwkSettings();
57  
58  	/**
59  	 * Contains variable assignments which are applied prior to
60  	 * executing the script (-v assignments).
61  	 * The values may be of type <code>Integer</code>,
62  	 * <code>Double</code>, <code>String</code>,
63  	 * {@link io.jawk.jrt.AssocArray} (for array variables),
64  	 * any {@link java.util.Map} that Jawk exposes directly to the script,
65  	 * or any {@link java.util.List} that Jawk materializes as an array with
66  	 * zero-based {@link java.lang.Long} keys.
67  	 * <p>
68  	 * When a {@link java.util.Map} is provided, the Jawk runtime may mutate it
69  	 * during execution. Callers must therefore supply a mutable map
70  	 * implementation. Numeric indices written by the runtime into such maps and
71  	 * indices derived from {@link java.util.List} values use
72  	 * {@link java.lang.Long} keys (for example, <code>0L</code>,
73  	 * <code>1L</code>, ...).
74  	 * </p>
75  	 */
76  	private final Map<String, Object> variables = new HashMap<String, Object>();
77  
78  	/**
79  	 * Initial Field Separator (FS) value.
80  	 * <code>null</code> means the default FS value.
81  	 */
82  	private volatile String fieldSeparator = null;
83  
84  	/**
85  	 * Whether to maintain array keys in sorted order;
86  	 * <code>false</code> by default.
87  	 */
88  	private volatile boolean useSortedArrayKeys = false;
89  
90  	/**
91  	 * Whether to accept gawk-style arrays of arrays syntax such as {@code a[i][j]}
92  	 * and subarray operands in array-only positions such as {@code split(..., a[i])}.
93  	 * <code>true</code> by default.
94  	 */
95  	private volatile boolean allowArraysOfArrays = true;
96  
97  	/**
98  	 * Locale for the output of numbers
99  	 * <code>US-English</code> by default.
100 	 */
101 	private volatile Locale locale = Locale.US;
102 
103 	/**
104 	 * Default value for RS, when not set specifically by the AWK script.
105 	 * Defaults to {@link Awk#DEFAULT_RS} per POSIX. Platform-specific
106 	 * end-of-line handling is the responsibility of the input source.
107 	 */
108 	private volatile String defaultRS = Awk.DEFAULT_RS;
109 
110 	/**
111 	 * Monotonically increasing counter incremented whenever the settings change.
112 	 * It allows callers that cache derived runtime state to detect when a new
113 	 * snapshot must be built.
114 	 */
115 	private final AtomicLong modificationCount = new AtomicLong();
116 
117 	/**
118 	 * <p>
119 	 * toDescriptionString.
120 	 * </p>
121 	 *
122 	 * @return a human readable representation of the parameters values.
123 	 */
124 	public String toDescriptionString() {
125 		StringBuilder desc = new StringBuilder();
126 
127 		final char newLine = '\n';
128 
129 		desc.append("variables = ").append(getVariables()).append(newLine);
130 		desc.append("fieldSeparator = ").append(getFieldSeparator()).append(newLine);
131 		desc.append("useSortedArrayKeys = ").append(isUseSortedArrayKeys()).append(newLine);
132 		desc.append("allowArraysOfArrays = ").append(isAllowArraysOfArrays()).append(newLine);
133 		return desc.toString();
134 	}
135 
136 	/**
137 	 * Provides a description of extensions that are enabled/disabled.
138 	 * The default compiler implementation uses this method
139 	 * to describe extensions which are compiled into the script.
140 	 * The description is then provided to the user within the usage.
141 	 *
142 	 * @return A description of the extensions which are enabled/disabled.
143 	 */
144 	public String toExtensionDescription() {
145 		StringBuilder extensions = new StringBuilder();
146 
147 		if (isUseSortedArrayKeys()) {
148 			extensions.append(", associative array keys are sorted");
149 		}
150 		if (isAllowArraysOfArrays()) {
151 			extensions.append(", arrays of arrays");
152 		}
153 		if (extensions.length() > 0) {
154 			return "{extensions: " + extensions.substring(2) + "}";
155 		} else {
156 			return "{no compiled extensions utilized}";
157 		}
158 	}
159 
160 	@SuppressWarnings("unused")
161 	private void addInitialVariable(String keyValue) {
162 		int equalsIdx = keyValue.indexOf('=');
163 		String name = keyValue.substring(0, equalsIdx);
164 		String valueString = keyValue.substring(equalsIdx + 1);
165 		Object value;
166 		// deduce type
167 		try {
168 			value = Integer.parseInt(valueString);
169 		} catch (NumberFormatException nfe) {
170 			try {
171 				value = Double.parseDouble(valueString);
172 			} catch (NumberFormatException nfe2) {
173 				value = valueString;
174 			}
175 		}
176 		// note: can overwrite previously defined variables
177 		putVariable(name, value);
178 	}
179 
180 	/**
181 	 * Contains variable assignments which are applied prior to
182 	 * executing the script (-v assignments).
183 	 * The values may be of type <code>Integer</code>,
184 	 * <code>Double</code>, <code>String</code>,
185 	 * {@link io.jawk.jrt.AssocArray} (for array variables),
186 	 * any {@link java.util.Map} that Jawk exposes directly to the script,
187 	 * or any {@link java.util.List} that Jawk materializes as an array with
188 	 * zero-based {@link java.lang.Long} keys.
189 	 *
190 	 * @return the variables
191 	 */
192 	public Map<String, Object> getVariables() {
193 		synchronized (variables) {
194 			return new HashMap<String, Object>(variables);
195 		}
196 	}
197 
198 	/**
199 	 * Returns the number of explicit mutations applied to this settings
200 	 * instance.
201 	 * <p>
202 	 * The value is intended for cache invalidation only; it has no behavioral
203 	 * meaning other than changing whenever one of the configuration mutators is
204 	 * called.
205 	 * </p>
206 	 *
207 	 * @return the current modification counter
208 	 */
209 	public long getModificationCount() {
210 		return modificationCount.get();
211 	}
212 
213 	/**
214 	 * Contains variable assignments which are applied prior to
215 	 * executing the script (-v assignments).
216 	 * The values may be of type <code>Integer</code>,
217 	 * <code>Double</code>, <code>String</code>,
218 	 * {@link io.jawk.jrt.AssocArray} (for array variables),
219 	 * any {@link java.util.Map} that Jawk exposes directly to the script,
220 	 * or any {@link java.util.List} that Jawk materializes as an array with
221 	 * zero-based {@link java.lang.Long} keys.
222 	 *
223 	 * @param variables the variables to set
224 	 */
225 	public void setVariables(Map<String, Object> variables) {
226 		synchronized (this.variables) {
227 			this.variables.clear();
228 			this.variables.putAll(variables);
229 		}
230 		markModified();
231 	}
232 
233 	/**
234 	 * Put or replace a variable entry.
235 	 *
236 	 * @param name Variable name
237 	 * @param value Variable value
238 	 */
239 	public void putVariable(String name, Object value) {
240 		synchronized (variables) {
241 			variables.put(name, value);
242 		}
243 		markModified();
244 	}
245 
246 	/**
247 	 * Initial Field Separator (FS) value.
248 	 * <code>null</code> means the default FS value.
249 	 *
250 	 * @return the fieldSeparator
251 	 */
252 	public String getFieldSeparator() {
253 		return fieldSeparator;
254 	}
255 
256 	/**
257 	 * Initial Field Separator (FS) value.
258 	 * <code>null</code> means the default FS value.
259 	 *
260 	 * @param fieldSeparator the fieldSeparator to set
261 	 */
262 	public void setFieldSeparator(String fieldSeparator) {
263 		this.fieldSeparator = fieldSeparator;
264 		markModified();
265 	}
266 
267 	/**
268 	 * Whether to maintain array keys in sorted order;
269 	 * <code>false</code> by default.
270 	 *
271 	 * @return the useSortedArrayKeys
272 	 */
273 	public boolean isUseSortedArrayKeys() {
274 		return useSortedArrayKeys;
275 	}
276 
277 	/**
278 	 * Whether to maintain array keys in sorted order;
279 	 * <code>false</code> by default.
280 	 *
281 	 * @param useSortedArrayKeys the useSortedArrayKeys to set
282 	 */
283 	public void setUseSortedArrayKeys(boolean useSortedArrayKeys) {
284 		this.useSortedArrayKeys = useSortedArrayKeys;
285 		markModified();
286 	}
287 
288 	/**
289 	 * Whether to accept gawk-style arrays of arrays syntax such as {@code a[i][j]}
290 	 * and subarray operands in array-only positions such as {@code split(..., a[i])}.
291 	 *
292 	 * @return {@code true} when arrays of arrays are enabled at compile time
293 	 */
294 	public boolean isAllowArraysOfArrays() {
295 		return allowArraysOfArrays;
296 	}
297 
298 	/**
299 	 * Enables or disables gawk-style arrays of arrays syntax such as
300 	 * {@code a[i][j]} and subarray operands in array-only positions such as
301 	 * {@code split(..., a[i])} or {@code for (k in a[i])}.
302 	 *
303 	 * @param allowArraysOfArrays {@code true} to accept arrays-of-arrays features
304 	 */
305 	public void setAllowArraysOfArrays(boolean allowArraysOfArrays) {
306 		this.allowArraysOfArrays = allowArraysOfArrays;
307 		markModified();
308 	}
309 
310 	/**
311 	 * <p>
312 	 * Getter for the field <code>locale</code>.
313 	 * </p>
314 	 *
315 	 * @return the Locale that will be used for outputting numbers
316 	 */
317 	public Locale getLocale() {
318 		return locale;
319 	}
320 
321 	/**
322 	 * Sets the Locale for outputting numbers.
323 	 *
324 	 * @param pLocale The locale to be used (e.g.: <code>Locale.US</code>)
325 	 */
326 	public void setLocale(Locale pLocale) {
327 		locale = pLocale == null ? Locale.US : pLocale;
328 		markModified();
329 	}
330 
331 	/**
332 	 * <p>
333 	 * Getter for the field <code>defaultRS</code>.
334 	 * </p>
335 	 *
336 	 * @return the default RS, when not set by the AWK script
337 	 */
338 	public String getDefaultRS() {
339 		return defaultRS;
340 	}
341 
342 	/**
343 	 * Sets the default RS, when not set by the AWK script
344 	 *
345 	 * @param rs The regular expression that separates records
346 	 */
347 	public void setDefaultRS(String rs) {
348 		defaultRS = Objects.requireNonNull(rs, "defaultRS");
349 		markModified();
350 	}
351 
352 	protected final void markModified() {
353 		modificationCount.incrementAndGet();
354 	}
355 
356 	private static final class ImmutableAwkSettings extends AwkSettings {
357 
358 		private ImmutableAwkSettings() {
359 			super();
360 		}
361 
362 		@Override
363 		public void setVariables(Map<String, Object> variables) {
364 			throw unsupported();
365 		}
366 
367 		@Override
368 		public void putVariable(String name, Object value) {
369 			throw unsupported();
370 		}
371 
372 		@Override
373 		public void setFieldSeparator(String fieldSeparator) {
374 			throw unsupported();
375 		}
376 
377 		@Override
378 		public void setUseSortedArrayKeys(boolean useSortedArrayKeys) {
379 			throw unsupported();
380 		}
381 
382 		@Override
383 		public void setAllowArraysOfArrays(boolean allowArraysOfArrays) {
384 			throw unsupported();
385 		}
386 
387 		@Override
388 		public void setLocale(Locale pLocale) {
389 			throw unsupported();
390 		}
391 
392 		@Override
393 		public void setDefaultRS(String rs) {
394 			throw unsupported();
395 		}
396 
397 		private UnsupportedOperationException unsupported() {
398 			return new UnsupportedOperationException("DEFAULT_SETTINGS is immutable");
399 		}
400 	}
401 }