2 * Copyright © 2016 Keith Packard <keithp@keithp.com>
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.
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.
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.
18 package org.altusmetrum.altoslib_11;
24 public class AltosHashSet extends Hashtable<String,String> {
25 static private int get(StringReader reader) throws IOException {
29 static public String get_token(StringReader reader) throws IOException {
35 ArrayList<Integer> chars = new ArrayList<Integer>();
38 if (c == -1 || c == ';')
45 int[] ch = new int[chars.size()];
46 for (int i = 0; i < ch.length; i++)
48 return new String(ch, 0, ch.length);
51 static private void put(StringWriter writer, int c) throws IOException {
55 static public void put_token(StringWriter writer, String token) throws IOException {
56 for (int i = 0; i < token.length(); i++) {
57 int c = token.codePointAt(i);
69 public String toString() {
71 StringWriter writer = new StringWriter();
73 for (String key : keySet()) {
74 String value = get(key);
75 put_token(writer, key);
76 put_token(writer, value);
78 return writer.toString();
79 } catch (IOException ie) {
84 public void putBoolean(String key, boolean value) {
85 put(key, value ? "t" : "f");
88 public boolean getBoolean(String key, boolean def) {
89 String value = get(key);
93 if (value.equals("t"))
95 if (value.equals("f"))
100 public void putInt(String key, int value) {
101 put(key, Integer.toString(value));
104 public int getInt(String key, int def) {
105 String value = get(key);
110 return AltosParse.parse_int(value);
111 } catch (ParseException pe) {
116 public void putIntArray(String key, int value[]) {
120 StringWriter writer = new StringWriter();
123 for (int i = 0; i < value.length; i++)
124 put_token(writer, Integer.toString(value[i]));
125 put(key, writer.toString());
126 } catch (IOException ie) {
130 public int[] getIntArray(String key, int[] def) {
131 String value = get(key);
136 StringReader reader = new StringReader(value);
137 ArrayList<Integer> array = new ArrayList<Integer>();
140 while ((elt = get_token(reader)) != null)
141 array.add(AltosParse.parse_int(elt));
142 int[] ret = new int[array.size()];
143 for (int i = 0; i < ret.length; i++)
144 ret[i] = array.get(i);
146 } catch (ParseException pe) {
148 } catch (IOException ie) {
153 public void putLong(String key, long value) {
154 put(key, Long.toString(value));
157 public long getLong(String key, long def) {
158 String value = get(key);
163 return AltosParse.parse_long(value);
164 } catch (ParseException pe) {
169 public void putDouble(String key, double value) {
170 put(key, AltosParse.format_double_net(value));
173 public double getDouble(String key, double def) {
174 String value = get(key);
179 return AltosParse.parse_double_net(value);
180 } catch (ParseException pe) {
185 public void putDoubleArray(String key, double value[]) {
189 StringWriter writer = new StringWriter();
192 for (int i = 0; i < value.length; i++)
193 put_token(writer, AltosParse.format_double_net(value[i]));
194 put(key, writer.toString());
195 } catch (IOException ie) {
199 public double[] getDoubleArray(String key, double[] def) {
200 String value = get(key);
205 StringReader reader = new StringReader(value);
206 ArrayList<Double> array = new ArrayList<Double>();
209 while ((elt = get_token(reader)) != null)
210 array.add(AltosParse.parse_double_net(elt));
211 double[] ret = new double[array.size()];
212 for (int i = 0; i < ret.length; i++)
213 ret[i] = array.get(i);
215 } catch (ParseException pe) {
217 } catch (IOException ie) {
222 public String getString(String key, String def) {
223 String value = get(key);
230 public void putString(String key, String value) {
235 public AltosHashSet getHash(String key) {
236 String value = get(key);
241 return new AltosHashSet(value);
242 } catch (IOException ie) {
247 public void putHash(String key, AltosHashSet h) {
248 put(key, h.toString());
251 public void putHashable(String key, AltosHashable h) {
255 put(key, h.hashSet().toString());
258 private AltosHashSet (String string) throws IOException {
259 StringReader reader = new StringReader(string);
263 key = get_token(reader);
264 value = get_token(reader);
265 if (key == null || value == null)
271 public AltosHashSet() {
274 static public AltosHashSet fromString(String string) {
276 return new AltosHashSet(string);
277 } catch (IOException ie) {
282 static public AltosHashSet[] array(String string) {
288 StringReader reader = new StringReader(string);
289 ArrayList<AltosHashSet> array = new ArrayList<AltosHashSet>();
292 while ((element = get_token(reader)) != null)
293 array.add(new AltosHashSet(element));
294 return array.toArray(new AltosHashSet[0]);
295 } catch (IOException ie) {
300 static public String toString(AltosHashSet[] sets) {
305 StringWriter writer = new StringWriter();
307 for (AltosHashSet h : sets) {
308 String element = h.toString();
309 put_token(writer, element);
311 return writer.toString();
312 } catch (IOException ie) {