Class 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 a JsonArray 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 and JsonObject


    JSON Binding Helper-Class:
    JSON-Binding is the art of converting data that has been stored, saved or transmitted using Java-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 class SettingsRec<T, U>).

    Every one of the Json-Processing classes (in this Json Java-Package) that begin with the letters 'RJArr' are classes which iterate JsonArray'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 a JsonArray are successfully processed, they are then sent to either a Java-Array, a Stream or a User-Provided Consumer.

    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 class ProcessJsonArray). In this class, in order to differentiate between a JsonArray which is to be converted into a Java int[]-Array, and one which is to be placed into a Java long[]-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 the SettingsRec, 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).

    1. 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.

    2. Second, simply call the specified Json-Array Processing method from class ProcessJsonArray
    See Also:
    Json, JsonArray



    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)
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • intArr

        🡇         External-Java:      🗕  🗗  🗖
        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) IntStream
        Invokes:   ProcessJsonArray.numericToJava(JsonArray, SettingsRec)
        Passes: Java IntStream Settings-Record
        Removes: Any JFlag masks which might insert null-entries in the return-stream
        See: 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 Java IntStream is a trivial-converstion:
         

        Java Line of Code:
        int[] arr = RJArrIntoPrimStream.intArr(ja, -1, 0, null).toArray();
        
      • longArr

        🡅  🡇         External-Java:      🗕  🗗  🗖
        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) LongStream
        Invokes:   ProcessJsonArray.numericToJava(JsonArray, SettingsRec)
        Passes: Java LongStream Settings-Record
        Removes: Any JFlag masks which might insert null-entries in the return-stream
        See: 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 Java LongStream is a trivial-converstion:
         

        Java Line of Code:
        long[] arr = RJArrIntoPrimStream.longArr(ja, -1, 0, null).toArray();
        
      • doubleArr

        🡅  🡇         External-Java:      🗕  🗗  🗖
        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) DoubleStream
        Invokes:   ProcessJsonArray.numericToJava(JsonArray, SettingsRec)
        Passes: Java DoubleStream Settings-Record
        Removes: Any JFlag masks which might insert null-entries in the return-stream
        See: 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 Java DoubleStream is a trivial-converstion:
         

        Java Line of Code:
        double[] arr = RJArrIntoPrimStream.doubleArr(ja, -1, 0, null).toArray();
        
      • intRec

        🡅  🡇         External-Java:      🗕  🗗  🗖
        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-Record
        Generates: 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-stream
        See:  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

        🡅  🡇         External-Java:      🗕  🗗  🗖
        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-Recod
        Generates: 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-stream
        See:  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

        🡅         External-Java:      🗕  🗗  🗖
        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-Record
        Generates: 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-stream
        See:  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