<commit_before>package org.cbioportal.service.impl;

import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;
import org.cbioportal.model.*;
import org.cbioportal.persistence.TreatmentRepository;
import org.cbioportal.service.TreatmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.util.stream.Collectors.toList;

@Service
public class TreatmentServiceImpl implements TreatmentService {
    @Autowired
    TreatmentRepository treatmentRepository;
    
    private Pair<List<String>, List<String>> filterIds(List<String> sampleIds, List<String> studyIds, ClinicalEventKeyCode key) {
        if (sampleIds == null || studyIds == null || sampleIds.size() != studyIds.size()) {
            return new ImmutablePair<>(sampleIds, studyIds);
        }
        Set<String> studiesWithTreatments = studyIds.stream()
            .filter(studyId -> treatmentRepository.studyIdHasTreatments(studyId, key))
            .collect(Collectors.toSet());
        
        ArrayList<String> filteredSampleIds = new ArrayList<>();
        ArrayList<String> filteredStudyIds = new ArrayList<>();
        for (int i = 0; i < sampleIds.size(); i++) {
            String studyId = studyIds.get(i);
            String sampleId = sampleIds.get(i);
            if (studiesWithTreatments.contains(studyId)) {
                filteredSampleIds.add(sampleId);
                filteredStudyIds.add(studyId);
            }
        }
        return new ImmutablePair<>(filteredSampleIds, filteredStudyIds);
    }<commit_msg>Do not run studyIdHasTreatments() more than neccesary<commit_after>package org.cbioportal.service.impl;

import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;
import org.cbioportal.model.*;
import org.cbioportal.persistence.TreatmentRepository;
import org.cbioportal.service.TreatmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.util.stream.Collectors.toList;

@Service
public class TreatmentServiceImpl implements TreatmentService {
    @Autowired
    TreatmentRepository treatmentRepository;
    
    private Pair<List<String>, List<String>> filterIds(List<String> sampleIds, List<String> studyIds, ClinicalEventKeyCode key) {
        if (sampleIds == null || studyIds == null || sampleIds.size() != studyIds.size()) {
            return new ImmutablePair<>(sampleIds, studyIds);
        }
        Set<String> studiesWithTreatments = studyIds.stream()
            .distinct()
            .filter(studyId -> treatmentRepository.studyIdHasTreatments(studyId, key))
            .collect(Collectors.toSet());
        
        ArrayList<String> filteredSampleIds = new ArrayList<>();
        ArrayList<String> filteredStudyIds = new ArrayList<>();
        for (int i = 0; i < sampleIds.size(); i++) {
            String studyId = studyIds.get(i);
            String sampleId = sampleIds.get(i);
            if (studiesWithTreatments.contains(studyId)) {
                filteredSampleIds.add(sampleId);
                filteredStudyIds.add(studyId);
            }
        }
        return new ImmutablePair<>(filteredSampleIds, filteredStudyIds);
    }
{'fraction_non_alphanumeric': 0.10929432013769363, 'fraction_numerical': 0.0017211703958691911, 'mean_word_length': 3.4533844189016603, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, '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': '1893046', 'n_tokens_mistral': 978, 'n_tokens_neox': 941, 'n_words': 187}