<commit_before>package checkdep.check;

import java.util.Set;
import java.util.TreeSet;
import java.util.stream.Collectors;

import jdepend.framework.DependencyConstraint;
import checkdep.common.JDependDependency;
import checkdep.value.depend.Dependencies;
import checkdep.value.depend.Dependency;
import checkdep.value.depend.PackageName;
import checkdep.value.violation.Violation;
import checkdep.value.violation.Violations;

public class JDependConstraintChecker implements ConstraintChecker {
  private final Constraints constraints;

  public JDependConstraintChecker(Constraints constraints) {
    this.constraints = constraints;
  }

  @Override
  public Violations check(Dependencies dependencies) {
    Dependencies constraintDeps = toDependencies(createJDependConstraint());
    Set<Violation> needlessSet = check(constraintDeps, dependencies);
    if (!needlessSet.isEmpty()) {
      throw new NeedlessConstraintException(needlessSet.toString());
    }
    return new Violations(check(dependencies, constraintDeps));
  }

  private Set<Violation> check(Dependencies actualDeps, Dependencies expectDeps) {
    Set<Violation> res = new TreeSet<>();
    for (Dependency actual : actualDeps.values()) {
      res.addAll(checkEfferents(actual,
          expectDeps.get(actual.getName()).orElse(Dependency.NULL)));
    }
    return res;
  }

  private Set<Violation> checkEfferents(Dependency actual, Dependency expect) {
    return actual.getEfferents().stream()
        .filter(efferent -> !expect.getEfferents().contains(efferent))
        .map(efferent -> new Violation(actual.getName(), efferent))
        .collect(Collectors.toSet());
  }

  private DependencyConstraint createJDependConstraint() {
    DependencyConstraint constraint = new DependencyConstraint();
    for (Constraint item : constraints) {
      PackageName from = item.getFrom();
      PackageName to = item.getTo();
      constraint.addPackage(from.getValue()).dependsUpon(
          constraint.addPackage(to.getValue()));
    }
    return constraint;
  }
<commit_msg>refactoring JDependConstraintChecker#check() to stream<commit_after>package checkdep.check;

import java.util.Set;
import java.util.stream.Collectors;

import jdepend.framework.DependencyConstraint;
import checkdep.common.JDependDependency;
import checkdep.value.depend.Dependencies;
import checkdep.value.depend.Dependency;
import checkdep.value.depend.PackageName;
import checkdep.value.violation.Violation;
import checkdep.value.violation.Violations;

public class JDependConstraintChecker implements ConstraintChecker {
  private final Constraints constraints;

  public JDependConstraintChecker(Constraints constraints) {
    this.constraints = constraints;
  }

  @Override
  public Violations check(Dependencies dependencies) {
    Dependencies constraintDeps = toDependencies(createJDependConstraint());
    Set<Violation> needlessSet = check(constraintDeps, dependencies);
    if (!needlessSet.isEmpty()) {
      throw new NeedlessConstraintException(needlessSet.toString());
    }
    return new Violations(check(dependencies, constraintDeps));
  }

  private Set<Violation> check(Dependencies actualDeps, Dependencies expectDeps) {
    return actualDeps.values().stream()
        .flatMap(actual -> checkEfferents(actual,
            expectDeps.get(actual.getName()).orElse(Dependency.NULL)).stream())
        .sorted()
        .collect(Collectors.toSet());
  }

  private Set<Violation> checkEfferents(Dependency actual, Dependency expect) {
    return actual.getEfferents().stream()
        .filter(efferent -> !expect.getEfferents().contains(efferent))
        .map(efferent -> new Violation(actual.getName(), efferent))
        .collect(Collectors.toSet());
  }

  private DependencyConstraint createJDependConstraint() {
    DependencyConstraint constraint = new DependencyConstraint();
    for (Constraint item : constraints) {
      PackageName from = item.getFrom();
      PackageName to = item.getTo();
      constraint.addPackage(from.getValue()).dependsUpon(
          constraint.addPackage(to.getValue()));
    }
    return constraint;
  }

{'fraction_non_alphanumeric': 0.10386767209924593, 'fraction_numerical': 0.0, 'mean_word_length': 4.679558011049724, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5009682', 'n_tokens_mistral': 1139, 'n_tokens_neox': 1102, 'n_words': 218}