Add bottom gas to ascent gases.
[dive.git] / src / net / ametros / dive / writer / AbstractWriter.java
blob4128c657ce12fba56216e0d6f943aa378faba979
1 /* Ametros Dive Computer
2 * Copyright (C) 2010 Geoff Johnstone
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 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.ametros.dive.writer;
20 import net.ametros.dive.ComputedDive;
21 import net.ametros.dive.ComputedStop;
22 import net.ametros.dive.data.Gas;
24 import java.io.File;
25 import java.io.FileOutputStream;
26 import java.io.OutputStream;
27 import java.util.Comparator;
28 import java.util.EnumSet;
29 import java.util.Map;
30 import java.util.Set;
31 import java.util.SortedSet;
32 import java.util.TreeMap;
33 import java.util.TreeSet;
34 import java.util.regex.Matcher;
35 import java.util.regex.Pattern;
38 /** Writes dive tables. */
39 public abstract class AbstractWriter implements Writer
41 /** Groups dives by depth, and then by duration. */
42 protected final Map<Integer, SortedSet<ComputedDive>> dives =
43 new TreeMap<Integer, SortedSet<ComputedDive>>();
46 @Override
47 public void clear()
49 dives.clear();
53 @Override
54 public void add (Iterable<ComputedDive> dives)
56 for (ComputedDive d: dives)
57 add (d);
61 @Override
62 public void add (ComputedDive[] dives)
64 for (ComputedDive d: dives)
65 add (d);
69 @Override
70 public void add (ComputedDive dive)
72 final int depth = dive.getDepth();
74 if (!dives.containsKey (depth))
76 dives.put (depth,
77 new TreeSet<ComputedDive> (new Comparator<ComputedDive>() {
78 @Override
79 public int compare (ComputedDive d1, ComputedDive d2)
81 return d1.getDuration() - d2.getDuration();
83 }));
86 dives.get (depth).add (dive);
90 @Override
91 public void write (File file) throws Exception
93 OutputStream os = null;
94 boolean deleteFile = false;
96 try
98 os = new FileOutputStream (file);
99 deleteFile = true;
100 write (os);
101 os.close();
102 os = null;
103 deleteFile = false;
105 finally
107 try { if (null != os) os.close(); }
108 finally { if (deleteFile) file.delete(); }
113 private static final Pattern formatNx = Pattern.compile ("NX_(\\d+)");
114 private static final Pattern formatTx = Pattern.compile ("TX_(\\d+)_(\\d+)");
116 public String format (Gas gas)
118 final String str = "" + gas;
120 Matcher m = formatNx.matcher (str);
121 if (m.matches())
122 return "Nx " + m.group (1);
124 m = formatTx.matcher (str);
125 if (m.matches())
126 return "Tx " + m.group (1) + '/' + m.group (2);
128 return str;
132 /** Gets a set of stops representing all (Gas, Depth) pairs for all stops
133 * for all dives to a given bottom depth, ordered by decreasing depth and
134 * then by gas.
136 protected SortedSet<ComputedStop> getStopKeys (int bottomDepth)
138 final SortedSet<ComputedStop> stops =
139 new TreeSet<ComputedStop> (new Comparator<ComputedStop>() {
140 @Override
141 public int compare (ComputedStop s1, ComputedStop s2)
143 final int d1 = s1.getDepth();
144 final int d2 = s2.getDepth();
146 if (d1 != d2)
147 return d2 - d1;
149 final Gas g1 = s1.getGas();
150 final Gas g2 = s2.getGas();
152 return g1.toString().compareTo (g2.toString());
156 for (ComputedDive d: dives.get (bottomDepth))
157 for (ComputedStop s: d.getStops())
158 stops.add (s);
160 return stops;
164 /** Gets a set of the gases used for dives to a given bottom depth. */
165 protected Set<Gas> getGases (int bottomDepth)
167 final Set<Gas> gases = EnumSet.noneOf (Gas.class);
169 for (ComputedDive d: dives.get (bottomDepth))
171 gases.add (d.getGas());
173 for (ComputedStop s: d.getStops())
174 gases.add (s.getGas());
177 return gases;
181 public long ceil (double in)
183 return (long)Math.ceil (in);