Package Torello.Java
Class StrCSV
- java.lang.Object
-
- Torello.Java.StrCSV
-
public class StrCSV extends java.lang.Object
A utility that builds Comma Separated Value String's (and can parse them as well).
The routines in this class may seem trivial, and they are, but they are geared largely for use in testing, debugging and also use in methods like'toString()'
. Not having to re-write these loops over and over again anytime an array needs to be printed can make such situations mentioned a lot easier. Though the methods in this class have level of complexity that is no where near those in'StrReplace'
or even'StrCmpr'
, they still can be very handy for myriad of places when the contents of array need to be printed or inspected quickly - (usually on a single line of text).
This class' methods, essentially, convert arrays and other lists into a longjava.lang.String
such that each element of theString
is separated by a comma. These routines are also capable of parsing aString
whose elements are separated by commas, back into an array.
Hi-Lited Source-Code:- View Here: Torello/Java/StrCSV.java
- Open New Browser-Tab: Torello/Java/StrCSV.java
File Size: 84,649 Bytes Line Count: 2,028 '\n' Characters Found
Stateless Class:This class neither contains any program-state, nor can it be instantiated. The@StaticFunctional
Annotation may also be called 'The Spaghetti Report'.Static-Functional
classes are, essentially, C-Styled Files, without any constructors or non-static member fields. It is a concept very similar to the Java-Bean's@Stateless
Annotation.
- 1 Constructor(s), 1 declared private, zero-argument constructor
- 35 Method(s), 35 declared static
- 1 Field(s), 1 declared static, 1 declared final
-
-
Method Summary
Parse CSV Modifier and Type Method static String[]
CSV(String s)
static String[]
CSV(String s, boolean performTrim, boolean eliminateZeroLengthStrings)
Generate CSV Modifier and Type Method static String
toCSV(Iterable<?> i, boolean trim, boolean printNulls, Integer maxLength)
static <T> String
toCSV(Iterable<T> i, Function<? super T,String> toString, boolean printNulls, Integer maxLength)
static String
toCSV(String[] sArr, boolean trim, boolean printNulls, Integer maxLength)
static <T> String
toCSV(T[][] tArr, IntIntTFunc<? super T,String> toString, IntPredicate keepRow, boolean separateLines, Integer maxLengthInner, Integer maxLengthOuter)
static <T> String
toCSV(T[] tArr, boolean trim, boolean printNulls, Integer maxLength)
static <T> String
toCSV(T[] tArr, IntTFunction<? super T,String> toString, boolean printNulls, Integer maxLength)
Generate CSV, Range-Limited Modifier and Type Method static <T> String
toCSV(T[] tArr, int sPos, int ePos, boolean trim, boolean printNulls, Integer maxLength)
static <T> String
toCSV(T[] tArr, int sPos, int ePos, IntTFunction<? super T,String> toString, boolean printNulls, Integer maxLength)
Generate CSV, Primitive-Array Modifier and Type Method static String
toCSV(boolean[] arr, IntBoolFunction<String> toString, Integer maxLength)
static String
toCSV(byte[] arr, IntByteFunction<String> toString, Integer maxLength)
static String
toCSV(char[] arr, IntCharFunction<String> toString, Integer maxLength)
static String
toCSV(double[] arr, IntDoubleFunction<String> toString, Integer maxLength)
static String
toCSV(float[] arr, IntFloatFunction<String> toString, Integer maxLength)
static String
toCSV(int[] arr, BiIntFunction<String> toString, Integer maxLength)
static String
toCSV(long[] arr, IntLongFunction<String> toString, Integer maxLength)
static String
toCSV(short[] arr, IntShortFunction<String> toString, Integer maxLength)
Generate CSV, Primitive-Array, Range-Limited Modifier and Type Method static String
toCSV(boolean[] arr, int sPos, int ePos, IntBoolFunction<String> toString, Integer maxLength)
static String
toCSV(byte[] arr, int sPos, int ePos, IntByteFunction<String> toString, Integer maxLength)
static String
toCSV(char[] arr, int sPos, int ePos, IntCharFunction<String> toString, Integer maxLength)
static String
toCSV(double[] arr, int sPos, int ePos, IntDoubleFunction<String> toString, Integer maxLength)
static String
toCSV(float[] arr, int sPos, int ePos, IntFloatFunction<String> toString, Integer maxLength)
static String
toCSV(int[] arr, int sPos, int ePos, BiIntFunction<String> toString, Integer maxLength)
static String
toCSV(long[] arr, int sPos, int ePos, IntLongFunction<String> toString, Integer maxLength)
static String
toCSV(short[] arr, int sPos, int ePos, IntShortFunction<String> toString, Integer maxLength)
Generate CSV, 2-D Primitive-Array Modifier and Type Method static String
toCSV(boolean[][] arr, IntIntBoolFunc<String> toString, IntPredicate keepRow, boolean separateLines, Integer maxLengthInner, Integer maxLengthOuter)
static String
toCSV(byte[][] arr, IntIntByteFunc<String> toString, IntPredicate keepRow, boolean separateLines, Integer maxLengthInner, Integer maxLengthOuter)
static String
toCSV(char[][] arr, IntIntCharFunc<String> toString, IntPredicate keepRow, boolean separateLines, Integer maxLengthInner, Integer maxLengthOuter)
static String
toCSV(double[][] arr, IntIntDoubleFunc<String> toString, IntPredicate keepRow, boolean separateLines, Integer maxLengthInner, Integer maxLengthOuter)
static String
toCSV(float[][] arr, IntIntFloatFunc<String> toString, IntPredicate keepRow, boolean separateLines, Integer maxLengthInner, Integer maxLengthOuter)
static String
toCSV(int[][] arr, TriIntFunc<String> toString, IntPredicate keepRow, boolean separateLines, Integer maxLengthInner, Integer maxLengthOuter)
static String
toCSV(long[][] arr, IntIntLongFunc<String> toString, IntPredicate keepRow, boolean separateLines, Integer maxLengthInner, Integer maxLengthOuter)
static String
toCSV(short[][] arr, IntIntShortFunc<String> toString, IntPredicate keepRow, boolean separateLines, Integer maxLengthInner, Integer maxLengthOuter)
-
-
-
Method Detail
-
CSV
public static java.lang.String[] CSV(java.lang.String s)
-
CSV
public static java.lang.String[] CSV(java.lang.String s, boolean performTrim, boolean eliminateZeroLengthStrings)
This will return a list ofString
that are in-between each-and-every comma that is found inside the parameter-String
's'
Java Stream API:
This method uses Java 8 or 9'spackage java.util.stream.*
. If this package is not familiar, it is usually just a way (after some practice), of (sort-of) converting for-loops into more readable method-calls. Java-Streams instead substitute words such as:'filter', 'map', 'forEach'
and'toArray'
. This method's code is nothing more than that.- Parameters:
s
- This accepts any java-String
, but it is expecting one that contains commas.performTrim
- If this parameter is set toTRUE
, then allString's
will be trimmed of white-space before being placed in the returnedString
-array, by calling Java'sString.trim()
method.eliminateZeroLengthStrings
- If this parameter is set toTRUE
, then allString's
that have zero-length will be eliminated from the returnedString[]
array.
NOTE: Regardless of whether or not atrim()
operation was performed, allString's
that are trimmed of white-space, would have the'trim'
done before the'eliminate'
operation.- Returns:
- This will return the individual
String's
from a larger-String
that contained comma-separated values. - Code:
- Exact Method Body:
Stream<String> stream = StringParse.COMMA_REGEX.splitAsStream(s).filter((String csv) -> csv != null); if (performTrim) stream = stream.map((String csv) -> csv.trim()); if (eliminateZeroLengthStrings) stream = stream.filter((String csv) -> csv.length() > 0); return stream.toArray(String[]::new);
-
toCSV
public static java.lang.String toCSV(java.lang.String[] sArr, boolean trim, boolean printNulls, java.lang.Integer maxLength)
Convenience Method
Invokes:toCSV(Iterable, boolean, boolean, Integer)
Converts:String[] Array
toList<String>
.
-
toCSV
public static java.lang.String toCSV(java.lang.Iterable<?> i, boolean trim, boolean printNulls, java.lang.Integer maxLength)
This method will turn the elements of any javaIterable
type into ajava.lang.String
. The returnedString
shall have the individual elements of parameter'i'
converted toString's
, each separated by a comma.- Parameters:
i
- Any JavaIterable
. The JavaObject.toString()
will be invoked on each of the elements produced by theIterable
, and commas shall be inserted between each element.
NOTE: The symbols<?>
appended to the (almost) 'raw-type' here, are only there to prevent the java-compiler from issuing warnings regarding the use of "Raw Types." This warning is, actually, only issued if the command-line option-Xlint:all
option is used.trim
- This is a boolean, and when setTRUE
, theString.trim()
shall be invoked on eachString
inserted into the CSV list before insertion.printNulls
- This is a boolean, and when setTRUE
, each object returned by the iterator shall be checked for a 'null' value before insertion into the output-String
- to avoid null-pointer exceptions. Instead the four-characterString
'null' will be inserted instead of throwing this exception.
When this parameter receivesFALSE
, if the input parameterIterable<?> i
contains a null value, then this method will simply throw aNullPointerException
.maxLength
- Allows a user request that the returnedString
have length no longer than'maxLength'
characters. If the final-computed CSV-String
is longer than this parameter's value, it will be truncated tomaxLength - 4
, and' ...'
will be appended, instead, to the end of the return-String
.
NOTE: This parameter may be null, or negative, and if it is, it shall simply be ignored, and the fully-computed CSV-String
returned. Furthermore, don't forget that Java Compiler syntax rules allow a primitive-'int'
variable or literal to be passed to method-parameters of typejava.lang.Integer
. For example'80'
could be passed to this parameter, directly, instead of'Integer.valueOf(80)'
. (Againnull
may also be passed.)- Returns:
- This will return a CSV
String
containing the individual elements of the inputIterable
parameter'i'
, where each element has been converted to aString
and is separated by a comma. - Throws:
java.lang.NullPointerException
- If theIterable
returns a null value, and the'printNulls'
parameter were set toFALSE
, then this method would throw an exception.- Code:
- Exact Method Body:
StringBuilder sb = new StringBuilder(); boolean first = true; if (trim && printNulls) for (Object o : i) { if (o == null) o = "null"; sb.append((first ? "" : ", ") + o.toString().trim()); first = false; } else if (trim && (! printNulls)) for (Object o : i) { sb.append((first ? "" : ", ") + o.toString().trim()); first = false; } else if ((! trim) && printNulls) for (Object o : i) { if (o == null) o = "null"; sb.append((first ? "" : ", ") + o.toString()); first = false; } else // !trim !printNulls for (Object o : i) { sb.append((first ? "" : ", ") + o.toString()); first = false; } if ((maxLength != null) && (sb.length() > maxLength.intValue())) { sb.setLength(maxLength - 4); sb.append(" ..."); return sb.toString(); } return sb.toString();
-
toCSV
public static <T> java.lang.String toCSV (java.lang.Iterable<T> i, java.util.function.Function<? super T,java.lang.String> toString, boolean printNulls, java.lang.Integer maxLength)
This method will turn the elements of any javaIterable
type into ajava.lang.String
. The returnedString
shall have the individual elements of parameter'i'
converted toString's
, each separated by a comma.- Type Parameters:
T
- The type used by thejava.lang.Iterable
. The'toString'
parameter / function-pointer also must accept this type, or a super-type.- Parameters:
i
- Any JavaIterable
. The functional-interface parameter'toString'
will be invoked on each of the elements produced by theIterable
, and commas shall be inserted between each element.toString
- This instance ofjava.util.function.Function<A, B>
must have a method that accepts a parameter having type'T'
, and returns aString
. This is simply an "over-riding" of Java's basic'toString()'
method. In fact, if the class that is being used for variable-type parameter'T'
has a'toString'
method that is sufficient or "good enough", then this method should not be used, but rather the simpler method:toCSV(Iterable, boolean, boolean, Integer)
.printNulls
- When this parameter isTRUE
, the'toString.apply(T)'
method shall receive a null value, and theString
results returned by this method shall be inserted into the outputString
when the inputIterable 'i'
contains a null value. Thusly, the behavior of this method for 'nulls' in the input'i'
parameter should be no different than any other value found within the inputIterable 'i'
.
When this parameter receivesFALSE
, any time a null value is encountered from the inputIterable<T> 'i'
, that value shall be skipped and the outputString
that is returned will have one fewer element in it's CSV list.maxLength
- Allows a user request that the returnedString
have length no longer than'maxLength'
characters. If the final-computed CSV-String
is longer than this parameter's value, it will be truncated tomaxLength - 4
, and' ...'
will be appended, instead, to the end of the return-String
.
NOTE: This parameter may be null, or negative, and if it is, it shall simply be ignored, and the fully-computed CSV-String
returned. Furthermore, don't forget that Java Compiler syntax rules allow a primitive-'int'
variable or literal to be passed to method-parameters of typejava.lang.Integer
. For example'80'
could be passed to this parameter, directly, instead of'Integer.valueOf(80)'
. (Againnull
may also be passed.)- Returns:
- This will return a CSV
String
containing the individual elements of the inputIterable
parameter'i'
, where each element has been converted to aString
- using the provided'toString'
function - and is separated by a comma. - Code:
- Exact Method Body:
StringBuilder sb = new StringBuilder(); boolean first = true; if (printNulls) for (T t : i) { sb.append((first ? "" : ", ") + toString.apply(t)); first = false; } else for (T t : i) if (t != null) { sb.append((first ? "" : ", ") + toString.apply(t)); first = false; } if ((maxLength != null) && (sb.length() > maxLength.intValue())) { sb.setLength(maxLength - 4); sb.append(" ..."); return sb.toString(); } return sb.toString();
-
toCSV
public static <T> java.lang.String toCSV(T[] tArr, boolean trim, boolean printNulls, java.lang.Integer maxLength)
-
toCSV
public static <T> java.lang.String toCSV(T[] tArr, int sPos, int ePos, boolean trim, boolean printNulls, java.lang.Integer maxLength)
This method will turn the elements of any javaIterable
type into ajava.lang.String
. The returnedString
shall have the individual elements of parameter'i'
converted toString's
, each separated by a comma.- Parameters:
tArr
- Any array type'T'
. Java's'toString'
method will be invoked on each of these elements, and returned in aString
where each element has been separated by a comma.sPos
- This is the (integer)Array
-index that sets a limit for the left-mostArray
-position to inspect/search inside the inputArray
-parameter.
This value is considered 'inclusive' meaning that the element at thisArray
-index will be visited by this method.
NOTE: If this value is negative, or larger than the length of the input-Array
, an exception will be thrown.ePos
- This is the (integer)Array
-index that sets a limit for the right-mostArray
-position to inspect/search inside the inputVector
-parameter.
This value is considered 'exclusive' meaning that the element at thisArray
-index will not be visited by this method.
NOTE: If this value is larger than the length of input theArray
-parameter, an exception will throw.
ALSO: Passing a negative value to this parameter,'ePos'
, will cause its value to be reset to the length of the inputArray
-parameter.trim
- This is a boolean, and when setTRUE
, theString.trim()
shall be invoked on eachString
inserted into the CSV list before insertion.printNulls
- This is a boolean, and when setTRUE
, each instance of'T'
contained by'tArr'
shall be checked for a 'null' value before insertion into the output-String
- to avoid null-pointer exceptions. If a null is found, the four-characterString
'null' will be inserted instead of throwing this exception.
When this parameter receivesFALSE
, if a null value is encountered within the input-parameter arrayT[] tArr
, then this method will, in fact, throw a NullPointerException.maxLength
- Allows a user request that the returnedString
have length no longer than'maxLength'
characters. If the final-computed CSV-String
is longer than this parameter's value, it will be truncated tomaxLength - 4
, and' ...'
will be appended, instead, to the end of the return-String
.
NOTE: This parameter may be null, or negative, and if it is, it shall simply be ignored, and the fully-computed CSV-String
returned. Furthermore, don't forget that Java Compiler syntax rules allow a primitive-'int'
variable or literal to be passed to method-parameters of typejava.lang.Integer
. For example'80'
could be passed to this parameter, directly, instead of'Integer.valueOf(80)'
. (Againnull
may also be passed.)- Returns:
- This will return a CSV
String
containing the individual elements of the input arrayT[] tArr
parameter, where each element shall have been converted to aString
and separated by a comma. - Throws:
java.lang.NullPointerException
- If theIterable
returns a null value, and the'printNulls'
parameter were set toFALSE
, then this method would throw an exception.java.lang.ArrayIndexOutOfBoundsException
- This exception shall be thrown if any of the following are true:- If
'sPos'
is negative, or ifsPos
is greater-than-or-equal-to thelength
of theArray
- If
'ePos'
is zero, or greater than the length of theArray
- If the value of
'sPos'
is a larger integer than'ePos'
. If'ePos'
was negative, it is first reset toArray.length
, before this check is done.
- If
- Code:
- Exact Method Body:
LV l = new LV(tArr, sPos, ePos); StringBuilder sb = new StringBuilder(); boolean first = true; if (trim && printNulls) for (int i=l.start; i < l.end; i++) { if (tArr[i] == null) sb.append((first ? "" : ", ") + "null"); else sb.append((first ? "" : ", ") + tArr[i].toString().trim()); first = false; } else if (trim && (! printNulls)) for (int i=l.start; i < l.end; i++) { sb.append((first ? "" : ", ") + tArr[i].toString().trim()); first = false; } else if ((! trim) && printNulls) for (int i=l.start; i < l.end; i++) { if (tArr[i] == null) sb.append((first ? "" : ", ") + "null"); else sb.append((first ? "" : ", ") + tArr[i].toString()); first = false; } else // !trim !printNulls for (int i=l.start; i < l.end; i++) { sb.append((first ? "" : ", ") + tArr[i].toString()); first = false; } if ((maxLength != null) && (sb.length() > maxLength.intValue())) { sb.setLength(maxLength - 4); sb.append(" ..."); return sb.toString(); } return sb.toString();
-
toCSV
public static <T> java.lang.String toCSV (T[] tArr, IntTFunction<? super T,java.lang.String> toString, boolean printNulls, java.lang.Integer maxLength)
-
toCSV
public static <T> java.lang.String toCSV (T[] tArr, int sPos, int ePos, IntTFunction<? super T,java.lang.String> toString, boolean printNulls, java.lang.Integer maxLength)
Converts an array of a variable-type parameterT[]
to a CSVString
This version of'toCSV'
allows a programmer to define the exact'toString()'
that is used on each object-instance in the provided array. There is a simpler version of this method where the invokationT.toString()
is used instead.- Type Parameters:
T
- The type used by theT[]
-Array. The'toString'
parameter / function-pointer also must accept this type.- Parameters:
tArr
- An array ofObject's
of a given variable-type'T'
sPos
- This is the (integer)Array
-index that sets a limit for the left-mostArray
-position to inspect/search inside the inputArray
-parameter.
This value is considered 'inclusive' meaning that the element at thisArray
-index will be visited by this method.
NOTE: If this value is negative, or larger than the length of the input-Array
, an exception will be thrown.ePos
- This is the (integer)Array
-index that sets a limit for the right-mostArray
-position to inspect/search inside the inputVector
-parameter.
This value is considered 'exclusive' meaning that the element at thisArray
-index will not be visited by this method.
NOTE: If this value is larger than the length of input theArray
-parameter, an exception will throw.
ALSO: Passing a negative value to this parameter,'ePos'
, will cause its value to be reset to the length of the inputArray
-parameter.toString
- A method that will convertObject's
of type'T'
to aString
. This parameter may not be null, because in such cases it would be appropriate to use:toCSV(Object[], int, int, boolean, boolean, Integer)
NOTE: This functional-interface is used instead of simply callingObject.toString()
in order to allow a programmer to provide arbitrarily definedtoString()
methods. If the standardtoString()
method for a givenObject
is not sufficient, then provide a different one here using this parameter.
This'toString'
functional-interface is expected to accept both an instance of a type'T'
variable, AND an integer. The integer that is accepted is simply the array-index where the'T'
variable parameter is located within the array.
NOTE: This function may return a zero-lengthString
, and when/if it does, theObject
located at that array-index shall not be printed to the output-String
. Also, if this function ever returns null, then aNullPointerException
shall throw immediately.printNulls
- When this parameter isTRUE
, if a null is encountered within the input-parameter arrayT[] tArr
, then null shall be passed to the Functional Interface input-parameter methodtoString.apply(T)
, and theString
result from that method shall be inserted into theString
that is returned.
When this parameter receivesFALSE
, then anytime a null value is found within the input-parameter arrayT[] tArr
, it will be skipped completely, and the outputString
will simply contain one fewer output-String
.maxLength
- Allows a user request that the returnedString
have length no longer than'maxLength'
characters. If the final-computed CSV-String
is longer than this parameter's value, it will be truncated tomaxLength - 4
, and' ...'
will be appended, instead, to the end of the return-String
.
NOTE: This parameter may be null, or negative, and if it is, it shall simply be ignored, and the fully-computed CSV-String
returned. Furthermore, don't forget that Java Compiler syntax rules allow a primitive-'int'
variable or literal to be passed to method-parameters of typejava.lang.Integer
. For example'80'
could be passed to this parameter, directly, instead of'Integer.valueOf(80)'
. (Againnull
may also be passed.)- Returns:
- a CSV version of the input parameter
'tArr'
where each instance of'T'
shall have been converted to aString
using the provided'toString(...)'
method. - Throws:
java.lang.ArrayIndexOutOfBoundsException
- This exception shall be thrown if any of the following are true:- If
'sPos'
is negative, or ifsPos
is greater-than-or-equal-to thelength
of theArray
- If
'ePos'
is zero, or greater than the length of theArray
- If the value of
'sPos'
is a larger integer than'ePos'
. If'ePos'
was negative, it is first reset toArray.length
, before this check is done.
- If
java.lang.NullPointerException
- if parameter'toString'
is null, or if any of the return values produced by'toString'
are null- Code:
- Exact Method Body:
LV l = new LV(tArr, sPos, ePos); StringBuilder sb = new StringBuilder(); int i = l.start; String s; if (printNulls) { for (; i < l.end; i++) if ((s = toString.apply(i, tArr[i])).length() > 0) { sb.append(s); break; } for (i++; i < l.end; i++) if ((s = toString.apply(i, tArr[i])) == null) throwNPE(i, tArr[i]); else sb.append(", " + s); } else { for (; i < l.end; i++) if ((tArr[i] != null) && ((s = toString.apply(i, tArr[i])).length() > 0)) { sb.append(s); break; } for (i++; i < l.end; i++) if (tArr[i] != null) if ((s = toString.apply(i, tArr[i])) == null) throwNPE(i, tArr[i]); else sb.append(", " + s); } if ((maxLength != null) && (sb.length() > maxLength.intValue())) { sb.setLength(maxLength - 4); sb.append(" ..."); return sb.toString(); } return sb.toString();
-
toCSV
public static <T> java.lang.String toCSV (T[][] tArr, IntIntTFunc<? super T,java.lang.String> toString, java.util.function.IntPredicate keepRow, boolean separateLines, java.lang.Integer maxLengthInner, java.lang.Integer maxLengthOuter)
This class prints a two dimensional<T>
-array to aString
.- Type Parameters:
T
- The type used by theT[][]
-Array. The'toString'
parameter / function-pointer also must accept this type.- Parameters:
tArr
- This may be any Java Two-Dimensional
-array.toString
- This Java Lambda (Functional Interface) Parameter is one which accepts two integers, and a primitive-
. The integers provided shall contain the indices to an element of the 2D Array parameter, and the
at that Array location. ThetoString.apply(int, int,
method inside this lambda should return any Java) String
to be printed to the output, for the given array-location and value.
NOTE: This lambda may return a zero-lengthString
, and when or if it does, that particular array-location will be 'skipped' (not printed) to the outputString
.
ALSO: This parameter may be null, and if it is, it will be ignored and all array-
, themselves, will simply be printed to the output's String
instead.
FINALLY: If this lambda's'apply'
function ever returns null, it will result in aNullPointerException
keepRow
- This parameter may also be passed null, and if it is, it's also ignored. This integer-Predicate
receives an'int'
that corresponds to the first-order array index (the outer-array-index, a.k.a.'
). When this lambda-[x][]' Predicate
returnsTRUE
the inner sub-array'
will be included in the output-[]' String
. When this lamda returnsFALSE
, the entire sub-array will be eliminated from the output-String
.
AGAIN: When this lamda-Predicate
receives null, it is ignored, meaning all sub-arrays are printed to the output-String
- or at least untilmaxLengthInner
is reached!.separateLines
- When this parameter receives TRUE, each of the sub-array's (
) will be separated by a newline[] '\n'
character. When this parameter is passed FALSE, all
inner-arrays will be printed to a single-line of text.[] maxLengthInner
- This parameter may be null, or negative, and if it is, it shall be ignored. Remember that Java Compiler syntax allows primitive-literals to be passed to function-parameters of typeInteger
. This means, you may pass'80'
here, rather than worrying about passingInteger.valueOf(80)
. Again, you may also pass'null'
, effectively making thie somewhat similar to a JavaScript "Optional" parameter.
When this parameter receives a non-nullint
, its intention is to specify an outputString's
'maximum-length' - for the 'Inner' Arrays (sub-Arrays). When this 'maximumString
length' is reached, an' ...'
(ellipsis) is appended to the end of the printed-String
, and the printing logic moves on to the next sub-array.
NOTE: Unlike parametermaxLengthOuter
, this parameter is specifying the maximum number of characters that the user wants printed to the outputString
- not the number array-element's .
FINALLY: Because the output-String
sub-arrays contain spaces, commas, and brackets, when a positive integer is passed here, that integer must be greater than'7'
. If it is not, anIllegalArgumentException
is thrown. Again, a negative value shall be treated the same as if'null'
were passed.maxLengthOuter
- This parameter may be null, or negative, and if it is, it will be ignored.
Again, Java compiler-syntax allows integer literals to be passed directly to this parameter.
This 'maximum length' specifies how many of the sub-arrays will be printed to the Output-String
. If there are more sub-arrays than the value passed tomaxLengthOter
, an elipsis' ...'
shall be appended to the end of the returned-String
.
NOTE: The meaning of this integer-value is different than that of parameter'maxLengthInner'
. Here, it is used to specify a maximum number of elements (specifically, 'sub-arrays'), while in'maxLengthInner'
, it is meant to specify a maximum number of output characters (a.k.a. aString
-length).
Remember that printing a large-two dimensional array will will result in a very longString
. In conjunction with the'separateLines'
paramater and/or empty-String
return-values from the'toString'
lambda, the length of the output-String
can be reasonably sized.- Returns:
- A printed version of this two-dimensional array.
- Throws:
java.lang.NullPointerException
- If the'toString'
method is non-null, but returns a null value.java.lang.IllegalArgumentException
- If'maxLengthInner'
is less than'7'
.- Code:
- Exact Method Body:
// maxLengthInner *MUST* be >= 7, since minimum-string-length is " [...]" // A value of 0 to 6 must throw an exception. // A negative value is treated the same as 'null' if ((maxLengthInner != null) && (maxLengthInner < 7) && (maxLengthInner >= 0)) throw new IllegalArgumentException (IAE_MESSAGE_MAXLENINNER.replace("TOK", maxLengthInner.toString())); else if ((maxLengthInner != null) && (maxLengthInner < 0)) maxLengthInner = null; StringBuilder sbOuter = new StringBuilder(); // Primary StringBuilder StringBuilder sbInner = new StringBuilder(); // StrinBuilder for the sub/inner arrays String s = null; // temp variable int i = 0; // outer-loop loop-counter int numRows = 0; // Count sub-arrays // If the value passed to 'maxLengthOuter' is negative, treat it the same as 'null' // SPECIFICALLY: No Max Length. if ((maxLengthOuter != null) && (maxLengthOuter < 0)) maxLengthOuter = null; sbOuter.append('['); for (i=0; i < tArr.length; i++) { // The "Keep Row" Predicate is used to check whether a sub-array should even be // included at all. If "Keep Row" exists, and it rejects the outer array index, // then the entire row itself should be skipped. if ((keepRow != null) && (! keepRow.test(i))) continue; numRows++; if ((maxLengthOuter != null) && (numRows > maxLengthOuter)) break; int j = 0; // System.out.println("end: " + end + ",\ti: " + i + ",\tj: " + j); // The purpose to this sub-loop is such that there is a "provided user option" where // the 'toString-lambda' may return a ZERO-LENGTH-STRING, and when this happens, the // array-location that resulted in a ZERO-LENGTH-STRING shall be ignored/skipped // completely. if (toString != null) while ( (j < tArr[i].length) && ((s = toString.apply(i, j, tArr[i][j])).length() == 0) ) j++; else s = "" + tArr[i][j].toString(); // If "separateLines" be sure to add a newline. if (separateLines) sbOuter.append('\n'); // Add the opening brackets to this new sub-array that was just computed. sbInner.append(" [" + s); j++; // Main Printing Loop while (j < tArr[i].length) { if (toString != null) { if ((s = toString.apply(i, j, tArr[i][j++])).length() > 0) sbInner.append(", " + s); } else sbInner.append(", " + tArr[i][j++].toString()); if ((maxLengthInner != null) && (sbInner.length() > maxLengthInner)) break; } // NOTE: The '+ 1' is needed because, as of yet, the trailing ']' has not been added. if ((maxLengthInner != null) && ((sbInner.length() + 1) > maxLengthInner)) { int pos = sbInner.length() - 4; char c = sbInner.charAt(pos); while ((c != ',') && (c != '[')) c = sbInner.charAt(--pos); sbInner.setLength(pos+1); sbInner.append((c == '[') ? "..." : " ..."); } sbInner.append(']'); sbOuter.append(sbInner.toString()); sbInner.setLength(0); } // This shall only execute if the if (i < tArr.length) sbOuter.append((separateLines ? "\n" : "") + " ..."); sbOuter.append(separateLines ? "\n]" : " ]"); return sbOuter.toString();
-
toCSV
public static java.lang.String toCSV (byte[] arr, IntByteFunction<java.lang.String> toString, java.lang.Integer maxLength)
-
toCSV
public static java.lang.String toCSV (byte[] arr, int sPos, int ePos, IntByteFunction<java.lang.String> toString, java.lang.Integer maxLength)
This method shall separate thebyte's
from an input array by commas, and print them to aString
(which it then returns). The user may specify a functional-interface so that the values inside the CSV-String
are not just the array-byte's
, themselves, but rather are any modification, or 'dressing up,' of those values as is neccessary.
It is not mandatory to pass a'toString'
function-pointer to this method. This parameter may be null, and if it is it shall be ignored.- Parameters:
arr
- Any array ofbyte
.sPos
- This is the (integer)Array
-index that sets a limit for the left-mostArray
-position to inspect/search inside the inputArray
-parameter.
This value is considered 'inclusive' meaning that the element at thisArray
-index will be visited by this method.
NOTE: If this value is negative, or larger than the length of the input-Array
, an exception will be thrown.ePos
- This is the (integer)Array
-index that sets a limit for the right-mostArray
-position to inspect/search inside the inputVector
-parameter.
This value is considered 'exclusive' meaning that the element at thisArray
-index will not be visited by this method.
NOTE: If this value is larger than the length of input theArray
-parameter, an exception will throw.
ALSO: Passing a negative value to this parameter,'ePos'
, will cause its value to be reset to the length of the inputArray
-parameter.toString
- This should be an implementation of functional-interfaceIntByteFunction<String>
. The expected inputs and outputs of this function are listed below:- Input 1:
int i
This input parameter shall receive the index of the array element being printed. - Input 2:
byte b
This input parameter shall receive the actualbyte
that is located atarr[i]
. - Output:
String
This output parameter must return anyString
the users wishes to be appended to the comma-separated outputString
for the passedIntByteFunction
and array index.
NOTE: If this function returns null,NullPointerException
will throw, however when this function returns a zero-lengthString
, for a particular array-index andbyte
combination, then thatbyte
shall not be printed to the outputString
NOTE: This ('toString
') parameter may be null. If null is, indeed, passed to this function-pointer / functional-interface parameter, then the output CSV-String
that is returned will simply have the array-numbers themselves, unmodified, printed to theString
, and separated by commas.- Input 1:
maxLength
- Allows a user request that the returnedString
have length no longer than'maxLength'
characters. If the final-computed CSV-String
is longer than this parameter's value, it will be truncated tomaxLength - 4
, and' ...'
will be appended, instead, to the end of the return-String
.
NOTE: This parameter may be null, or negative, and if it is, it shall simply be ignored, and the fully-computed CSV-String
returned. Furthermore, don't forget that Java Compiler syntax rules allow a primitive-'int'
variable or literal to be passed to method-parameters of typejava.lang.Integer
. For example'80'
could be passed to this parameter, directly, instead of'Integer.valueOf(80)'
. (Againnull
may also be passed.)- Returns:
- A
String
that contains thebyte
elements of this array, separated by comma's. If a valid instance ofIntByteFunction
has been passed to parameter'toString'
, then the output of that interface's'apply(int, byte)'
method will be returned, separated by commas. - Throws:
java.lang.ArrayIndexOutOfBoundsException
- This exception shall be thrown if any of the following are true:- If
'sPos'
is negative, or ifsPos
is greater-than-or-equal-to thelength
of theArray
- If
'ePos'
is zero, or greater than the length of theArray
- If the value of
'sPos'
is a larger integer than'ePos'
. If'ePos'
was negative, it is first reset toArray.length
, before this check is done.
- If
java.lang.NullPointerException
- If'toString'
returns a null value.- Code:
- Exact Method Body:
LV l = new LV(sPos, ePos, arr); StringBuilder sb = new StringBuilder(); int i = l.start; String s; if (toString == null) { sb.append(arr[i++]); while (i < l.end) sb.append(", " + arr[i++]); } else { for (; i < l.end; i++) if ((s = toString.apply(i, arr[i])).length() > 0) { sb.append(s); break; } for (i++; i < l.end; i++) if ((s = toString.apply(i, arr[i])) == null) throwNPE(i, arr[i]); else sb.append(", " + s); } if ((maxLength != null) && (sb.length() > maxLength.intValue())) { sb.setLength(maxLength - 4); sb.append(" ..."); return sb.toString(); } return sb.toString();
-
toCSV
public static java.lang.String toCSV (short[] arr, IntShortFunction<java.lang.String> toString, java.lang.Integer maxLength)
-
toCSV
public static java.lang.String toCSV (short[] arr, int sPos, int ePos, IntShortFunction<java.lang.String> toString, java.lang.Integer maxLength)
This method shall separate theshort's
from an input array by commas, and print them to aString
(which it then returns). The user may specify a functional-interface so that the values inside the CSV-String
are not just the array-short's
, themselves, but rather are any modification, or 'dressing up,' of those values as is neccessary.
It is not mandatory to pass a'toString'
function-pointer to this method. This parameter may be null, and if it is it shall be ignored.- Parameters:
arr
- Any array ofshort
-integers.sPos
- This is the (integer)Array
-index that sets a limit for the left-mostArray
-position to inspect/search inside the inputArray
-parameter.
This value is considered 'inclusive' meaning that the element at thisArray
-index will be visited by this method.
NOTE: If this value is negative, or larger than the length of the input-Array
, an exception will be thrown.ePos
- This is the (integer)Array
-index that sets a limit for the right-mostArray
-position to inspect/search inside the inputVector
-parameter.
This value is considered 'exclusive' meaning that the element at thisArray
-index will not be visited by this method.
NOTE: If this value is larger than the length of input theArray
-parameter, an exception will throw.
ALSO: Passing a negative value to this parameter,'ePos'
, will cause its value to be reset to the length of the inputArray
-parameter.toString
- This should be an implementation of functional-interfaceIntShortFunction<String>
. The expected inputs and outputs of this function are listed below:- Input 1:
int i
This input parameter shall receive the index of the array element being printed. - Input 2:
short s
This input parameter shall receive the actualshort
that is located atarr[i]
. - Output:
String
This output parameter must return anyString
the users wishes to be appended to the comma-separated outputString
for the passedIntShortFunction
and array index.
NOTE: If this function returns null,NullPointerException
will throw, however when this function returns a zero-lengthString
, for a particular array-index andshort
combination, then thatshort
shall not be printed to the outputString
NOTE: This ('toString
') parameter may be null. If null is, indeed, passed to this function-pointer / functional-interface parameter, then the output CSV-String
that is returned will simply have the array-numbers themselves, unmodified, printed to theString
, and separated by commas.- Input 1:
maxLength
- Allows a user request that the returnedString
have length no longer than'maxLength'
characters. If the final-computed CSV-String
is longer than this parameter's value, it will be truncated tomaxLength - 4
, and' ...'
will be appended, instead, to the end of the return-String
.
NOTE: This parameter may be null, or negative, and if it is, it shall simply be ignored, and the fully-computed CSV-String
returned. Furthermore, don't forget that Java Compiler syntax rules allow a primitive-'int'
variable or literal to be passed to method-parameters of typejava.lang.Integer
. For example'80'
could be passed to this parameter, directly, instead of'Integer.valueOf(80)'
. (Againnull
may also be passed.)- Returns:
- A
String
that contains theshort
elements of this array, separated by comma's. If a valid instance ofIntShortFunction
has been passed to parameter'toString'
, then the output of that interface's'apply(int, short)'
method will be returned, separated by commas. - Throws:
java.lang.ArrayIndexOutOfBoundsException
- This exception shall be thrown if any of the following are true:- If
'sPos'
is negative, or ifsPos
is greater-than-or-equal-to thelength
of theArray
- If
'ePos'
is zero, or greater than the length of theArray
- If the value of
'sPos'
is a larger integer than'ePos'
. If'ePos'
was negative, it is first reset toArray.length
, before this check is done.
- If
java.lang.NullPointerException
- If'toString'
returns a null value.- Code:
- Exact Method Body:
LV l = new LV(sPos, ePos, arr); StringBuilder sb = new StringBuilder(); int i = l.start; String s; if (toString == null) { sb.append(arr[i++]); while (i < l.end) sb.append(", " + arr[i++]); } else { for (; i < l.end; i++) if ((s = toString.apply(i, arr[i])).length() > 0) { sb.append(s); break; } for (i++; i < l.end; i++) if ((s = toString.apply(i, arr[i])) == null) throwNPE(i, arr[i]); else sb.append(", " + s); } if ((maxLength != null) && (sb.length() > maxLength.intValue())) { sb.setLength(maxLength - 4); sb.append(" ..."); return sb.toString(); } return sb.toString();
-
toCSV
public static java.lang.String toCSV (int[] arr, BiIntFunction<java.lang.String> toString, java.lang.Integer maxLength)
-
toCSV
public static java.lang.String toCSV (int[] arr, int sPos, int ePos, BiIntFunction<java.lang.String> toString, java.lang.Integer maxLength)
This method shall separate theint's
from an input array by commas, and print them to aString
(which it then returns). The user may specify a functional-interface so that the values inside the CSV-String
are not just the array-int's
, themselves, but rather are any modification, or 'dressing up,' of those values as is neccessary.
It is not mandatory to pass a'toString'
function-pointer to this method. This parameter may be null, and if it is it shall be ignored.- Parameters:
arr
- Any array of integers.sPos
- This is the (integer)Array
-index that sets a limit for the left-mostArray
-position to inspect/search inside the inputArray
-parameter.
This value is considered 'inclusive' meaning that the element at thisArray
-index will be visited by this method.
NOTE: If this value is negative, or larger than the length of the input-Array
, an exception will be thrown.ePos
- This is the (integer)Array
-index that sets a limit for the right-mostArray
-position to inspect/search inside the inputVector
-parameter.
This value is considered 'exclusive' meaning that the element at thisArray
-index will not be visited by this method.
NOTE: If this value is larger than the length of input theArray
-parameter, an exception will throw.
ALSO: Passing a negative value to this parameter,'ePos'
, will cause its value to be reset to the length of the inputArray
-parameter.toString
- This should be an implementation of functional-interfaceBiIntFunction<String>
. The expected inputs and outputs of this function are listed below:- Input 1:
int i
This input parameter shall receive the index of the array element being printed. - Input 2:
int j
This input parameter shall receive the actualint
that is located atarr[i]
. - Output:
String
This output parameter must return anyString
the users wishes to be appended to the comma-separated outputString
for the passedBiIntFunction
and array index.
NOTE: If this function returns null,NullPointerException
will throw, however when this function returns a zero-lengthString
, for a particular array-index andint
combination, then thatint
shall not be printed to the outputString
NOTE: This ('toString
') parameter may be null. If null is, indeed, passed to this function-pointer / functional-interface parameter, then the output CSV-String
that is returned will simply have the array-numbers themselves, unmodified, printed to theString
, and separated by commas.- Input 1:
maxLength
- Allows a user request that the returnedString
have length no longer than'maxLength'
characters. If the final-computed CSV-String
is longer than this parameter's value, it will be truncated tomaxLength - 4
, and' ...'
will be appended, instead, to the end of the return-String
.
NOTE: This parameter may be null, or negative, and if it is, it shall simply be ignored, and the fully-computed CSV-String
returned. Furthermore, don't forget that Java Compiler syntax rules allow a primitive-'int'
variable or literal to be passed to method-parameters of typejava.lang.Integer
. For example'80'
could be passed to this parameter, directly, instead of'Integer.valueOf(80)'
. (Againnull
may also be passed.)- Returns:
- A
String
that contains theint
elements of this array, separated by comma's. If a valid instance ofBiIntFunction
has been passed to parameter'toString'
, then the output of that interface's'apply(int, int)'
method will be returned, separated by commas. - Throws:
java.lang.ArrayIndexOutOfBoundsException
- This exception shall be thrown if any of the following are true:- If
'sPos'
is negative, or ifsPos
is greater-than-or-equal-to thelength
of theArray
- If
'ePos'
is zero, or greater than the length of theArray
- If the value of
'sPos'
is a larger integer than'ePos'
. If'ePos'
was negative, it is first reset toArray.length
, before this check is done.
- If
java.lang.NullPointerException
- If'toString'
returns a null value.- Code:
- Exact Method Body:
LV l = new LV(sPos, ePos, arr); StringBuilder sb = new StringBuilder(); int i = l.start; String s; if (toString == null) { sb.append(arr[i++]); while (i < l.end) sb.append(", " + arr[i++]); } else { for (; i < l.end; i++) if ((s = toString.apply(i, arr[i])).length() > 0) { sb.append(s); break; } for (i++; i < l.end; i++) if ((s = toString.apply(i, arr[i])) == null) throwNPE(i, arr[i]); else sb.append(", " + s); } if ((maxLength != null) && (sb.length() > maxLength.intValue())) { sb.setLength(maxLength - 4); sb.append(" ..."); return sb.toString(); } return sb.toString();
-
toCSV
public static java.lang.String toCSV (long[] arr, IntLongFunction<java.lang.String> toString, java.lang.Integer maxLength)
-
toCSV
public static java.lang.String toCSV (long[] arr, int sPos, int ePos, IntLongFunction<java.lang.String> toString, java.lang.Integer maxLength)
This method shall separate thelong's
from an input array by commas, and print them to aString
(which it then returns). The user may specify a functional-interface so that the values inside the CSV-String
are not just the array-long's
, themselves, but rather are any modification, or 'dressing up,' of those values as is neccessary.
It is not mandatory to pass a'toString'
function-pointer to this method. This parameter may be null, and if it is it shall be ignored.- Parameters:
arr
- Any array oflong
-integers.sPos
- This is the (integer)Array
-index that sets a limit for the left-mostArray
-position to inspect/search inside the inputArray
-parameter.
This value is considered 'inclusive' meaning that the element at thisArray
-index will be visited by this method.
NOTE: If this value is negative, or larger than the length of the input-Array
, an exception will be thrown.ePos
- This is the (integer)Array
-index that sets a limit for the right-mostArray
-position to inspect/search inside the inputVector
-parameter.
This value is considered 'exclusive' meaning that the element at thisArray
-index will not be visited by this method.
NOTE: If this value is larger than the length of input theArray
-parameter, an exception will throw.
ALSO: Passing a negative value to this parameter,'ePos'
, will cause its value to be reset to the length of the inputArray
-parameter.toString
- This should be an implementation of functional-interfaceIntLongFunction<String>
. The expected inputs and outputs of this function are listed below:- Input 1:
int i
This input parameter shall receive the index of the array element being printed. - Input 2:
long l
This input parameter shall receive the actuallong
that is located atarr[i]
. - Output:
String
This output parameter must return anyString
the users wishes to be appended to the comma-separated outputString
for the passedIntLongFunction
and array index.
NOTE: If this function returns null,NullPointerException
will throw, however when this function returns a zero-lengthString
, for a particular array-index andlong
combination, then thatlong
shall not be printed to the outputString
NOTE: This ('toString
') parameter may be null. If null is, indeed, passed to this function-pointer / functional-interface parameter, then the output CSV-String
that is returned will simply have the array-numbers themselves, unmodified, printed to theString
, and separated by commas.- Input 1:
maxLength
- Allows a user request that the returnedString
have length no longer than'maxLength'
characters. If the final-computed CSV-String
is longer than this parameter's value, it will be truncated tomaxLength - 4
, and' ...'
will be appended, instead, to the end of the return-String
.
NOTE: This parameter may be null, or negative, and if it is, it shall simply be ignored, and the fully-computed CSV-String
returned. Furthermore, don't forget that Java Compiler syntax rules allow a primitive-'int'
variable or literal to be passed to method-parameters of typejava.lang.Integer
. For example'80'
could be passed to this parameter, directly, instead of'Integer.valueOf(80)'
. (Againnull
may also be passed.)- Returns:
- A
String
that contains thelong
elements of this array, separated by comma's. If a valid instance ofIntLongFunction
has been passed to parameter'toString'
, then the output of that interface's'apply(int, long)'
method will be returned, separated by commas. - Throws:
java.lang.ArrayIndexOutOfBoundsException
- This exception shall be thrown if any of the following are true:- If
'sPos'
is negative, or ifsPos
is greater-than-or-equal-to thelength
of theArray
- If
'ePos'
is zero, or greater than the length of theArray
- If the value of
'sPos'
is a larger integer than'ePos'
. If'ePos'
was negative, it is first reset toArray.length
, before this check is done.
- If
java.lang.NullPointerException
- If'toString'
returns a null value.- Code:
- Exact Method Body:
LV l = new LV(sPos, ePos, arr); StringBuilder sb = new StringBuilder(); int i = l.start; String s; if (toString == null) { sb.append(arr[i++]); while (i < l.end) sb.append(", " + arr[i++]); } else { for (; i < l.end; i++) if ((s = toString.apply(i, arr[i])).length() > 0) { sb.append(s); break; } for (i++; i < l.end; i++) if ((s = toString.apply(i, arr[i])) == null) throwNPE(i, arr[i]); else sb.append(", " + s); } if ((maxLength != null) && (sb.length() > maxLength.intValue())) { sb.setLength(maxLength - 4); sb.append(" ..."); return sb.toString(); } return sb.toString();
-
toCSV
public static java.lang.String toCSV (float[] arr, IntFloatFunction<java.lang.String> toString, java.lang.Integer maxLength)
-
toCSV
public static java.lang.String toCSV (float[] arr, int sPos, int ePos, IntFloatFunction<java.lang.String> toString, java.lang.Integer maxLength)
This method shall separate thefloat's
from an input array by commas, and print them to aString
(which it then returns). The user may specify a functional-interface so that the values inside the CSV-String
are not just the array-float's
, themselves, but rather are any modification, or 'dressing up,' of those values as is neccessary.
It is not mandatory to pass a'toString'
function-pointer to this method. This parameter may be null, and if it is it shall be ignored.- Parameters:
arr
- Any array offloat
.sPos
- This is the (integer)Array
-index that sets a limit for the left-mostArray
-position to inspect/search inside the inputArray
-parameter.
This value is considered 'inclusive' meaning that the element at thisArray
-index will be visited by this method.
NOTE: If this value is negative, or larger than the length of the input-Array
, an exception will be thrown.ePos
- This is the (integer)Array
-index that sets a limit for the right-mostArray
-position to inspect/search inside the inputVector
-parameter.
This value is considered 'exclusive' meaning that the element at thisArray
-index will not be visited by this method.
NOTE: If this value is larger than the length of input theArray
-parameter, an exception will throw.
ALSO: Passing a negative value to this parameter,'ePos'
, will cause its value to be reset to the length of the inputArray
-parameter.toString
- This should be an implementation of functional-interfaceIntFloatFunction<String>
. The expected inputs and outputs of this function are listed below:- Input 1:
int i
This input parameter shall receive the index of the array element being printed. - Input 2:
float f
This input parameter shall receive the actualfloat
that is located atarr[i]
. - Output:
String
This output parameter must return anyString
the users wishes to be appended to the comma-separated outputString
for the passedIntFloatFunction
and array index.
NOTE: If this function returns null,NullPointerException
will throw, however when this function returns a zero-lengthString
, for a particular array-index andfloat
combination, then thatfloat
shall not be printed to the outputString
NOTE: This ('toString
') parameter may be null. If null is, indeed, passed to this function-pointer / functional-interface parameter, then the output CSV-String
that is returned will simply have the array-numbers themselves, unmodified, printed to theString
, and separated by commas.- Input 1:
maxLength
- Allows a user request that the returnedString
have length no longer than'maxLength'
characters. If the final-computed CSV-String
is longer than this parameter's value, it will be truncated tomaxLength - 4
, and' ...'
will be appended, instead, to the end of the return-String
.
NOTE: This parameter may be null, or negative, and if it is, it shall simply be ignored, and the fully-computed CSV-String
returned. Furthermore, don't forget that Java Compiler syntax rules allow a primitive-'int'
variable or literal to be passed to method-parameters of typejava.lang.Integer
. For example'80'
could be passed to this parameter, directly, instead of'Integer.valueOf(80)'
. (Againnull
may also be passed.)- Returns:
- A
String
that contains thefloat
elements of this array, separated by comma's. If a valid instance ofIntFloatFunction
has been passed to parameter'toString'
, then the output of that interface's'apply(int, float)'
method will be returned, separated by commas. - Throws:
java.lang.ArrayIndexOutOfBoundsException
- This exception shall be thrown if any of the following are true:- If
'sPos'
is negative, or ifsPos
is greater-than-or-equal-to thelength
of theArray
- If
'ePos'
is zero, or greater than the length of theArray
- If the value of
'sPos'
is a larger integer than'ePos'
. If'ePos'
was negative, it is first reset toArray.length
, before this check is done.
- If
java.lang.NullPointerException
- If'toString'
returns a null value.- Code:
- Exact Method Body:
LV l = new LV(sPos, ePos, arr); StringBuilder sb = new StringBuilder(); int i = l.start; String s; if (toString == null) { sb.append(arr[i++]); while (i < l.end) sb.append(", " + arr[i++]); } else { for (; i < l.end; i++) if ((s = toString.apply(i, arr[i])).length() > 0) { sb.append(s); break; } for (i++; i < l.end; i++) if ((s = toString.apply(i, arr[i])) == null) throwNPE(i, arr[i]); else sb.append(", " + s); } if ((maxLength != null) && (sb.length() > maxLength.intValue())) { sb.setLength(maxLength - 4); sb.append(" ..."); return sb.toString(); } return sb.toString();
-
toCSV
public static java.lang.String toCSV (double[] arr, IntDoubleFunction<java.lang.String> toString, java.lang.Integer maxLength)
-
toCSV
public static java.lang.String toCSV (double[] arr, int sPos, int ePos, IntDoubleFunction<java.lang.String> toString, java.lang.Integer maxLength)
This method shall separate thedouble's
from an input array by commas, and print them to aString
(which it then returns). The user may specify a functional-interface so that the values inside the CSV-String
are not just the array-double's
, themselves, but rather are any modification, or 'dressing up,' of those values as is neccessary.
It is not mandatory to pass a'toString'
function-pointer to this method. This parameter may be null, and if it is it shall be ignored.- Parameters:
arr
- Any array ofdouble
.sPos
- This is the (integer)Array
-index that sets a limit for the left-mostArray
-position to inspect/search inside the inputArray
-parameter.
This value is considered 'inclusive' meaning that the element at thisArray
-index will be visited by this method.
NOTE: If this value is negative, or larger than the length of the input-Array
, an exception will be thrown.ePos
- This is the (integer)Array
-index that sets a limit for the right-mostArray
-position to inspect/search inside the inputVector
-parameter.
This value is considered 'exclusive' meaning that the element at thisArray
-index will not be visited by this method.
NOTE: If this value is larger than the length of input theArray
-parameter, an exception will throw.
ALSO: Passing a negative value to this parameter,'ePos'
, will cause its value to be reset to the length of the inputArray
-parameter.toString
- This should be an implementation of functional-interfaceIntDoubleFunction<String>
. The expected inputs and outputs of this function are listed below:- Input 1:
int i
This input parameter shall receive the index of the array element being printed. - Input 2:
double d
This input parameter shall receive the actualdouble
that is located atarr[i]
. - Output:
String
This output parameter must return anyString
the users wishes to be appended to the comma-separated outputString
for the passedIntDoubleFunction
and array index.
NOTE: If this function returns null,NullPointerException
will throw, however when this function returns a zero-lengthString
, for a particular array-index anddouble
combination, then thatdouble
shall not be printed to the outputString
NOTE: This ('toString
') parameter may be null. If null is, indeed, passed to this function-pointer / functional-interface parameter, then the output CSV-String
that is returned will simply have the array-numbers themselves, unmodified, printed to theString
, and separated by commas.- Input 1:
maxLength
- Allows a user request that the returnedString
have length no longer than'maxLength'
characters. If the final-computed CSV-String
is longer than this parameter's value, it will be truncated tomaxLength - 4
, and' ...'
will be appended, instead, to the end of the return-String
.
NOTE: This parameter may be null, or negative, and if it is, it shall simply be ignored, and the fully-computed CSV-String
returned. Furthermore, don't forget that Java Compiler syntax rules allow a primitive-'int'
variable or literal to be passed to method-parameters of typejava.lang.Integer
. For example'80'
could be passed to this parameter, directly, instead of'Integer.valueOf(80)'
. (Againnull
may also be passed.)- Returns:
- A
String
that contains thedouble
elements of this array, separated by comma's. If a valid instance ofIntDoubleFunction
has been passed to parameter'toString'
, then the output of that interface's'apply(int, double)'
method will be returned, separated by commas. - Throws:
java.lang.ArrayIndexOutOfBoundsException
- This exception shall be thrown if any of the following are true:- If
'sPos'
is negative, or ifsPos
is greater-than-or-equal-to thelength
of theArray
- If
'ePos'
is zero, or greater than the length of theArray
- If the value of
'sPos'
is a larger integer than'ePos'
. If'ePos'
was negative, it is first reset toArray.length
, before this check is done.
- If
java.lang.NullPointerException
- If'toString'
returns a null value.- Code:
- Exact Method Body:
LV l = new LV(sPos, ePos, arr); StringBuilder sb = new StringBuilder(); int i = l.start; String s; if (toString == null) { sb.append(arr[i++]); while (i < l.end) sb.append(", " + arr[i++]); } else { for (; i < l.end; i++) if ((s = toString.apply(i, arr[i])).length() > 0) { sb.append(s); break; } for (i++; i < l.end; i++) if ((s = toString.apply(i, arr[i])) == null) throwNPE(i, arr[i]); else sb.append(", " + s); } if ((maxLength != null) && (sb.length() > maxLength.intValue())) { sb.setLength(maxLength - 4); sb.append(" ..."); return sb.toString(); } return sb.toString();
-
toCSV
public static java.lang.String toCSV (boolean[] arr, IntBoolFunction<java.lang.String> toString, java.lang.Integer maxLength)
-
toCSV
public static java.lang.String toCSV (boolean[] arr, int sPos, int ePos, IntBoolFunction<java.lang.String> toString, java.lang.Integer maxLength)
This method shall separate theboolean's
from an input array by commas, and print them to aString
(which it then returns). The user may specify a functional-interface so that the values inside the CSV-String
are not just the array-boolean's
, themselves, but rather are any modification, or 'dressing up,' of those values as is neccessary.
It is not mandatory to pass a'toString'
function-pointer to this method. This parameter may be null, and if it is it shall be ignored.- Parameters:
arr
- Any array ofboolean
.sPos
- This is the (integer)Array
-index that sets a limit for the left-mostArray
-position to inspect/search inside the inputArray
-parameter.
This value is considered 'inclusive' meaning that the element at thisArray
-index will be visited by this method.
NOTE: If this value is negative, or larger than the length of the input-Array
, an exception will be thrown.ePos
- This is the (integer)Array
-index that sets a limit for the right-mostArray
-position to inspect/search inside the inputVector
-parameter.
This value is considered 'exclusive' meaning that the element at thisArray
-index will not be visited by this method.
NOTE: If this value is larger than the length of input theArray
-parameter, an exception will throw.
ALSO: Passing a negative value to this parameter,'ePos'
, will cause its value to be reset to the length of the inputArray
-parameter.toString
- This should be an implementation of functional-interfaceIntBoolFunction<String>
. The expected inputs and outputs of this function are listed below:- Input 1:
int i
This input parameter shall receive the index of the array element being printed. - Input 2:
boolean b
This input parameter shall receive the actualboolean
that is located atarr[i]
. - Output:
String
This output parameter must return anyString
the users wishes to be appended to the comma-separated outputString
for the passedIntBoolFunction
and array index.
NOTE: If this function returns null,NullPointerException
will throw, however when this function returns a zero-lengthString
, for a particular array-index andboolean
combination, then thatboolean
shall not be printed to the outputString
NOTE: This ('toString
') parameter may be null. If null is, indeed, passed to this function-pointer / functional-interface parameter, then the output CSV-String
that is returned will simply have the array-booleans themselves, unmodified, printed to theString
, and separated by commas.- Input 1:
maxLength
- Allows a user request that the returnedString
have length no longer than'maxLength'
characters. If the final-computed CSV-String
is longer than this parameter's value, it will be truncated tomaxLength - 4
, and' ...'
will be appended, instead, to the end of the return-String
.
NOTE: This parameter may be null, or negative, and if it is, it shall simply be ignored, and the fully-computed CSV-String
returned. Furthermore, don't forget that Java Compiler syntax rules allow a primitive-'int'
variable or literal to be passed to method-parameters of typejava.lang.Integer
. For example'80'
could be passed to this parameter, directly, instead of'Integer.valueOf(80)'
. (Againnull
may also be passed.)- Returns:
- A
String
that contains theboolean
elements of this array, separated by comma's. If a valid instance ofIntBoolFunction
has been passed to parameter'toString'
, then the output of that interface's'apply(int, boolean)'
method will be returned, separated by commas. - Throws:
java.lang.ArrayIndexOutOfBoundsException
- This exception shall be thrown if any of the following are true:- If
'sPos'
is negative, or ifsPos
is greater-than-or-equal-to thelength
of theArray
- If
'ePos'
is zero, or greater than the length of theArray
- If the value of
'sPos'
is a larger integer than'ePos'
. If'ePos'
was negative, it is first reset toArray.length
, before this check is done.
- If
java.lang.NullPointerException
- If'toString'
returns a null value.- Code:
- Exact Method Body:
LV l = new LV(sPos, ePos, arr); StringBuilder sb = new StringBuilder(); int i = l.start; String s; if (toString == null) { sb.append(arr[i++]); while (i < l.end) sb.append(", " + arr[i++]); } else { for (; i < l.end; i++) if ((s = toString.apply(i, arr[i])).length() > 0) { sb.append(s); break; } for (i++; i < l.end; i++) if ((s = toString.apply(i, arr[i])) == null) throwNPE(i, arr[i]); else sb.append(", " + s); } if ((maxLength != null) && (sb.length() > maxLength.intValue())) { sb.setLength(maxLength - 4); sb.append(" ..."); return sb.toString(); } return sb.toString();
-
toCSV
public static java.lang.String toCSV (char[] arr, IntCharFunction<java.lang.String> toString, java.lang.Integer maxLength)
-
toCSV
public static java.lang.String toCSV (char[] arr, int sPos, int ePos, IntCharFunction<java.lang.String> toString, java.lang.Integer maxLength)
This method shall separate thechar's
from an input array by commas, and print them to aString
(which it then returns). The user may specify a functional-interface so that the values inside the CSV-String
are not just the array-char's
, themselves, but rather are any modification, or 'dressing up,' of those values as is neccessary.
It is not mandatory to pass a'toString'
function-pointer to this method. This parameter may be null, and if it is it shall be ignored.- Parameters:
arr
- Any array ofboolean
.sPos
- This is the (integer)Array
-index that sets a limit for the left-mostArray
-position to inspect/search inside the inputArray
-parameter.
This value is considered 'inclusive' meaning that the element at thisArray
-index will be visited by this method.
NOTE: If this value is negative, or larger than the length of the input-Array
, an exception will be thrown.ePos
- This is the (integer)Array
-index that sets a limit for the right-mostArray
-position to inspect/search inside the inputVector
-parameter.
This value is considered 'exclusive' meaning that the element at thisArray
-index will not be visited by this method.
NOTE: If this value is larger than the length of input theArray
-parameter, an exception will throw.
ALSO: Passing a negative value to this parameter,'ePos'
, will cause its value to be reset to the length of the inputArray
-parameter.toString
- This should be an implementation of functional-interfaceIntCharFunction<String>
. The expected inputs and outputs of this function are listed below:- Input 1:
int i
This input parameter shall receive the index of the array element being printed. - Input 2:
char c
This input parameter shall receive the actualchar
that is located atarr[i]
. - Output:
String
This output parameter must return anyString
the users wishes to be appended to the comma-separated outputString
for the passedIntCharFunction
and array index.
NOTE: If this function returns null,NullPointerException
will throw, however when this function returns a zero-lengthString
, for a particular array-index andchar
combination, then thatchar
shall not be printed to the outputString
NOTE: This ('toString
') parameter may be null. If null is, indeed, passed to this function-pointer / functional-interface parameter, then the output CSV-String
that is returned will simply have the array-characters themselves, unmodified, printed to theString
, and separated by commas.- Input 1:
maxLength
- Allows a user request that the returnedString
have length no longer than'maxLength'
characters. If the final-computed CSV-String
is longer than this parameter's value, it will be truncated tomaxLength - 4
, and' ...'
will be appended, instead, to the end of the return-String
.
NOTE: This parameter may be null, or negative, and if it is, it shall simply be ignored, and the fully-computed CSV-String
returned. Furthermore, don't forget that Java Compiler syntax rules allow a primitive-'int'
variable or literal to be passed to method-parameters of typejava.lang.Integer
. For example'80'
could be passed to this parameter, directly, instead of'Integer.valueOf(80)'
. (Againnull
may also be passed.)- Returns:
- A
String
that contains thechar
elements of this array, separated by comma's. If a valid instance ofIntCharFunction
has been passed to parameter'toString'
, then the output of that interface's'apply(int, char)'
method will be returned, separated by commas. - Throws:
java.lang.ArrayIndexOutOfBoundsException
- This exception shall be thrown if any of the following are true:- If
'sPos'
is negative, or ifsPos
is greater-than-or-equal-to thelength
of theArray
- If
'ePos'
is zero, or greater than the length of theArray
- If the value of
'sPos'
is a larger integer than'ePos'
. If'ePos'
was negative, it is first reset toArray.length
, before this check is done.
- If
java.lang.NullPointerException
- If'toString'
returns a null value.- Code:
- Exact Method Body:
LV l = new LV(sPos, ePos, arr); StringBuilder sb = new StringBuilder(); int i = l.start; String s; if (toString == null) { sb.append(arr[i++]); while (i < l.end) sb.append(", " + arr[i++]); } else { for (i=l.start; i < l.end; i++) if ((s = toString.apply(i, arr[i])).length() > 0) { sb.append(s); break; } for (i++; i < l.end; i++) if ((s = toString.apply(i, arr[i])) == null) throwNPE(i, arr[i]); else sb.append(", " + s); } if ((maxLength != null) && (sb.length() > maxLength.intValue())) { sb.setLength(maxLength - 4); sb.append(" ..."); return sb.toString(); } return sb.toString();
-
toCSV
public static java.lang.String toCSV (byte[][] arr, IntIntByteFunc<java.lang.String> toString, java.util.function.IntPredicate keepRow, boolean separateLines, java.lang.Integer maxLengthInner, java.lang.Integer maxLengthOuter)
This class prints a two dimensionalbyte
-array to aString
.- Parameters:
arr
- This may be any Java Two-Dimensionalbyte
-array.toString
- This Java Lambda (Functional Interface) Parameter is one which accepts two integers, and a primitive-byte
. The integers provided shall contain the indices to an element of the 2D Array parameter, and thebyte
at that Array location. ThetoString.apply(int, int, byte)
method inside this lambda should return any JavaString
to be printed to the output, for the given array-location and value.
NOTE: This lambda may return a zero-lengthString
, and when or if it does, that particular array-location will be 'skipped' (not printed) to the outputString
.
ALSO: This parameter may be null, and if it is, it will be ignored and all array-byte's
, themselves, will simply be printed to the outputString
instead.
FINALLY: If this lambda's'apply'
function ever returns null, it will result in aNullPointerException
keepRow
- This parameter may also be passed null, and if it is, it's also ignored. This integer-Predicate
receives an'int'
that corresponds to the first-order array index (the outer-array-index, a.k.a.'byte[x][]'
). When this lambda-Predicate
returnsTRUE
the inner sub-array'byte[]'
will be included in the output-String
. When this lamda returnsFALSE
, the entire sub-array will be eliminated from the output-String
.
AGAIN: When this lamda-Predicate
receives null, it is ignored, meaning all sub-arrays are printed to the output-String
- or at least untilmaxLengthInner
is reached!.separateLines
- When this parameter receives TRUE, each of the sub-array's (byte[]
) will be separated by a newline'\n'
character. When this parameter is passed FALSE, allbyte[]
inner-arrays will be printed to a single-line of text.maxLengthInner
- This parameter may be null, or negative, and if it is, it shall be ignored. Remember that Java Compiler syntax allows primitive-literals to be passed to function-parameters of typeInteger
. This means, you may pass'80'
here, rather than worrying about passingInteger.valueOf(80)
. Again, you may also pass'null'
, effectively making thie somewhat similar to a JavaScript "Optional" parameter.
When this parameter receives a non-nullint
, its intention is to specify an outputString's
'maximum-length' - for the 'Inner' Arrays (sub-Arrays). When this 'maximumString
length' is reached, an' ...'
(ellipsis) is appended to the end of the printed-String
, and the printing logic moves on to the next sub-array.
NOTE: Unlike parametermaxLengthOuter
, this parameter is specifying the maximum number of characters that the user wants printed to the outputString
- not the number array-element byte's.
FINALLY: Because the output-String
sub-arrays contain spaces, commas, and brackets, when a positive integer is passed here, that integer must be greater than'7'
. If it is not, anIllegalArgumentException
is thrown. Again, a negative value shall be treated the same as if'null'
were passed.maxLengthOuter
- This parameter may be null, or negative, and if it is, it will be ignored.
Again, Java compiler-syntax allows integer literals to be passed directly to this parameter.
This 'maximum length' specifies how many of the sub-arrays will be printed to the Output-String
. If there are more sub-arrays than the value passed tomaxLengthOter
, an elipsis' ...'
shall be appended to the end of the returned-String
.
NOTE: The meaning of this integer-value is different than that of parameter'maxLengthInner'
. Here, it is used to specify a maximum number of elements (specifically, 'sub-arrays'), while in'maxLengthInner'
, it is meant to specify a maximum number of output characters (a.k.a. aString
-length).
Remember that printing a large-two dimensional array will will result in a very longString
. In conjunction with the'separateLines'
paramater and/or empty-String
return-values from the'toString'
lambda, the length of the output-String
can be reasonably sized.- Returns:
- A printed version of this two-dimensional array.
- Throws:
java.lang.NullPointerException
- If the'toString'
method is non-null, but returns a null value.java.lang.IllegalArgumentException
- If'maxLengthInner'
is less than'7'
.- Code:
- Exact Method Body:
// maxLengthInner *MUST* be >= 7, since minimum-string-length is " [...]" // A value of 0 to 6 must throw an exception. // A negative value is treated the same as 'null' if ((maxLengthInner != null) && (maxLengthInner < 7) && (maxLengthInner >= 0)) throw new IllegalArgumentException (IAE_MESSAGE_MAXLENINNER.replace("TOK", maxLengthInner.toString())); else if ((maxLengthInner != null) && (maxLengthInner < 0)) maxLengthInner = null; StringBuilder sbOuter = new StringBuilder(); // Primary StringBuilder StringBuilder sbInner = new StringBuilder(); // StrinBuilder for the sub/inner arrays String s = null; // temp variable int i = 0; // outer-loop loop-counter int numRows = 0; // Count sub-arrays // If the value passed to 'maxLengthOuter' is negative, treat it the same as 'null' // SPECIFICALLY: No Max Length. if ((maxLengthOuter != null) && (maxLengthOuter < 0)) maxLengthOuter = null; sbOuter.append('['); for (i=0; i < arr.length; i++) { // The "Keep Row" Predicate is used to check whether a sub-array should even be // included at all. If "Keep Row" exists, and it rejects the outer array index, // then the entire row itself should be skipped. if ((keepRow != null) && (! keepRow.test(i))) continue; numRows++; if ((maxLengthOuter != null) && (numRows > maxLengthOuter)) break; int j = 0; // System.out.println("end: " + end + ",\ti: " + i + ",\tj: " + j); // The purpose to this sub-loop is such that there is a "provided user option" where // the 'toString-lambda' may return a ZERO-LENGTH-STRING, and when this happens, the // array-location that resulted in a ZERO-LENGTH-STRING shall be ignored/skipped // completely. if (toString != null) while ( (j < arr[i].length) && ((s = toString.apply(i, j, arr[i][j])).length() == 0) ) j++; else s = "" + arr[i][j]; // If "separateLines" be sure to add a newline. if (separateLines) sbOuter.append('\n'); // Add the opening brackets to this new sub-array that was just computed. sbInner.append(" [" + s); j++; // Main Printing Loop while (j < arr[i].length) { if (toString != null) { if ((s = toString.apply(i, j, arr[i][j++])).length() > 0) sbInner.append(", " + s); } else sbInner.append(", " + arr[i][j++]); if ((maxLengthInner != null) && (sbInner.length() > maxLengthInner)) break; } // NOTE: The '+ 1' is needed because, as of yet, the trailing ']' has not been added. if ((maxLengthInner != null) && ((sbInner.length() + 1) > maxLengthInner)) { int pos = sbInner.length() - 4; char c = sbInner.charAt(pos); while ((c != ',') && (c != '[')) c = sbInner.charAt(--pos); sbInner.setLength(pos+1); sbInner.append((c == '[') ? "..." : " ..."); } sbInner.append(']'); sbOuter.append(sbInner.toString()); sbInner.setLength(0); } // This shall only execute if the if (i < arr.length) sbOuter.append((separateLines ? "\n" : "") + " ..."); sbOuter.append(separateLines ? "\n]" : " ]"); return sbOuter.toString();
-
toCSV
public static java.lang.String toCSV (short[][] arr, IntIntShortFunc<java.lang.String> toString, java.util.function.IntPredicate keepRow, boolean separateLines, java.lang.Integer maxLengthInner, java.lang.Integer maxLengthOuter)
This class prints a two dimensionalshort
-array to aString
.- Parameters:
arr
- This may be any Java Two-Dimensionalshort
-array.toString
- This Java Lambda (Functional Interface) Parameter is one which accepts two integers, and a primitive-short
. The integers provided shall contain the indices to an element of the 2D Array parameter, and theshort
at that Array location. ThetoString.apply(int, int, short)
method inside this lambda should return any JavaString
to be printed to the output, for the given array-location and value.
NOTE: This lambda may return a zero-lengthString
, and when or if it does, that particular array-location will be 'skipped' (not printed) to the outputString
.
ALSO: This parameter may be null, and if it is, it will be ignored and all array-short's
, themselves, will simply be printed to the outputString
instead.
FINALLY: If this lambda's'apply'
function ever returns null, it will result in aNullPointerException
keepRow
- This parameter may also be passed null, and if it is, it's also ignored. This integer-Predicate
receives an'int'
that corresponds to the first-order array index (the outer-array-index, a.k.a.'short[x][]'
). When this lambda-Predicate
returnsTRUE
the inner sub-array'short[]'
will be included in the output-String
. When this lamda returnsFALSE
, the entire sub-array will be eliminated from the output-String
.
AGAIN: When this lamda-Predicate
receives null, it is ignored, meaning all sub-arrays are printed to the output-String
- or at least untilmaxLengthInner
is reached!.separateLines
- When this parameter receives TRUE, each of the sub-array's (short[]
) will be separated by a newline'\n'
character. When this parameter is passed FALSE, allshort[]
inner-arrays will be printed to a single-line of text.maxLengthInner
- This parameter may be null, or negative, and if it is, it shall be ignored. Remember that Java Compiler syntax allows primitive-literals to be passed to function-parameters of typeInteger
. This means, you may pass'80'
here, rather than worrying about passingInteger.valueOf(80)
. Again, you may also pass'null'
, effectively making thie somewhat similar to a JavaScript "Optional" parameter.
When this parameter receives a non-nullint
, its intention is to specify an outputString's
'maximum-length' - for the 'Inner' Arrays (sub-Arrays). When this 'maximumString
length' is reached, an' ...'
(ellipsis) is appended to the end of the printed-String
, and the printing logic moves on to the next sub-array.
NOTE: Unlike parametermaxLengthOuter
, this parameter is specifying the maximum number of characters that the user wants printed to the outputString
- not the number array-element short's.
FINALLY: Because the output-String
sub-arrays contain spaces, commas, and brackets, when a positive integer is passed here, that integer must be greater than'7'
. If it is not, anIllegalArgumentException
is thrown. Again, a negative value shall be treated the same as if'null'
were passed.maxLengthOuter
- This parameter may be null, or negative, and if it is, it will be ignored.
Again, Java compiler-syntax allows integer literals to be passed directly to this parameter.
This 'maximum length' specifies how many of the sub-arrays will be printed to the Output-String
. If there are more sub-arrays than the value passed tomaxLengthOter
, an elipsis' ...'
shall be appended to the end of the returned-String
.
NOTE: The meaning of this integer-value is different than that of parameter'maxLengthInner'
. Here, it is used to specify a maximum number of elements (specifically, 'sub-arrays'), while in'maxLengthInner'
, it is meant to specify a maximum number of output characters (a.k.a. aString
-length).
Remember that printing a large-two dimensional array will will result in a very longString
. In conjunction with the'separateLines'
paramater and/or empty-String
return-values from the'toString'
lambda, the length of the output-String
can be reasonably sized.- Returns:
- A printed version of this two-dimensional array.
- Throws:
java.lang.NullPointerException
- If the'toString'
method is non-null, but returns a null value.java.lang.IllegalArgumentException
- If'maxLengthInner'
is less than'7'
.- Code:
- Exact Method Body:
// maxLengthInner *MUST* be >= 7, since minimum-string-length is " [...]" // A value of 0 to 6 must throw an exception. // A negative value is treated the same as 'null' if ((maxLengthInner != null) && (maxLengthInner < 7) && (maxLengthInner >= 0)) throw new IllegalArgumentException (IAE_MESSAGE_MAXLENINNER.replace("TOK", maxLengthInner.toString())); else if ((maxLengthInner != null) && (maxLengthInner < 0)) maxLengthInner = null; StringBuilder sbOuter = new StringBuilder(); // Primary StringBuilder StringBuilder sbInner = new StringBuilder(); // StrinBuilder for the sub/inner arrays String s = null; // temp variable int i = 0; // outer-loop loop-counter int numRows = 0; // Count sub-arrays // If the value passed to 'maxLengthOuter' is negative, treat it the same as 'null' // SPECIFICALLY: No Max Length. if ((maxLengthOuter != null) && (maxLengthOuter < 0)) maxLengthOuter = null; sbOuter.append('['); for (i=0; i < arr.length; i++) { // The "Keep Row" Predicate is used to check whether a sub-array should even be // included at all. If "Keep Row" exists, and it rejects the outer array index, // then the entire row itself should be skipped. if ((keepRow != null) && (! keepRow.test(i))) continue; numRows++; if ((maxLengthOuter != null) && (numRows > maxLengthOuter)) break; int j = 0; // System.out.println("end: " + end + ",\ti: " + i + ",\tj: " + j); // The purpose to this sub-loop is such that there is a "provided user option" where // the 'toString-lambda' may return a ZERO-LENGTH-STRING, and when this happens, the // array-location that resulted in a ZERO-LENGTH-STRING shall be ignored/skipped // completely. if (toString != null) while ( (j < arr[i].length) && ((s = toString.apply(i, j, arr[i][j])).length() == 0) ) j++; else s = "" + arr[i][j]; // If "separateLines" be sure to add a newline. if (separateLines) sbOuter.append('\n'); // Add the opening brackets to this new sub-array that was just computed. sbInner.append(" [" + s); j++; // Main Printing Loop while (j < arr[i].length) { if (toString != null) { if ((s = toString.apply(i, j, arr[i][j++])).length() > 0) sbInner.append(", " + s); } else sbInner.append(", " + arr[i][j++]); if ((maxLengthInner != null) && (sbInner.length() > maxLengthInner)) break; } // NOTE: The '+ 1' is needed because, as of yet, the trailing ']' has not been added. if ((maxLengthInner != null) && ((sbInner.length() + 1) > maxLengthInner)) { int pos = sbInner.length() - 4; char c = sbInner.charAt(pos); while ((c != ',') && (c != '[')) c = sbInner.charAt(--pos); sbInner.setLength(pos+1); sbInner.append((c == '[') ? "..." : " ..."); } sbInner.append(']'); sbOuter.append(sbInner.toString()); sbInner.setLength(0); } // This shall only execute if the if (i < arr.length) sbOuter.append((separateLines ? "\n" : "") + " ..."); sbOuter.append(separateLines ? "\n]" : " ]"); return sbOuter.toString();
-
toCSV
public static java.lang.String toCSV (int[][] arr, TriIntFunc<java.lang.String> toString, java.util.function.IntPredicate keepRow, boolean separateLines, java.lang.Integer maxLengthInner, java.lang.Integer maxLengthOuter)
This class prints a two dimensionalint
-array to aString
.- Parameters:
arr
- This may be any Java Two-Dimensionalint
-array.toString
- This Java Lambda (Functional Interface) Parameter is one which accepts two integers, and a primitive-int
. The integers provided shall contain the indices to an element of the 2D Array parameter, and theint
at that Array location. ThetoString.apply(int, int, int)
method inside this lambda should return any JavaString
to be printed to the output, for the given array-location and value.
NOTE: This lambda may return a zero-lengthString
, and when or if it does, that particular array-location will be 'skipped' (not printed) to the outputString
.
ALSO: This parameter may be null, and if it is, it will be ignored and all array-int's
, themselves, will simply be printed to the outputString
instead.
FINALLY: If this lambda's'apply'
function ever returns null, it will result in aNullPointerException
keepRow
- This parameter may also be passed null, and if it is, it's also ignored. This integer-Predicate
receives an'int'
that corresponds to the first-order array index (the outer-array-index, a.k.a.'int[x][]'
). When this lambda-Predicate
returnsTRUE
the inner sub-array'int[]'
will be included in the output-String
. When this lamda returnsFALSE
, the entire sub-array will be eliminated from the output-String
.
AGAIN: When this lamda-Predicate
receives null, it is ignored, meaning all sub-arrays are printed to the output-String
- or at least untilmaxLengthInner
is reached!.separateLines
- When this parameter receives TRUE, each of the sub-array's (int[]
) will be separated by a newline'\n'
character. When this parameter is passed FALSE, allint[]
inner-arrays will be printed to a single-line of text.maxLengthInner
- This parameter may be null, or negative, and if it is, it shall be ignored. Remember that Java Compiler syntax allows primitive-literals to be passed to function-parameters of typeInteger
. This means, you may pass'80'
here, rather than worrying about passingInteger.valueOf(80)
. Again, you may also pass'null'
, effectively making thie somewhat similar to a JavaScript "Optional" parameter.
When this parameter receives a non-nullint
, its intention is to specify an outputString's
'maximum-length' - for the 'Inner' Arrays (sub-Arrays). When this 'maximumString
length' is reached, an' ...'
(ellipsis) is appended to the end of the printed-String
, and the printing logic moves on to the next sub-array.
NOTE: Unlike parametermaxLengthOuter
, this parameter is specifying the maximum number of characters that the user wants printed to the outputString
- not the number array-element int's.
FINALLY: Because the output-String
sub-arrays contain spaces, commas, and brackets, when a positive integer is passed here, that integer must be greater than'7'
. If it is not, anIllegalArgumentException
is thrown. Again, a negative value shall be treated the same as if'null'
were passed.maxLengthOuter
- This parameter may be null, or negative, and if it is, it will be ignored.
Again, Java compiler-syntax allows integer literals to be passed directly to this parameter.
This 'maximum length' specifies how many of the sub-arrays will be printed to the Output-String
. If there are more sub-arrays than the value passed tomaxLengthOter
, an elipsis' ...'
shall be appended to the end of the returned-String
.
NOTE: The meaning of this integer-value is different than that of parameter'maxLengthInner'
. Here, it is used to specify a maximum number of elements (specifically, 'sub-arrays'), while in'maxLengthInner'
, it is meant to specify a maximum number of output characters (a.k.a. aString
-length).
Remember that printing a large-two dimensional array will will result in a very longString
. In conjunction with the'separateLines'
paramater and/or empty-String
return-values from the'toString'
lambda, the length of the output-String
can be reasonably sized.- Returns:
- A printed version of this two-dimensional array.
- Throws:
java.lang.NullPointerException
- If the'toString'
method is non-null, but returns a null value.java.lang.IllegalArgumentException
- If'maxLengthInner'
is less than'7'
.- Code:
- Exact Method Body:
// maxLengthInner *MUST* be >= 7, since minimum-string-length is " [...]" // A value of 0 to 6 must throw an exception. // A negative value is treated the same as 'null' if ((maxLengthInner != null) && (maxLengthInner < 7) && (maxLengthInner >= 0)) throw new IllegalArgumentException (IAE_MESSAGE_MAXLENINNER.replace("TOK", maxLengthInner.toString())); else if ((maxLengthInner != null) && (maxLengthInner < 0)) maxLengthInner = null; StringBuilder sbOuter = new StringBuilder(); // Primary StringBuilder StringBuilder sbInner = new StringBuilder(); // StrinBuilder for the sub/inner arrays String s = null; // temp variable int i = 0; // outer-loop loop-counter int numRows = 0; // Count sub-arrays // If the value passed to 'maxLengthOuter' is negative, treat it the same as 'null' // SPECIFICALLY: No Max Length. if ((maxLengthOuter != null) && (maxLengthOuter < 0)) maxLengthOuter = null; sbOuter.append('['); for (i=0; i < arr.length; i++) { // The "Keep Row" Predicate is used to check whether a sub-array should even be // included at all. If "Keep Row" exists, and it rejects the outer array index, // then the entire row itself should be skipped. if ((keepRow != null) && (! keepRow.test(i))) continue; numRows++; if ((maxLengthOuter != null) && (numRows > maxLengthOuter)) break; int j = 0; // System.out.println("end: " + end + ",\ti: " + i + ",\tj: " + j); // The purpose to this sub-loop is such that there is a "provided user option" where // the 'toString-lambda' may return a ZERO-LENGTH-STRING, and when this happens, the // array-location that resulted in a ZERO-LENGTH-STRING shall be ignored/skipped // completely. if (toString != null) while ( (j < arr[i].length) && ((s = toString.apply(i, j, arr[i][j])).length() == 0) ) j++; else s = "" + arr[i][j]; // If "separateLines" be sure to add a newline. if (separateLines) sbOuter.append('\n'); // Add the opening brackets to this new sub-array that was just computed. sbInner.append(" [" + s); j++; // Main Printing Loop while (j < arr[i].length) { if (toString != null) { if ((s = toString.apply(i, j, arr[i][j++])).length() > 0) sbInner.append(", " + s); } else sbInner.append(", " + arr[i][j++]); if ((maxLengthInner != null) && (sbInner.length() > maxLengthInner)) break; } // NOTE: The '+ 1' is needed because, as of yet, the trailing ']' has not been added. if ((maxLengthInner != null) && ((sbInner.length() + 1) > maxLengthInner)) { int pos = sbInner.length() - 4; char c = sbInner.charAt(pos); while ((c != ',') && (c != '[')) c = sbInner.charAt(--pos); sbInner.setLength(pos+1); sbInner.append((c == '[') ? "..." : " ..."); } sbInner.append(']'); sbOuter.append(sbInner.toString()); sbInner.setLength(0); } // This shall only execute if the if (i < arr.length) sbOuter.append((separateLines ? "\n" : "") + " ..."); sbOuter.append(separateLines ? "\n]" : " ]"); return sbOuter.toString();
-
toCSV
public static java.lang.String toCSV (long[][] arr, IntIntLongFunc<java.lang.String> toString, java.util.function.IntPredicate keepRow, boolean separateLines, java.lang.Integer maxLengthInner, java.lang.Integer maxLengthOuter)
This class prints a two dimensionallong
-array to aString
.- Parameters:
arr
- This may be any Java Two-Dimensionallong
-array.toString
- This Java Lambda (Functional Interface) Parameter is one which accepts two integers, and a primitive-long
. The integers provided shall contain the indices to an element of the 2D Array parameter, and thelong
at that Array location. ThetoString.apply(int, int, long)
method inside this lambda should return any JavaString
to be printed to the output, for the given array-location and value.
NOTE: This lambda may return a zero-lengthString
, and when or if it does, that particular array-location will be 'skipped' (not printed) to the outputString
.
ALSO: This parameter may be null, and if it is, it will be ignored and all array-long's
, themselves, will simply be printed to the outputString
instead.
FINALLY: If this lambda's'apply'
function ever returns null, it will result in aNullPointerException
keepRow
- This parameter may also be passed null, and if it is, it's also ignored. This integer-Predicate
receives an'int'
that corresponds to the first-order array index (the outer-array-index, a.k.a.'long[x][]'
). When this lambda-Predicate
returnsTRUE
the inner sub-array'long[]'
will be included in the output-String
. When this lamda returnsFALSE
, the entire sub-array will be eliminated from the output-String
.
AGAIN: When this lamda-Predicate
receives null, it is ignored, meaning all sub-arrays are printed to the output-String
- or at least untilmaxLengthInner
is reached!.separateLines
- When this parameter receives TRUE, each of the sub-array's (long[]
) will be separated by a newline'\n'
character. When this parameter is passed FALSE, alllong[]
inner-arrays will be printed to a single-line of text.maxLengthInner
- This parameter may be null, or negative, and if it is, it shall be ignored. Remember that Java Compiler syntax allows primitive-literals to be passed to function-parameters of typeInteger
. This means, you may pass'80'
here, rather than worrying about passingInteger.valueOf(80)
. Again, you may also pass'null'
, effectively making thie somewhat similar to a JavaScript "Optional" parameter.
When this parameter receives a non-nullint
, its intention is to specify an outputString's
'maximum-length' - for the 'Inner' Arrays (sub-Arrays). When this 'maximumString
length' is reached, an' ...'
(ellipsis) is appended to the end of the printed-String
, and the printing logic moves on to the next sub-array.
NOTE: Unlike parametermaxLengthOuter
, this parameter is specifying the maximum number of characters that the user wants printed to the outputString
- not the number array-element long's.
FINALLY: Because the output-String
sub-arrays contain spaces, commas, and brackets, when a positive integer is passed here, that integer must be greater than'7'
. If it is not, anIllegalArgumentException
is thrown. Again, a negative value shall be treated the same as if'null'
were passed.maxLengthOuter
- This parameter may be null, or negative, and if it is, it will be ignored.
Again, Java compiler-syntax allows integer literals to be passed directly to this parameter.
This 'maximum length' specifies how many of the sub-arrays will be printed to the Output-String
. If there are more sub-arrays than the value passed tomaxLengthOter
, an elipsis' ...'
shall be appended to the end of the returned-String
.
NOTE: The meaning of this integer-value is different than that of parameter'maxLengthInner'
. Here, it is used to specify a maximum number of elements (specifically, 'sub-arrays'), while in'maxLengthInner'
, it is meant to specify a maximum number of output characters (a.k.a. aString
-length).
Remember that printing a large-two dimensional array will will result in a very longString
. In conjunction with the'separateLines'
paramater and/or empty-String
return-values from the'toString'
lambda, the length of the output-String
can be reasonably sized.- Returns:
- A printed version of this two-dimensional array.
- Throws:
java.lang.NullPointerException
- If the'toString'
method is non-null, but returns a null value.java.lang.IllegalArgumentException
- If'maxLengthInner'
is less than'7'
.- Code:
- Exact Method Body:
// maxLengthInner *MUST* be >= 7, since minimum-string-length is " [...]" // A value of 0 to 6 must throw an exception. // A negative value is treated the same as 'null' if ((maxLengthInner != null) && (maxLengthInner < 7) && (maxLengthInner >= 0)) throw new IllegalArgumentException (IAE_MESSAGE_MAXLENINNER.replace("TOK", maxLengthInner.toString())); else if ((maxLengthInner != null) && (maxLengthInner < 0)) maxLengthInner = null; StringBuilder sbOuter = new StringBuilder(); // Primary StringBuilder StringBuilder sbInner = new StringBuilder(); // StrinBuilder for the sub/inner arrays String s = null; // temp variable int i = 0; // outer-loop loop-counter int numRows = 0; // Count sub-arrays // If the value passed to 'maxLengthOuter' is negative, treat it the same as 'null' // SPECIFICALLY: No Max Length. if ((maxLengthOuter != null) && (maxLengthOuter < 0)) maxLengthOuter = null; sbOuter.append('['); for (i=0; i < arr.length; i++) { // The "Keep Row" Predicate is used to check whether a sub-array should even be // included at all. If "Keep Row" exists, and it rejects the outer array index, // then the entire row itself should be skipped. if ((keepRow != null) && (! keepRow.test(i))) continue; numRows++; if ((maxLengthOuter != null) && (numRows > maxLengthOuter)) break; int j = 0; // System.out.println("end: " + end + ",\ti: " + i + ",\tj: " + j); // The purpose to this sub-loop is such that there is a "provided user option" where // the 'toString-lambda' may return a ZERO-LENGTH-STRING, and when this happens, the // array-location that resulted in a ZERO-LENGTH-STRING shall be ignored/skipped // completely. if (toString != null) while ( (j < arr[i].length) && ((s = toString.apply(i, j, arr[i][j])).length() == 0) ) j++; else s = "" + arr[i][j]; // If "separateLines" be sure to add a newline. if (separateLines) sbOuter.append('\n'); // Add the opening brackets to this new sub-array that was just computed. sbInner.append(" [" + s); j++; // Main Printing Loop while (j < arr[i].length) { if (toString != null) { if ((s = toString.apply(i, j, arr[i][j++])).length() > 0) sbInner.append(", " + s); } else sbInner.append(", " + arr[i][j++]); if ((maxLengthInner != null) && (sbInner.length() > maxLengthInner)) break; } // NOTE: The '+ 1' is needed because, as of yet, the trailing ']' has not been added. if ((maxLengthInner != null) && ((sbInner.length() + 1) > maxLengthInner)) { int pos = sbInner.length() - 4; char c = sbInner.charAt(pos); while ((c != ',') && (c != '[')) c = sbInner.charAt(--pos); sbInner.setLength(pos+1); sbInner.append((c == '[') ? "..." : " ..."); } sbInner.append(']'); sbOuter.append(sbInner.toString()); sbInner.setLength(0); } // This shall only execute if the if (i < arr.length) sbOuter.append((separateLines ? "\n" : "") + " ..."); sbOuter.append(separateLines ? "\n]" : " ]"); return sbOuter.toString();
-
toCSV
public static java.lang.String toCSV (float[][] arr, IntIntFloatFunc<java.lang.String> toString, java.util.function.IntPredicate keepRow, boolean separateLines, java.lang.Integer maxLengthInner, java.lang.Integer maxLengthOuter)
This class prints a two dimensionalfloat
-array to aString
.- Parameters:
arr
- This may be any Java Two-Dimensionalfloat
-array.toString
- This Java Lambda (Functional Interface) Parameter is one which accepts two integers, and a primitive-float
. The integers provided shall contain the indices to an element of the 2D Array parameter, and thefloat
at that Array location. ThetoString.apply(int, int, float)
method inside this lambda should return any JavaString
to be printed to the output, for the given array-location and value.
NOTE: This lambda may return a zero-lengthString
, and when or if it does, that particular array-location will be 'skipped' (not printed) to the outputString
.
ALSO: This parameter may be null, and if it is, it will be ignored and all array-float's
, themselves, will simply be printed to the outputString
instead.
FINALLY: If this lambda's'apply'
function ever returns null, it will result in aNullPointerException
keepRow
- This parameter may also be passed null, and if it is, it's also ignored. This integer-Predicate
receives an'int'
that corresponds to the first-order array index (the outer-array-index, a.k.a.'float[x][]'
). When this lambda-Predicate
returnsTRUE
the inner sub-array'float[]'
will be included in the output-String
. When this lamda returnsFALSE
, the entire sub-array will be eliminated from the output-String
.
AGAIN: When this lamda-Predicate
receives null, it is ignored, meaning all sub-arrays are printed to the output-String
- or at least untilmaxLengthInner
is reached!.separateLines
- When this parameter receives TRUE, each of the sub-array's (float[]
) will be separated by a newline'\n'
character. When this parameter is passed FALSE, allfloat[]
inner-arrays will be printed to a single-line of text.maxLengthInner
- This parameter may be null, or negative, and if it is, it shall be ignored. Remember that Java Compiler syntax allows primitive-literals to be passed to function-parameters of typeInteger
. This means, you may pass'80'
here, rather than worrying about passingInteger.valueOf(80)
. Again, you may also pass'null'
, effectively making thie somewhat similar to a JavaScript "Optional" parameter.
When this parameter receives a non-nullint
, its intention is to specify an outputString's
'maximum-length' - for the 'Inner' Arrays (sub-Arrays). When this 'maximumString
length' is reached, an' ...'
(ellipsis) is appended to the end of the printed-String
, and the printing logic moves on to the next sub-array.
NOTE: Unlike parametermaxLengthOuter
, this parameter is specifying the maximum number of characters that the user wants printed to the outputString
- not the number array-element float's.
FINALLY: Because the output-String
sub-arrays contain spaces, commas, and brackets, when a positive integer is passed here, that integer must be greater than'7'
. If it is not, anIllegalArgumentException
is thrown. Again, a negative value shall be treated the same as if'null'
were passed.maxLengthOuter
- This parameter may be null, or negative, and if it is, it will be ignored.
Again, Java compiler-syntax allows integer literals to be passed directly to this parameter.
This 'maximum length' specifies how many of the sub-arrays will be printed to the Output-String
. If there are more sub-arrays than the value passed tomaxLengthOter
, an elipsis' ...'
shall be appended to the end of the returned-String
.
NOTE: The meaning of this integer-value is different than that of parameter'maxLengthInner'
. Here, it is used to specify a maximum number of elements (specifically, 'sub-arrays'), while in'maxLengthInner'
, it is meant to specify a maximum number of output characters (a.k.a. aString
-length).
Remember that printing a large-two dimensional array will will result in a very longString
. In conjunction with the'separateLines'
paramater and/or empty-String
return-values from the'toString'
lambda, the length of the output-String
can be reasonably sized.- Returns:
- A printed version of this two-dimensional array.
- Throws:
java.lang.NullPointerException
- If the'toString'
method is non-null, but returns a null value.java.lang.IllegalArgumentException
- If'maxLengthInner'
is less than'7'
.- Code:
- Exact Method Body:
// maxLengthInner *MUST* be >= 7, since minimum-string-length is " [...]" // A value of 0 to 6 must throw an exception. // A negative value is treated the same as 'null' if ((maxLengthInner != null) && (maxLengthInner < 7) && (maxLengthInner >= 0)) throw new IllegalArgumentException (IAE_MESSAGE_MAXLENINNER.replace("TOK", maxLengthInner.toString())); else if ((maxLengthInner != null) && (maxLengthInner < 0)) maxLengthInner = null; StringBuilder sbOuter = new StringBuilder(); // Primary StringBuilder StringBuilder sbInner = new StringBuilder(); // StrinBuilder for the sub/inner arrays String s = null; // temp variable int i = 0; // outer-loop loop-counter int numRows = 0; // Count sub-arrays // If the value passed to 'maxLengthOuter' is negative, treat it the same as 'null' // SPECIFICALLY: No Max Length. if ((maxLengthOuter != null) && (maxLengthOuter < 0)) maxLengthOuter = null; sbOuter.append('['); for (i=0; i < arr.length; i++) { // The "Keep Row" Predicate is used to check whether a sub-array should even be // included at all. If "Keep Row" exists, and it rejects the outer array index, // then the entire row itself should be skipped. if ((keepRow != null) && (! keepRow.test(i))) continue; numRows++; if ((maxLengthOuter != null) && (numRows > maxLengthOuter)) break; int j = 0; // System.out.println("end: " + end + ",\ti: " + i + ",\tj: " + j); // The purpose to this sub-loop is such that there is a "provided user option" where // the 'toString-lambda' may return a ZERO-LENGTH-STRING, and when this happens, the // array-location that resulted in a ZERO-LENGTH-STRING shall be ignored/skipped // completely. if (toString != null) while ( (j < arr[i].length) && ((s = toString.apply(i, j, arr[i][j])).length() == 0) ) j++; else s = "" + arr[i][j]; // If "separateLines" be sure to add a newline. if (separateLines) sbOuter.append('\n'); // Add the opening brackets to this new sub-array that was just computed. sbInner.append(" [" + s); j++; // Main Printing Loop while (j < arr[i].length) { if (toString != null) { if ((s = toString.apply(i, j, arr[i][j++])).length() > 0) sbInner.append(", " + s); } else sbInner.append(", " + arr[i][j++]); if ((maxLengthInner != null) && (sbInner.length() > maxLengthInner)) break; } // NOTE: The '+ 1' is needed because, as of yet, the trailing ']' has not been added. if ((maxLengthInner != null) && ((sbInner.length() + 1) > maxLengthInner)) { int pos = sbInner.length() - 4; char c = sbInner.charAt(pos); while ((c != ',') && (c != '[')) c = sbInner.charAt(--pos); sbInner.setLength(pos+1); sbInner.append((c == '[') ? "..." : " ..."); } sbInner.append(']'); sbOuter.append(sbInner.toString()); sbInner.setLength(0); } // This shall only execute if the if (i < arr.length) sbOuter.append((separateLines ? "\n" : "") + " ..."); sbOuter.append(separateLines ? "\n]" : " ]"); return sbOuter.toString();
-
toCSV
public static java.lang.String toCSV (double[][] arr, IntIntDoubleFunc<java.lang.String> toString, java.util.function.IntPredicate keepRow, boolean separateLines, java.lang.Integer maxLengthInner, java.lang.Integer maxLengthOuter)
This class prints a two dimensionaldouble
-array to aString
.- Parameters:
arr
- This may be any Java Two-Dimensionaldouble
-array.toString
- This Java Lambda (Functional Interface) Parameter is one which accepts two integers, and a primitive-double
. The integers provided shall contain the indices to an element of the 2D Array parameter, and thedouble
at that Array location. ThetoString.apply(int, int, double)
method inside this lambda should return any JavaString
to be printed to the output, for the given array-location and value.
NOTE: This lambda may return a zero-lengthString
, and when or if it does, that particular array-location will be 'skipped' (not printed) to the outputString
.
ALSO: This parameter may be null, and if it is, it will be ignored and all array-double's
, themselves, will simply be printed to the outputString
instead.
FINALLY: If this lambda's'apply'
function ever returns null, it will result in aNullPointerException
keepRow
- This parameter may also be passed null, and if it is, it's also ignored. This integer-Predicate
receives an'int'
that corresponds to the first-order array index (the outer-array-index, a.k.a.'double[x][]'
). When this lambda-Predicate
returnsTRUE
the inner sub-array'double[]'
will be included in the output-String
. When this lamda returnsFALSE
, the entire sub-array will be eliminated from the output-String
.
AGAIN: When this lamda-Predicate
receives null, it is ignored, meaning all sub-arrays are printed to the output-String
- or at least untilmaxLengthInner
is reached!.separateLines
- When this parameter receives TRUE, each of the sub-array's (double[]
) will be separated by a newline'\n'
character. When this parameter is passed FALSE, alldouble[]
inner-arrays will be printed to a single-line of text.maxLengthInner
- This parameter may be null, or negative, and if it is, it shall be ignored. Remember that Java Compiler syntax allows primitive-literals to be passed to function-parameters of typeInteger
. This means, you may pass'80'
here, rather than worrying about passingInteger.valueOf(80)
. Again, you may also pass'null'
, effectively making thie somewhat similar to a JavaScript "Optional" parameter.
When this parameter receives a non-nullint
, its intention is to specify an outputString's
'maximum-length' - for the 'Inner' Arrays (sub-Arrays). When this 'maximumString
length' is reached, an' ...'
(ellipsis) is appended to the end of the printed-String
, and the printing logic moves on to the next sub-array.
NOTE: Unlike parametermaxLengthOuter
, this parameter is specifying the maximum number of characters that the user wants printed to the outputString
- not the number array-element double's.
FINALLY: Because the output-String
sub-arrays contain spaces, commas, and brackets, when a positive integer is passed here, that integer must be greater than'7'
. If it is not, anIllegalArgumentException
is thrown. Again, a negative value shall be treated the same as if'null'
were passed.maxLengthOuter
- This parameter may be null, or negative, and if it is, it will be ignored.
Again, Java compiler-syntax allows integer literals to be passed directly to this parameter.
This 'maximum length' specifies how many of the sub-arrays will be printed to the Output-String
. If there are more sub-arrays than the value passed tomaxLengthOter
, an elipsis' ...'
shall be appended to the end of the returned-String
.
NOTE: The meaning of this integer-value is different than that of parameter'maxLengthInner'
. Here, it is used to specify a maximum number of elements (specifically, 'sub-arrays'), while in'maxLengthInner'
, it is meant to specify a maximum number of output characters (a.k.a. aString
-length).
Remember that printing a large-two dimensional array will will result in a very longString
. In conjunction with the'separateLines'
paramater and/or empty-String
return-values from the'toString'
lambda, the length of the output-String
can be reasonably sized.- Returns:
- A printed version of this two-dimensional array.
- Throws:
java.lang.NullPointerException
- If the'toString'
method is non-null, but returns a null value.java.lang.IllegalArgumentException
- If'maxLengthInner'
is less than'7'
.- Code:
- Exact Method Body:
// maxLengthInner *MUST* be >= 7, since minimum-string-length is " [...]" // A value of 0 to 6 must throw an exception. // A negative value is treated the same as 'null' if ((maxLengthInner != null) && (maxLengthInner < 7) && (maxLengthInner >= 0)) throw new IllegalArgumentException (IAE_MESSAGE_MAXLENINNER.replace("TOK", maxLengthInner.toString())); else if ((maxLengthInner != null) && (maxLengthInner < 0)) maxLengthInner = null; StringBuilder sbOuter = new StringBuilder(); // Primary StringBuilder StringBuilder sbInner = new StringBuilder(); // StrinBuilder for the sub/inner arrays String s = null; // temp variable int i = 0; // outer-loop loop-counter int numRows = 0; // Count sub-arrays // If the value passed to 'maxLengthOuter' is negative, treat it the same as 'null' // SPECIFICALLY: No Max Length. if ((maxLengthOuter != null) && (maxLengthOuter < 0)) maxLengthOuter = null; sbOuter.append('['); for (i=0; i < arr.length; i++) { // The "Keep Row" Predicate is used to check whether a sub-array should even be // included at all. If "Keep Row" exists, and it rejects the outer array index, // then the entire row itself should be skipped. if ((keepRow != null) && (! keepRow.test(i))) continue; numRows++; if ((maxLengthOuter != null) && (numRows > maxLengthOuter)) break; int j = 0; // System.out.println("end: " + end + ",\ti: " + i + ",\tj: " + j); // The purpose to this sub-loop is such that there is a "provided user option" where // the 'toString-lambda' may return a ZERO-LENGTH-STRING, and when this happens, the // array-location that resulted in a ZERO-LENGTH-STRING shall be ignored/skipped // completely. if (toString != null) while ( (j < arr[i].length) && ((s = toString.apply(i, j, arr[i][j])).length() == 0) ) j++; else s = "" + arr[i][j]; // If "separateLines" be sure to add a newline. if (separateLines) sbOuter.append('\n'); // Add the opening brackets to this new sub-array that was just computed. sbInner.append(" [" + s); j++; // Main Printing Loop while (j < arr[i].length) { if (toString != null) { if ((s = toString.apply(i, j, arr[i][j++])).length() > 0) sbInner.append(", " + s); } else sbInner.append(", " + arr[i][j++]); if ((maxLengthInner != null) && (sbInner.length() > maxLengthInner)) break; } // NOTE: The '+ 1' is needed because, as of yet, the trailing ']' has not been added. if ((maxLengthInner != null) && ((sbInner.length() + 1) > maxLengthInner)) { int pos = sbInner.length() - 4; char c = sbInner.charAt(pos); while ((c != ',') && (c != '[')) c = sbInner.charAt(--pos); sbInner.setLength(pos+1); sbInner.append((c == '[') ? "..." : " ..."); } sbInner.append(']'); sbOuter.append(sbInner.toString()); sbInner.setLength(0); } // This shall only execute if the if (i < arr.length) sbOuter.append((separateLines ? "\n" : "") + " ..."); sbOuter.append(separateLines ? "\n]" : " ]"); return sbOuter.toString();
-
toCSV
public static java.lang.String toCSV (char[][] arr, IntIntCharFunc<java.lang.String> toString, java.util.function.IntPredicate keepRow, boolean separateLines, java.lang.Integer maxLengthInner, java.lang.Integer maxLengthOuter)
This class prints a two dimensionalchar
-array to aString
.- Parameters:
arr
- This may be any Java Two-Dimensionalchar
-array.toString
- This Java Lambda (Functional Interface) Parameter is one which accepts two integers, and a primitive-char
. The integers provided shall contain the indices to an element of the 2D Array parameter, and thechar
at that Array location. ThetoString.apply(int, int, char)
method inside this lambda should return any JavaString
to be printed to the output, for the given array-location and value.
NOTE: This lambda may return a zero-lengthString
, and when or if it does, that particular array-location will be 'skipped' (not printed) to the outputString
.
ALSO: This parameter may be null, and if it is, it will be ignored and all array-char's
, themselves, will simply be printed to the outputString
instead.
FINALLY: If this lambda's'apply'
function ever returns null, it will result in aNullPointerException
keepRow
- This parameter may also be passed null, and if it is, it's also ignored. This integer-Predicate
receives an'int'
that corresponds to the first-order array index (the outer-array-index, a.k.a.'char[x][]'
). When this lambda-Predicate
returnsTRUE
the inner sub-array'char[]'
will be included in the output-String
. When this lamda returnsFALSE
, the entire sub-array will be eliminated from the output-String
.
AGAIN: When this lamda-Predicate
receives null, it is ignored, meaning all sub-arrays are printed to the output-String
- or at least untilmaxLengthInner
is reached!.separateLines
- When this parameter receives TRUE, each of the sub-array's (char[]
) will be separated by a newline'\n'
character. When this parameter is passed FALSE, allchar[]
inner-arrays will be printed to a single-line of text.maxLengthInner
- This parameter may be null, or negative, and if it is, it shall be ignored. Remember that Java Compiler syntax allows primitive-literals to be passed to function-parameters of typeInteger
. This means, you may pass'80'
here, rather than worrying about passingInteger.valueOf(80)
. Again, you may also pass'null'
, effectively making thie somewhat similar to a JavaScript "Optional" parameter.
When this parameter receives a non-nullint
, its intention is to specify an outputString's
'maximum-length' - for the 'Inner' Arrays (sub-Arrays). When this 'maximumString
length' is reached, an' ...'
(ellipsis) is appended to the end of the printed-String
, and the printing logic moves on to the next sub-array.
NOTE: Unlike parametermaxLengthOuter
, this parameter is specifying the maximum number of characters that the user wants printed to the outputString
- not the number array-element char's.
FINALLY: Because the output-String
sub-arrays contain spaces, commas, and brackets, when a positive integer is passed here, that integer must be greater than'7'
. If it is not, anIllegalArgumentException
is thrown. Again, a negative value shall be treated the same as if'null'
were passed.maxLengthOuter
- This parameter may be null, or negative, and if it is, it will be ignored.
Again, Java compiler-syntax allows integer literals to be passed directly to this parameter.
This 'maximum length' specifies how many of the sub-arrays will be printed to the Output-String
. If there are more sub-arrays than the value passed tomaxLengthOter
, an elipsis' ...'
shall be appended to the end of the returned-String
.
NOTE: The meaning of this integer-value is different than that of parameter'maxLengthInner'
. Here, it is used to specify a maximum number of elements (specifically, 'sub-arrays'), while in'maxLengthInner'
, it is meant to specify a maximum number of output characters (a.k.a. aString
-length).
Remember that printing a large-two dimensional array will will result in a very longString
. In conjunction with the'separateLines'
paramater and/or empty-String
return-values from the'toString'
lambda, the length of the output-String
can be reasonably sized.- Returns:
- A printed version of this two-dimensional array.
- Throws:
java.lang.NullPointerException
- If the'toString'
method is non-null, but returns a null value.java.lang.IllegalArgumentException
- If'maxLengthInner'
is less than'7'
.- Code:
- Exact Method Body:
// maxLengthInner *MUST* be >= 7, since minimum-string-length is " [...]" // A value of 0 to 6 must throw an exception. // A negative value is treated the same as 'null' if ((maxLengthInner != null) && (maxLengthInner < 7) && (maxLengthInner >= 0)) throw new IllegalArgumentException (IAE_MESSAGE_MAXLENINNER.replace("TOK", maxLengthInner.toString())); else if ((maxLengthInner != null) && (maxLengthInner < 0)) maxLengthInner = null; StringBuilder sbOuter = new StringBuilder(); // Primary StringBuilder StringBuilder sbInner = new StringBuilder(); // StrinBuilder for the sub/inner arrays String s = null; // temp variable int i = 0; // outer-loop loop-counter int numRows = 0; // Count sub-arrays // If the value passed to 'maxLengthOuter' is negative, treat it the same as 'null' // SPECIFICALLY: No Max Length. if ((maxLengthOuter != null) && (maxLengthOuter < 0)) maxLengthOuter = null; sbOuter.append('['); for (i=0; i < arr.length; i++) { // The "Keep Row" Predicate is used to check whether a sub-array should even be // included at all. If "Keep Row" exists, and it rejects the outer array index, // then the entire row itself should be skipped. if ((keepRow != null) && (! keepRow.test(i))) continue; numRows++; if ((maxLengthOuter != null) && (numRows > maxLengthOuter)) break; int j = 0; // System.out.println("end: " + end + ",\ti: " + i + ",\tj: " + j); // The purpose to this sub-loop is such that there is a "provided user option" where // the 'toString-lambda' may return a ZERO-LENGTH-STRING, and when this happens, the // array-location that resulted in a ZERO-LENGTH-STRING shall be ignored/skipped // completely. if (toString != null) while ( (j < arr[i].length) && ((s = toString.apply(i, j, arr[i][j])).length() == 0) ) j++; else s = "" + arr[i][j]; // If "separateLines" be sure to add a newline. if (separateLines) sbOuter.append('\n'); // Add the opening brackets to this new sub-array that was just computed. sbInner.append(" [" + s); j++; // Main Printing Loop while (j < arr[i].length) { if (toString != null) { if ((s = toString.apply(i, j, arr[i][j++])).length() > 0) sbInner.append(", " + s); } else sbInner.append(", " + arr[i][j++]); if ((maxLengthInner != null) && (sbInner.length() > maxLengthInner)) break; } // NOTE: The '+ 1' is needed because, as of yet, the trailing ']' has not been added. if ((maxLengthInner != null) && ((sbInner.length() + 1) > maxLengthInner)) { int pos = sbInner.length() - 4; char c = sbInner.charAt(pos); while ((c != ',') && (c != '[')) c = sbInner.charAt(--pos); sbInner.setLength(pos+1); sbInner.append((c == '[') ? "..." : " ..."); } sbInner.append(']'); sbOuter.append(sbInner.toString()); sbInner.setLength(0); } // This shall only execute if the if (i < arr.length) sbOuter.append((separateLines ? "\n" : "") + " ..."); sbOuter.append(separateLines ? "\n]" : " ]"); return sbOuter.toString();
-
toCSV
public static java.lang.String toCSV (boolean[][] arr, IntIntBoolFunc<java.lang.String> toString, java.util.function.IntPredicate keepRow, boolean separateLines, java.lang.Integer maxLengthInner, java.lang.Integer maxLengthOuter)
This class prints a two dimensionalboolean
-array to aString
.- Parameters:
arr
- This may be any Java Two-Dimensionalboolean
-array.toString
- This Java Lambda (Functional Interface) Parameter is one which accepts two integers, and a primitive-boolean
. The integers provided shall contain the indices to an element of the 2D Array parameter, and theboolean
at that Array location. ThetoString.apply(int, int, boolean)
method inside this lambda should return any JavaString
to be printed to the output, for the given array-location and value.
NOTE: This lambda may return a zero-lengthString
, and when or if it does, that particular array-location will be 'skipped' (not printed) to the outputString
.
ALSO: This parameter may be null, and if it is, it will be ignored and all array-boolean's
, themselves, will simply be printed to the outputString
instead.
FINALLY: If this lambda's'apply'
function ever returns null, it will result in aNullPointerException
keepRow
- This parameter may also be passed null, and if it is, it's also ignored. This integer-Predicate
receives an'int'
that corresponds to the first-order array index (the outer-array-index, a.k.a.'boolean[x][]'
). When this lambda-Predicate
returnsTRUE
the inner sub-array'boolean[]'
will be included in the output-String
. When this lamda returnsFALSE
, the entire sub-array will be eliminated from the output-String
.
AGAIN: When this lamda-Predicate
receives null, it is ignored, meaning all sub-arrays are printed to the output-String
- or at least untilmaxLengthInner
is reached!.separateLines
- When this parameter receives TRUE, each of the sub-array's (boolean[]
) will be separated by a newline'\n'
character. When this parameter is passed FALSE, allboolean[]
inner-arrays will be printed to a single-line of text.maxLengthInner
- This parameter may be null, or negative, and if it is, it shall be ignored. Remember that Java Compiler syntax allows primitive-literals to be passed to function-parameters of typeInteger
. This means, you may pass'80'
here, rather than worrying about passingInteger.valueOf(80)
. Again, you may also pass'null'
, effectively making thie somewhat similar to a JavaScript "Optional" parameter.
When this parameter receives a non-nullint
, its intention is to specify an outputString's
'maximum-length' - for the 'Inner' Arrays (sub-Arrays). When this 'maximumString
length' is reached, an' ...'
(ellipsis) is appended to the end of the printed-String
, and the printing logic moves on to the next sub-array.
NOTE: Unlike parametermaxLengthOuter
, this parameter is specifying the maximum number of characters that the user wants printed to the outputString
- not the number array-element boolean's.
FINALLY: Because the output-String
sub-arrays contain spaces, commas, and brackets, when a positive integer is passed here, that integer must be greater than'7'
. If it is not, anIllegalArgumentException
is thrown. Again, a negative value shall be treated the same as if'null'
were passed.maxLengthOuter
- This parameter may be null, or negative, and if it is, it will be ignored.
Again, Java compiler-syntax allows integer literals to be passed directly to this parameter.
This 'maximum length' specifies how many of the sub-arrays will be printed to the Output-String
. If there are more sub-arrays than the value passed tomaxLengthOter
, an elipsis' ...'
shall be appended to the end of the returned-String
.
NOTE: The meaning of this integer-value is different than that of parameter'maxLengthInner'
. Here, it is used to specify a maximum number of elements (specifically, 'sub-arrays'), while in'maxLengthInner'
, it is meant to specify a maximum number of output characters (a.k.a. aString
-length).
Remember that printing a large-two dimensional array will will result in a very longString
. In conjunction with the'separateLines'
paramater and/or empty-String
return-values from the'toString'
lambda, the length of the output-String
can be reasonably sized.- Returns:
- A printed version of this two-dimensional array.
- Throws:
java.lang.NullPointerException
- If the'toString'
method is non-null, but returns a null value.java.lang.IllegalArgumentException
- If'maxLengthInner'
is less than'7'
.- Code:
- Exact Method Body:
// maxLengthInner *MUST* be >= 7, since minimum-string-length is " [...]" // A value of 0 to 6 must throw an exception. // A negative value is treated the same as 'null' if ((maxLengthInner != null) && (maxLengthInner < 7) && (maxLengthInner >= 0)) throw new IllegalArgumentException (IAE_MESSAGE_MAXLENINNER.replace("TOK", maxLengthInner.toString())); else if ((maxLengthInner != null) && (maxLengthInner < 0)) maxLengthInner = null; StringBuilder sbOuter = new StringBuilder(); // Primary StringBuilder StringBuilder sbInner = new StringBuilder(); // StrinBuilder for the sub/inner arrays String s = null; // temp variable int i = 0; // outer-loop loop-counter int numRows = 0; // Count sub-arrays // If the value passed to 'maxLengthOuter' is negative, treat it the same as 'null' // SPECIFICALLY: No Max Length. if ((maxLengthOuter != null) && (maxLengthOuter < 0)) maxLengthOuter = null; sbOuter.append('['); for (i=0; i < arr.length; i++) { // The "Keep Row" Predicate is used to check whether a sub-array should even be // included at all. If "Keep Row" exists, and it rejects the outer array index, // then the entire row itself should be skipped. if ((keepRow != null) && (! keepRow.test(i))) continue; numRows++; if ((maxLengthOuter != null) && (numRows > maxLengthOuter)) break; int j = 0; // System.out.println("end: " + end + ",\ti: " + i + ",\tj: " + j); // The purpose to this sub-loop is such that there is a "provided user option" where // the 'toString-lambda' may return a ZERO-LENGTH-STRING, and when this happens, the // array-location that resulted in a ZERO-LENGTH-STRING shall be ignored/skipped // completely. if (toString != null) while ( (j < arr[i].length) && ((s = toString.apply(i, j, arr[i][j])).length() == 0) ) j++; else s = "" + arr[i][j]; // If "separateLines" be sure to add a newline. if (separateLines) sbOuter.append('\n'); // Add the opening brackets to this new sub-array that was just computed. sbInner.append(" [" + s); j++; // Main Printing Loop while (j < arr[i].length) { if (toString != null) { if ((s = toString.apply(i, j, arr[i][j++])).length() > 0) sbInner.append(", " + s); } else sbInner.append(", " + arr[i][j++]); if ((maxLengthInner != null) && (sbInner.length() > maxLengthInner)) break; } // NOTE: The '+ 1' is needed because, as of yet, the trailing ']' has not been added. if ((maxLengthInner != null) && ((sbInner.length() + 1) > maxLengthInner)) { int pos = sbInner.length() - 4; char c = sbInner.charAt(pos); while ((c != ',') && (c != '[')) c = sbInner.charAt(--pos); sbInner.setLength(pos+1); sbInner.append((c == '[') ? "..." : " ..."); } sbInner.append(']'); sbOuter.append(sbInner.toString()); sbInner.setLength(0); } // This shall only execute if the if (i < arr.length) sbOuter.append((separateLines ? "\n" : "") + " ..."); sbOuter.append(separateLines ? "\n]" : " ]"); return sbOuter.toString();
-
-