MATSIM
CountsWriterV1.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * CountsWriter.java
4  * *
5  * *********************************************************************** *
6  * *
7  * copyright : (C) 2007 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.counts;
22 
27 
28 import java.io.IOException;
29 import java.io.OutputStream;
30 import java.io.Serializable;
31 import java.util.Collections;
32 import java.util.Comparator;
33 import java.util.List;
34 import java.util.Vector;
35 
36 @Deprecated
37 final class CountsWriterV1 extends MatsimXmlWriter implements MatsimWriter {
38 
39  private final CountsWriterHandler handler;
40  private final Counts counts;
41 
42  public CountsWriterV1(final Counts counts) {
43  this( new IdentityTransformation() , counts );
44  }
45 
46  public CountsWriterV1(
47  final CoordinateTransformation coordinateTransformation,
48  final Counts counts) {
49  this.counts = counts;
50 
51  // use the newest writer-version by default
52  this.handler = new CountsWriterHandlerImplV1(coordinateTransformation);
53  }
54 
55  @Override
56  public final void write(final String filename) {
57  try {
58  openFile(filename);
59  doTheWriting();
60  close();
61  }
62  catch (IOException e) {
63  throw new RuntimeException(e);
64  }
65  }
66 
67  public final void write(final OutputStream stream) {
68  try {
69  openOutputStream(stream);
70  doTheWriting();
71  close();
72  }
73  catch (IOException e) {
74  throw new RuntimeException(e);
75  }
76  }
77 
78  private void doTheWriting() throws IOException {
79  // write custom header
80  writeXmlHead();
81 
82  this.handler.startCounts(this.counts, this.writer);
83  this.handler.writeSeparator(this.writer);
84 
85  List<Count> countsTemp = new Vector<Count>();
86  countsTemp.addAll(this.counts.getCounts().values());
87  Collections.sort(countsTemp, new CountComparator());
88 
89  for (Count c : countsTemp) {
90  List<Volume> volumesTemp = new Vector<Volume>();
91  volumesTemp.addAll(c.getVolumes().values());
92  Collections.sort(volumesTemp, new VolumeComparator());
93  this.handler.startCount(c, this.writer);
94  for (Volume v : volumesTemp) {
95  this.handler.startVolume(v, this.writer);
96  this.handler.endVolume(this.writer);
97  }
98  this.handler.endCount(this.writer);
99  this.handler.writeSeparator(this.writer);
100  this.writer.flush();
101  }
102  this.handler.endCounts(this.writer);
103  }
104 
105  @Override
106  public final String toString() {
107  return super.toString();
108  }
109 
110  static class VolumeComparator implements Comparator<Volume>, Serializable {
111  private static final long serialVersionUID = 1L;
112 
113  @Override
114  public int compare(final Volume v1, final Volume v2) {
115  return Double.compare(v1.getHourOfDayStartingWithOne(), v2.getHourOfDayStartingWithOne());
116  }
117  }
118 
119  static class CountComparator implements Comparator<Count>, Serializable {
120  private static final long serialVersionUID = 1L;
121 
122  @Override
123  public int compare(final Count c1, final Count c2) {
124 
125  // This seemed to be intended for a rather special case with a rather special
126  // numbering of counts... michaz mar 10
127 
128 // int i1 = Integer.parseInt(c1.getCsId().substring(c1.getCsId().length()-3));
129 // int i2 = Integer.parseInt(c2.getCsId().substring(c2.getCsId().length()-3));
130 // if (i1 < i2) return -1;
131 // else if (i1 > i2) return 1;
132 // else return 0;
133 
134 
135  return c1.getCsLabel().compareTo(c2.getCsLabel());
136  }
137  }
138 }
final void openOutputStream(OutputStream outputStream)