Package Torello.Java.JSON
Class RJArrIntoPrimStream
- java.lang.Object
-
- Torello.Java.JSON.RJArrIntoPrimStream
-
public class RJArrIntoPrimStream extends java.lang.Object
The JDK has provided simple Primitive-Stream classes among those in its powerful Stream-API classes since JDK 8. This class provides a simple and direct way to convert aJsonArray
into one of Java's three Primitive-Stream types:java.util.stream.IntStream
java.util.stream.LongStream
java.util.stream.DoubleStream
Utilities for parsing Json Array's and sending the parsed values into a Java Primitive Stream.
This class builds on the J2EE Standard 'Glass-Fish' JSON Processor
There are several JSON Parsers available, and even more implementations for serializing and de-serializing data to/from JSON. The tool included in the J2EE is available on GitHub, and that is the one used by the Java HTML JAR Library. (See:javax.json.*
)
Primary Classes Used:JsonArray
andJsonObject
JSON Binding Helper-Class:
JSON-Binding is the art of converting data that has been stored, saved or transmitted usingJava-Script Object Notation
into a Primitive-Type or Object-Type of any Programming Language, for instance Java.JSON
often arrives into Java-Program Memory from an external Internet Connection, and may have traveled hundreds or even thousands of miles from a Host-Server.
Unlike Java-Types which are checked by the Java-Compiler each-and-every time a programmer compiles his project, any guarantee that JSON-Type Data is pristine, uncorrupted, and in any kind of pre-agreed format is never completely assured.
Being able to handle changes that might be made to an API (possibly from great distances away, and without the Software-Manager's consent) is the type of feature that robust JSON-Code simply has to offer.
Binding-Helper Features:- Utilizes the Java-Standard
javax.json.*
Package-Library, & its Glass-Fish Implementation - Handles the Transfer & Conversion of All Json-Type's into Java-Type's with just One Line of Code
- Provides all manner of User-Configurable Exception-Handling &
Error-Decision Management via Class
JFlag
- Provides a Fine-Grained Suite of Exception-Classes, all with Consistent & Meaningful Error-Messages
- Primary Helper-Classes for the (Experimental) Google-Chrome
Headless Browser
Package
Optimize Loops:
For each method within this class, there are actually two different variants of what is, essentially, the exact same method. Methods in this class whose name ends with the letters'Rec'
are methods that will produce what is known as a 'Settings Record', (see classSettingsRec<T, U>
).
Every one of the Json-Processing classes (in this Json Java-Package) that begin with the letters'RJArr'
are classes which iterateJsonArray's
. First, the class does some form of processing and testing to ensure that the contents of the arrays are properly converted into standard Java-Types. After the elements of aJsonArray
are successfully processed, they are then sent to either a Java-Array
, aStream
or a User-ProvidedConsumer
.
In order to guarantee that there is a homogenous algorithm for processing these array indices, only four Java 'for
-loops' have actually been written. (These loops may be easily inspected by clicking on the'Hi-Lited Source-Code'
button inside of classProcessJsonArray
). In this class, in order to differentiate between aJsonArray
which is to be converted into a Javaint[]
-Array, and one which is to be placed into a Javalong[]
-Array, a particular "Settings Record" is constructed (which "configures" the loop's body).
If only one invocation of a method is needed, then using the method that ends with'Arr'
should be perfectly sufficient! If the methods in this class are going to be invoked from inside of a loop (or invoked many times), then it would be much wiser to, first, obtain an instance of theSettingsRec
, and then invoke the Array-Processing code yourself.
In this way, generating twice as many short-lived, ephemeral, object instances (that are immediately discarded, and then collected by the Garbage-Collector) can be avoided. To do this is quite simple. Please review the documentation for the methods in this class whose name ends with'Rec'
- and look at the example code provided for how to use them.
The Loop Optimized variants of the methods in this class are, very simple, two step procedures (as opposed to just a single method invocation).- First, generate an instance of the
SettingsRec
class. Make sure to heed to Generic-Type Parameters returned by the Settings-Record Generator-Method you have called. - Second, simply call the specified Json-Array Processing method from class
ProcessJsonArray
Hi-Lited Source-Code:This File's Source Code:
- View Here: Torello/Java/JSON/RJArrIntoPrimStream.java
- Open New Browser-Tab: Torello/Java/JSON/RJArrIntoPrimStream.java
File Size: 8,122 Bytes Line Count: 204 '\n' Characters Found
Json-Array Reader-Helper:
- View Here: BASIC_TYPES.java
- Open New Browser-Tab: BASIC_TYPES.java
File Size: 16,119 Bytes Line Count: 393 '\n' Characters Found
Json-Array Reader-Helper:
- View Here: STREAM_BUILDER.java
- Open New Browser-Tab: STREAM_BUILDER.java
File Size: 2,282 Bytes Line Count: 73 '\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
- 6 Method(s), 6 declared static
- 0 Field(s)
-
-
Method Summary
Read the Contents of a Json-Array into a Java Primitive-Stream Modifier and Type Method static DoubleStream
doubleArr(JsonArray ja, double defaultValue, int FLAGS, ToDoubleFunction<String> optionalUserParser)
static IntStream
intArr(JsonArray ja, int defaultValue, int FLAGS, ToIntFunction<String> optionalUserParser)
static LongStream
longArr(JsonArray ja, long defaultValue, int FLAGS, ToLongFunction<String> optionalUserParser)
Loop Optimization: Generate a Reusable SettingsRec Instance Modifier and Type Method static SettingsRec<Double,
DoubleStream>doubleRec(double defaultValue, int FLAGS, ToDoubleFunction<String> optionalUserParser)
static SettingsRec<Integer,
IntStream>intRec(int defaultValue, int FLAGS, ToIntFunction<String> optionalUserParser)
static SettingsRec<Long,
LongStream>longRec(long defaultValue, int FLAGS, ToLongFunction<String> optionalUserParser)
-
-
-
Method Detail
-
intArr
public static java.util.stream.IntStream intArr (JsonArray ja, int defaultValue, int FLAGS, java.util.function.ToIntFunction<java.lang.String> optionalUserParser)
Generate a Java (Primitive) IntStreamInvokes: ProcessJsonArray.numericToJava(JsonArray, SettingsRec)
Passes: Java IntStream
Settings-RecordRemoves: Any JFlag
masks which might insert null-entries in the return-streamSee: Class JFlag
for information about parameter'FLAGS'
See: JFlag.NOT_ALLOWED_RET_NULL_MASKS
for a list of all disallowed & filtered flags.Producing an array, int[]
, from a standard JavaIntStream
is a trivial-converstion:
Java Line of Code:
int[] arr = RJArrIntoPrimStream.intArr(ja, -1, 0, null).toArray();
-
longArr
public static java.util.stream.LongStream longArr (JsonArray ja, long defaultValue, int FLAGS, java.util.function.ToLongFunction<java.lang.String> optionalUserParser)
Generate a Java (Primitive) LongStreamInvokes: ProcessJsonArray.numericToJava(JsonArray, SettingsRec)
Passes: Java LongStream
Settings-RecordRemoves: Any JFlag
masks which might insert null-entries in the return-streamSee: Class JFlag
for information about parameter'FLAGS'
See: JFlag.NOT_ALLOWED_RET_NULL_MASKS
for a list of all disallowed & filtered flags.Producing an array, long[]
, from a standard JavaLongStream
is a trivial-converstion:
Java Line of Code:
long[] arr = RJArrIntoPrimStream.longArr(ja, -1, 0, null).toArray();
-
doubleArr
public static java.util.stream.DoubleStream doubleArr (JsonArray ja, double defaultValue, int FLAGS, java.util.function.ToDoubleFunction<java.lang.String> optionalUserParser)
Generate a Java (Primitive) DoubleStreamInvokes: ProcessJsonArray.numericToJava(JsonArray, SettingsRec)
Passes: Java DoubleStream
Settings-RecordRemoves: Any JFlag
masks which might insert null-entries in the return-streamSee: Class JFlag
for information about parameter'FLAGS'
See: JFlag.NOT_ALLOWED_RET_NULL_MASKS
for a list of all disallowed & filtered flags.Producing an array, double[]
, from a standard JavaDoubleStream
is a trivial-converstion:
Java Line of Code:
double[] arr = RJArrIntoPrimStream.doubleArr(ja, -1, 0, null).toArray();
-
intRec
public static SettingsRec<java.lang.Integer,java.util.stream.IntStream> intRec (int defaultValue, int FLAGS, java.util.function.ToIntFunction<java.lang.String> optionalUserParser)
Generate a Java (Primitive) IntStream Configuration-RecordGenerates: A Integer-Retrieval Settings-Record. Used For: The record produced from this method may be used as a parameter to invoke: ProcessJsonArray.numericToJava(JsonArray, SettingsRec)
See: Class JFlag
for information about the'FLAGS'
parameter.Removes: Any JFlag
masks which might insert null-entries in the return-streamSee: JFlag.NOT_ALLOWED_RET_NULL_MASKS
for a list of all disallowed & filtered flags.Producing a Java Primitive 'IntStream' by using this record is a two step process. It may be acheived as Follows:
Example:
SettingsRec<Integer, IntStream> rec = RJArrIntoPrimStream.intRec(0, 0, null); // Reuse the same record in each loop-iteration: only 1 Constructor-Invocation for (JsonArray ja : someJsonArraySource) ProcessJsonArray .numericToJava(ja, rec) // Returns an IntStream (from the parsed JsonArray) .forEach(System.out::println); // Prints out each number in the stream
-
longRec
public static SettingsRec<java.lang.Long,java.util.stream.LongStream> longRec (long defaultValue, int FLAGS, java.util.function.ToLongFunction<java.lang.String> optionalUserParser)
Generate a Java (Primitive) LongStream Configuration-RecodGenerates: A Long-Retrieval Settings-Record. Used For: The record produced from this method may be used as a parameter to invoke: ProcessJsonArray.numericToJava(JsonArray, SettingsRec)
See: Class JFlag
for information about the'FLAGS'
parameter.Removes: Any JFlag
masks which might insert null-entries in the return-streamSee: JFlag.NOT_ALLOWED_RET_NULL_MASKS
for a list of all disallowed & filtered flags.Producing a Java Primitive 'LongStream' by using this record is a two step process. It may be acheived as Follows:
Example:
SettingsRec<Long, LongStream> rec = RJArrIntoPrimStream.longRec(0, 0, null); // Reuse the same record in each loop-iteration: only 1 Constructor-Invocation for (JsonArray ja : someJsonArraySource) ProcessJsonArray .numericToJava(ja, rec) // Returns an IntStream (from the parsed JsonArray) .forEach(System.out::println); // Prints out each number in the stream
-
doubleRec
public static SettingsRec<java.lang.Double,java.util.stream.DoubleStream> doubleRec (double defaultValue, int FLAGS, java.util.function.ToDoubleFunction<java.lang.String> optionalUserParser)
Generate a Java (Primitive) DoubleStream Configuration-RecordGenerates: A Double-Retrieval Settings-Record. Used For: The record produced from this method may be used as a parameter to invoke: ProcessJsonArray.numericToJava(JsonArray, SettingsRec)
See: Class JFlag
for information about the'FLAGS'
parameter.Removes: Any JFlag
masks which might insert null-entries in the return-streamSee: JFlag.NOT_ALLOWED_RET_NULL_MASKS
for a list of all disallowed & filtered flags.Producing a Java Primitive 'DoubleStream' by using this record is a two step process. It may be acheived as Follows:
Example:
SettingsRec<Double, DoubleStream> rec = RJArrIntoPrimStream.doubleRec(0, 0, null); // Reuse the same record in each loop-iteration: only 1 Constructor-Invocation for (JsonArray ja : someJsonArraySource) ProcessJsonArray .numericToJava(ja, rec) // Returns an IntStream (from the parsed JsonArray) .forEach(System.out::println); // Prints out each number in the stream
-
-