-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSSPInstanceReader.java
More file actions
97 lines (86 loc) · 3.8 KB
/
Copy pathSSPInstanceReader.java
File metadata and controls
97 lines (86 loc) · 3.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package com.aim.project.ssp.instance.reader;
import java.io.BufferedReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;
import com.aim.project.ssp.instance.Location;
import com.aim.project.ssp.instance.SSPInstance;
import com.aim.project.ssp.interfaces.SSPInstanceInterface;
import com.aim.project.ssp.interfaces.SSPInstanceReaderInterface;
/**
* Reads and parses SSP instance data from a file.
* Implements the SSPInstanceReaderInterface to handle the reading of an SSP instance from a given file path.
*
* @author Sushant Nepal
* @since 17/03/2025
*/
public class SSPInstanceReader implements SSPInstanceReaderInterface {
/**
* Reads an SSP instance from the specified file path.
* The file format includes a name, comment, hotel location, airport location, and a list of points of interest.
*
* @param path Path to the SSP instance file.
* @param random Random instance for generating random values.
* @return A new SSPInstance object containing the parsed data.
* @throws RuntimeException If an error occurs while reading the file or if required information is missing.
*/
@Override
public SSPInstanceInterface readSSPInstance(Path path, Random random) {
// Try-with-resources to automatically close the BufferedReader
try (BufferedReader br = Files.newBufferedReader(path)) {
String line;
Location hotelLocation = null;
Location airportLocation = null;
Set<Location> uniquePoints = new HashSet<>(); // Set to avoid duplicates
// Process the file line by line
while ((line = br.readLine()) != null) {
if (!line.startsWith("NAME :")) {
if (!line.startsWith("COMMENT :")) {
switch (line) {
case "HOTEL_LOCATION" -> hotelLocation = parseLocation(br.readLine());
case "AIRPORT_LOCATION" -> airportLocation = parseLocation(br.readLine());
case "POINTS_OF_INTEREST" -> {
// Read and store unique points of interest
while ((line = br.readLine()) != null && !line.equals("EOF")) {
uniquePoints.add(parseLocation(line)); // Only unique locations are stored
}
}
}
}
}
}
// Ensure all required fields are present before returning
if (hotelLocation == null || airportLocation == null || uniquePoints.isEmpty()) {
throw new IOException("Invalid SSP file format: Missing required information.");
}
// Return a new SSPInstance with the parsed data
return new SSPInstance(uniquePoints.size(), uniquePoints.toArray(new Location[0]), hotelLocation, airportLocation, random);
} catch (IOException e) {
// Wrap IOException in RuntimeException to provide additional context
throw new RuntimeException("Error reading SSP file: " + path, e);
}
}
/**
* Parses a location from a string representing coordinates.
*
* @param line The string containing the coordinates (e.g., "x y").
* @return A Location object with the parsed coordinates.
* @throws IOException If the coordinate format is invalid.
*/
private Location parseLocation(String line) throws IOException {
// Split the line into coordinates and validate
String[] coords = line.trim().split("\\s+");
if (coords.length != 2) {
throw new IOException("Invalid coordinate format: " + line);
}
try {
// Return a new Location object created from the parsed coordinates
return new Location(Integer.parseInt(coords[0]), Integer.parseInt(coords[1]));
} catch (NumberFormatException e) {
throw new IOException("Invalid coordinate values: " + line, e);
}
}
}