]> git.gag.com Git - fw/altos/blob - altoslib/AltosEepromIterable.java
fix for jenkins.sh
[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; 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 org.altusmetrum.altoslib_4;
19
20 import java.io.*;
21 import java.util.*;
22 import java.text.*;
23
24 class AltosEepromOrdered implements Comparable<AltosEepromOrdered> {
25         AltosEeprom     eeprom;
26         int             index;
27         int             tick;
28
29         int cmdi() {
30                 if (eeprom.cmd == AltosLib.AO_LOG_FLIGHT)
31                         return 0;
32                 return 1;
33         }
34
35         public int compareTo(AltosEepromOrdered o) {
36                 int     cmd_diff = cmdi() - o.cmdi();
37
38                 if (cmd_diff != 0)
39                         return cmd_diff;
40
41                 int     tick_diff = tick - o.tick;
42
43                 if (tick_diff != 0)
44                         return tick_diff;
45                 return index - o.index;
46         }
47
48         AltosEepromOrdered (AltosEeprom eeprom, int index, int tick) {
49                 this.eeprom = eeprom;
50                 this.index = index;
51                 this.tick = tick;
52         }
53 }
54
55 class AltosEepromOrderedIterator implements Iterator<AltosEeprom> {
56         TreeSet<AltosEepromOrdered>     olist;
57         Iterator<AltosEepromOrdered>    oiterator;
58
59         public AltosEepromOrderedIterator(Iterable<AltosEeprom> eeproms) {
60                 olist = new TreeSet<AltosEepromOrdered>();
61
62                 int     tick = 0;
63                 int     index = 0;
64                 boolean first = true;
65
66                 for (AltosEeprom e : eeproms) {
67                         int     t = e.tick;
68                         if (first)
69                                 tick = t;
70                         else {
71                                 while (t < tick - 32767)
72                                         t += 65536;
73                                 tick = t;
74                         }
75                         olist.add(new AltosEepromOrdered(e, index++, tick));
76                         first = false;
77                 }
78
79                 oiterator = olist.iterator();
80         }
81
82         public boolean hasNext() {
83                 return oiterator.hasNext();
84         }
85
86         public AltosEeprom next() {
87                 return oiterator.next().eeprom;
88         }
89
90         public void remove () {
91         }
92 }
93
94 public class AltosEepromIterable implements Iterable<AltosEeprom> {
95         public LinkedList<AltosEeprom> eeproms;
96
97         public void write(PrintStream out) {
98                 for (AltosEeprom eeprom : eeproms)
99                         eeprom.write(out);
100         }
101
102         public AltosState state() {
103                 AltosState      state = new AltosState();
104
105                 for (AltosEeprom header : eeproms)
106                         header.update_state(state);
107                 return state;
108         }
109
110         public AltosEepromIterable(LinkedList<AltosEeprom> eeproms) {
111                 this.eeproms = eeproms;
112         }
113
114         public Iterator<AltosEeprom> iterator() {
115                 if (eeproms == null)
116                         eeproms = new LinkedList<AltosEeprom>();
117                 return new AltosEepromOrderedIterator(eeproms);
118         }
119 }