Dave Jarvis' Repositories

git clone https://repo.autonoma.ca/repo/keenwrite.git

Add debug command-line option

AuthorDaveJarvis <email>
Date2021-11-09 21:27:40 GMT-0800
Commit732224af996547b7cccc80d79447bd368e07393e
Parentc099cdf
Delta73 lines added, 0 lines removed, 73-line increase
src/main/java/com/keenwrite/util/ArrayScanner.java
+/* Copyright 2020-2021 White Magic Software, Ltd. -- All rights reserved. */
+package com.keenwrite.util;
+
+import static java.lang.Math.max;
+
+/**
+ * Scans an array (haystack) for a particular value (needle).
+ *
+ * <p>
+ * This class is {@code null}-hostile.
+ */
+public class ArrayScanner {
+
+ /**
+ * The index value returned when an element is not found in an array.
+ */
+ public static final int MISSING = -1;
+
+ /**
+ * Finds the index of the given needle in the haystack.
+ *
+ * @param haystack The haystack to search through for the needle.
+ * @param needle The needle to find in the haystack.
+ * @return Index of the needle within the haystack, or {@link #MISSING}
+ * if not found.
+ */
+ public static int indexOf( final Object[] haystack, final Object needle ) {
+ assert haystack != null;
+ assert needle != null;
+
+ return indexOf( haystack, needle, 0 );
+ }
+
+ /**
+ * Finds the index of the given needle in the haystack.
+ *
+ * @param haystack The haystack to search through for the needle.
+ * @param needle The needle to find in the haystack.
+ * @param offset The starting offset into the haystack to begin looking
+ * (the value may be greater than or less than the number
+ * of array elements).
+ * @return Index of the needle within the haystack, or {@link #MISSING}
+ * if not found.
+ */
+ public static int indexOf(
+ final Object[] haystack, final Object needle, int offset ) {
+ assert haystack != null;
+ assert needle != null;
+
+ for( int i = max( 0, offset ); i < haystack.length; i++ ) {
+ if( needle.equals( haystack[ i ] ) ) {
+ return i;
+ }
+ }
+
+ return MISSING;
+ }
+
+ /**
+ * Checks if the object is in the given array.
+ *
+ * @param haystack The haystack to search through for the needle.
+ * @param needle The needle to find in the haystack.
+ * @return {@code true} if the array contains the object.
+ */
+ public static boolean contains(
+ final Object[] haystack, final Object needle ) {
+ assert haystack != null;
+ assert needle != null;
+
+ return indexOf( haystack, needle ) != MISSING;
+ }
+}