Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

AndPredicate.java 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2023 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.api.batch.fs.internal.predicates;
  21. import java.util.ArrayList;
  22. import java.util.Collection;
  23. import java.util.Collections;
  24. import java.util.List;
  25. import org.sonar.api.batch.fs.FilePredicate;
  26. import org.sonar.api.batch.fs.FileSystem.Index;
  27. import org.sonar.api.batch.fs.InputFile;
  28. /**
  29. * @since 4.2
  30. */
  31. class AndPredicate extends AbstractFilePredicate implements OperatorPredicate {
  32. private final List<OptimizedFilePredicate> predicates = new ArrayList<>();
  33. private AndPredicate() {
  34. }
  35. public static FilePredicate create(Collection<FilePredicate> predicates) {
  36. if (predicates.isEmpty()) {
  37. return TruePredicate.TRUE;
  38. }
  39. AndPredicate result = new AndPredicate();
  40. for (FilePredicate filePredicate : predicates) {
  41. if (filePredicate == TruePredicate.TRUE) {
  42. continue;
  43. } else if (filePredicate == FalsePredicate.FALSE) {
  44. return FalsePredicate.FALSE;
  45. } else if (filePredicate instanceof AndPredicate andPredicate) {
  46. result.predicates.addAll(andPredicate.predicates);
  47. } else {
  48. result.predicates.add(OptimizedFilePredicateAdapter.create(filePredicate));
  49. }
  50. }
  51. Collections.sort(result.predicates);
  52. return result;
  53. }
  54. @Override
  55. public boolean apply(InputFile f) {
  56. for (OptimizedFilePredicate predicate : predicates) {
  57. if (!predicate.apply(f)) {
  58. return false;
  59. }
  60. }
  61. return true;
  62. }
  63. @Override
  64. public Iterable<InputFile> filter(Iterable<InputFile> target) {
  65. Iterable<InputFile> result = target;
  66. for (OptimizedFilePredicate predicate : predicates) {
  67. result = predicate.filter(result);
  68. }
  69. return result;
  70. }
  71. @Override
  72. public Iterable<InputFile> get(Index index) {
  73. if (predicates.isEmpty()) {
  74. return index.inputFiles();
  75. }
  76. // Optimization, use get on first predicate then filter with next predicates
  77. Iterable<InputFile> result = predicates.get(0).get(index);
  78. for (int i = 1; i < predicates.size(); i++) {
  79. result = predicates.get(i).filter(result);
  80. }
  81. return result;
  82. }
  83. Collection<OptimizedFilePredicate> predicates() {
  84. return predicates;
  85. }
  86. @Override
  87. public List<FilePredicate> operands() {
  88. return predicates.stream().map(p -> (FilePredicate) p).toList();
  89. }
  90. }