aboutsummaryrefslogtreecommitdiff
path: root/java/com/xagasoft/gats/GatsDictionary.java
blob: e359dfaa357d43e87c4c3379ef76fcbdf6b50b1c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package com.xagasoft.gats;

import java.io.InputStream;
import java.io.OutputStream;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import java.util.Hashtable;

/**
 * Gats dictionary, or hash table.  This stores any number of items, keyed with
 * strings.  The values can be any valid com.xagasoft.gats.GatsObject descendant
 * object.  This class is often used as the root of complex Gats structures.
 * <p>
 * This class implements all standard java Map interface features but contains
 * additional helper functions to make inserting and extracting values from the
 * dictionary much easier, please see the extra get / put functions for more
 * information.
 * <p>
 * Keys are stored as GatsStrings in the end, but are plain Java strings until
 * encoding occurs to make use easier.  This means that using extended
 * characters in the strings may be dangerous.  In a future version, the keys
 * may have to be encoded in UTF-8 which will solve this problem.
 */
public class GatsDictionary extends GatsObject implements Map<String,GatsObject>
{
	private Hashtable<String, GatsObject> hValue =
		new Hashtable<String, GatsObject>();

	public GatsDictionary()
	{
	}

	public int getType()
	{
		return GatsObject.DICTIONARY;
	}

	void read( InputStream is, char cType ) throws java.io.IOException
	{
		for(;;)
		{
			GatsObject objKey = GatsObject.read( is );
			if( objKey == null )
				break;
			if( objKey.getType() != GatsObject.STRING )
				throw new java.io.IOException("bleh");
			put( objKey.toString(), GatsObject.read( is ) );
		}
	}

	void write( OutputStream os ) throws java.io.IOException
	{
		os.write( (int)'d' );
		for( String sKey : hValue.keySet() )
		{
			new GatsString( sKey ).write( os );
			hValue.get( sKey ).write( os );
		}
		os.write( (int)'e' );
	}

	public String toString()
	{
		return hValue.toString();
	}

	public void clear() 
	{
		hValue.clear();
	}

	public boolean containsKey( Object key )
	{
		return hValue.containsKey( key );
	}

	public boolean containsValue( Object value )
	{
		return hValue.containsValue( value );
	}

	public Set<java.util.Map.Entry<String, GatsObject>> entrySet()
	{
		return hValue.entrySet();
	}

	public GatsObject get( Object key )
	{
		return hValue.get( key );
	}

	public boolean isEmpty()
	{
		return hValue.isEmpty();
	}

	public Set<String> keySet()
	{
		return hValue.keySet();
	}

	public GatsObject put( String key, GatsObject value )
	{
		return hValue.put( key, value );
	}

	/**
	 * Helper function that inserts a new com.xagasoft.gats.GatsInteger with
	 * the value val.
	 */
	public GatsObject put( String key, long val )
	{
		return hValue.put( key, new GatsInteger( val ) );
	}

	/**
	 * Helper function that inserts a new com.xagasoft.gats.GatsFloat with the
	 * value val.
	 */
	public GatsObject put( String key, double val )
	{
		return hValue.put( key, new GatsFloat( val ) );
	}

	/**
	 * Helper function that inserts a new com.xagasoft.gats.GatsBoolean with the
	 * value val.
	 */
	public GatsObject put( String key, boolean val )
	{
		return hValue.put( key, new GatsBoolean( val ) );
	}

	/**
	 * Helper function that inserts a new com.xagasoft.gats.GatsString with the
	 * value val.
	 */
	public GatsObject put( String key, String val )
	{
		return hValue.put( key, new GatsString( val ) );
	}

	/**
	 * Helper function that inserts a new com.xagasoft.gats.GatsString with the
	 * value val.
	 */
	public GatsObject put( String key, byte val[] )
	{
		return hValue.put( key, new GatsString( val ) );
	}

	/**
	 * Helper function that gets the specified GatsObject, casts it to a
	 * com.xagasoft.gats.GatsInteger and extracts the value from it.  If the
	 * key specified does not appear in the GatsDictionary or is not the correct
	 * type you will get the expected exception.
	 */
	public long getInt( String key ) throws KeyNotFoundException
	{
		GatsInteger ret = (GatsInteger)hValue.get( key );
		if( ret == null )
			throw new KeyNotFoundException( this, key );
		return ret.getValue();
	}

	/**
	 * Helper function that gets the specified GatsObject, casts it to a
	 * com.xagasoft.gats.GatsFloat and extracts the value from it.  If the
	 * key specified does not appear in the GatsDictionary or is not the correct
	 * type you will get the expected exception.
	 */
	public double getFloat( String key ) throws KeyNotFoundException
	{
		GatsFloat ret = (GatsFloat)hValue.get( key );
		if( ret == null )
			throw new KeyNotFoundException( this, key );
		return ret.getValue();
	}

	/**
	 * Helper function that gets the specified GatsObject, casts it to a
	 * com.xagasoft.gats.GatsBool and extracts the value from it.  If the
	 * key specified does not appear in the GatsDictionary or is not the correct
	 * type you will get the expected exception.
	 */
	public boolean getBool( String key ) throws KeyNotFoundException
	{
		GatsBoolean ret = (GatsBoolean)hValue.get( key );
		if( ret == null )
			throw new KeyNotFoundException( this, key );
		return ret.getValue();
	}

	/**
	 * Helper function that gets the specified GatsObject, casts it to a
	 * com.xagasoft.gats.GatsString and extracts the value from it.  If the
	 * key specified does not appear in the GatsDictionary or is not the correct
	 * type you will get the expected exception.
	 */
	public byte[] getString( String key ) throws KeyNotFoundException
	{
		GatsString ret = (GatsString)hValue.get( key );
		if( ret == null )
			throw new KeyNotFoundException( this, key );
		return ret.getValue();
	}

	/**
	 * Helper function that gets the specified GatsObject, casts it to a
	 * com.xagasoft.gats.GatsDictionary and returns it.  If the
	 * key specified does not appear in the GatsDictionary or is not the correct
	 * type you will get the expected exception.
	 */
	public GatsDictionary getDict( String key ) throws KeyNotFoundException
	{
		GatsDictionary ret = (GatsDictionary)hValue.get( key );
		if( ret == null )
			throw new KeyNotFoundException( this, key );
		return ret;
	}

	/**
	 * Helper function that gets the specified GatsObject, casts it to a
	 * com.xagasoft.gats.GatsList and returns it.  If the
	 * key specified does not appear in the GatsDictionary or is not the correct
	 * type you will get the expected exception.
	 */
	public GatsList getList( String key ) throws KeyNotFoundException
	{
		GatsList ret = (GatsList)hValue.get( key );
		if( ret == null )
			throw new KeyNotFoundException( this, key );
		return ret;
	}

	public void putAll( Map<? extends String, ? extends GatsObject> src )
	{
		hValue.putAll( src );
	}

	public GatsObject remove( Object key )
	{
		return hValue.remove( key );
	}

	public int size()
	{
		return hValue.size();
	}

	public Collection<GatsObject> values()
	{
		return hValue.values();
	}
};