LCOV - code coverage report
Current view: top level - index/query - NotPredicate.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 22 25 88.0 %
Date: 2022-11-19 15:00:39 Functions: 11 11 100.0 %

          Line data    Source code
       1             : // Copyright (C) 2009 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.index.query;
      16             : 
      17             : import static com.google.common.base.Preconditions.checkState;
      18             : 
      19             : import java.util.Collection;
      20             : import java.util.Collections;
      21             : import java.util.List;
      22             : 
      23             : /** Negates the result of another predicate. */
      24             : public final class NotPredicate<T> extends Predicate<T> implements Matchable<T> {
      25             :   private final Predicate<T> that;
      26             : 
      27          57 :   NotPredicate(Predicate<T> that) {
      28          57 :     if (that instanceof NotPredicate) {
      29           0 :       throw new IllegalArgumentException("Double negation unsupported");
      30             :     }
      31          57 :     this.that = that;
      32          57 :   }
      33             : 
      34             :   @Override
      35             :   public final List<Predicate<T>> getChildren() {
      36          56 :     return Collections.singletonList(that);
      37             :   }
      38             : 
      39             :   @Override
      40             :   public final int getChildCount() {
      41          56 :     return 1;
      42             :   }
      43             : 
      44             :   @Override
      45             :   public final Predicate<T> getChild(int i) {
      46          56 :     if (i != 0) {
      47           0 :       throw new ArrayIndexOutOfBoundsException(i);
      48             :     }
      49          56 :     return that;
      50             :   }
      51             : 
      52             :   @Override
      53             :   public Predicate<T> copy(Collection<? extends Predicate<T>> children) {
      54           6 :     if (children.size() != 1) {
      55           1 :       throw new IllegalArgumentException("Expected exactly one child");
      56             :     }
      57           6 :     return new NotPredicate<>(children.iterator().next());
      58             :   }
      59             : 
      60             :   @Override
      61             :   public boolean isMatchable() {
      62          56 :     return that.isMatchable();
      63             :   }
      64             : 
      65             :   @Override
      66             :   public boolean match(T object) {
      67          54 :     checkState(
      68          54 :         that.isMatchable(),
      69             :         "match invoked, but child predicate %s doesn't implement %s",
      70             :         that,
      71          54 :         Matchable.class.getName());
      72          54 :     return !that.asMatchable().match(object);
      73             :   }
      74             : 
      75             :   @Override
      76             :   public int getCost() {
      77          56 :     return that.estimateCost();
      78             :   }
      79             : 
      80             :   @Override
      81             :   public int hashCode() {
      82           1 :     return ~that.hashCode();
      83             :   }
      84             : 
      85             :   @Override
      86             :   public boolean equals(Object other) {
      87           2 :     if (other == null) {
      88           0 :       return false;
      89             :     }
      90           2 :     return other instanceof NotPredicate
      91           2 :         && getChildren().equals(((Predicate<?>) other).getChildren());
      92             :   }
      93             : 
      94             :   @Override
      95             :   public final String toString() {
      96           6 :     return "-" + that.toString();
      97             :   }
      98             : }

Generated by: LCOV version 1.16+git.20220603.dfeb750