MATSIM
PersonMoneyEventsCollector.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * PersonMoneyEventsCollector.java
4  * *
5  * *********************************************************************** *
6  * *
7  * copyright : (C) 2021 by the members listed in the COPYING, *
8  * LICENSE and WARRANTY file. *
9  * email : info at matsim dot org *
10  * *
11  * *********************************************************************** *
12  * *
13  * This program is free software; you can redistribute it and/or modify *
14  * it under the terms of the GNU General Public License as published by *
15  * the Free Software Foundation; either version 2 of the License, or *
16  * (at your option) any later version. *
17  * See also COPYING, LICENSE and WARRANTY file *
18  * *
19  * *********************************************************************** */
20 
21 package org.matsim.analysis.personMoney;
22 
23 import com.google.inject.Inject;
24 import org.apache.commons.csv.CSVFormat;
25 import org.apache.commons.csv.CSVPrinter;
26 import org.apache.logging.log4j.LogManager;
30 import org.matsim.core.utils.io.IOUtils;
31 
32 import java.io.IOException;
33 import java.util.ArrayList;
34 import java.util.List;
35 
42 
43  private final List<PersonMoneyEvent> personMoneyEventList = new ArrayList<>();
44  private final String DEL;
45 
46  @Inject
47  PersonMoneyEventsCollector (GlobalConfigGroup globalConfigGroup) {
48  this.DEL = globalConfigGroup.getDefaultDelimiter();
49  }
50 
51  @Override
52  public void reset(int iteration) {
53  personMoneyEventList.clear();
54  }
55 
56  @Override
57  public void handleEvent(PersonMoneyEvent event) {
58  personMoneyEventList.add(event);
59  }
60 
61  void writeAllPersonMoneyEvents(String outputFilename) {
62  try (CSVPrinter csvPrinter = new CSVPrinter(IOUtils.getBufferedWriter(outputFilename),
63  CSVFormat.DEFAULT.withDelimiter(DEL.charAt(0)))) {
64  csvPrinter.printRecord("time", "person", "amount", "purpose", "transactionPartner", "reference");
65 
66  for (PersonMoneyEvent personMoneyEvent: personMoneyEventList) {
67  csvPrinter.printRecord(personMoneyEvent.getTime(), personMoneyEvent.getPersonId(),
68  personMoneyEvent.getAmount(), personMoneyEvent.getPurpose(),
69  personMoneyEvent.getTransactionPartner(), personMoneyEvent.getReference());
70  }
71  } catch (IOException e) {
72  LogManager.getLogger(getClass()).error("Could not write " + outputFilename + ".");
73  }
74  }
75 }
static BufferedWriter getBufferedWriter(URL url, Charset charset, boolean append)
Definition: IOUtils.java:390