2323
2424import org .exist .numbering .NodeId ;
2525
26+ import javax .annotation .Nullable ;
2627import java .util .ArrayList ;
2728import java .util .Collection ;
2829import java .util .LinkedList ;
@@ -78,20 +79,20 @@ public boolean overlaps(final Offset other) {
7879 }
7980
8081 private final int context ;
81- protected final NodeId nodeId ;
82- private final String matchTerm ;
82+ private final NodeId nodeId ;
83+ private @ Nullable final String matchTerm ;
8384 private int [] offsets ;
8485 private int [] lengths ;
8586
8687 private int currentOffset = 0 ;
8788
88- protected Match nextMatch = null ;
89+ private @ Nullable Match nextMatch = null ;
8990
90- protected Match (final int contextId , final NodeId nodeId , final String matchTerm ) {
91+ protected Match (final int contextId , final NodeId nodeId , @ Nullable final String matchTerm ) {
9192 this (contextId , nodeId , matchTerm , 1 );
9293 }
9394
94- protected Match (final int contextId , final NodeId nodeId , final String matchTerm , final int frequency ) {
95+ protected Match (final int contextId , final NodeId nodeId , @ Nullable final String matchTerm , final int frequency ) {
9596 this .context = contextId ;
9697 this .nodeId = nodeId ;
9798 this .matchTerm = matchTerm ;
@@ -120,16 +121,24 @@ public int getContextId() {
120121 return context ;
121122 }
122123
123- public abstract Match createInstance (final int contextId , final NodeId nodeId , final String matchTerm );
124+ public @ Nullable Match getNextMatch () {
125+ return nextMatch ;
126+ }
127+
128+ public void setNextMatch (@ Nullable final Match nextMatch ) {
129+ this .nextMatch = nextMatch ;
130+ }
131+
132+ public abstract Match createInstance (final int contextId , final NodeId nodeId , @ Nullable final String matchTerm );
124133 public abstract Match newCopy ();
125134 public abstract String getIndexId ();
126135
127136 public void addOffset (final int offset , final int length ) {
128- if (currentOffset == offsets .length ) {
129- final int noffsets [] = new int [currentOffset + 1 ];
137+ if (currentOffset == offsets .length ) {
138+ final int [] noffsets = new int [currentOffset + 1 ];
130139 System .arraycopy (offsets , 0 , noffsets , 0 , currentOffset );
131140 offsets = noffsets ;
132- final int nlengths [] = new int [currentOffset + 1 ];
141+ final int [] nlengths = new int [currentOffset + 1 ];
133142 System .arraycopy (lengths , 0 , nlengths , 0 , currentOffset );
134143 lengths = nlengths ;
135144 }
@@ -151,7 +160,7 @@ public Offset getOffset(final int pos) {
151160
152161 public List <Offset > getOffsets () {
153162 final List <Offset > result = new ArrayList <>(currentOffset );
154- for (int i = 0 ; i < currentOffset ; i ++) {
163+ for (int i = 0 ; i < currentOffset ; i ++) {
155164 result .add (getOffset (i ));
156165 }
157166 return result ;
@@ -178,24 +187,24 @@ public Match continuedBy(final Match other) {
178187 * the other match in the specified distance range if such
179188 * a match exists or null if no such match found
180189 */
181- public Match followedBy (final Match other , final int minDistance , final int maxDistance ) {
190+ public @ Nullable Match followedBy (final Match other , final int minDistance , final int maxDistance ) {
182191 final List <Offset > newMatchOffsets = new LinkedList <>();
183- for (int i = 0 ; i < currentOffset ; i ++) {
184- for (int j = 0 ; j < other .currentOffset ; j ++) {
192+ for (int i = 0 ; i < currentOffset ; i ++) {
193+ for (int j = 0 ; j < other .currentOffset ; j ++) {
185194 final int distance = other .offsets [j ] - (offsets [i ] + lengths [i ]);
186- if (distance >= minDistance && distance <= maxDistance ) {
195+ if (distance >= minDistance && distance <= maxDistance ) {
187196 newMatchOffsets .add (new Offset (offsets [i ], lengths [i ] + distance + other .lengths [j ]));
188197 }
189198 }
190199 }
191200
192- if (newMatchOffsets .isEmpty ()) {
201+ if (newMatchOffsets .isEmpty ()) {
193202 return null ;
194203 }
195204
196205 final int wildCardSize = newMatchOffsets .get (0 ).length - matchTerm .length () - other .matchTerm .length ();
197206 final StringBuilder matched = new StringBuilder (matchTerm );
198- for (int ii = 0 ; ii < wildCardSize ; ii ++) {
207+ for (int ii = 0 ; ii < wildCardSize ; ii ++) {
199208 matched .append ('?' );
200209 }
201210 matched .append (other .matchTerm );
@@ -212,13 +221,13 @@ public Match followedBy(final Match other, final int minDistance, final int maxD
212221 * @param maxExpand The maximum number of characters to expand this match by
213222 * @return The expanded match if possible, or null if no offset is far enough from the start.
214223 */
215- public Match expandBackward (final int minExpand , final int maxExpand ) {
216- Match result = null ;
217- for (int i = 0 ; i < currentOffset ; i ++) {
218- if (offsets [i ] - minExpand >= 0 ) {
219- if (result == null ) {
224+ public @ Nullable Match expandBackward (final int minExpand , final int maxExpand ) {
225+ @ Nullable Match result = null ;
226+ for (int i = 0 ; i < currentOffset ; i ++) {
227+ if (offsets [i ] - minExpand >= 0 ) {
228+ if (result == null ) {
220229 final StringBuilder matched = new StringBuilder ();
221- for (int ii = 0 ; ii < minExpand ; ii ++) {
230+ for (int ii = 0 ; ii < minExpand ; ii ++) {
222231 matched .append ('?' );
223232 }
224233 matched .append (matchTerm );
@@ -240,12 +249,12 @@ public Match expandBackward(final int minExpand, final int maxExpand) {
240249 * @param dataLength The length of the valued of the node, limiting the expansion
241250 * @return The expanded match if possible, or null if no offset is far enough from the end.
242251 */
243- public Match expandForward (final int minExpand , final int maxExpand , final int dataLength ) {
244- Match result = null ;
245- for (int i = 0 ; i < currentOffset ; i ++) {
246- if (offsets [i ] + lengths [i ] + minExpand <= dataLength ) {
252+ public @ Nullable Match expandForward (final int minExpand , final int maxExpand , final int dataLength ) {
253+ @ Nullable Match result = null ;
254+ for (int i = 0 ; i < currentOffset ; i ++) {
255+ if (offsets [i ] + lengths [i ] + minExpand <= dataLength ) {
247256 final int expand = Math .min (dataLength - offsets [i ] - lengths [i ], maxExpand );
248- if (result == null ) {
257+ if (result == null ) {
249258 final StringBuilder matched = new StringBuilder (matchTerm );
250259 for (int ii = 0 ; ii < expand ; ii ++) {
251260 matched .append ('?' );
@@ -258,10 +267,10 @@ public Match expandForward(final int minExpand, final int maxExpand, final int d
258267 return result ;
259268 }
260269
261- private Match filterOffsets (final Predicate <Offset > predicate ) {
270+ private @ Nullable Match filterOffsets (final Predicate <Offset > predicate ) {
262271 final Match result = createInstance (context , nodeId , matchTerm );
263272 getOffsets ().stream ().filter (predicate ).forEach (result ::addOffset );
264- if (result .currentOffset == 0 ) {
273+ if (result .currentOffset == 0 ) {
265274 return null ;
266275 } else {
267276 return result ;
@@ -275,7 +284,7 @@ private Match filterOffsets(final Predicate<Offset> predicate) {
275284 * @return a match containing only offsets starting at the given position,
276285 * or null if no such offset exists.
277286 */
278- public Match filterOffsetsStartingAt (final int pos ) {
287+ public @ Nullable Match filterOffsetsStartingAt (final int pos ) {
279288 return filterOffsets (offset -> offset .offset == pos );
280289 }
281290
@@ -286,7 +295,7 @@ public Match filterOffsetsStartingAt(final int pos) {
286295 * @return A match containing only offsets ending at the given position,
287296 * or null if no such offset exists.
288297 */
289- public Match filterOffsetsEndingAt (final int pos ) {
298+ public @ Nullable Match filterOffsetsEndingAt (final int pos ) {
290299 return filterOffsets (offset -> offset .offset + offset .length == pos );
291300 }
292301
@@ -297,7 +306,7 @@ public Match filterOffsetsEndingAt(final int pos) {
297306 * @return a match containing only non-overlapping offsets
298307 */
299308 public Match filterOutOverlappingOffsets () {
300- if (currentOffset == 0 ) {
309+ if (currentOffset == 0 ) {
301310 return newCopy ();
302311 }
303312 final List <Offset > newMatchOffsets = getOffsets ();
@@ -311,15 +320,15 @@ public Match filterOutOverlappingOffsets() {
311320 });
312321 final List <Offset > nonOverlappingMatchOffsets = new LinkedList <>();
313322 nonOverlappingMatchOffsets .add (newMatchOffsets .remove (0 ));
314- for (final Offset o : newMatchOffsets ) {
323+ for (final Offset o : newMatchOffsets ) {
315324 boolean overlapsExistingOffset = false ;
316- for (final Offset eo : nonOverlappingMatchOffsets ) {
325+ for (final Offset eo : nonOverlappingMatchOffsets ) {
317326 if (eo .overlaps (o )) {
318327 overlapsExistingOffset = true ;
319328 break ;
320329 }
321330 }
322- if (!overlapsExistingOffset ) {
331+ if (!overlapsExistingOffset ) {
323332 nonOverlappingMatchOffsets .add (o );
324333 }
325334 }
@@ -336,8 +345,8 @@ public Match filterOutOverlappingOffsets() {
336345 * @return true if a match starts at the given position
337346 */
338347 public boolean hasMatchAt (final int pos ) {
339- for (int i = 0 ; i < currentOffset ; i ++) {
340- if (offsets [i ] == pos ) {
348+ for (int i = 0 ; i < currentOffset ; i ++) {
349+ if (offsets [i ] == pos ) {
341350 return true ;
342351 }
343352 }
@@ -351,31 +360,27 @@ public boolean hasMatchAt(final int pos) {
351360 * @return true if the given position is within a match
352361 */
353362 public boolean hasMatchAround (final int pos ) {
354- for (int i = 0 ; i < currentOffset ; i ++) {
355- if (offsets [i ] + lengths [i ] >= pos ) {
363+ for (int i = 0 ; i < currentOffset ; i ++) {
364+ if (offsets [i ] + lengths [i ] >= pos ) {
356365 return true ;
357366 }
358367 }
359368 return false ;
360369 }
361370
362371 public void mergeOffsets (final Match other ) {
363- for (int i = 0 ; i < other .currentOffset ; i ++) {
364- if (!hasMatchAt (other .offsets [i ])) {
372+ for (int i = 0 ; i < other .currentOffset ; i ++) {
373+ if (!hasMatchAt (other .offsets [i ])) {
365374 addOffset (other .offsets [i ], other .lengths [i ]);
366375 }
367376 }
368377 }
369378
370- public Match getNextMatch () {
371- return nextMatch ;
372- }
373-
374379 public static boolean matchListEquals (final Match m1 , final Match m2 ) {
375380 Match n1 = m1 ;
376381 Match n2 = m2 ;
377- while (n1 != null ) {
378- if (n2 == null || n1 != n2 ) {
382+ while (n1 != null ) {
383+ if (n2 == null || n1 != n2 ) {
379384 return false ;
380385 }
381386 n1 = n1 .nextMatch ;
@@ -386,23 +391,16 @@ public static boolean matchListEquals(final Match m1, final Match m2) {
386391
387392 @ Override
388393 public boolean equals (final Object other ) {
389- if (!(other instanceof Match )) {
394+ if (other instanceof Match ) {
395+ final Match om = (Match ) other ;
396+ return om .matchTerm != null
397+ && om .matchTerm .equals (matchTerm )
398+ && om .nodeId .equals (nodeId );
399+ } else {
390400 return false ;
391401 }
392- final Match om = (Match ) other ;
393- return om .matchTerm != null &&
394- om .matchTerm .equals (matchTerm ) &&
395- om .nodeId .equals (nodeId );
396402 }
397403
398- public boolean matchEquals (final Match other ) {
399- if (this == other ) {
400- return true ;
401- }
402- return
403- (nodeId == other .nodeId || nodeId .equals (other .nodeId )) &&
404- matchTerm .equals (other .matchTerm );
405- }
406404
407405 /**
408406 * Used to sort matches. Terms are compared by their string
@@ -418,19 +416,23 @@ public int compareTo(final Match other) {
418416 @ Override
419417 public String toString () {
420418 final StringBuilder buf = new StringBuilder ();
421- if (matchTerm != null ) {
419+ if (matchTerm != null ) {
422420 buf .append (matchTerm );
423421 }
424422
425- for (int i = 0 ; i < currentOffset ; i ++) {
426- buf .append (" [" );
423+ for (int i = 0 ; i < currentOffset ; i ++) {
424+ if (buf .length () > 0 ) {
425+ buf .append (' ' );
426+ }
427+ buf .append ('[' );
427428 buf .append (offsets [i ]).append (':' ).append (lengths [i ]);
428- buf .append ("]" );
429+ buf .append (']' );
429430 }
430431
431- if (nextMatch != null ) {
432- buf .append (' ' ).append (nextMatch . toString () );
432+ if (nextMatch != null ) {
433+ buf .append (' ' ).append (nextMatch );
433434 }
435+
434436 return buf .toString ();
435437 }
436438}
0 commit comments