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 private StringWriter writer;
27 static private int get(StringReader reader) throws IOException {
31 static private String get_token(StringReader reader) throws IOException {
37 ArrayList<Integer> chars = new ArrayList<Integer>();
42 if (c == -1 || c == ';')
47 int[] ch = new int[chars.size()];
48 for (int i = 0; i < ch.length; i++)
50 return new String(ch, 0, ch.length);
53 static private void put(StringWriter writer, int c) throws IOException {
57 static private void put_token(StringWriter writer, String token) throws IOException {
58 for (int i = 0; i < token.length(); i++) {
59 int c = token.codePointAt(i);
71 public String toString() {
73 StringWriter writer = new StringWriter();
75 for (String key : keySet()) {
76 String value = get(key);
77 put_token(writer, key);
78 put_token(writer, value);
80 return writer.toString();
81 } catch (IOException ie) {
86 public void putInt(String key, int value) {
87 put(key, Integer.toString(value));
90 public int getInt(String key, int def) {
91 String value = get(key);
96 return AltosParse.parse_int(value);
97 } catch (ParseException pe) {
102 public void putDouble(String key, double value) {
103 put(key, AltosParse.format_double_net(value));
106 public double getDouble(String key, double def) {
107 String value = get(key);
112 return AltosParse.parse_double_net(value);
113 } catch (ParseException pe) {
118 public String getString(String key, String def) {
119 String value = get(key);
126 public void putString(String key, String value) {
130 public AltosHashSet (String string) throws IOException {
131 StringReader reader = new StringReader(string);
135 key = get_token(reader);
136 value = get_token(reader);
137 if (key == null || value == null)
143 public AltosHashSet() {
146 static public AltosHashSet[] array(String string) throws IOException {
151 StringReader reader = new StringReader(string);
152 ArrayList<AltosHashSet> array = new ArrayList<AltosHashSet>();
155 while ((element = get_token(reader)) != null)
156 array.add(new AltosHashSet(element));
157 return array.toArray(new AltosHashSet[0]);
160 static public String toString(AltosHashSet[] sets) throws IOException {
165 StringWriter writer = new StringWriter();
167 for (AltosHashSet h : sets) {
168 String element = h.toString();
169 put_token(writer, element);
171 return writer.toString();