All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.opentripplanner.model.TripStopTimes Maven / Gradle / Ivy

package org.opentripplanner.model;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * A multimap from Trip to a sorted list of StopTimes.
 * 

* The list of stop times for a given trip is guarantied to be sorted. */ public class TripStopTimes { private static final List EMPTY_LIST = Collections.emptyList(); private Map> map = new HashMap<>(); /** * Return a unmodifiable, nullsafe list of stop times for the given trip. * An emptyempty list is returned if no values exist for a given key. */ public List get(Trip key) { List list = map.get(key); return list == null ? EMPTY_LIST : Collections.unmodifiableList(list); } public void addAll(Collection values) { Set keysUpdated = new HashSet<>(); for (StopTime value : values) { Trip key = value.getTrip(); keysUpdated.add(key); map.computeIfAbsent(key, trip -> new ArrayList<>()).add(value); } // Sort and updated stops for all keys touched. for (Trip key : keysUpdated) { Collections.sort(map.get(key)); } } public void replace(Trip key, Collection list) { map.replace(key, sort(list)); } public void put(Trip key, Collection list) { map.put(key, sort(list)); } public void removeIf(Predicate test) { List removeKeys = map.keySet().stream().filter(test).collect(Collectors.toList()); for (Trip removeKey : removeKeys) { map.remove(removeKey); } } /** * Return a copy of the internal map. Changes in the source are not reflected * in the destination (returned Map), and visa versa. *

* The returned map is immutable. */ public Map> asImmutableMap() { return Map.copyOf(map); } public int size() { return map.size(); } /** * Return a iterable set of keys. Please do not remove keys the effect is undefined. */ public Iterable keys() { return map.keySet(); } /* private methods */ private static List sort(Collection list) { List values = new ArrayList<>(list); Collections.sort(values); return values; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy