xref: /OpenGrok/opengrok-indexer/src/main/java/org/opengrok/indexer/search/context/PhraseHighlight.java (revision 5d9f3aa0ca3da3a714233f987fa732f62c0965f6)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * See LICENSE.txt included in this distribution for the specific
9  * language governing permissions and limitations under the License.
10  *
11  * When distributing Covered Code, include this CDDL HEADER in each
12  * file and include the License file at LICENSE.txt.
13  * If applicable, add the following below this CDDL HEADER, with the
14  * fields enclosed by brackets "[]" replaced with your own identifying
15  * information: Portions Copyright [yyyy] [name of copyright owner]
16  *
17  * CDDL HEADER END
18  */
19 
20 /*
21  * Copyright (c) 2018, Chris Fraire <cfraire@me.com>.
22  */
23 package org.opengrok.indexer.search.context;
24 
25 /**
26  * Represents a highlighted phrase within a line -- possibly with bounds
27  * indicating that the highlight begins or ends on another line.
28  */
29 public class PhraseHighlight {
30 
31     /**
32      * a value that has been translated from start offset w.r.t. document
33      * start to a value w.r.t. line start -- or -1 if not beginning this
34      * line
35      */
36     private final int lineStart;
37     /**
38      * a value that has been translated from start offset w.r.t. document
39      * start to a value w.r.t. line start -- or {@link Integer#MAX_VALUE} if
40      * not ending this line
41      */
42     private final int lineEnd;
43 
create(int start, int end)44     public static PhraseHighlight create(int start, int end) {
45         return new PhraseHighlight(start, end);
46     }
47 
createStarter(int start)48     public static PhraseHighlight createStarter(int start) {
49         return new PhraseHighlight(start, Integer.MAX_VALUE);
50     }
51 
createEnder(int end)52     public static PhraseHighlight createEnder(int end) {
53         return new PhraseHighlight(-1, end);
54     }
55 
createEntire()56     public static PhraseHighlight createEntire() {
57         return new PhraseHighlight(-1, Integer.MAX_VALUE);
58     }
59 
60     /**
61      * Gets a value that has been translated from start offset w.r.t. document
62      * start to a value w.r.t. line start -- or -1 if not beginning this
63      * line.
64      * @return offset
65      */
getLineStart()66     public int getLineStart() {
67         return lineStart;
68     }
69 
70     /**
71      * Gets a value that has been translated from start offset w.r.t. document
72      * start to a value w.r.t. line start -- or {@link Integer#MAX_VALUE} if
73      * not ending this line.
74      * @return offset
75      */
getLineEnd()76     public int getLineEnd() {
77         return lineEnd;
78     }
79 
80     /**
81      * Determines if the specified {@code other} overlaps with this instance.
82      * @return {@code true} if the instances overlap
83      * @param other object to compare to
84      */
overlaps(PhraseHighlight other)85     public boolean overlaps(PhraseHighlight other) {
86         return (lineStart >= other.lineStart && lineStart <= other.lineEnd) ||
87             (other.lineStart >= lineStart && other.lineStart <= lineEnd) ||
88             (lineEnd >= other.lineStart && lineEnd <= other.lineEnd) ||
89             (other.lineEnd >= lineStart && other.lineEnd <= lineEnd);
90     }
91 
92     /**
93      * Creates a new instance that is the merging of this instance and the
94      * specified {@code other}.
95      * @param other object to compare to
96      * @return a defined instance
97      */
merge(PhraseHighlight other)98     public PhraseHighlight merge(PhraseHighlight other) {
99         int mergeStart = Math.min(lineStart, other.lineStart);
100         int mergeEnd = Math.max(lineEnd, other.lineEnd);
101         return PhraseHighlight.create(mergeStart, mergeEnd);
102     }
103 
104     /** Private to enforce static create() methods. */
PhraseHighlight(int start, int end)105     private PhraseHighlight(int start, int end) {
106         this.lineStart = start;
107         this.lineEnd = end;
108     }
109 }
110