int serviceListSize = readIntegerValue();
serviceList = new ArrayList<MrService>(serviceListSize);
long serviceId = 0L;
// 2 phases because service dependencies are not in low to high order
for (int i = 0; i < serviceListSize; i++) {
MrService service = new MrService();
service.setId(serviceId);
service.setFromDependencyServiceList(new ArrayList<MrService>(5));
serviceList.add(service);
serviceId++;
}
for (int i = 0; i < serviceListSize; i++) {
MrService service = serviceList.get(i);
String line = readStringValue();
String[] lineTokens = splitBySpace(line);
service.setLocationSpread(Integer.parseInt(lineTokens[0]));
int serviceDependencyListSize = Integer.parseInt(lineTokens[1]);
List<MrService> toDependencyServiceList = new ArrayList<MrService>(serviceDependencyListSize);
for (int j = 0; j < serviceDependencyListSize; j++) {
int toServiceIndex = Integer.parseInt(lineTokens[2 + j]);
if (toServiceIndex >= serviceList.size()) {
throw new IllegalArgumentException("Service with id (" + serviceId
+ ") has a non existing toServiceIndex (" + toServiceIndex + ").");
}
MrService toService = serviceList.get(toServiceIndex);
if (toService.equals(service)) {
throw new IllegalStateException("The toService (" + toService
+ ") cannot be equal to the service (" + service + ").");
}
toDependencyServiceList.add(toService);
toService.getFromDependencyServiceList().add(service);
}
service.setToDependencyServiceList(toDependencyServiceList);
int numberOfTokens = 2 + serviceDependencyListSize;
if (lineTokens.length != numberOfTokens) {
throw new IllegalArgumentException("Read line (" + line + ") has " + lineTokens.length