1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 | package Torello.HTML.NodeSearch; import java.util.*; import java.util.function.Predicate; import Torello.HTML.*; import Torello.Java.LV; /** * A Java Generic {@code Iterator}-Class that for iterating {@link TagNode}, {@link TextNode} and * {@code CommentNode} instances which match user-provided search-criteria. * * <EMBED CLASS='external-html' DATA-FILE-ID=HNLI_EXTENDS_LITER> * <EMBED CLASS='external-html' DATA-FILE-ID=HNLI_EASY_TO_USE> * * @param <E> The type of {@code HTMLNode} being iterated. */ @SuppressWarnings("unchecked") @Torello.JavaDoc.JDHeaderBackgroundImg public class HNLI<E extends HTMLNode> extends AbstractHNLI<E, E> { // ******************************************************************************************** // ******************************************************************************************** // Private Fields // ******************************************************************************************** // ******************************************************************************************** // -1 means a "Right-Direction Match" has not been found/identified yet. private int hasNextVectorPos = -1; // -1 means a "Left-Direction Match" has not been found/identified yet. private int hasPrevVectorPos = -1; // ******************************************************************************************** // ******************************************************************************************** // Only Constructor **AND** Package-Private Abstract-Method Implementations // ******************************************************************************************** // ******************************************************************************************** /** * This will produce an {@code Iterator<E>}. The last parameter to this constructor * {@code Class<E> c} is required since, as per Java's Erasure "Feature," there is no way * to identify what the Variable-Type Parameter {@code 'E'} evaluates at Run-Time. * * @param html This may be any HTML {@code Vector} or sub-section. * * @param p This is a {@code java.util.function.Predicate} that identifies when the * {@code Iterator} should consider an instance of {@code 'E'} to be a "Match." * * @param c This parameter should just be the value of a call to {@code 'E'.getClass()} where * {@code 'E'} is the instance of the variable-type parameter {@code 'E'} used in this method. */ HNLI (Vector<? extends HTMLNode> html, Predicate<E> p, Class<E> c) { super(html, p, c); } void RESET_MATCHES() { hasNextVectorPos = hasPrevVectorPos = -1; } int REMOVE() { v.remove(cursor); return 1; } // ******************************************************************************************** // ******************************************************************************************** // "Previous" - Retrieval Operations // ******************************************************************************************** // ******************************************************************************************** /** * Use this method to find out whether the underlying {@code Vector} and current {@code cursor} * position would retrieve another match if {@code 'previous()'} or {@code 'previousIndex()'} * were called. * * @return This shall return {@code TRUE} if calling the {@code previous()}, or * {@code previousIndex()} methods would return another node-match. This method shall return * {@code FALSE} if calling {@code previous} would generate / throw a * {@code 'NoSuchElementException'} - <I>because there are no more matches in the underlying * {@code Vector}, given the current {@code cursor} position.</I> * * @throws ConcurrentModificationException * <EMBED CLASS='external-html' DATA-FILE-ID=CONC_MOD_EX> */ public boolean hasPrevious() { CHECK_CME(); if (hasPrevVectorPos != -1) return true; Object o; // Temp Object int LOOP_BOUNDARY = (minCursor == -1) ? 0 : minCursor; if (cursor == -1) cursor = LOOP_BOUNDARY; // will return false // System.out.println("Loop Boundary: " + LOOP_BOUNDARY + ", cursor: " + cursor); while (--cursor >= LOOP_BOUNDARY) if (c.isInstance(o = v.elementAt(cursor)) && p.test(c.cast(o))) { hasPrevVectorPos = cursor; return true; } return false; } /** * Returns the nearest node-match in the underlying {@code Vector}, given the current * {@code cursor} position - <I>when searching in the left-direction, or in the direction of * decreasing {@code Vector}-indices.</I> * * @return This shall return the node-match that is directly previous to the current * {@code cursor} position. * * @throws ConcurrentModificationException * <EMBED CLASS='external-html' DATA-FILE-ID=CONC_MOD_EX> * * @throws NoSuchElementException If there aren't any more matches available, this exception * shall throw. Avoid having to catch this exception by always calling method * {@code 'hasPrevious'}, and only invoking {@code 'previous'} if that method returned * {@code TRUE}. */ public E previous() { return (E) v.elementAt(previousIndex()); } /** * Returns the nearest node-match, <I><B>as an integer {@code Vector}-index</I></B>, in the * underlying {@code Vector}, given the current {@code cursor} position - <I>when searching in * the left-direction, or in the direction of decreasing {@code Vector}-indices.</I> * * @return This shall return the node-match that is directly previous to the current * {@code cursor} position. * * @throws ConcurrentModificationException * <EMBED CLASS='external-html' DATA-FILE-ID=CONC_MOD_EX> * * @throws NoSuchElementException If there aren't any more matches available, this exception * shall throw. Avoid having to catch this exception by always calling method * {@code 'hasPrevious()'}, and only invoking {@code 'previousIndex()'} if that method * returned <B>TRUE.</B> */ public int previousIndex() { CHECK_CME(); int temp = hasPrevVectorPos; hasPrevVectorPos = hasNextVectorPos = -1; modifiedSince = false; if (temp != -1) return temp; Object o; // Temp Object int LOOP_BOUNDARY = (minCursor == -1) ? 0 : minCursor; if (cursor == -1) cursor = LOOP_BOUNDARY; // will throw exception while (--cursor >= LOOP_BOUNDARY) if (c.isInstance(o = v.elementAt(cursor)) && p.test(c.cast(o))) return cursor; throw new NoSuchElementException("There are no more 'previous' elements available."); } // ******************************************************************************************** // ******************************************************************************************** // "Next" - Retrieval Operations // ******************************************************************************************** // ******************************************************************************************** /** * Use this method to find out whether the underlying {@code Vector} and current {@code cursor} * position would retrieve another match if {@code 'next()'} or {@code 'nextIndex()'} were * called. * * @return This shall return {@code TRUE} if calling the {@code next()}, or {@code nextIndex()} * methods would return another node-match. This method shall return {@code FALSE} if calling * {@code next()} would generate / throw a {@code 'NoSuchElementException'} - <I>because there * are no more matches in the underlying {@code Vector}, given the current {@code cursor} * position.</I> * * @throws ConcurrentModificationException * <EMBED CLASS='external-html' DATA-FILE-ID=CONC_MOD_EX> */ public boolean hasNext() { CHECK_CME(); if (hasNextVectorPos != -1) return true; Object o; // Temp Object int LOOP_BOUNDARY = (maxCursor == -1) ? (v.size() - 1) : maxCursor; if (cursor == -1) cursor = (minCursor == -1) ? -1 : (minCursor-1); // System.out.println("Loop Boundary: " + LOOP_BOUNDARY + ", cursor: " + cursor); while (++cursor <= LOOP_BOUNDARY) if (c.isInstance(o = v.elementAt(cursor)) && p.test(c.cast(o))) { hasNextVectorPos=cursor; return true; } return false; } /** * Returns the nearest node-match in the underlying {@code Vector}, given the current * {@code cursor} position - <I>when searching in the right-direction, or in the direction of * increasing {@code Vector}-indices.</I> * * @return This shall return the node-match that is directly next to the current * {@code cursor} position. * * @throws ConcurrentModificationException * <EMBED CLASS='external-html' DATA-FILE-ID=CONC_MOD_EX> * * @throws NoSuchElementException If there aren't any more matches available, this exception * shall throw. Avoid having to catch this exception by always calling method * {@code 'hasNext()'}, and only invoking {@code 'next()'} if that method returned {@code TRUE}. */ public E next() { return (E) v.elementAt(nextIndex()); } /** * Returns the nearest node-match, <I><B>as an integer {@code Vector}-index</I></B>, in the * underlying {@code Vector}, given the current {@code cursor} position - <I>when searching in * the right-direction, or in the direction of increasing {@code Vector}-indices.</I> * * @return This shall return the node-match that is directly next to the current {@code cursor} * position. * * @throws ConcurrentModificationException * <EMBED CLASS='external-html' DATA-FILE-ID=CONC_MOD_EX> * * @throws NoSuchElementException If there aren't any more matches available, this exception * shall throw. Avoid having to catch this exception by always calling method * {@code 'hasNext()'}, and only invoking {@code 'nextIndex()'} if that method returned * <B>TRUE.</B> */ public int nextIndex() { CHECK_CME(); int temp = hasNextVectorPos; hasPrevVectorPos = hasNextVectorPos = -1; modifiedSince = false; if (temp != -1) return temp; Object o; // Temp Object int LOOP_BOUNDARY = (maxCursor == -1) ? (v.size() - 1) : maxCursor; if (cursor == -1) cursor = (minCursor == -1) ? -1 : (minCursor-1); while (++cursor <= LOOP_BOUNDARY) if (c.isInstance(o = v.elementAt(cursor)) && p.test(c.cast(o))) return cursor; throw new NoSuchElementException("There are no more 'next' elements available."); } // ******************************************************************************************** // ******************************************************************************************** // "First" and "Last" - Retrieval Operations // ******************************************************************************************** // ******************************************************************************************** /** * Convenience Method. * <BR />Invokes: {@link #firstIndex()} * <BR />Retrieves: {@code 'E'} node-instance from the {@code Vector} */ public E first() { return (E) v.elementAt(firstIndex()); } /** * This method will "reset the internal {@code cursor}" to the beginning, and return the index * of the first node-match (rather than the node itself). * * <EMBED CLASS='external-html' DATA-FILE-ID=CMERESET> * * @return The index of the very-first node-match in this list. As with other * {@code Iterator}-retrieval methods, if the underlying {@code Vector} has been changed using * calls to: {@code set, remove,} or {@code add}, then this method will return the * first-integer index of the node-match for the modified-{@code Vector}. */ public int firstIndex() { cursor = (minCursor == -1) ? 0 : minCursor; hasNextVectorPos = hasPrevVectorPos = -1; expectedSize = v.size(); // NOTE: A call to first, last, firstIndex, or lastIndex // "resets" the CME Monitor-Logic ==> expectedSize = v.size(); return nextIndex(); } /** * Convenience Method. * <BR />Invokes: {@link #lastIndex()} * <BR />Retrieves: {@code 'E'} node-instance from the {@code Vector} */ public E last() { return (E) v.elementAt(lastIndex()); } /** * This method will "advance the internal {@code cursor}" to the end of the {@code Vector}, and * return the index of the last node-match (rather than the node itself). * * <EMBED CLASS='external-html' DATA-FILE-ID=CMERESET> * * @return The index of the very-last node-match in this list. As with other * {@code Iterator}-retrieval methods, if the underlying {@code Vector} has been changed using * calls to: {@code set, remove,} or {@code add}, then this method will return the * last-integer index of the node-match for the modified-{@code Vector}. */ public int lastIndex() { cursor = (maxCursor == -1) ? (v.size() - 1) : maxCursor; hasNextVectorPos = hasPrevVectorPos = -1; expectedSize = v.size(); // NOTE: A call to first, last, firstIndex, or lastIndex // "resets" the CME Monitor-Logic ==> expectedSize = v.size(); return previousIndex(); } } |