488d52e8432e6413c87f8e27f2496189d322cd89
[fw/altos] / altoslib / AltosHashSet.java
1 /*
2  * Copyright © 2016 Keith Packard <keithp@keithp.com>
3  *
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.
7  *
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.
12  *
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.
16  */
17
18 package org.altusmetrum.altoslib_11;
19
20 import java.io.*;
21 import java.util.*;
22 import java.text.*;
23
24 public class AltosHashSet extends Hashtable<String,String> {
25         private StringWriter    writer;
26
27         static private int get(StringReader reader) throws IOException {
28                 return reader.read();
29         }
30
31         static private String get_token(StringReader reader) throws IOException {
32                 int     c = get(reader);
33
34                 if (c == -1)
35                         return null;
36
37                 ArrayList<Integer>      chars = new ArrayList<Integer>();
38
39                 for (;;) {
40                         chars.add(c);
41                         c = get(reader);
42                         if (c == -1 || c == ';')
43                                 break;
44                         if (c == '\\')
45                                 c = get(reader);
46                 }
47                 int[] ch = new int[chars.size()];
48                 for (int i = 0; i < ch.length; i++)
49                         ch[i] = chars.get(i);
50                 return new String(ch, 0, ch.length);
51         }
52
53         static private void put(StringWriter writer, int c) throws IOException {
54                 writer.write(c);
55         }
56
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);
60
61                         switch (c) {
62                         case ';':
63                         case '\\':
64                                 put(writer, '\\');
65                         }
66                         put(writer, c);
67                 }
68                 put(writer, ';');
69         }
70
71         public String toString() {
72                 try {
73                         StringWriter writer = new StringWriter();
74
75                         for (String key : keySet()) {
76                                 String value = get(key);
77                                 put_token(writer, key);
78                                 put_token(writer, value);
79                         }
80                         return writer.toString();
81                 } catch (IOException ie) {
82                         return null;
83                 }
84         }
85
86         public void putInt(String key, int value) {
87                 put(key, Integer.toString(value));
88         }
89
90         public int getInt(String key, int def) {
91                 String  value = get(key);
92
93                 if (value == null)
94                         return def;
95                 try {
96                         return AltosParse.parse_int(value);
97                 } catch (ParseException pe) {
98                         return def;
99                 }
100         }
101
102         public void putDouble(String key, double value) {
103                 put(key, AltosParse.format_double_net(value));
104         }
105
106         public double getDouble(String key, double def) {
107                 String  value = get(key);
108
109                 if (value == null)
110                         return def;
111                 try {
112                         return AltosParse.parse_double_net(value);
113                 } catch (ParseException pe) {
114                         return def;
115                 }
116         }
117
118         public String getString(String key, String def) {
119                 String  value = get(key);
120
121                 if (value == null)
122                         return def;
123                 return value;
124         }
125
126         public void putString(String key, String value) {
127                 put(key, value);
128         }
129
130         public AltosHashSet (String string) throws IOException {
131                 StringReader reader = new StringReader(string);
132                 String  key, value;
133
134                 for (;;) {
135                         key = get_token(reader);
136                         value = get_token(reader);
137                         if (key == null || value == null)
138                                 break;
139                         put(key, value);
140                 }
141         }
142
143         public AltosHashSet() {
144         }
145
146         static public AltosHashSet[] array(String string) throws IOException {
147
148                 if (string == null)
149                         return null;
150
151                 StringReader            reader = new StringReader(string);
152                 ArrayList<AltosHashSet> array = new ArrayList<AltosHashSet>();
153                 String                  element;
154
155                 while ((element = get_token(reader)) != null)
156                         array.add(new AltosHashSet(element));
157                 return array.toArray(new AltosHashSet[0]);
158         }
159
160         static public String toString(AltosHashSet[] sets) throws IOException {
161
162                 if (sets == null)
163                         return null;
164
165                 StringWriter            writer = new StringWriter();
166
167                 for (AltosHashSet h : sets) {
168                         String          element = h.toString();
169                         put_token(writer, element);
170                 }
171                 return writer.toString();
172         }
173 }