public boolean areConsecutiveVisits(List<Integer> visits, Patient patient) { if (patient != null && visits != null && (visits.size() > 0)) { List<Visit> patientVisits = visitService.getVisitsByPatient(patient, true, false); if ((patientVisits != null) && (patientVisits.size() > 0)) { ArrayList<Integer> allVisits = new ArrayList<Integer>(); int j = 0; for (Visit visit : patientVisits) { allVisits.add(j++, visit.getId()); } if (allVisits.containsAll(visits)) { // find the index of the first candidate for a consecutive visit int i = allVisits.indexOf(visits.get(0)); // make sure there are still more elements in the list than the the number of candidate // consecutives if ((allVisits.size() - i) >= visits.size()) { for (Integer candidateVisit : visits) { if (allVisits.get(i).compareTo(candidateVisit) == 0) { i++; } else { return false; } } return true; } } } } return false; }