]> git.somenet.org - pub/jan/lsdc.git/blob - src/at/ac/tuwien/lsdc/util/CSVParser.java
GITOLITE.txt
[pub/jan/lsdc.git] / src / at / ac / tuwien / lsdc / util / CSVParser.java
1 package at.ac.tuwien.lsdc.util;
2
3 import java.io.File;
4 import java.io.FileReader;
5 import java.io.IOException;
6 import java.util.LinkedList;
7 import java.util.List;
8
9 import at.ac.tuwien.lsdc.types.Application;
10 import au.com.bytecode.opencsv.CSVReader;
11 import au.com.bytecode.opencsv.CSVWriter;
12
13 public class CSVParser {
14         public static LinkedList<Application> parseFile(File csvFile)
15                         throws IOException {
16                 CSVReader reader = new CSVReader(new FileReader(csvFile), ';',
17                                 CSVWriter.NO_QUOTE_CHARACTER);
18                 List<String[]> file = reader.readAll();
19                 LinkedList<Application> apps = new LinkedList<Application>();
20                 // start at 1 to ignore header
21                 for (int i = 1; i < file.size(); i++) {
22                         String[] line = file.get(i);
23                         if (line.length != 5) {
24                                 reader.close();
25                                 throw new IOException(
26                                                 "CSVParser: Not right amount of elements in line " + i
27                                                                 + 1);
28                         } else {
29                                 try {
30                                         Application a = parseLine(line);
31                                         apps.add(a);
32                                 } catch (NumberFormatException e) {
33                                         reader.close();
34                                         throw new IOException("Couldn't parse line");
35                                 }
36                         }
37                 }
38                 reader.close();
39                 return apps;
40         }
41
42         private static Application parseLine(String[] appStr)
43                         throws NumberFormatException {
44                 long timestamp = Long.parseLong(appStr[0]);
45                 int size = Integer.parseInt(appStr[1]);
46                 int ram = Integer.parseInt(appStr[2]);
47                 int cpu = Integer.parseInt(appStr[3]);
48                 int duration = Integer.parseInt(appStr[4]);
49                 Application app = new Application(timestamp, size, ram, cpu, duration);
50                 return app;
51         }
52
53 }