altosui: Wait for valid callsign/flight when graphing
[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 ((state.data.seen & AltosRecord.seen_flight) != 0) {
40                         if (dataSet.callsign == null && state.data.callsign != null)
41                                 dataSet.callsign = state.data.callsign;
42
43                         if (dataSet.serial == 0 && state.data.serial != 0)
44                                 dataSet.serial = state.data.serial;
45
46                         if (dataSet.flight == 0 && state.data.flight != 0)
47                                 dataSet.flight = state.data.flight;
48                 }
49
50                 return new AltosGraphDataPoint(state);
51         }
52
53         public AltosGraphIterator (Iterator<AltosRecord> iterator, AltosGraphDataSet dataSet) {
54                 this.iterator = iterator;
55                 this.state = null;
56                 this.dataSet = dataSet;
57         }
58
59         public void remove() {
60         }
61 }
62
63 class AltosGraphIterable implements Iterable<AltosUIDataPoint> {
64         AltosGraphDataSet       dataSet;
65
66         public Iterator<AltosUIDataPoint> iterator() {
67                 return new AltosGraphIterator(dataSet.records.iterator(), dataSet);
68         }
69
70         public AltosGraphIterable(AltosGraphDataSet dataSet) {
71                 this.dataSet = dataSet;
72         }
73 }
74
75 public class AltosGraphDataSet implements AltosUIDataSet {
76         String                  callsign;
77         int                     serial;
78         int                     flight;
79         AltosRecordIterable     records;
80
81         public String name() {
82                 if (callsign != null)
83                         return String.format("%s - %d/%d", callsign, serial, flight);
84                 else
85                         return String.format("%d/%d", serial, flight);
86         }
87
88         public Iterable<AltosUIDataPoint> dataPoints() {
89                 return new AltosGraphIterable(this);
90         }
91
92         public AltosGraphDataSet (AltosRecordIterable records) {
93                 this.records = records;
94                 this.callsign = null;
95                 this.serial = 0;
96                 this.flight = 0;
97         }
98 }