Switch from GPLv2 to GPLv2+
[fw/altos] / altoslib / AltosEepromIterable.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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 package org.altusmetrum.altoslib_11;
20
21 import java.io.*;
22 import java.util.*;
23 import java.text.*;
24
25 class AltosEepromOrdered implements Comparable<AltosEepromOrdered> {
26         AltosEeprom     eeprom;
27         int             index;
28         int             tick;
29
30         int cmdi() {
31                 if (eeprom.cmd == AltosLib.AO_LOG_FLIGHT)
32                         return 0;
33                 return 1;
34         }
35
36         public int compareTo(AltosEepromOrdered o) {
37                 int     cmd_diff = cmdi() - o.cmdi();
38
39                 if (cmd_diff != 0)
40                         return cmd_diff;
41
42                 if (eeprom.has_seconds() && o.eeprom.has_seconds()) {
43                         int     seconds_diff = eeprom.seconds() - o.eeprom.seconds();
44
45                         if (seconds_diff != 0)
46                                 return seconds_diff;
47                 }
48
49                 int     tick_diff = tick - o.tick;
50
51                 if (tick_diff != 0)
52                         return tick_diff;
53                 return index - o.index;
54         }
55
56         AltosEepromOrdered (AltosEeprom eeprom, int index, int tick) {
57                 this.eeprom = eeprom;
58                 this.index = index;
59                 this.tick = tick;
60         }
61 }
62
63 class AltosEepromOrderedIterator implements Iterator<AltosEeprom> {
64         TreeSet<AltosEepromOrdered>     olist;
65         Iterator<AltosEepromOrdered>    oiterator;
66
67         public AltosEepromOrderedIterator(Iterable<AltosEeprom> eeproms) {
68                 olist = new TreeSet<AltosEepromOrdered>();
69
70                 int     tick = 0;
71                 int     index = 0;
72                 boolean first = true;
73
74                 for (AltosEeprom e : eeproms) {
75                         int     t = e.tick;
76                         if (first)
77                                 tick = t;
78                         else {
79                                 while (t < tick - 32767)
80                                         t += 65536;
81                                 tick = t;
82                         }
83                         olist.add(new AltosEepromOrdered(e, index++, tick));
84                         first = false;
85                 }
86
87                 oiterator = olist.iterator();
88         }
89
90         public boolean hasNext() {
91                 return oiterator.hasNext();
92         }
93
94         public AltosEeprom next() {
95                 return oiterator.next().eeprom;
96         }
97
98         public void remove () {
99         }
100 }
101
102 public class AltosEepromIterable implements Iterable<AltosEeprom> {
103         public LinkedList<AltosEeprom> eeproms;
104
105         public void write(PrintStream out) {
106                 for (AltosEeprom eeprom : eeproms)
107                         eeprom.write(out);
108         }
109
110         public AltosState state() {
111                 AltosState      state = new AltosState();
112
113                 for (AltosEeprom header : eeproms)
114                         header.update_state(state);
115                 return state;
116         }
117
118         public AltosEepromIterable(LinkedList<AltosEeprom> eeproms) {
119                 this.eeproms = eeproms;
120         }
121
122         public Iterator<AltosEeprom> iterator() {
123                 if (eeproms == null)
124                         eeproms = new LinkedList<AltosEeprom>();
125                 return new AltosEepromOrderedIterator(eeproms);
126         }
127 }