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.approval; 16 : 17 : import com.google.gerrit.index.query.Predicate; 18 : import com.google.gerrit.server.project.ProjectCache; 19 : import com.google.inject.Inject; 20 : import com.google.inject.assistedinject.Assisted; 21 : import java.util.Collection; 22 : import java.util.Objects; 23 : 24 : /** Predicate that matches patch set approvals we want to copy based on the value. */ 25 : public class MagicValuePredicate extends ApprovalPredicate { 26 152 : enum MagicValue { 27 152 : MIN, 28 152 : MAX, 29 152 : ANY 30 : } 31 : 32 : public interface Factory { 33 : MagicValuePredicate create(MagicValue value); 34 : } 35 : 36 : private final MagicValue value; 37 : private final ProjectCache projectCache; 38 : 39 : @Inject 40 16 : MagicValuePredicate(ProjectCache projectCache, @Assisted MagicValue value) { 41 16 : this.projectCache = projectCache; 42 16 : this.value = value; 43 16 : } 44 : 45 : @Override 46 : public boolean match(ApprovalContext ctx) { 47 : short pValue; 48 13 : switch (value) { 49 : case ANY: 50 5 : return true; 51 : case MIN: 52 10 : pValue = ctx.labelType().getMaxNegative(); 53 10 : break; 54 : case MAX: 55 7 : pValue = ctx.labelType().getMaxPositive(); 56 7 : break; 57 : default: 58 0 : throw new IllegalArgumentException("unrecognized label value: " + value); 59 : } 60 11 : return pValue == ctx.approvalValue(); 61 : } 62 : 63 : @Override 64 : public Predicate<ApprovalContext> copy( 65 : Collection<? extends Predicate<ApprovalContext>> children) { 66 0 : return new MagicValuePredicate(projectCache, value); 67 : } 68 : 69 : @Override 70 : public int hashCode() { 71 0 : return Objects.hash(value); 72 : } 73 : 74 : @Override 75 : public boolean equals(Object other) { 76 0 : if (!(other instanceof MagicValuePredicate)) { 77 0 : return false; 78 : } 79 0 : return ((MagicValuePredicate) other).value.equals(value); 80 : } 81 : }