xref: /OpenGrok/opengrok-indexer/src/main/java/org/opengrok/indexer/search/context/ContextArgs.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 an immutable settings instance for coordinating objects related
27  * to producing context presentations.
28  */
29 public class ContextArgs {
30     /** Not Lucene-related, so {@code int} does fine. */
31     private static final int CONTEXT_WIDTH = 100;
32 
33     /** Lucene uhighlight-related, so {@code short} is safer. */
34     private final short contextSurround;
35 
36     /** Lucene uhighlight-related, so {@code short} is safer. */
37     private final short contextLimit;
38 
39     /**
40      * Initializes an instance with the specified values.
41      * <p>
42      * {@code short} is used because some Lucene classes were found to choke
43      * when OpenGrok used {@link Integer#MAX_VALUE} to mean "unbounded".
44      * {@code short} is safer therefore but unfortunately somewhat syntactically
45      * inconvenient.
46      * @param contextSurround a non-negative value
47      * @param contextLimit a positive value
48      */
ContextArgs(short contextSurround, short contextLimit)49     public ContextArgs(short contextSurround, short contextLimit) {
50         if (contextSurround < 0) {
51             throw new IllegalArgumentException(
52                 "contextSurround cannot be negative");
53         }
54         if (contextLimit < 1) {
55             throw new IllegalArgumentException(
56                 "contextLimit must be positive");
57         }
58         this.contextSurround = contextSurround;
59         this.contextLimit = contextLimit;
60     }
61 
62     /**
63      * Gets the number of lines of leading and trailing context surrounding each
64      * match line to present.
65      * <p>
66      * (N.b. the value is used w.r.t. {@link #getContextLimit()} and therefore
67      * w.r.t. Lucene {@code uhighlight}, and {@code short} is safer, though
68      * syntactically inconvenient, to avoid numeric overlow that may occur with
69      * {@code int} in that library.)
70      * @return a non-negative value
71      */
getContextSurround()72     public short getContextSurround() {
73         return contextSurround;
74     }
75 
76     /**
77      * Gets the maximum number of lines to present, after which a "more..." link
78      * is displayed to allow the user to view full match results.
79      * <p>
80      * (N.b. the value is used with Lucene {@code uhighlight}, and {@code short}
81      * is safer, though syntactically inconvenient, to avoid numeric overlow
82      * that may occur with {@code int} in that library.)
83      * @return a positive value
84      */
getContextLimit()85     public short getContextLimit() {
86         return contextLimit;
87     }
88 
89     /**
90      * Gets a value indicating the maximum width to show for lines in a context
91      * presentation.
92      * @return a positive value
93      */
getContextWidth()94     public int getContextWidth() {
95         return CONTEXT_WIDTH;
96     }
97 }
98