Merge remote-tracking branch 'mjb/master'
[fw/altos] / altosui / AltosGraphDataSet.java
1 /*
2  * Copyright © 2013 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 package altosui;
19
20 import java.lang.*;
21 import java.io.*;
22 import java.util.*;
23 import org.altusmetrum.altoslib_1.*;
24 import org.altusmetrum.altosuilib_1.*;
25
26 class AltosGraphIterator implements Iterator<AltosUIDataPoint> {
27         AltosGraphDataSet       dataSet;
28         Iterator<AltosRecord>   iterator;
29
30         AltosState      state;
31
32         public boolean hasNext() {
33                 return iterator.hasNext();
34         }
35
36         public AltosUIDataPoint next() {
37                 state = new AltosState(iterator.next(), state);
38
39                 if (dataSet.callsign == null && state.data.callsign != null)
40                         dataSet.callsign = state.data.callsign;
41
42                 if (dataSet.serial == 0 && state.data.serial != 0)
43                         dataSet.serial = state.data.serial;
44
45                 if (dataSet.flight == 0 && state.data.flight != 0)
46                         dataSet.flight = state.data.flight;
47
48                 return new AltosGraphDataPoint(state);
49         }
50
51         public AltosGraphIterator (Iterator<AltosRecord> iterator, AltosGraphDataSet dataSet) {
52                 this.iterator = iterator;
53                 this.state = null;
54                 this.dataSet = dataSet;
55         }
56
57         public void remove() {
58         }
59 }
60
61 class AltosGraphIterable implements Iterable<AltosUIDataPoint> {
62         AltosGraphDataSet       dataSet;
63
64         public Iterator<AltosUIDataPoint> iterator() {
65                 return new AltosGraphIterator(dataSet.records.iterator(), dataSet);
66         }
67
68         public AltosGraphIterable(AltosGraphDataSet dataSet) {
69                 this.dataSet = dataSet;
70         }
71 }
72
73 public class AltosGraphDataSet implements AltosUIDataSet {
74         String                  callsign;
75         int                     serial;
76         int                     flight;
77         AltosRecordIterable     records;
78
79         public String name() {
80                 if (callsign != null)
81                         return String.format("%s - %d/%d", callsign, serial, flight);
82                 else
83                         return String.format("%d/%d", serial, flight);
84         }
85
86         public Iterable<AltosUIDataPoint> dataPoints() {
87                 return new AltosGraphIterable(this);
88         }
89
90         public AltosGraphDataSet (AltosRecordIterable records) {
91                 this.records = records;
92                 this.callsign = null;
93                 this.serial = 0;
94                 this.flight = 0;
95         }
96 }