diff options
author | Mike Buland <eichlan@xagasoft.com> | 2012-02-13 16:53:22 +0000 |
---|---|---|
committer | Mike Buland <eichlan@xagasoft.com> | 2012-02-13 16:53:22 +0000 |
commit | 3d872dc4a5f91ca9c1041526358ceb4aaa31d39c (patch) | |
tree | ceaeb797f86d3a2f7a4e0b4aaa28fe109f02488c /java | |
parent | 06f34a151f59343809154268da272022994fef45 (diff) | |
download | libgats-3d872dc4a5f91ca9c1041526358ceb4aaa31d39c.tar.gz libgats-3d872dc4a5f91ca9c1041526358ceb4aaa31d39c.tar.bz2 libgats-3d872dc4a5f91ca9c1041526358ceb4aaa31d39c.tar.xz libgats-3d872dc4a5f91ca9c1041526358ceb4aaa31d39c.zip |
Trying a more sensible handling for get helpers in GatsDictionary, they throw
an exception now if the key isn't found. getInt and getFloat will both probably
be complimented by getBigInt and getBigFloat or something like it later on,
but we'll always return types from those helpers that can't contain a null,
so exceptions it is.
Diffstat (limited to 'java')
-rw-r--r-- | java/FileExample.java | 12 | ||||
-rw-r--r-- | java/com/xagasoft/gats/GatsDictionary.java | 28 | ||||
-rw-r--r-- | java/com/xagasoft/gats/KeyNotFoundException.java | 23 |
3 files changed, 55 insertions, 8 deletions
diff --git a/java/FileExample.java b/java/FileExample.java index f85f88d..6f180d9 100644 --- a/java/FileExample.java +++ b/java/FileExample.java | |||
@@ -73,6 +73,14 @@ class FileExample | |||
73 | // the string object yourself. | 73 | // the string object yourself. |
74 | System.out.println("Sub string: " + new String( | 74 | System.out.println("Sub string: " + new String( |
75 | root.getDict("Dictionary").getString("name") ) ); | 75 | root.getDict("Dictionary").getString("name") ) ); |
76 | |||
77 | root.get("aoeu"); | ||
78 | root.getDict("aoeu"); | ||
79 | root.getList("aoeu"); | ||
80 | root.getInt("aoeu"); | ||
81 | root.getFloat("aoeu"); | ||
82 | root.getBool("aoeu"); | ||
83 | root.getString("aoeu"); | ||
76 | } | 84 | } |
77 | catch( FileNotFoundException e ) | 85 | catch( FileNotFoundException e ) |
78 | { | 86 | { |
@@ -82,6 +90,10 @@ class FileExample | |||
82 | { | 90 | { |
83 | System.out.println("Error reading data."); | 91 | System.out.println("Error reading data."); |
84 | } | 92 | } |
93 | catch( KeyNotFoundException e ) | ||
94 | { | ||
95 | System.out.println("Key not found."); | ||
96 | } | ||
85 | } | 97 | } |
86 | 98 | ||
87 | public static void main( String args[] ) | 99 | public static void main( String args[] ) |
diff --git a/java/com/xagasoft/gats/GatsDictionary.java b/java/com/xagasoft/gats/GatsDictionary.java index 654dc6c..3517b68 100644 --- a/java/com/xagasoft/gats/GatsDictionary.java +++ b/java/com/xagasoft/gats/GatsDictionary.java | |||
@@ -147,9 +147,12 @@ public class GatsDictionary extends GatsObject implements Map<String,GatsObject> | |||
147 | * key specified does not appear in the GatsDictionary or is not the correct | 147 | * key specified does not appear in the GatsDictionary or is not the correct |
148 | * type you will get the expected exception. | 148 | * type you will get the expected exception. |
149 | */ | 149 | */ |
150 | public long getInt( String key ) | 150 | public long getInt( String key ) throws KeyNotFoundException |
151 | { | 151 | { |
152 | return ((GatsInteger)hValue.get( key )).getValue(); | 152 | GatsInteger ret = (GatsInteger)hValue.get( key ); |
153 | if( ret == null ) | ||
154 | throw new KeyNotFoundException( this, key ); | ||
155 | return ret.getValue(); | ||
153 | } | 156 | } |
154 | 157 | ||
155 | /** | 158 | /** |
@@ -158,9 +161,12 @@ public class GatsDictionary extends GatsObject implements Map<String,GatsObject> | |||
158 | * key specified does not appear in the GatsDictionary or is not the correct | 161 | * key specified does not appear in the GatsDictionary or is not the correct |
159 | * type you will get the expected exception. | 162 | * type you will get the expected exception. |
160 | */ | 163 | */ |
161 | public double getFloat( String key ) | 164 | public double getFloat( String key ) throws KeyNotFoundException |
162 | { | 165 | { |
163 | return ((GatsFloat)hValue.get( key )).getValue(); | 166 | GatsFloat ret = (GatsFloat)hValue.get( key ); |
167 | if( ret == null ) | ||
168 | throw new KeyNotFoundException( this, key ); | ||
169 | return ret.getValue(); | ||
164 | } | 170 | } |
165 | 171 | ||
166 | /** | 172 | /** |
@@ -169,9 +175,12 @@ public class GatsDictionary extends GatsObject implements Map<String,GatsObject> | |||
169 | * key specified does not appear in the GatsDictionary or is not the correct | 175 | * key specified does not appear in the GatsDictionary or is not the correct |
170 | * type you will get the expected exception. | 176 | * type you will get the expected exception. |
171 | */ | 177 | */ |
172 | public boolean getBool( String key ) | 178 | public boolean getBool( String key ) throws KeyNotFoundException |
173 | { | 179 | { |
174 | return ((GatsBoolean)hValue.get( key )).getValue(); | 180 | GatsBoolean ret = (GatsBoolean)hValue.get( key ); |
181 | if( ret == null ) | ||
182 | throw new KeyNotFoundException( this, key ); | ||
183 | return ret.getValue(); | ||
175 | } | 184 | } |
176 | 185 | ||
177 | /** | 186 | /** |
@@ -180,9 +189,12 @@ public class GatsDictionary extends GatsObject implements Map<String,GatsObject> | |||
180 | * key specified does not appear in the GatsDictionary or is not the correct | 189 | * key specified does not appear in the GatsDictionary or is not the correct |
181 | * type you will get the expected exception. | 190 | * type you will get the expected exception. |
182 | */ | 191 | */ |
183 | public byte[] getString( String key ) | 192 | public byte[] getString( String key ) throws KeyNotFoundException |
184 | { | 193 | { |
185 | return ((GatsString)hValue.get( key )).getValue(); | 194 | GatsString ret = (GatsString)hValue.get( key ); |
195 | if( ret == null ) | ||
196 | throw new KeyNotFoundException( this, key ); | ||
197 | return ret.getValue(); | ||
186 | } | 198 | } |
187 | 199 | ||
188 | /** | 200 | /** |
diff --git a/java/com/xagasoft/gats/KeyNotFoundException.java b/java/com/xagasoft/gats/KeyNotFoundException.java new file mode 100644 index 0000000..d46fc47 --- /dev/null +++ b/java/com/xagasoft/gats/KeyNotFoundException.java | |||
@@ -0,0 +1,23 @@ | |||
1 | package com.xagasoft.gats; | ||
2 | |||
3 | public class KeyNotFoundException extends Exception | ||
4 | { | ||
5 | private Object oSrc = null; | ||
6 | private String sKey = null; | ||
7 | |||
8 | public KeyNotFoundException( Object oSrc, String sKey ) | ||
9 | { | ||
10 | this.oSrc = oSrc; | ||
11 | this.sKey = sKey; | ||
12 | } | ||
13 | |||
14 | public Object getSource() | ||
15 | { | ||
16 | return oSrc; | ||
17 | } | ||
18 | |||
19 | public String getKey() | ||
20 | { | ||
21 | return sKey; | ||
22 | } | ||
23 | } | ||