Line data Source code
1 : // Copyright (C) 2021 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.server.query.change; 16 : 17 : import static com.google.gerrit.server.index.change.ChangeField.HASHTAG_SPEC; 18 : 19 : import dk.brics.automaton.RegExp; 20 : import dk.brics.automaton.RunAutomaton; 21 : 22 : public class RegexHashtagPredicate extends ChangeRegexPredicate { 23 : protected final RunAutomaton pattern; 24 : 25 : public RegexHashtagPredicate(String re) { 26 4 : super(HASHTAG_SPEC, re); 27 : 28 4 : if (re.startsWith("^")) { 29 4 : re = re.substring(1); 30 : } 31 : 32 4 : if (re.endsWith("$") && !re.endsWith("\\$")) { 33 4 : re = re.substring(0, re.length() - 1); 34 : } 35 : 36 4 : this.pattern = new RunAutomaton(new RegExp(re).toAutomaton()); 37 4 : } 38 : 39 : @Override 40 : public boolean match(ChangeData cd) { 41 2 : if (cd.hashtags().isEmpty()) { 42 0 : return false; 43 : } 44 2 : return cd.hashtags().stream().anyMatch(ht -> pattern.run(ht)); 45 : } 46 : 47 : @Override 48 : public int getCost() { 49 0 : return 1; 50 : } 51 : }