/ Utils.java
Utils.java
 1  import java.io.IOException;
 2  
 3  /// Utils - Utility functions
 4  ///
 5  /// SPDX-License-Identifier: GPL-3.0-or-later
 6  /// SPDX-FileCopyrightText: 2024 Jonas Smedegaard <dr@jones.dk>
 7  ///
 8  /// * v0.0.1
 9  ///   * initial release, derived frop delivery "assignment 6"
10  ///
11  /// @version 0.0.1
12  /// @see <https://app.radicle.xyz/nodes/ash.radicle.garden/rad:z3YzABgyz2D36LiKe3YcdJ6PcDCXM/tree/README.md>
13  /// @see <https://moodle.ruc.dk/course/section.php?id=201713>
14  final class Utils {
15  
16  	/// Dummy unused constructor to silence javadoc
17  	private Utils() { }
18  
19  	/// Check if a character is a printable ASCII character
20  	///
21  	/// @param ch  Character to test
22  	/// @return    test result as boolean
23  	public static boolean isPrintableASCII(final char ch) {
24  		return ch >= 32 && ch <= 126;
25  	}
26  
27  	/// Get relative position of a point from another point
28  	///
29  	/// @param pointA  a point as integer array
30  	/// @param pointB  another point for baseline as integer array
31  	/// @return        coordinates as integer array
32  	public static int[] deltaPosition(final int[] pointA, final int[] pointB) {
33  		if (!fitsDimensions(pointA) || !fitsDimensions(pointB)) {
34  			throw new IllegalArgumentException(
35  				"grid system mismatch"
36  			);
37  		}
38  		int[] pos = new int[pointA.length];
39  		for (int i = 0; i < pointA.length; i++) {
40  			pos[i] = pointA[i] - pointB[i];
41  		}
42  		return pos;
43  	}
44  
45  	/// Check if has supported dimension size
46  	///
47  	/// @param point  a point as integer array
48  	/// @return        test result as boolean
49  	public static boolean fitsDimensions(final int[] point) {
50  		return point != null && point.length == 2;
51  	}
52  
53  	/// Try to clear the console
54  	///
55  	/// @see <https://stackoverflow.com/a/64038023/18619283>
56  	public static void clearConsole() {
57  		try {
58  			if (System.getProperty("os.name").contains("Windows")) {
59  				new ProcessBuilder("cmd", "/c", "cls")
60  					.inheritIO().start().waitFor();
61  			} else {
62  				System.out.print("\033\143");
63  				System.out.flush();
64  			}
65  		} catch (IOException | InterruptedException ex) { }
66  	}
67  }