Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package opencm.domain.service;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.Comparator;
- import java.util.Iterator;
- import java.util.List;
- import java.util.Stack;
- import opencm.domain.interfaces.ConsultaRepository;
- import opencm.domain.model.Consulta;
- import opencm.domain.model.HorarioAtendimento;
- import org.joda.time.Interval;
- public final class AgendamentoService {
- private ConsultaRepository consultaRepository;
- public AgendamentoService(ConsultaRepository consultaRepository) {
- this.consultaRepository = consultaRepository;
- }
- private List<Interval> consultaIntervals(List<Consulta> consultas) {
- List<Interval> result = new ArrayList<Interval>();
- for (Consulta consulta : consultas) {
- result.add(new Interval(consulta.getInicio(), consulta.getDuracao()));
- }
- return mergeIntervals(result);
- }
- private List<Interval> atendimentoIntervals(List<HorarioAtendimento> horarios) {
- List<Interval> result = new ArrayList<Interval>();
- for (HorarioAtendimento horario : horarios) {
- result.add(new Interval(horario.getHoraInicial().toDateTimeToday(), horario.getHoraFinal().toDateTimeToday()));
- }
- return mergeIntervals(result);
- }
- private List<Interval> mergeIntervals(List<Interval> intervals) {
- List<Interval> sortedIntervals = new ArrayList<Interval>(intervals);
- Collections.sort(sortedIntervals, new Comparator<Interval>() { public int compare(Interval a, Interval b) {
- return a.getStart().compareTo(b.getStart());
- }});
- Iterator<Interval> sortedIntervalsIt = sortedIntervals.iterator();
- Stack<Interval> mergedIntervals = new Stack<Interval>();
- while (sortedIntervalsIt.hasNext()) {
- Interval current = sortedIntervalsIt.next();
- if (!mergedIntervals.empty() && current.abuts(mergedIntervals.peek())) {
- mergedIntervals.push(new Interval(mergedIntervals.pop().getStart(), current.getEnd()));
- } else {
- mergedIntervals.push(current);
- }
- }
- return mergedIntervals;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement