Search in sources :

Example 1 with TrainComparator

use of net.parostroj.timetable.actions.TrainComparator in project grafikon by jub77.

the class TrainTimetablesExtractor method getConcurrentTrains.

private Pair<Boolean, List<TranslatedString>> getConcurrentTrains(TimeInterval interval) {
    TrainComparator comparator = diagram.getTrainsData().getTrainComparator();
    Node node = interval.getOwnerAsNode();
    TimeInterval toBeChecked = interval;
    Train train = interval.getTrain();
    if (interval.isFirst() && train.getTimeIntervalBefore() != null)
        toBeChecked = train.getTimeIntervalBefore();
    else if (interval.isLast() && train.getTimeIntervalAfter() != null)
        toBeChecked = train.getTimeIntervalAfter();
    Set<TimeInterval> over = node.getOverlappingTimeIntervals(toBeChecked);
    boolean first = true;
    // check if the train is first in the station (start be marked with trapezoid)
    if (!interval.isFirst())
        for (Iterator<TimeInterval> i = over.iterator(); i.hasNext(); ) {
            TimeInterval checked = i.next();
            if (interval.getStart() > checked.getStart() || (interval.getStart() == checked.getStart() && comparator.compare(interval.getTrain(), checked.getTrain()) >= 0)) {
                first = false;
                break;
            }
        }
    if (over.isEmpty()) {
        return null;
    } else {
        List<Train> tTrains = new ArrayList<>(over.size());
        for (TimeInterval ti : over) {
            if (!tTrains.contains(ti.getTrain()))
                tTrains.add(ti.getTrain());
        }
        ElementSort<Train> s = new ElementSort<>(new TrainComparator(node.getDiagram().getTrainsData().getTrainSortPattern()));
        tTrains = s.sort(tTrains);
        List<TranslatedString> result = new LinkedList<>();
        for (Train t : tTrains) {
            result.add(t.getName());
        }
        return new Pair<>(first, result);
    }
}
Also used : TranslatedString(net.parostroj.timetable.model.TranslatedString) TimeInterval(net.parostroj.timetable.model.TimeInterval) Node(net.parostroj.timetable.model.Node) ArrayList(java.util.ArrayList) ElementSort(net.parostroj.timetable.actions.ElementSort) LinkedList(java.util.LinkedList) Iterator(java.util.Iterator) TrainComparator(net.parostroj.timetable.actions.TrainComparator) Train(net.parostroj.timetable.model.Train) Pair(net.parostroj.timetable.utils.Pair)

Aggregations

ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 LinkedList (java.util.LinkedList)1 ElementSort (net.parostroj.timetable.actions.ElementSort)1 TrainComparator (net.parostroj.timetable.actions.TrainComparator)1 Node (net.parostroj.timetable.model.Node)1 TimeInterval (net.parostroj.timetable.model.TimeInterval)1 Train (net.parostroj.timetable.model.Train)1 TranslatedString (net.parostroj.timetable.model.TranslatedString)1 Pair (net.parostroj.timetable.utils.Pair)1