Package Torello.HTML

Class NodeIndex<NODE extends HTMLNode>

  • Type Parameters:
    NODE - The class of HTMLNode represented by this NodeIndex instance.
    All Implemented Interfaces:
    java.io.Serializable, java.lang.CharSequence, java.lang.Cloneable, java.lang.Comparable<Replaceable>, Replaceable
    Direct Known Subclasses:
    CommentNodeIndex, TagNodeIndex, TextNodeIndex

    public abstract class NodeIndex<NODE extends HTMLNode>
    extends java.lang.Object
    implements java.lang.CharSequence, java.io.Serializable, java.lang.Cloneable, Replaceable
    The abstract parent class of all three NodeIndex classes, TagNodeIndex, TextNodeIndex and CommentNodeIndex.

    NodeIndex: HTMLNode 'plus' the Vector-index

    This class is just an extremely simple data-structure-class used, generally, for returning both the index of an instance-node of class 'HTMLNode' inside a vectorized-html web-page, and also the node itself. This class is the "parent class" of the extending classes: TextNodeIndex, TagNodeIndex and CommentNodeIndex. These instances are returned by all PEEK operations in the node-search package. The constructor of this class accepts an index, and a node and saves them as public fields of this class.

    STALE DATA NOTE: If a vectorized-html webpage is modified after any of these Node + Index classes are created / instantiated, and nodes are added or removed from the webpage, then the (integer) index data inside these classes would have become stale when they are next accessed.

    It is important to remember that Vector-position (a.k.a. "Vector-index") information that is stored inside instances of these (extremely-simple) classes will become stale, immediately if nodes are ever added or removed to the underlying Vector from which these Node + Index object-classes are created.
    See Also:
    HTMLNode, CommentNodeIndex, TagNodeIndex, TextNodeIndex, Serialized Form


    • Field Detail

      • serialVersionUID

        🡇    
        public static final long serialVersionUID
        This fulfils the SerialVersion UID requirement for all classes that implement Java's interface java.io.Serializable. 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.
        See Also:
        Constant Field Values
        Code:
        Exact Field Declaration Expression:
        public static final long serialVersionUID = 1;
        
      • index

        🡅  🡇    
        public final int index
        An index to a node from a web-page. This index must point to a the exact same node inside of a vectorized-html page as the node stored in member-field HTMLNode 'n'.
        Code:
        Exact Field Declaration Expression:
        public final int index;
        
      • n

        🡅  🡇    
        public NODE extends HTMLNode n
        A HTMLNode from a web-page. This node is supposed to be the same node stored at the index specified by member-field int 'index' of some vectorized-html web-page in memory or on disk.
        Code:
        Exact Field Declaration Expression:
        public NODE n;
        
      • comp2

        🡅  🡇    
        public static final java.util.Comparator<TextNodeIndex> comp2
        This is an "alternative Comparitor" that can be used for sorting instances of this class. It should work with the Collections.sort(List, Comparator) method in the standard JDK package java.util.*;

        NOTE: This version utilizes the standard JDK String.compareTo(String) method.
        See Also:
        HTMLNode.str
        Code:
        Exact Field Declaration Expression:
        public static final Comparator<TextNodeIndex> comp2 =
                (TextNodeIndex txni1, TextNodeIndex txni2) -> txni1.n.str.compareTo(txni2.n.str);
        
      • comp3

        🡅  🡇    
        public static final java.util.Comparator<TextNodeIndex> comp3
        This is an "alternative Comparitor" that can be used for sorting instances of this class. It should work with the Collections.sort(List, Comparator) method in the standard JDK package java.util.*;

        NOTE: This version utilizes the standard JDK String.compareToIgnoreCase(String) method.
        See Also:
        HTMLNode.str
        Code:
        Exact Field Declaration Expression:
        public static final Comparator<TextNodeIndex> comp3 =
                (TextNodeIndex txni1, TextNodeIndex txni2) -> txni1.n.str.compareToIgnoreCase(txni2.n.str);
        
    • Constructor Detail

      • NodeIndex

        🡅  🡇    
        protected NodeIndex​(int index,
                            NODE n)
        a default constructor. This assigns a value to the index field.
        Parameters:
        index - This is the vector-index location of HTMLNode 'n' inside of a vectorized-HTML web-page.

        STALE DATA NOTE: This class is a minor-use class, not one of the primary data classes. This instance shall become 'useless' the moment the vector that was used to instantiate this class is modified, and the node 'n' is no longer at vector-index 'index.' These "NodeIndex" classes are retained, not deprecated due to the fundamental nature of using the classes of the NodeSearch Package. Data is easily made stale. Generally, when modifying HTML Vectors, the easiest thing to do is to remember to modify a vector at specific locations by iterating from the end of the vector, back to the beginning. This will generally prevent "state-data vector-indexes" from rearing their ugly head.
        n - An HTMLNode that needs to be the node found in the underlying vector at vector-index 'index.'
        Throws:
        java.lang.IndexOutOfBoundsException - if index is negative, this exception is thrown.
        java.lang.NullPointerException - if n is null.
        Code:
        Exact Constructor Body:
         this.index = index;
         this.n = n;
        
         if (n == null)  throw new NullPointerException(
             "HTMLNode parameter 'n' to this constructor was passed a null value, but this " +
             "is not allowed here."
         );
        
         if (index < 0)  throw new IndexOutOfBoundsException(
             "Integer parameter 'index' to this constructor was passed a negative value: " +
             index
         );
        
    • Method Detail

      • newNodeIndex

        🡅  🡇    
        public static final NodeIndex<?> newNodeIndex​(int index,
                                                      HTMLNode n)
        Simple dispatch method helper that switches on the class of input parameter 'n'.
        Parameters:
        n - Any of the three Java HTML defined HTMLNode subclasses - TagNode, TextNode or CommentNode
        Returns:
        A NodeIndex inheriting class that is appropriate to 'n'.
        Throws:
        java.lang.IllegalArgumentException - If the user has extended class HTMLNode, and passed this unrecognized HTMLNode Type.
        Code:
        Exact Method Body:
         Class<?> newNodeClass = n.getClass();
        
         if (TagNode.class.isAssignableFrom(newNodeClass))
             return new TagNodeIndex(index, (TagNode) n);
        
         if (TextNode.class.isAssignableFrom(newNodeClass))
             return new TextNodeIndex(index, (TextNode) n);
        
         if (CommentNode.class.isAssignableFrom(newNodeClass))
             return new CommentNodeIndex(index, (CommentNode) n);
        
         throw new IllegalArgumentException
             ("Parameter 'n' has a Type that is an Unrecognized HTMLNode-SubClass Type");
        
      • equals

        🡅  🡇    
        public final boolean equals​(java.lang.Object o)
        Java's public boolean equals(Object o) requirements.

        FINAL METHOD: This method is final, and cannot be modified by sub-classes.
        Overrides:
        equals in class java.lang.Object
        Parameters:
        o - This may be any Java Object, but only ones of 'this' type whose internal-values are identical will bring this method to return true.
        Returns:
        TRUE If 'this' equals another object HTMLNode.
        Code:
        Exact Method Body:
         NodeIndex<?> other;
        
         return (    this == o)
                 || (    (o != null)
                     &&  (this.getClass().equals(o.getClass()))
                     &&  ((other = (NodeIndex) o).n.str.equals(this.n.str))
                     &&  (other.index == this.index));
        
      • hashCode

        🡅  🡇    
        public int hashCode()
        Java's hash-code requirement.
        Overrides:
        hashCode in class java.lang.Object
        Returns:
        A hash-code that may be used when storing this node in a java hashed-collection. The index of this NodeIndex ought to be be a unique hash.
        Code:
        Exact Method Body:
         return index;
        
      • charAt

        🡅  🡇    
        public final char charAt​(int index)
        Returns the char value at the specified index of the public final String str field of 'this' field public final HTMLNode n. An index ranges from zero to length() - 1. The first char value of the sequence is at index zero, the next at index one, and so on, as for array indexing.

        NOTE: If the char value specified by the index is a surrogate, the surrogate value is returned.

        FINAL METHOD: This method is final, and cannot be modified by sub-classes.
        Specified by:
        charAt in interface java.lang.CharSequence
        Parameters:
        index - The index of the char value to be returned
        Returns:
        The specified char value
        Code:
        Exact Method Body:
         return n.str.charAt(index);
        
      • length

        🡅  🡇    
        public final int length()
        Returns the length of the public final String str field of 'this' field public final HTMLNode n. The length is the number of 16-bit chars in the sequence.

        FINAL METHOD: This method is final, and cannot be modified by sub-classes.
        Specified by:
        length in interface java.lang.CharSequence
        Returns:
        the number of chars in this.n.str
        Code:
        Exact Method Body:
         return n.str.length();
        
      • subSequence

        🡅  🡇    
        public final java.lang.CharSequence subSequence​(int start,
                                                        int end)
        Returns a CharSequence that is a subsequence of the public final String str field of 'this' field public final HTMLNode n. The subsequence starts with the char value at the specified index and ends with the char value at index end - 1. The length (in chars) of the returned sequence is end - start, so if start == end then an empty sequence is returned.

        FINAL METHOD: This method is final, and cannot be modified by sub-classes.
        Specified by:
        subSequence in interface java.lang.CharSequence
        Parameters:
        start - The start index, inclusive
        end - The end index, exclusive
        Returns:
        The specified subsequence
        Code:
        Exact Method Body:
         return n.str.substring(start, end);
        
      • toString

        🡅  🡇    
        public final java.lang.String toString()
        Returns the public final String str field of 'this' field public final HTMLNode n.

        FINAL METHOD: This method is final, and cannot be modified by sub-classes.
        Specified by:
        toString in interface java.lang.CharSequence
        Overrides:
        toString in class java.lang.Object
        Returns:
        A string consisting of exactly this sequence of characters.
        See Also:
        HTMLNode.str
        Code:
        Exact Method Body:
         return n.str;
        
      • originalSize

        🡅  🡇    
        public int originalSize()
        Description copied from interface: Replaceable
        Reports how many nodes were copied into this instance. For implementing classes that inherit NodeIndex, this value will always be one. For others, it should report exactly how many HTMLNode's were copied.
        Specified by:
        originalSize in interface Replaceable
        Returns:
        Number of nodes originally contained by this instance.

        The purpose of Replaceable's is to allow a user to modify HTML using a smaller sub-list, without having to operate on the entire HTML-Vector since adding & removing nodes is one variant of Vector-modification, the original-size may often differ from the current-size.

        When modifying HTML, if a web-page is broken into smaller-pieces, and changes are restricted to those smaller sub-lists (and the original page is rebuilt, all at once, after all changes have been made) then those modifications should require far-fewer time-consuming list-shift operations, tremendously improving the performance of the code.
        Code:
        Exact Method Body:
         return 1;
        
      • currentSize

        🡅  🡇    
        public int currentSize()
        Description copied from interface: Replaceable
        Returns how many nodes are currently in this instance.
        Specified by:
        currentSize in interface Replaceable
        Returns:
        Number of nodes. See explanation of the original size, versus the current size here
        Code:
        Exact Method Body:
         return 1;
        
      • originalLocationStart

        🡅  🡇    
        public int originalLocationStart()
        Description copied from interface: Replaceable
        Returns the start-location within the original page-Vector from whence the HTML contents of this instance were retrieved.

        NOTE: The returned value is inclusive of the actual, original-range of this instance. This means the first HTMLNode copied into this instance' internal data-structure was at originalLocationStart().

        ALSO: The two concrete implementatons of this interface (NodeIndex and SubSection) - both enforce the 'final' modifier on their location-fields. (See: index and SubSection.location).
        Specified by:
        originalLocationStart in interface Replaceable
        Returns:
        The Vector start-index from whence this HTML was copied.
        Code:
        Exact Method Body:
         return index;
        
      • originalLocationEnd

        🡅  🡇    
        public int originalLocationEnd()
        Description copied from interface: Replaceable
        Returns the end-location within the original page-Vector from whence the HTML contents of this instance were retrieved.

        NOTE: The returned value is exclusive of the actual, original-range of this instance. This means the last HTMLNode to actually be copied into this instance' internal data-structure was located at originalLocationEnd() - 1

        ALSO: The two concrete implementatons of this interface (NodeIndex and SubSection) - both enforce the 'final' modifier on their location-fields. (See: index and SubSection.location).
        Specified by:
        originalLocationEnd in interface Replaceable
        Returns:
        The Vector end-index from whence this HTML was copied.
        Code:
        Exact Method Body:
         return index + 1;
        
      • currentNodes

        🡅  🡇    
        public java.util.Vector<HTMLNodecurrentNodes()
        Description copied from interface: Replaceable
        All nodes currently contained by this Replaceable. The concrete-classes which implement Replaceable (SubSection & TagNodeIndex) allow for the html they hold to be modified. The modification to a Replaceable happens independently from the original HTML Page out of which it was copied.

        NOTE: Replaceable's are, sort-of, the exact opposite of Java's List method 'subList'. According to the Sun / Oracle Documentation for java.util.List.subList(int fromIndex, int toIndex), any changes made to an instance of a 'subList' are immediately reflected back into the original List from where they were created.

        The List.subList operation has the advantage of being extremely easy to work with - however, an HTML-Page Vector has the potential to be hundreds of nodes long. Any operations that involve insertion or deletion will likely be terribly inefficient. Again - see ReplaceNodes.r(Vector, Iterable, boolean) to understand how quick updates on HTML-Pages is done using the Replaceable interface.
        Specified by:
        currentNodes in interface Replaceable
        Returns:
        An HTML-Vector of the nodes.
        Code:
        Exact Method Body:
         if (CURRENT_NODES == null)  CURRENT_NODES = new Vector<>(1);
         CURRENT_NODES.add(n);
         return CURRENT_NODES;
        
      • addAllInto

        🡅  🡇    
        public boolean addAllInto​(java.util.Vector<HTMLNode> fileVec)
        Description copied from interface: Replaceable
        Add all nodes currently retained in this instance into the HTML-Vector parameter html. The nodes are appended to the end of 'html'. Implementing classes NodeIndex and SubSection simply use the Java Vector method's add (for NodeIndex) and addAll (for SubSection).
        Specified by:
        addAllInto in interface Replaceable
        Parameters:
        fileVec - The HTML-Vector into which the nodes will be appended (to the end of this Vector, using Vector methods add or addAll dependent upon whether one or more-than-one nodes are being inserted).
        Returns:
        The result of Vector method add, or method allAll
        Code:
        Exact Method Body:
         return fileVec.add(n);
        
      • addAllInto

        🡅  🡇    
        public boolean addAllInto​(int index,
                                  java.util.Vector<HTMLNode> fileVec)
        Description copied from interface: Replaceable
        Add all nodes currently retained in this instance into the HTML-Vector parameter html.
        Specified by:
        addAllInto in interface Replaceable
        Parameters:
        index - The 'html' parameter's Vector-index where these nodes are to be inserted
        fileVec - The HTML-Vector into which the nodes will be appended (to the end of this Vector, using Vector methods add or addAll dependent upon whether one or more-than-one nodes are being inserted).
        Returns:
        The result of Vector method add, or method allAll
        Code:
        Exact Method Body:
         fileVec.insertElementAt(n, index);
         return true;