Package Torello.HTML

Interface ReplaceFunction

  • All Superinterfaces:
    Functional Interface:
    This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

    public interface ReplaceFunction
    A function-pointer definition that facilitates the substituting of HTMLNode elements in Vectorized-HTML with other, user-provided, elements.

    This simple Functional-Interface should receive an HTMLNode and the position in an HTML-Vector identifying where this node was retrieved (a.k.a. the Vector-index where this node was located). It is up to the user to compute, instantiate & provide a new node to be used to replace the old one.

    This interface is actually as trivial as it looks. The vast majority of the classes in Java-HTML Scrape & Search Packages are written to eliminate the need for worrying too much about complicated for-loops that iterate Vectorized-HTML. For example, all of the NodeSearch package's Find / Get / Remove classes simply cycle through Vector's of HTML pages looking for matches using simply for-loops.

    Though the concept of a for-loop is easy to understand, all of the edge-cases and and assorted details can add up to a very cumbersome amount of work. Below is an example of the simplest of the replacer-methods in this class:

    // The intent of the classes: ReplaceFunction, and ReplaceNodes
    // ... are to make the following example a one-line command, rather than a for-loop:
    if (posArr.length != newNodes.size()) throw new ArrayIndexOutOfBoundsException(
        "The pointer array 'posArr', and the replacement-node array 'newNodes' do not have equal lengths!\n" +
        "posArr.length=" + posArr.length + ", newNodes.size()=" + newNodes.size()
    int newNodesPos = 0;
    for (int pos : posArr) v.setElementAt(newNodes.elementAt(newNodesPos++), pos);

    The code (above) could be "replaced" with the single Line of Code:

    Java Line of Code:
    ReplaceNodes.r(page, imagePosArr, newHTMLImageNodes);

    • Field Detail

      • serialVersionUID

        🡇     🗕  🗗  🗖
        static final long serialVersionUID
        This fulfils the SerialVersion UID requirement for all classes that implement Java's interface Using the Serializable Implementation offered by java is very easy, and can make saving program state when debugging a lot easier. It can also be used in place of more complicated systems like "hibernate" to store data as well.

        Functional Interfaces are usually not thought of as Data Objects that need to be saved, stored and retrieved; however, having the ability to store intermediate results along with the lambda-functions that helped get those results can make debugging easier.
        See Also:
        Constant Field Values
        Exact Field Declaration Expression:
         public static final long serialVersionUID = 1;
    • Method Detail

      • getReplacement

        🡅     🗕  🗗  🗖
        HTMLNode getReplacement​(HTMLNode n,
                                int curVecPos,
                                int iterationCount)
        The intention here is to provide a "replace node" for a particular position in the original-Vector<HTMLNode>

        FunctionalInterface Target-Method:
        This method corresponds to the @FunctionalInterface Annotation's method requirement. It is the only non-default, non-static method in this interface, and may be the target of a Lambda-Expression or '::' (double-colon) Function-Pointer.
        n - This is the "old node" that needs replacing. It will have been obtained from the original HTML page Vector.
        curVecPos - The position in the original Vector. This value may be ignored, but is provided as a matter of convenience.
        iterationCount - The for-loop that will be making calls to 'getReplacement(...)' provides the a loop-count to this method. All that means is there is a "loop variable" that is updated-by-one (incremented) each time this method is called.
        A new node to be substituted for the current node at the position identified by {code 'curArrPos'}.