Re: Fun with Primitives
Re: Fun with Primitives
- Subject: Re: Fun with Primitives
- From: Wolfram Stebel <email@hidden>
- Date: Tue, 13 Dec 2005 08:55:21 +0100
- Thread-topic: Fun with Primitives
Am 13.12.2005 6:00 Uhr schrieb "Owen McKerrow" unter <email@hidden>:
> Hi All,
>
> I have a method that loops over a NSMutableDictionary by keyword, and
> then uses that keyword as a key value path to set the value of an
> object for another page to the value stored in the dictionary.
>
> For example we have the key value pair in the dictionary of
> "personName", "Bob".
> So I set the value in the next page with the following call
>
> nextPage.takeValueForKeyPath(dictionary.objectForKey(keyName),keyName);
>
> So in this case we set the valueForKeyPath "personName" on the next
> page to the value found in the dictionary which matches the same key,
> in this case that value would be Bob.
>
> However this breaks down when I have to deal with primitives. A
> NSMutableDictionary won't take a primitive ( e.g. an int) as a
> possible object, so we cast it to an Integer. However when you then
> try and assign this to the int on the next page it of course doesn't
> work as the int can't be assigned to an Integer.
>
> So we could check what class type we have for dictionary.objectForKey
> (keyName) and if its an Integer, use .intValue(), but that means ALL
> Integers would be cast back and this is not what we would want.
>
> Taking that one step further you can check if
> nextPage.valueForKeyPath(keyName).getClass().isInstance
> (Integer.TYPE), that is checking if you are about to set an int or
> not on the nextPage and if so cast the object to an Integer ( Integer
> value = (Integer) dictionary.objectForKey(keyName)) and then
> use .intValue() for the ones which pass this test However this also
> breaks as nextPage.takeValueForKeyPath() needs an Object not a
> primitive.
>
> I know I could just write a method that takes an Integer and assign's
> it to the int :
>
> setNumber (Integer newNum) {
> setNumber(newNum.intValue());
> }
>
> But I was hoping to make the method generic enough that it could just
> do it, otherwise I would have to write this extra method every-time I
> wanted to use it ( which may be the only way to go ).
>
> But before I headed down this path I thought I would ask and see if
> anyone has any ideas or suggestions ?
Hi Owen,
this is a part of code from a component, where i buffer some values of the
component in the session:
// return parameters for BAETable
// + personBatch.numberOfObjectsPerBatch
public int selectedPage;
public boolean initialSortAscending = true;
public String initialSortKey = "name";
public NSMutableDictionary getReturnParameters ()
{
NSMutableDictionary result = new NSMutableDictionary ();
result.takeValueForKey ( this.getNumberOfObjectsPerBatch (),
"numberOfObjectsPerBatch" );
result.takeValueForKey ( Boolean.valueOf ( initialSortAscending ),
"initialSortAscending" );
result.takeValueForKey ( initialSortKey, "initialSortKey" );
result.takeValueForKey ( new Integer ( selectedPage ),
"selectedPage" );
return result;
}
As you can see, the primitives are stored via the wrapper classes in a dict
too.
This is the method in session, that pops the values from a "stack" and sets
the values in any of my components.
public void popReturnParameters ( String theCalledPage, WOComponent
nextPage ) throws NSValidation.ValidationException
{
NSMutableDictionary result = ( NSMutableDictionary )
returnParameters.valueForKey ( theCalledPage );
if ( result == null )
{
// return parameters have not been pushed
// could log
return;
}
returnParameters.removeObjectForKey ( theCalledPage );
NSArray allKeys = result.allKeys ();
for ( int i = 0; i < allKeys.count (); i++ )
{
String key = ( String ) allKeys.objectAtIndex ( i );
nextPage.takeValueForKey ( result.objectForKey ( key ), key );
}
}
The values from the wrapper classes are restored perfectly into the
primitives.
I think, thats exactly what you described as not working, but it works
great!
Wolfram
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden