/
Nicholas
/
java_basics
Обзор
Документация
Войти
/
Nicholas
/
java_basics
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
ExceptionsDebuggingAndTesting/homework_2/SPBMetro/src/main/java/StationIndex.java
60 строк
2 KB
Skillbox
Add lesson
17 сен 2023, 15:57
17 сен 2023, 15:57
3b39337
Код
Авторство
О чём код?
import core.Line; import core.Station; import java.util.*; import java.util.stream.Collectors; public class StationIndex { private final Map<Integer, Line> number2line; private final TreeSet<Station> stations; private final Map<Station, TreeSet<Station>> connections; public StationIndex() { number2line = new HashMap<>(); stations = new TreeSet<>(); connections = new TreeMap<>(); } public void addStation(Station station) { stations.add(station); } public void addLine(Line line) { number2line.put(line.getNumber(), line); } public void addConnection(List<Station> stations) { for (Station station : stations) { if (!connections.containsKey(station)) { connections.put(station, new TreeSet<>()); } TreeSet<Station> connectedStations = connections.get(station); connectedStations.addAll(stations.stream() .filter(s -> !s.equals(station)).collect(Collectors.toList())); } } public Line getLine(int number) { return number2line.get(number); } public Station getStation(String name) { for (Station station : stations) { if (station.getName().equalsIgnoreCase(name)) { return station; } } return null; } public Station getStation(String name, int lineNumber) { Station query = new Station(name, getLine(lineNumber)); Station station = stations.ceiling(query); return station.equals(query) ? station : null; } public Set<Station> getConnectedStations(Station station) { return connections.containsKey(station) ? connections.get(station) : new TreeSet<>(); } }