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
;
25 import java
.io
.FileOutputStream
;
26 import java
.io
.OutputStream
;
27 import java
.util
.Comparator
;
28 import java
.util
.EnumSet
;
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
>>();
54 public void add (Iterable
<ComputedDive
> dives
)
56 for (ComputedDive d
: dives
)
62 public void add (ComputedDive
[] dives
)
64 for (ComputedDive d
: dives
)
70 public void add (ComputedDive dive
)
72 final int depth
= dive
.getDepth();
74 if (!dives
.containsKey (depth
))
77 new TreeSet
<ComputedDive
> (new Comparator
<ComputedDive
>() {
79 public int compare (ComputedDive d1
, ComputedDive d2
)
81 return d1
.getDuration() - d2
.getDuration();
86 dives
.get (depth
).add (dive
);
91 public void write (File file
) throws Exception
93 OutputStream os
= null;
94 boolean deleteFile
= false;
98 os
= new FileOutputStream (file
);
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
);
122 return "Nx " + m
.group (1);
124 m
= formatTx
.matcher (str
);
126 return "Tx " + m
.group (1) + '/' + m
.group (2);
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
136 protected SortedSet
<ComputedStop
> getStopKeys (int bottomDepth
)
138 final SortedSet
<ComputedStop
> stops
=
139 new TreeSet
<ComputedStop
> (new Comparator
<ComputedStop
>() {
141 public int compare (ComputedStop s1
, ComputedStop s2
)
143 final int d1
= s1
.getDepth();
144 final int d2
= s2
.getDepth();
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())
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());
181 public long ceil (double in
)
183 return (long)Math
.ceil (in
);