/* * Copyright © 2010 Keith Packard * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; version 2 of the License. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. */ package org.altusmetrum.altoslib_2; import java.io.*; import java.util.*; import java.text.*; class AltosTelemetryOrdered implements Comparable { AltosTelemetry telem; int index; int tick; public int compareTo(AltosTelemetryOrdered o) { int tick_diff = tick - o.tick; if (tick_diff != 0) return tick_diff; return index - o.index; } AltosTelemetryOrdered (AltosTelemetry telem, int index, int tick) { this.telem = telem; this.index = index; this.tick = tick; } } class AltosTelemetryOrderedIterator implements Iterator { TreeSet olist; Iterator oiterator; public AltosTelemetryOrderedIterator(Iterable telems) { olist = new TreeSet(); int tick = 0; int index = 0; boolean first = true; for (AltosTelemetry e : telems) { int t = e.tick; if (first) tick = t; else { while (t < tick - 32767) t += 65536; tick = t; } olist.add(new AltosTelemetryOrdered(e, index++, tick)); first = false; } oiterator = olist.iterator(); } public boolean hasNext() { return oiterator.hasNext(); } public AltosTelemetry next() { return oiterator.next().telem; } public void remove () { } } public class AltosTelemetryIterable implements Iterable { LinkedList telems; public Iterator iterator () { if (telems == null) telems = new LinkedList(); return new AltosTelemetryOrderedIterator(telems); } public AltosTelemetryIterable (FileInputStream input) { telems = new LinkedList (); try { for (;;) { String line = AltosLib.gets(input); if (line == null) { break; } try { AltosTelemetry telem = AltosTelemetry.parse(line); if (telem == null) break; telems.add(telem); } catch (ParseException pe) { System.out.printf("parse exception %s\n", pe.getMessage()); } catch (AltosCRCException ce) { } } } catch (IOException io) { System.out.printf("io exception\n"); } } }