From 3d872dc4a5f91ca9c1041526358ceb4aaa31d39c Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Mon, 13 Feb 2012 16:53:22 +0000 Subject: 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. --- java/FileExample.java | 12 ++++++++++ java/com/xagasoft/gats/GatsDictionary.java | 28 +++++++++++++++++------- java/com/xagasoft/gats/KeyNotFoundException.java | 23 +++++++++++++++++++ 3 files changed, 55 insertions(+), 8 deletions(-) create mode 100644 java/com/xagasoft/gats/KeyNotFoundException.java 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 // the string object yourself. System.out.println("Sub string: " + new String( root.getDict("Dictionary").getString("name") ) ); + + root.get("aoeu"); + root.getDict("aoeu"); + root.getList("aoeu"); + root.getInt("aoeu"); + root.getFloat("aoeu"); + root.getBool("aoeu"); + root.getString("aoeu"); } catch( FileNotFoundException e ) { @@ -82,6 +90,10 @@ class FileExample { System.out.println("Error reading data."); } + catch( KeyNotFoundException e ) + { + System.out.println("Key not found."); + } } 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 * 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 ) + public long getInt( String key ) throws KeyNotFoundException { - return ((GatsInteger)hValue.get( key )).getValue(); + GatsInteger ret = (GatsInteger)hValue.get( key ); + if( ret == null ) + throw new KeyNotFoundException( this, key ); + return ret.getValue(); } /** @@ -158,9 +161,12 @@ public class GatsDictionary extends GatsObject implements Map * 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 ) + public double getFloat( String key ) throws KeyNotFoundException { - return ((GatsFloat)hValue.get( key )).getValue(); + GatsFloat ret = (GatsFloat)hValue.get( key ); + if( ret == null ) + throw new KeyNotFoundException( this, key ); + return ret.getValue(); } /** @@ -169,9 +175,12 @@ public class GatsDictionary extends GatsObject implements Map * 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 ) + public boolean getBool( String key ) throws KeyNotFoundException { - return ((GatsBoolean)hValue.get( key )).getValue(); + GatsBoolean ret = (GatsBoolean)hValue.get( key ); + if( ret == null ) + throw new KeyNotFoundException( this, key ); + return ret.getValue(); } /** @@ -180,9 +189,12 @@ public class GatsDictionary extends GatsObject implements Map * 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 ) + public byte[] getString( String key ) throws KeyNotFoundException { - return ((GatsString)hValue.get( key )).getValue(); + GatsString ret = (GatsString)hValue.get( key ); + if( ret == null ) + throw new KeyNotFoundException( this, key ); + return ret.getValue(); } /** 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 @@ +package com.xagasoft.gats; + +public class KeyNotFoundException extends Exception +{ + private Object oSrc = null; + private String sKey = null; + + public KeyNotFoundException( Object oSrc, String sKey ) + { + this.oSrc = oSrc; + this.sKey = sKey; + } + + public Object getSource() + { + return oSrc; + } + + public String getKey() + { + return sKey; + } +} -- cgit v1.2.3