altoslib: Create AltosProgrammer class
[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_2.*;
24 import org.altusmetrum.altosuilib_1.*;
25
26 class AltosGraphIterator implements Iterator<AltosUIDataPoint> {
27         AltosGraphDataSet       dataSet;
28         Iterator<AltosState>    iterator;
29
30         public boolean hasNext() {
31                 return iterator.hasNext();
32         }
33
34         public AltosUIDataPoint next() {
35                 AltosState      state = iterator.next();
36
37                 if (state.flight != AltosLib.MISSING) {
38                         if (dataSet.callsign == null && state.callsign != null)
39                                 dataSet.callsign = state.callsign;
40
41                         if (dataSet.serial == 0 && state.serial != 0)
42                                 dataSet.serial = state.serial;
43
44                         if (dataSet.flight == 0 && state.flight != 0)
45                                 dataSet.flight = state.flight;
46                 }
47
48                 return new AltosGraphDataPoint(state);
49         }
50
51         public AltosGraphIterator (Iterator<AltosState> iterator, AltosGraphDataSet dataSet) {
52                 this.iterator = iterator;
53                 this.dataSet = dataSet;
54         }
55
56         public void remove() {
57         }
58 }
59
60 class AltosGraphIterable implements Iterable<AltosUIDataPoint> {
61         AltosGraphDataSet       dataSet;
62
63         public Iterator<AltosUIDataPoint> iterator() {
64                 return new AltosGraphIterator(dataSet.states.iterator(), dataSet);
65         }
66
67         public AltosGraphIterable(AltosGraphDataSet dataSet) {
68                 this.dataSet = dataSet;
69         }
70 }
71
72 public class AltosGraphDataSet implements AltosUIDataSet {
73         String                  callsign;
74         int                     serial;
75         int                     flight;
76         AltosStateIterable      states;
77
78         public String name() {
79                 if (callsign != null)
80                         return String.format("%s - %d/%d", callsign, serial, flight);
81                 else
82                         return String.format("%d/%d", serial, flight);
83         }
84
85         public Iterable<AltosUIDataPoint> dataPoints() {
86                 return new AltosGraphIterable(this);
87         }
88
89         public AltosGraphDataSet (AltosStateIterable states) {
90                 this.states = states;
91                 this.callsign = null;
92                 this.serial = 0;
93                 this.flight = 0;
94         }
95 }