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) 2005, 2018, Oracle and/or its affiliates. All rights reserved. 22 * Portions Copyright (c) 2018, 2020, Chris Fraire <cfraire@me.com>. 23 */ 24 package org.opengrok.indexer.search.context; 25 26 import java.util.Locale; 27 28 /** 29 * Base class for matching a line against terms. 30 */ 31 // Avoid the warning about 'WAIT' being confusing w.r.t. Object.wait(). 32 @SuppressWarnings("java:S1845") 33 public abstract class LineMatcher { 34 // Line Matcher States 35 public static final int NOT_MATCHED = 0; 36 public static final int MATCHED = 1; 37 public static final int WAIT = 2; 38 39 /** 40 * Tells whether the matching should be done in a case-insensitive manner. 41 */ 42 private final boolean caseInsensitive; 43 44 /** 45 * Create a {@code LineMatcher} instance. 46 * 47 * @param caseInsensitive if {@code true}, matching should not consider 48 * case differences significant 49 */ LineMatcher(boolean caseInsensitive)50 LineMatcher(boolean caseInsensitive) { 51 this.caseInsensitive = caseInsensitive; 52 } 53 54 /** 55 * Check if two strings are equal. If this is a case-insensitive matcher, 56 * the check will return true if the only difference between the strings 57 * is difference in case. 58 */ equal(String s1, String s2)59 boolean equal(String s1, String s2) { 60 return compareStrings(s1, s2) == 0; 61 } 62 63 /** 64 * Compare two strings and return -1, 0 or 1 if the first string is 65 * lexicographically smaller than, equal to or greater than the second 66 * string. If this is a case-insensitive matcher, case differences will 67 * be ignored. 68 */ compareStrings(String s1, String s2)69 int compareStrings(String s1, String s2) { 70 if (s1 == null) { 71 return s2 == null ? 0 : -1; 72 } else if (caseInsensitive) { 73 return s1.compareToIgnoreCase(s2); 74 } else { 75 return s1.compareTo(s2); 76 } 77 } 78 79 /** 80 * Normalize a string token for comparison with other string tokens. That 81 * is, convert to lower case if this is a case-insensitive matcher. 82 * Otherwise, return the string itself. 83 */ normalizeString(String s)84 String normalizeString(String s) { 85 if (s == null) { 86 return null; 87 } else if (caseInsensitive) { 88 return s.toLowerCase(Locale.ROOT); 89 } else { 90 return s; 91 } 92 } 93 match(String line)94 public abstract int match(String line); 95 } 96